module 9 process synchronization

59
Operating systems Module 9 Process synchronization PART I 1 Tami Sorgente, Florida Atlantic University

Upload: others

Post on 30-May-2022

31 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Module 9 Process synchronization

Operating systemsModule 9

Process synchronizationPART I

1Tami Sorgente, Florida Atlantic University

Page 2: Module 9 Process synchronization

MODULE 9 – PROCESS SYNCHRONIZATION

Software solutionso Semaphores

2Tami Sorgente, Florida Atlantic University

Page 3: Module 9 Process synchronization

SEMAPHORE – SOFTWARE SOLUTION

Three operations: Initialization, wait, and signal

Wait and signal control access: indivisible (atomic) operations

binary, counting, complex

Wait (P – proberen “to test”)wait (S) {

while ( S<= 0); // no-op

S--;

}

Signal (V – verhogen “to increment”)signal (S) {

S++ ;

}

3Tami Sorgente, Florida Atlantic University

Page 4: Module 9 Process synchronization

COMPLEX SEMAPHOREint semaphore S = 1; //binary semaphore for list of sleeping processes

wait(S){

if (semaphore S == 0) { // semaphore is unavailable-

//add process to list - block itself

//atomically add process to a queue of processes waiting for the semaphore }

else {

semaphore S --; // decrement semaphore

}

}

signal(S){

if (semaphore S == 0 && there is a process on the wait queue){//wake up the first process in the queue of waiting processes}

else {

semaphore S ++; //increment semaphore -

}

} 4Tami Sorgente, Florida Atlantic University

Page 5: Module 9 Process synchronization

SEMAPHORE CONCEPT

5

Global variable with 3 operations◦ Initialization

◦ wait

◦ signal

Values of semaphores:

◦ Binary (only 0 and 1)

◦ Counting (any numerical value)

Types of semaphores:

◦ Standard – busy waiting

◦ Complex – block while waiting

Tami Sorgente, Florida Atlantic University

Page 6: Module 9 Process synchronization

SEMAPHORE

ANIMATION

6

wait (S) {

while ( S<= 0); // no-op

S--;

}

Signal (S) {

S++ ;

}

Initialization:

Semphore S = 0;

Dr: Signal (S);

Tami Sorgente, Florida Atlantic University

Page 7: Module 9 Process synchronization

SEMAPHORE

77

wait (S) {

while ( S<= 0); // no-op

S--;

}

Signal (S) {

S++ ;

}

Dr: Signal (S);

Tami Sorgente, Florida Atlantic University

Page 8: Module 9 Process synchronization

SEMAPHORE

8

wait (S) {

while ( S<= 0); // no-op

S--;

}

Signal (S) {

S++ ;

}

Dr:

Pn: Wait (S);

Tami Sorgente, Florida Atlantic University

Page 9: Module 9 Process synchronization

SEMAPHORE

99

wait (S) {

while ( S<= 0); // no-op

S--;

}

Signal (S) {

S++ ;

}

Dr:

Pn: Wait (S);

Tami Sorgente, Florida Atlantic University

Page 10: Module 9 Process synchronization

SEMAPHORE

10

wait (S) {

while ( S<= 0); // no-op

S--;

}

Signal (S) {

S++ ;

}

Dr:

Pn:

P1(wait) P3(wait) P2(wait)

Tami Sorgente, Florida Atlantic University

Page 11: Module 9 Process synchronization

SEMAPHORE

1111

wait (S) {

while ( S<= 0); // no-op

S--;

}

Signal (S) {

S++ ;

}

Dr:

Pn:

Pn: Signal (S);

P1(wait) P3(wait) P2(wait)

Tami Sorgente, Florida Atlantic University

Page 12: Module 9 Process synchronization

SEMAPHORE

12

wait (S) {

while ( S<= 0); // no-op

S--;

}

Signal (S) {

S++ ;

}

Dr:

P1: wait(S)

P3(wait) P2(wait)

P1

Tami Sorgente, Florida Atlantic University

Page 13: Module 9 Process synchronization

COMPLEX SEMAPHORE

13

wait(S){

if (semaphore S == 0) { // block and queue}

else { semaphore S --; }// decrement semaphore

}

signal(S){

if (semaphore S == 0 && there is a process

{//wake up the first process in the queue }

else {semaphore S ++; } //increment semaphore

}

Initialization:

Semphore S = 0;

Dr: Signal (S);

Tami Sorgente, Florida Atlantic University

Page 14: Module 9 Process synchronization

COMPLEX SEMAPHORE

1414

Dr: Signal (S);

wait(S){

if (semaphore S == 0) { // block and queue}

else { semaphore S --; }// decrement semaphore

}

signal(S){

if (semaphore S == 0 && there is a process

{//wake up the first process in the queue }

else {semaphore S ++; } //increment semaphore

}

Tami Sorgente, Florida Atlantic University

Page 15: Module 9 Process synchronization

COMPLEX SEMAPHORE

15

Dr:

Pn: Wait (S);

wait(S){

if (semaphore S == 0) { // block and queue}

else { semaphore S --; }// decrement semaphore

}

signal(S){

if (semaphore S == 0 && there is a process

{//wake up the first process in the queue }

else {semaphore S ++; } //increment semaphore

}

Tami Sorgente, Florida Atlantic University

Page 16: Module 9 Process synchronization

COMPLEX SEMAPHORE

1616

Dr:

Pn: Wait (S);

wait(S){

if (semaphore S == 0) { // block and queue}

else { semaphore S --; }// decrement semaphore

}

signal(S){

if (semaphore S == 0 && there is a process

{//wake up the first process in the queue }

else {semaphore S ++; } //increment semaphore

}

Tami Sorgente, Florida Atlantic University

Page 17: Module 9 Process synchronization

COMPLEX SEMAPHORE

17

Dr:

Pn:

wait(S){

if (semaphore S == 0) { // block and queue}

else { semaphore S --; }// decrement semaphore

}

signal(S){

if (semaphore S == 0 && there is a process

{//wake up the first process in the queue }

else {semaphore S ++; } //increment semaphore

}

P1(wait/block) P3(wait/ block) P2(wait/block)

Tami Sorgente, Florida Atlantic University

Page 18: Module 9 Process synchronization

COMPLEX SEMAPHORE

1818

Pn: Signal (S);

wait(S){

if (semaphore S == 0) { // block and queue}

else { semaphore S --; }// decrement semaphore

}

signal(S){

if (semaphore S == 0 && there is a process

{//wake up the first process in the queue }

else {semaphore S ++; } //increment semaphore

}

P1(wait/block) P3(wait/ block) P2(wait/block)

Tami Sorgente, Florida Atlantic University

Page 19: Module 9 Process synchronization

COMPLEX SEMAPHORE

19

wait(S){

if (semaphore S == 0) { // block and queue}

else { semaphore S --; }// decrement semaphore

}

signal(S){

if (semaphore S == 0 && there is a process

{//wake up the first process in the queue }

else {semaphore S ++; } //increment semaphore

}

P1

P3(wait/ block) P2(wait/block)

Tami Sorgente, Florida Atlantic University

Page 20: Module 9 Process synchronization

COUNTING SEMAPHORE

20

Semaphore Sr = 3; //complex, counting semaphore

Semaphore S = 1; // standard, binary semaphore

Flag[3] = {0}; //flag for each resource to indicate busy or available

Wait( Sr ); //Semaphore to indicate if there is an available resource

wait( S ); //binary semaphore to let only one process check the flags

int resourceFree; //local variable of first resource that is available

int i = 0; //counter

do while (flagR[ i ] != 0 ) //loop to find the available resource

i + +;

end while;

flagR [ i ] = 1; //set resource as busy

resource Free = i; //assign a value to the resourceFree local variable

signal ( S ); //allow another process to check resource flags

useResource (resourceFree); //use the resource

flagR [ resourceFree ] = 0; //set the resource available when done

Signal (Sr ); //signal, wake up next process or increment semaphoreTami Sorgente, Florida Atlantic University

Page 21: Module 9 Process synchronization

MODULE 9 – PROCESS SYNCHRONIZATION

Software solutionso Semaphores

21Tami Sorgente, Florida Atlantic University

Page 22: Module 9 Process synchronization

Operating systemsModule 9

Process synchronizationPART I1

22Tami Sorgente, Florida Atlantic University

Page 23: Module 9 Process synchronization

MODULE 9 – PROCESS SYNCHRONIZATION

Software solutionso counting semaphore example

23Tami Sorgente, Florida Atlantic University

Page 24: Module 9 Process synchronization

COUNTING SEMAPHORE

24

Semaphore Sr = 3; //complex, counting semaphore

Semaphore S = 1; // standard, binary semaphore

Flag[3] = {0}; //flag for each resource to indicate busy or available

Wait( Sr ); //Semaphore to indicate if there is an available resource

wait( S ); //binary semaphore to let only one process check the flags

int resourceFree; //local variable of first resource that is available

int i = 0; //counter

do while (flagR[ i ] != 0 ) //loop to find the available resource

i + +;

end while;

flagR [ i ] = 1; //set resource as busy

resource Free = i; //assign a value to the resourceFree local variable

signal ( S ); //allow another process to check resource flags

useResource (resourceFree); //use the resource

flagR [ resourceFree ] = 0; //set the resource available when done

Signal (Sr ); //signal, wake up next process or increment semaphoreTami Sorgente, Florida Atlantic University

Page 25: Module 9 Process synchronization

INITIALIZATION

25

Semaphore Sr = 3; //complex, counting semaphore

Semaphore S = 1; // standard, binary semaphore

Flag[3] = {0}; //flag for each resource to indicate busy or available

Semaphore Sr Semaphore S

Tami Sorgente, Florida Atlantic University

Page 26: Module 9 Process synchronization

WAIT AND SIGNAL

26

Semaphore Sr//counting/complex

Semaphore S//binary/standard

wait(Sr){ if (semaphore Sr == 0) { // block and queue}

else { semaphore Sr --; }// decrement semaphore}

signal(Sr){ if (semaphore Sr == 0 && there is a process

{//wake up the first process in the queue }

else {semaphore Sr ++; } //increment semaphore}

wait(S){ while (semaphore S == 0) ; //no op

S --; } // decrement semaphore

signal(S){ S ++; } //increment

Tami Sorgente, Florida Atlantic University

Page 27: Module 9 Process synchronization

WAIT AND SIGNAL

27

Semaphore Sr//counting/complex

Semaphore S//binary/standard

wait(Sr){ if (semaphore Sr == 0) { // block and queue}

else { semaphore Sr --; }// decrement semaphore}

signal(Sr){ if (semaphore Sr == 0 && there is a process

{//wake up the first process in the queue }

else {semaphore Sr ++; } //increment semaphore}

wait(S){ while (semaphore S == 0) ; //no op

S --; } // decrement semaphore

signal(S){ S ++; } //increment

Tami Sorgente, Florida Atlantic University

Page 28: Module 9 Process synchronization

WAIT AND SIGNAL

28

Semaphore Sr//counting/complex

Semaphore S//binary/standard

wait(Sr){ if (semaphore Sr == 0) { // block and queue}

else { semaphore Sr --; }// decrement semaphore}

signal(Sr){ if (semaphore Sr == 0 && there is a process

{//wake up the first process in the queue }

else {semaphore Sr ++; } //increment semaphore}

wait(S){ while (semaphore S == 0) ; //no op

S --; } // decrement semaphore

signal(S){ S ++; } //increment

Tami Sorgente, Florida Atlantic University

Page 29: Module 9 Process synchronization

WAIT AND SIGNAL

29

Semaphore Sr//counting/complex

Semaphore S//binary/standard

wait(Sr){ if (semaphore Sr == 0) { // block and queue}

else { semaphore Sr --; }// decrement semaphore}

signal(Sr){ if (semaphore Sr == 0 && there is a process

{//wake up the first process in the queue }

else {semaphore Sr ++; } //increment semaphore}

wait(S){ while (semaphore S == 0) ; //no op

S --; } // decrement semaphore

signal(S){ S ++; } //increment

Tami Sorgente, Florida Atlantic University

Page 30: Module 9 Process synchronization

WAIT AND SIGNAL

30

Semaphore Sr//counting/complex

Semaphore S//binary/standard

wait(Sr){ if (semaphore Sr == 0) { // block and queue}

else { semaphore Sr --; }// decrement semaphore}

signal(Sr){ if (semaphore Sr == 0 && there is a process

{//wake up the first process in the queue }

else {semaphore Sr ++; } //increment semaphore}

wait(S){ while (semaphore S == 0) ; //no op

S --; } // decrement semaphore

signal(S){ S ++; } //increment

Tami Sorgente, Florida Atlantic University

Page 31: Module 9 Process synchronization

WAIT AND SIGNAL

31

Semaphore Sr//counting/complex

Semaphore S//binary/standard

wait(Sr){ if (semaphore Sr == 0) { // block and queue}

else { semaphore Sr --; }// decrement semaphore}

signal(Sr){ if (semaphore Sr == 0 && there is a process

{//wake up the first process in the queue }

else {semaphore Sr ++; } //increment semaphore}

wait(S){ while (semaphore S == 0) ; //no op

S --; } // decrement semaphore

signal(S){ S ++; } //increment

Tami Sorgente, Florida Atlantic University

Page 32: Module 9 Process synchronization

WAIT AND SIGNAL

32

Semaphore Sr//counting/complex

Semaphore S//binary/standard

wait(Sr){ if (semaphore Sr == 0) { // block and queue}

else { semaphore Sr --; }// decrement semaphore}

signal(Sr){ if (semaphore Sr == 0 && there is a process

{//wake up the first process in the queue }

else {semaphore Sr ++; } //increment semaphore}

wait(S){ while (semaphore S == 0) ; //no op

S --; } // decrement semaphore

signal(S){ S ++; } //increment

Tami Sorgente, Florida Atlantic University

Page 33: Module 9 Process synchronization

WAIT AND SIGNAL

33

Semaphore Sr//counting/complex

Semaphore S//binary/standard

wait(Sr){ if (semaphore Sr == 0) { // block and queue}

else { semaphore Sr --; }// decrement semaphore}

signal(Sr){ if (semaphore Sr == 0 && there is a process

{//wake up the first process in the queue }

else {semaphore Sr ++; } //increment semaphore}

wait(S){ while (semaphore S == 0) ; //no op

S --; } // decrement semaphore

signal(S){ S ++; } //increment

Tami Sorgente, Florida Atlantic University

Page 34: Module 9 Process synchronization

WAIT AND SIGNAL

34

Semaphore Sr//counting/complex

Semaphore S//binary/standard

wait(Sr){ if (semaphore Sr == 0) { // block and queue}

else { semaphore Sr --; }// decrement semaphore}

signal(Sr){ if (semaphore Sr == 0 && there is a process

{//wake up the first process in the queue }

else {semaphore Sr ++; } //increment semaphore}

wait(S){ while (semaphore S == 0) ; //no op

S --; } // decrement semaphore

signal(S){ S ++; } //increment

Tami Sorgente, Florida Atlantic University

Page 35: Module 9 Process synchronization

WAIT AND SIGNAL

35

Semaphore Sr//counting/complex

Semaphore S//binary/standard

wait(Sr){ if (semaphore Sr == 0) { // block and queue}

else { semaphore Sr --; }// decrement semaphore}

signal(Sr){ if (semaphore Sr == 0 && there is a process

{//wake up the first process in the queue }

else {semaphore Sr ++; } //increment semaphore}

wait(S){ while (semaphore S == 0) ; //no op

S --; } // decrement semaphore

signal(S){ S ++; } //increment

Tami Sorgente, Florida Atlantic University

Page 36: Module 9 Process synchronization

WAIT AND SIGNAL

36

Semaphore Sr//counting/complex

Semaphore S//binary/standard

wait(Sr){ if (semaphore Sr == 0) { // block and queue}

else { semaphore Sr --; }// decrement semaphore}

signal(Sr){ if (semaphore Sr == 0 && there is a process

{//wake up the first process in the queue }

else {semaphore Sr ++; } //increment semaphore}

wait(S){ while (semaphore S == 0) ; //no op

S --; } // decrement semaphore

signal(S){ S ++; } //increment

Tami Sorgente, Florida Atlantic University

Page 37: Module 9 Process synchronization

WAIT AND SIGNAL

37

Semaphore Sr//counting/complex

Semaphore S//binary/standard

wait(Sr){ if (semaphore Sr == 0) { // block and queue}

else { semaphore Sr --; }// decrement semaphore}

signal(Sr){ if (semaphore Sr == 0 && there is a process

{//wake up the first process in the queue }

else {semaphore Sr ++; } //increment semaphore}

wait(S){ while (semaphore S == 0) ; //no op

S --; } // decrement semaphore

signal(S){ S ++; } //increment

Tami Sorgente, Florida Atlantic University

Page 38: Module 9 Process synchronization

WAIT AND SIGNAL

38

Semaphore Sr//counting/complex

Semaphore S//binary/standard

wait(Sr){ if (semaphore Sr == 0) { // block and queue}

else { semaphore Sr --; }// decrement semaphore}

signal(Sr){ if (semaphore Sr == 0 && there is a process

{//wake up the first process in the queue }

else {semaphore Sr ++; } //increment semaphore}

wait(S){ while (semaphore S == 0) ; //no op

S --; } // decrement semaphore

signal(S){ S ++; } //increment

Tami Sorgente, Florida Atlantic University

Page 39: Module 9 Process synchronization

WAIT AND SIGNAL

39

Semaphore Sr//counting/complex

Semaphore S//binary/standard

wait(Sr){ if (semaphore Sr == 0) { // block and queue}

else { semaphore Sr --; }// decrement semaphore}

signal(Sr){ if (semaphore Sr == 0 && there is a process

{//wake up the first process in the queue }

else {semaphore Sr ++; } //increment semaphore}

wait(S){ while (semaphore S == 0) ; //no op

S --; } // decrement semaphore

signal(S){ S ++; } //increment

Tami Sorgente, Florida Atlantic University

Page 40: Module 9 Process synchronization

WAIT AND SIGNAL

40

Semaphore Sr//counting/complex

Semaphore S//binary/standard

wait(Sr){ if (semaphore Sr == 0) { // block and queue}

else { semaphore Sr --; }// decrement semaphore}

signal(Sr){ if (semaphore Sr == 0 && there is a process

{//wake up the first process in the queue }

else {semaphore Sr ++; } //increment semaphore}

wait(S){ while (semaphore S == 0) ; //no op

S --; } // decrement semaphore

signal(S){ S ++; } //increment

Tami Sorgente, Florida Atlantic University

Page 41: Module 9 Process synchronization

WAIT AND SIGNAL

41

Semaphore Sr//counting/complex

Semaphore S//binary/standard

wait(Sr){ if (semaphore Sr == 0) { // block and queue}

else { semaphore Sr --; }// decrement semaphore}

signal(Sr){ if (semaphore Sr == 0 && there is a process

{//wake up the first process in the queue }

else {semaphore Sr ++; } //increment semaphore}

wait(S){ while (semaphore S == 0) ; //no op

S --; } // decrement semaphore

signal(S){ S ++; } //increment

Tami Sorgente, Florida Atlantic University

Page 42: Module 9 Process synchronization

WAIT AND SIGNAL

42

Semaphore Sr//counting/complex

Semaphore S//binary/standard

wait(Sr){ if (semaphore Sr == 0) { // block and queue}

else { semaphore Sr --; }// decrement semaphore}

signal(Sr){ if (semaphore Sr == 0 && there is a process

{//wake up the first process in the queue }

else {semaphore Sr ++; } //increment semaphore}

wait(S){ while (semaphore S == 0) ; //no op

S --; } // decrement semaphore

signal(S){ S ++; } //increment

Tami Sorgente, Florida Atlantic University

Page 43: Module 9 Process synchronization

WAIT AND SIGNAL

43

Semaphore Sr//counting/complex

Semaphore S//binary/standard

wait(Sr){ if (semaphore Sr == 0) { // block and queue}

else { semaphore Sr --; }// decrement semaphore}

signal(Sr){ if (semaphore Sr == 0 && there is a process

{//wake up the first process in the queue }

else {semaphore Sr ++; } //increment semaphore}

wait(S){ while (semaphore S == 0) ; //no op

S --; } // decrement semaphore

signal(S){ S ++; } //increment

Tami Sorgente, Florida Atlantic University

Page 44: Module 9 Process synchronization

Operating systemsModule 9

Process synchronizationPART II1

44Tami Sorgente, Florida Atlantic University

Page 45: Module 9 Process synchronization

MODULE 9 – PROCESS SYNCHRONIZATION

Software solutionso Monitors

Classic Problems of Synchronizationo Producer/ Consumer

o Dining Philosophers

o Sharing multiple instances of a resource

o Readers and writers

45Tami Sorgente, Florida Atlantic University

Page 46: Module 9 Process synchronization

MONITORS

A high-level abstraction that provides a convenient and effective mechanism for process synchronization

Abstract data type, internal variables only accessible by code within the procedure

Only one process may be active within the monitor at a time

monitor monitor-name

{

// shared variable declarations

procedure Request (…) { …. }

procedure Release(…) {……}

Initialization code (…) { … }

}

}

46Tami Sorgente, Florida Atlantic University

Page 47: Module 9 Process synchronization

SCHEMATIC VIEW OF A MONITOR

Monitor:

47Tami Sorgente, Florida Atlantic University

Page 48: Module 9 Process synchronization

CONDITION VARIABLES

condition x, y;

Two operations are allowed on a

condition variable:

o x.wait() – a process that invokes the

operation is suspended until x.signal()

o x.signal() – resumes one of processes (if

any) that invoked x.wait()

If no x.wait() on the variable, then it has no effect

on the variable

48Tami Sorgente, Florida Atlantic University

Page 49: Module 9 Process synchronization

MONITOR WITH CONDITION VARIABLES

49Tami Sorgente, Florida Atlantic University

Page 50: Module 9 Process synchronization

CLASSICAL PROBLEMS OF

SYNCHRONIZATION

Classical problems used to test

synchronization schemes

o Bounded-Buffer Problem

Producer/ consumer

o Readers and Writers Problem

o Dining-Philosophers Problem

50Tami Sorgente, Florida Atlantic University

Page 51: Module 9 Process synchronization

PRODUCER/ CONSUMER (BOUNDED BUFFER)

PRODUCER:

while (true) {/* produce an item in next produced */

while (counter == BUFFER_SIZE) ;

/* do nothing */

buffer[in] = next_produced;

in = (in + 1) % BUFFER_SIZE;

counter++;

}

CONSUMER:

while (true) {

while (counter == 0) ;

/* do nothing */

next_consumed = buffer[out];

out = (out + 1) % BUFFER_SIZE;

counter--;

/* consume the item in next consumed */

}

51Tami Sorgente, Florida Atlantic University

Page 52: Module 9 Process synchronization

BOUNDED-BUFFER PROBLEM

n buffers, each can hold one item

Semaphore mutex initialized to the value 1

Semaphore full initialized to the value 0

Semaphore empty initialized to the value n

52Tami Sorgente, Florida Atlantic University

Page 53: Module 9 Process synchronization

BOUNDED BUFFER PROBLEM (CONT.)

The structure of the producer process

do {

.../* produce an item in next_produced

*/

...

wait(empty);

wait(mutex);

.../* add next produced to the buffer */

...

signal(mutex);

signal(full);

} while (true);

53Tami Sorgente, Florida Atlantic University

Page 54: Module 9 Process synchronization

BOUNDED BUFFER PROBLEM (CONT.)

The structure of the consumer process

Do {

wait(full);

wait(mutex);

.../* remove an item from buffer to

next_consumed */

...

signal(mutex);

signal(empty);

.../* consume the item in next consumed */

...} while (true);

54Tami Sorgente, Florida Atlantic University

Page 55: Module 9 Process synchronization

DINING-PHILOSOPHERS PROBLEM

Philosophers spend their lives alternating thinking and

eating

Don’t interact with their neighbors, occasionally try to

pick up 2 chopsticks (one at a time) to eat from bowl

o Need both to eat, then release both when done

In the case of 5 philosophers

o Shared data

Bowl of rice (data set)

Semaphore chopstick [5]

initialized to 1

55Tami Sorgente, Florida Atlantic University

Page 56: Module 9 Process synchronization

DINING-PHILOSOPHERS PROBLEM

ALGORITHM

The structure of Philosopher :do {

wait (chopstick[i] );

wait (chopStick[ (i + 1) % 5] );

// eat

signal (chopstick[i] );

signal (chopstick[ (i + 1) % 5] );

// think

} while (TRUE);

What is the problem with this algorithm?

56Tami Sorgente, Florida Atlantic University

Page 57: Module 9 Process synchronization

Dining-Philosophers Problem

Algorithm

57Tami Sorgente, Florida Atlantic University

Page 58: Module 9 Process synchronization

MONITOR SOLUTION TO DINING PHILOSOPHERS

monitor DiningPhilosophers

{

enum { THINKING; HUNGRY, EATING) state [5] ;

condition self [5];

void pickup (int i) {

state[i] = HUNGRY;

test(i);

if (state[i] != EATING) self[i].wait;

}

void putdown (int i) {

state[i] = THINKING;

// test left and right neighbors

test((i + 4) % 5);

test((i + 1) % 5);

}

void test (int i) {

if ((state[(i + 4) % 5] != EATING) &&

(state[i] == HUNGRY) &&

(state[(i + 1) % 5] != EATING) ) {

state[i] = EATING ;

self[i].signal () ;

}

}

initialization_code() {

for (int i = 0; i < 5; i++)

state[i] = THINKING;

}

}

58Tami Sorgente, Florida Atlantic University

Page 59: Module 9 Process synchronization

MODULE 9 – PROCESS SYNCHRONIZATION

Software solutionso Monitors

Classic Problems of Synchronizationo Producer/ Consumer

o Dining Philosophers

o Sharing multiple instances of a resource

o Readers and writers

59Tami Sorgente, Florida Atlantic University