farfromfearless

Sistem Operasi Pertemuan 4 Session 7 dan 8

  • Posted: October 27, 2015
  • |
  • Author: keithharrisk
  • |
  • Filed under: Sistem Operasi
  • |
  • Tags: No tags set for this entry.

SISTEM OPERASI
Pertemuan 4 Session 7 & 8

Threads

Keith Harris Kristanto – 1701355665

Thread, atau kadang-kadang disebut proses ringan (lightweight), adalah unit dasar dari utilisasi CPU. Di dalamnya terdapat ID thread, program counter, register, dan stack. Dan saling berbagi dengan thread lain dalam proses yang sama.

Lightweight process => Karena Threads mempunyai beberapa properti untuk proses

Multithreading => Mengijinkan multiple thread di dalam suatu proses

z

y

Keuntungan dari Threads : 

  • Memakan waktu yang sedikit untik membuat threads yang baru dari pada memprosesnya
  • Lebih cepat membasmi threads dari pada proses
  • Mengurangi waktu pada saat menukarkan kedua threads pada waktu yang bersamaan.

Threads Implementation :

Keuntungan :

  • Mengijinkan setiap process untuk mengatur algoritma sendiri
  • Performance
  • Tidak memerlukan blocking system call yang baru

Kerugian :

  • Implementasi mengblokir system call
  • Threads lain tidak bisa dijalankan kecuali thread yang pertama menyerah .
  • Memiliki dampak yang lebih besar terhadap membuat dan memusnahkan thread

 

x

 

 

w

 

Thread State :

  1. Spawn:
    • Biasanya , ketika sebuah proses baru melahirkan , thread untuk proses yang juga melahirkan .  

    • Thread dalam proses dapat menelurkan thread lain dalam proses yang sama , memberikan pointer instruksi dan argumen untuk thread baru . Thread baru disediakan dengan konteks mendaftar dan tumpukan ruang sendiri dan ditempatkan pada antrian siap .

  2. Block :
    • Ketika thread perlu menunggu untuk sampai waktu yang tepat , dia akan memblokir ( menyimpan register pengguna , program counter , dan tumpukan pointer ) .

    • Prosesor mungkin sekarang beralih ke eksekusi benang siap lain yang sama atau proses yang berbeda .

  3. Unblock  :
    • Ketika threand telah di blokir ,threand akan berpindah menjadi posisi ready queue
  4. Finish :
    • Ketika selesai , register context dan stack akan dipindahkan.

v

 

Posix (Portable Operating System Interface) Threads

u

Salah Satu thread programing :

#include<pthread.h>
#include<stdio.h>
#include<stdlib.h>

#define NUMBER_OF_THREADS   10

void *print_hello_world(void *tid){
printf(“Hello World. Greetings from Threads %d”,tid);
pthread_exit(NULL);
}

  1. pthread_self()
    1. To obain its ID
  2. pthread_join()
    1. To join or rejoin various flows of control
    2. Wen called, the calling thread is suspended untl the execution of the target thread is termnated
    3. Releases resources (i.e. prevents zombie threads)

Contoh 2 :

#include <iostream>
#include <pthread.h>
using namespace std;
int main(int argc, char** argv){
pthread_t thread_a, thread_b;
int N;
if (argc != 2){
cout << “Error” << endl;
return 0;
}
N=atoi(argv[1]);
pthread_create(&thread_a, NULL, task1, &N);
pthread_create(&thread_b, NULL, task2, &N);
cout << “Waiting to join” << endl;
pthread_join(thread_a, NULL);
pthread_join(thread_b, NULL);
return 0;
}

Contoh Function untuk task :

void* task1(void* x){
int* temp = (int*) x;
for (int count 0; count < *temp;
count++
{
cout << “Thread A” << endl;
}
cout << “Thread A Complete” << endl;
return NULL;
}

Contoh Sting dan Attributes

#include <iostream>

#include <pthread.h>

using namespace std;

int main(int argc, char** argv)

{

pthread_t thread_a, thread_b;

pthread_attr_t attr;

int N;

if (argc != 2)

{

cout << “Error” << endl;

return 0;

}

N=atoi(argv[1]);

pthread_attr_init(&attr);

pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);

pthread_create(&thread_a, &attr, task1, &N);

pthread_create(&thread_b, NULL, task2, &N);

cout << “Waiting to join” << endl;

pthread_join(thread_b, NULL);

return 0;

}

Terminating thread

  • pthread_exit()
    • Function mengambil data dari pointer pada saat balik dan juga pada saat threadnya join menjadi satu .

No comments as yet.

Anonymous - Gravatar

No comments have yet been made to this posting.

Commentors on this Post-

Leave a Comment-

Comment Guidelines: Basic XHTML is allowed (a href, strong, em, code). All line breaks and paragraphs are automatically generated. Off-topic or inappropriate comments will be edited or deleted. Email addresses will never be published. Keep it PG-13 people!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>

All fields marked with "*" are required.