18
CSC 360 Instructor: Kui Wu More on Process Synchronization Semaphore, Monitor, Condition Variables

More on Process Synchronization Semaphore, Monitor, Condition Variables

Embed Size (px)

DESCRIPTION

More on Process Synchronization Semaphore, Monitor, Condition Variables. Agenda. Review of Mutex Semaphore Examples Monitor Condition Variables Dining Philosophers Implementation of Monitors Implementation of Condition Variables. 1. Mutex (1). Mutual exclusion (mutex) only two states - PowerPoint PPT Presentation

Citation preview

Page 1: More on Process Synchronization Semaphore, Monitor, Condition Variables

CSC 360 Instructor: Kui Wu

More on Process Synchronization

Semaphore, Monitor, Condition Variables

Page 2: More on Process Synchronization Semaphore, Monitor, Condition Variables

CSC 360 Instructor: Kui Wu

Agenda

1. Review of Mutex

2. Semaphore

3. Examples

4. Monitor

5. Condition Variables

6. Dining Philosophers

7. Implementation of Monitors

8. Implementation of Condition Variables

Page 3: More on Process Synchronization Semaphore, Monitor, Condition Variables

CSC 360 Instructor: Kui Wu3

1. Mutex (1)

Mutual exclusion (mutex)

• only two states– unlocked: there is no thread in critical section– locked: there is one thread in critical section

• state change is atomic– if it is unlocked, it can be locked by at most one thread

when entering the critical section– if it is locked, it can “only” be unlocked by the locking

thread when leaving the critical section

Page 4: More on Process Synchronization Semaphore, Monitor, Condition Variables

CSC 360 Instructor: Kui Wu4

Mutex: more• Mutex procedures

– create a mutex variable (initially unlocked)– (some threads) attempt to lock the mutex

• only one can lock the mutex– others may be blocked and waiting

• the one with the mutex– execute the critical section– unlock the mutex variable eventually

– destroy the mutex variable

1. Mutex (2)

Page 5: More on Process Synchronization Semaphore, Monitor, Condition Variables

CSC 360 Instructor: Kui Wu5

2. Semaphores (1)

Semaphore API

• Semaphore S – integer variable

– binary semaphore

– counting semaphore

• two indivisible (atomic) operations

– also known as P() and V()wait (S):

while S<= 0 do no-op;S--;

signal (S):

S++;

Q: busy-wait problem?

Page 6: More on Process Synchronization Semaphore, Monitor, Condition Variables

CSC 360 Instructor: Kui Wu6

2. Using semaphores (2)

Mutual exclusion• binary semaphore

• shared data semaphore mutex; // initially mutex = 1

• process Pi do { wait(mutex); /* critical section */

signal(mutex); /* remainder section */} while (1);

Resource access• counting semaphore

• initially, the number of resource instances

Page 7: More on Process Synchronization Semaphore, Monitor, Condition Variables

CSC 360 Instructor: Kui Wu7

2. Semaphore implementation (3)

Semaphores without busy waiting

• block(): block the caller process

• wakeup(): wakeup another processwait(S):

S.value--;if (S.value < 0) {

add this process to S.L;block();

}

signal(S): S.value++;if (S.value <= 0) {

remove a process P from S.L;wakeup(P);

}

Page 8: More on Process Synchronization Semaphore, Monitor, Condition Variables

CSC 360 Instructor: Kui Wu8

2. More on using semaphores (4)

Ordered execution

• initially, flag = 0;

• P1: …; do_me_first; signal (flag);

• P2: …; wait (flag); then_follow_on;

Caution

• deadlock

– wait (A); wait (B); …; signal (A); signal (B);

– wait (B); wait (A); …; signal (B); signal (A);

• starvation

Page 9: More on Process Synchronization Semaphore, Monitor, Condition Variables

CSC 360 Instructor: Kui Wu9

3. Examples (1): The producer-consumer problemWith semaphore

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

signal (mutex);

signal (empty);

// consume the item

}

Page 10: More on Process Synchronization Semaphore, Monitor, Condition Variables

CSC 360 Instructor: Kui Wu10

