IF524 – Sistem Operasi

7/

- 10

1

Masalah Klasik Komunikasi Antar Proses The Dining Philosophers Problem  Philosopher hanya makan/berpikir.  Makan dikerjakan menggunakan dua sumpit.  Pengambilan sumpit dilakukan satu persatu.

 Masalah: menulis program yg menghasilkan maksimum parallelisme untuk sejumlah philosopher?
BINA NUSANTARA Revisi : 2

Edisi : 1

Sept - 2002

IF524 – Sistem Operasi

7/

- 10

2

#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); } } Program diatas masih mungkin terjadi deadlock atau starvation

Edisi : 1

BINA NUSANTARA Revisi : 2

Sept - 2002

IF524 – Sistem Operasi

7/

- 10

3

Solusi yang benar:
#define N 5 /* number of philosopher */ #define LEFT (i+N-1) % N /* i’s left neighbor */ #define RIGHT (i+1) % N /* i’s right neighbor */ #define THINKING 0 /* philosopher is thinking */ #define HUNGRY 1 /* trying to get forks */ #define EATING 2 /* philosopher is eating */ typedef int semaphore; /* semaphore are an int */ int state[N]; /* keep track of everyone’s state */ semaphore mutex = 1; /*mutual exclusion for critical
regions */

semaphore s[N]; philosopher */
N-1) */

/*

one

semaphore

per

void philosopher(int i) { while (TRUE){ think();

/* i : which philosopher ( 0 to

/* repeat forever */ /* philosopher is thinking */
Sept - 2002

Edisi : 1

BINA NUSANTARA Revisi : 2

IF524 – Sistem Operasi

7/

- 10

4

take_forks(i); /*acquire block */
*/

two

fork

s

or

eat(); put_forks(i);

/* yum-yum, spaghetti */
/* put both forks back on table

}

}

void take_forks(int i) { down(&mutex); */ state(i) = HUNGRY;
hungry */

/* enter critical region
/* philosopher i is

*/

test(i);

/* try to acquire 2 forks /* exit critical region */ /* block if forks were not

up(&mutex); down(&s[i]); acquired */ }

void put_forks(int i) { down(&mutex); /* enter critical region */ state[i]=THINKING; /*philosopher finished
eating */
BINA NUSANTARA Revisi : 2

Edisi : 1

Sept - 2002

IF524 – Sistem Operasi

7/

- 10

5

test(LEFT); eat */
*/

/* see if left neighbor can now

test(RIGHT); /* see if right neighbor can now eat up(&mutex); /* exit critical region */

} void test(int i) /* i : which philosopher ( 0 to N-1) */ { if(state[i] == HUNGRY && state[LEFT] != EATING && state[RIGHT] != EATING) { state[i] = EATING; up(&s[i]); } }

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.
Edisi : 1 BINA NUSANTARA Revisi : 2 Sept - 2002

IF524 – Sistem Operasi

7/

- 10

6

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 */ down(&mutex); /* get exclusive to rc */ 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 */ } }
Edisi : 1 BINA NUSANTARA Revisi : 2 Sept - 2002

IF524 – Sistem Operasi

7/

- 10

7

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:
Edisi : 1 BINA NUSANTARA Revisi : 2 Sept - 2002

IF524 – Sistem Operasi

7/

- 10

8

#include “prototype.h” #define CHAIRS 5 /* # chair for waiting customers */ typedef int semaphore; /* use your imagination */ semaphore customers = 0;
service */ /* # of customers waiting for # of barbers waiting for

semaphore barber = 0; /*
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 */

Edisi : 1

BINA NUSANTARA Revisi : 2

Sept - 2002

IF524 – Sistem Operasi

7/

- 10

9

if(waiting < CHAIRS){
leave */

/* if no free chairs

,

waiting = waiting + 1; /* increment waiting */ up(customers); /* wake up barber
necessary */

if

up(mutex); down(barbers);
0 */

/* release access to waiting */ /* sleep if # of free barbers is /* be seated and be serviced */ /* shop is full; do not wait */

get_haircut(); } else { up(mutex) ; } }

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.
BINA NUSANTARA Revisi : 2

-

Edisi : 1

Sept - 2002

IF524 – Sistem Operasi

7/

- 10

10

- 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.

Contoh penjadwalan Shortest Job First  Shortest Remaining Time Next.

Edisi : 1

BINA NUSANTARA Revisi : 2

Sept - 2002

IF524 – Sistem Operasi

7/

- 10

11

 Three Level Scheduling

Edisi : 1

BINA NUSANTARA Revisi : 2

Sept - 2002