Masalah Klasik Komunikasi Antar Proses
The Dining Philosophers Problem Philosopher hanya makan/berpikir. Jika Makan memerlukan dua sumpit / garpu. Pengambilan sumpit / garpu dilakukan satu persatu.
Masalahnya apa ?
Program dibawah masih mungkin terjadi deadlock atau starvation mengapa ? #define N 5 /* jumlah philosopher */ void philosopher(int i) { while(TRUE) { think(); take_fork(i); take_fork((i+1) % N); eat(); put_fork(i); put_fork((i+1) % N); }
The Readers and Writers Problem Model akses database. Proses Reader dan Writer berkompetisi untuk baca dan tulis. Contoh: Airline Reservation Beberapa proses Reader dapat membaca pada saat yang sama. Jika sebuah proses Writer sedang menulis maka tidak boleh ada proses lain yang mengakses database. Program untuk problem readers dan writers adalah sebagai berikut :
typedef int semaphore; semaphore mutex = 1; /* control access to rc */ semaphore db = 1; /* control access to the database */ int rc = 0; /* # of processes reading or want to */ void reader(void) { while(TRUE) { down(&mutex); /* get exclusive to rc */ rc = rc + 1; /* # of reader increased */ if (rc == 1) down(&db); /* if the first reader */ up(&mutex); /* then release exclusive acces to rc */ read_data_base(); /* access the database */ rc = rc - 1; /* # of reader decreased */ if (rc == 0) up(&db); /* if the last reader */ up(&mutex); /*then release exclusive acces to rc */ use_data_read(); /* noncritical section */ }
void writer(void) { while(TRUE) { think_up_data(); /* noncritical section */ down(&db); /* get exclusive access */ write_data_base(); /* update the database */ up(&db); /* release exclusive acces */ }
The Sleeping Barber problem Terdapat seorang tukang cukur beserta kursinya dan sejumlah n kursi tunggu. Tukang cukur tidur jika tidak ada pelanggan dan bangun jika ada pelanggan datang. Masalah antrian (queuing), agar barber dan customer tidak terjadi race condition. Program untuk problem sleeping barber sbb:
#include “prototype.h” #define CHAIRS 5 /* # chair for waiting customers */ typedef int semaphore; /* use your imagination */ semaphore customers = 0; /* # of customers waiting for service */ semaphore barber = 0; /* # of barbers waiting for customers */ semaphore mutex = 1; /* for mutual exclusion */ int waiting = 0; /* customers are waiting (not being cut) */ void barber(void) { while(TRUE){ down(customers); /* sleep if # of customers is 0 */ down(mutex); /* acquire access to waiting */ waiting = waiting - 1; /* decrement waiting */ up(barbers); /* one barber is ready to cut hair */ up(mutex); /* release waiting */ cut_hair(); /* cut hair (outside critical region) */ }
void customer(void) { down(mutex); /* enter critical region */ if(waiting < CHAIRS){ /* if no free chairs , leave */ waiting = waiting + 1; /* increment waiting */ up(customers); /* wake up barber if necessary */ up(mutex); /* release access to waiting */ down(barbers); /* sleep if # of free barbers is 0 */ get_haircut(); /* be seated and be serviced */ } else { up(mutex) ; /* shop is full; do not wait */ } http://www.appletonline.com/JavaApplets/BarberShop/
Penjadwalan Prosesor (CPU Scheduling) Pada sistem multiprogram, beberapa proses berkompetisi memperebutkan CPU. Pemilihan proses dilakukan oleh scheduler berdasarkan scheduling algorithm (algoritma Penjadwalan). Issue dalam penjadwalan: Proses I/O bound dan compute bound. Penjadwalan nonpreemptive dan preemptive. Kategori: batch, interactive, dan real time Sasaran algoritma penjadwalan: all system, sistem batch, sistem interaktif, dan sistem real-time.
Penjadwalan Sistem Batch First-Come First-Served. Shortest Job First. Shortest Remaining Time Next.
Three Level Scheduling