3. Examples (2):The readers-writers problem

First readers-writers problem

• no readers kept waiting unless writer is writing

while (true) {

wait (wrt) ;

// writing is performed

signal (wrt) ;

}

while (true) { wait (mutex) ; readcount ++ ; if (readcount == 1) wait (wrt) ; signal (mutex); // reading is performed wait (mutex) ; readcount - - ; if (readcount == 0) signal (wrt) ; signal (mutex) ;}

Page 11: More on Process Synchronization Semaphore, Monitor, Condition Variables

CSC 360 Instructor: Kui Wu11

3. Example (3): dining philosophers

Shared data • Initially all values are 1

semaphore chopstick[5];Philosopher i:

do {wait(chopstick[i])wait(chopstick[(i+1) % 5])

…eat …

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

…think …

} while (1);

Q: any possible problems?

Page 12: More on Process Synchronization Semaphore, Monitor, Condition Variables

CSC 360 Instructor: Kui Wu

CSc 360Overview 12

4. MonitorA high-level abstraction (OO design) that provides a convenient and

effective mechanism for process synchronizationOnly one process may be active within the monitor at a time

monitor monitor-name{

// shared variable declarationsprocedure P1 (…) { …. }

…procedure Pn (…) {……}

Initialization code ( ….) { … }…

}}

• Any problem?

Page 13: More on Process Synchronization Semaphore, Monitor, Condition Variables

CSC 360 Instructor: Kui Wu

CSc 360Overview 13

5. Condition variables

No busy-waiting!

• condition variables

Two operations on a condition variable

• x.wait () – a process that invokes the operation is suspended.

• x.signal () – resumes one of processes (if any) that

invoked x.wait ()

Page 14: More on Process Synchronization Semaphore, Monitor, Condition Variables

CSC 360 Instructor: Kui Wu14

• Shared data semaphore chopstick[5]; // initially 1

• Using semaphores, for Philosopher i:do {

wait(chopstick[i])wait(chopstick[(i+1) % 5])

…eat…

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

…think…

} while (1);

• Any problem?

6. Dining philosophers (D.P.) problem (1)

Page 15: More on Process Synchronization Semaphore, Monitor, Condition Variables

CSC 360 Instructor: Kui Wu15

6. D.P. problem (2): monitors

monitor 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 16: More on Process Synchronization Semaphore, Monitor, Condition Variables

CSC 360 Instructor: Kui Wu16

6. DP monitor (3): more

void test (int i) { if ( (state[(i + 4) % 5] != EATING) && (state[i] == HUNGRY) && (state[(i + 1) % 5] != EATING) ) { state[i] = EATING ;

self[i].signal () ; // no effect if not blocked } }

initialization_code() { for (int i = 0; i < 5; i++) state[i] = THINKING;}

}

Any problem?

• Using monitors

dp.pickup (i)...

EAT...

dp.putdown (i)

Page 17: More on Process Synchronization Semaphore, Monitor, Condition Variables

CSC 360 Instructor: Kui Wu17

7. Implementation of monitors

Variables semaphore mutex; // for the monitor, initially = 1semaphore next; // for suspended processes, initially = 0int next-count = 0; // # of suspended processes

Each procedure F will be replaced bywait(mutex); // wait for the access to the monitor

… body of F;…

if (next-count > 0) // whether there are suspended processessignal(next);

else // free the monitor to other processessignal(mutex);

Mutual exclusion within a monitor is ensured

Page 18: More on Process Synchronization Semaphore, Monitor, Condition Variables

CSC 360 Instructor: Kui Wu18

8. Implementing Condition Variables

For each condition variable x, we have:semaphore x-sem; // initially = 0int x-count = 0;

The operation x.wait can be implemented as:x-count++;if (next-count > 0)

signal(next); // wake up the first one of the suspended qelse

signal(mutex);// free the monitorwait(x-sem); // join the x queuex-count--;

The operation x.signal can be implemented as:if (x-count > 0) { // no effect if x is not blocked

next-count++;

signal(x-sem);// wake up the first one of the x queue

wait(next); // join the suspended queue

next-count--;

}