Beruflich Dokumente
Kultur Dokumente
exit() { s = false; }
2
Semaphores (Dijkstra)
Synchronization tool that does not require busy waiting. Semaphore is an object contains a (private) integer value and 2 operations.
P operation, also called Down or Wait
Take a resource
Semaphore Implementation
Define a semaphore as a record typedef struct { int value; struct process *L; } semaphore; Assume two simple operations:
block(), suspends the process that invokes it. wakeup(P), changes the thread from the waiting state to the ready state.
4
Implementation
Semaphore operations now defined as P(S): S.value--; if (S.value < 0) { add this process to S.L; block(); } V(S): S.value++; if (S.value <= 0) { remove a process P from S.L; wakeup(P); } The P and V operations are atomic.
5
Bounded Buffer
(1 producer, 1 consumer) char buf[n], int front = 0, rear = 0; semaphore empty = n, full = 0; Producer() while (1) { produce message m; P(empty); buf[rear] = m; rear = rear +1; V(full); } Consumer () while (1) { P(full); m = buf[front]; front = front + 1; V(empty); consume m; }
9
Bounded Buffer
(multiple producers and consumers) char buf[n], int front = 0, rear = 0; semaphore empty = n, full = 0, mutex = 1; Producer() while (1) { produce message m; P(empty); P(mutex); buf[rear] = m; rear = rear +1; V(mutex); V(full) } Consumer () while (1) { P(full); P(mutex); m = buf[front]; front = front + 1; V(mutex); V(empty); consume m; }
10
Readers-Writers Problem
Given a database
Can have multiple readers at a time dont ever modify database Only one writer will modify database
12
Writer Process
writeEnter () { P(wrt); }
writeExit () { V(wrt); }
13
Reader Process
readEnter () { P(mutex); readcount++; if (readcount == 1) P(wrt); V(mutex); } readExit() { P(mutex); readcount--; if (readcount == 0) V(wrt); V(mutex); }
14
Dining-Philosophers Problem
15
Dining-Philosophers Problem
Philosopher i: do { P(chopstick[i]) P(chopstick[(i+1) % 5]) eat V(chopstick[i]); V(chopstick[(i+1) % 5]); think } while (1);
16
Dining-Philosophers Problem
(Deadlock Free)
Philosopher i: do { if (i ! = 0) { P(chopstick[i]); P(chopstick[i+1]); eat V(chopstick[i]); V(chopstick[i+1]); } else { P(chopstick[1]); P(chopstick[0]); eat V(chopstick[1]); V(chopstick[0]); } think } while (1);
17
Monitors (Hoare)
Abstract Data Type
Consists of vars and procedures, like C++ class. 3 key differences from a regular class:
Only one thread in monitor at a time (mutual exclusion is automatic); Special type of variable allowed, called condition variable
3 special ops allowed only on condition variables: wait, signal, broadcast
Monitors
monitor monitor-name { shared variable declarations procedure body P1 () { ... } procedure body P2 () { ... } procedure body Pn () { ... } { initialization code } }
20
Monitors
To allow a process to wait within the monitor, a condition variable must be declared, as condition x, y, cond; Given a condition variable cond
cond.wait():
thread is put on queue for cond, goes to sleep
cond.signal():
if queue for cond not empty, wakeup one thread
cond.broadcast
wakeup all threads waiting on queue for cond
21
22
23
Semantics of Signal
Signal and Wait (Hoare)
signaller immediately gives up control thread that was waiting executes
25
When cant get into database, wait on appropriate condition variable When done with database, signal others Note: cant just put code for reading database and code for writing database in the monitor (couldt have >1 reader)
26
V() vs Signal
V either wakes up a thread or increments value Signal only has effect if a thread waiting
28
cond->Signal():
if (nc > 0) { nc--; V(c); }
29
Java-style monitors
Integrated into the class mechanism
Annotation synchronized can be applied to a member function
This function executes with implicit mutual exclusion
Wait, Signal, and Broadcast are called mon wait, notify, and notifyAll, respectively
30