13
Synchronization II: CPE 261403 - Operating Systems http://www.e-cpe.org/moodle

Synchronization II: CPE 261403 - Operating Systems

Embed Size (px)

Citation preview

Page 1: Synchronization II: CPE 261403 - Operating Systems

Synchronization II:

CPE 261403 - Operating Systemshttp://www.e-cpe.org/moodle

Page 2: Synchronization II: CPE 261403 - Operating Systems

Classical Problems of Synchronization

Bounded-Buffer Problem (6.6.1)

Dining-Philosophers Problem (6.6.3)

(Readers-Writers Problem 6.6.2)

Page 3: Synchronization II: CPE 261403 - Operating Systems

Bounded-Buffer Problem

while (true) {

// produce an item

wait (empty);

wait (mutex);

// add the item to the buffer

signal (mutex);

signal (full);

}

while (true) { wait (full); wait (mutex);

// remove an item from buffer

signal (mutex); signal (empty);

// consume the removed item

}

Producer Consumer

1 0 N

Mutex Full EmptySemaphores

Page 4: Synchronization II: CPE 261403 - Operating Systems

Human Error is Hard to Debug

Page 5: Synchronization II: CPE 261403 - Operating Systems

Monitors Higher level API than Semaphores Can help prevent human error

Figure 6.19

Page 6: Synchronization II: CPE 261403 - Operating Systems

Bounded Buffer Problemmonitor BoundedBuffer {

function Produce (value) { if BufferLen < N // Add a value to Buffer} Function consume() {

If BufferLen > 0// remove a value from Buffer

}

}

// usage exampleBoundedBuffer.Produce(x)BoundedBuffer.Consume()

Page 7: Synchronization II: CPE 261403 - Operating Systems

Bounded Buffer Problemmonitor BoundedBuffer {

Condition contentCondition emptySpace

function Produce (value) { if BufferLen < N // Add a value to Buffer content.signal() else

emptySpace.wait()} Function consume() {

If BufferLen > 0// remove a value from Buffer

emptySpace.signal() else

content.wait()}

}

Page 8: Synchronization II: CPE 261403 - Operating Systems

Counting Semaphores vs Conditions

Semaphore i = 0

Signal (i)

// i will = 1

Condition i

i.signal()// nothing happens // if not waiting // process

Counting SemaphoresCondition

(Less bugs)

Page 9: Synchronization II: CPE 261403 - Operating Systems

Dining-Philosophers Problem

Figure 6.16

Page 10: Synchronization II: CPE 261403 - Operating Systems

A solution using SemaphoresEvery philosopher runs this code

While (true) { wait ( chopstick[i] ); wait ( chopStick[ (i + 1) % 5] );

// eat

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

// think

}

Page 11: Synchronization II: CPE 261403 - Operating Systems

Deadlock?

Page 12: Synchronization II: CPE 261403 - Operating Systems

Using Monitorsmonitor DP {

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);}

Page 13: Synchronization II: CPE 261403 - Operating Systems

Using Monitors (Cont.)

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;}

}