Dining Philosophers [88]
Problem:
questions
answer
Dining Philosophers [89]
demo-OK:
Exercise 7a: Philosopher 1 uses the 2 chopsticks (1 pair) to eat. Then Philosopher 2 uses the 2 chopsticks (1 pair) to eat. Both make progress and the access to the resources (chopsticks) is synchronized.
demo-BAD:
Exercise 7a: Philosopher 1 gets one (1) chopstick. Philosopher 2 gets the other (1) chopstick. Neither can eat. Both are deadlocked.
Dining Philosophers [90]
while(1) {
/* philosopher thinks */
pm_wait(chopstick1);
pm_wait(chopstick2);
/* philosopher eats */
pm_signal(chopstick1);
pm_signal(chopstick2);
}
Burns' Dining Philosophers [91]
Shared Variables:
Code: At p_i in {0,...,n-1}:
do forever
if even(i) then {
wait(FORK_{i+1}) /* left fork */
wait(FORK_i) /* right fork */
}
if odd(i) then {
wait(FORK_i) /* right fork */
wait(FORK_{i+1}) /* left fork */
}
/* Critical region */
signal(FORK_i)
signal(FORK_{i+1})
Producer/Consumer [92]
Problem:
=>
=> data
questions
answer
Producer/Consumer [93]
demo-OK:
Exercise 8a: Producer puts data into buffer; consumer gets data and writes it.
demo-BAD:
Exercise 8b: The producer fills the buffer with 5 numbers, then waits for the consumer to signal that the buffer is not_full. The consumer reads from the buffer, then waits for the producer to signal that the buffer is not_empty. Both are deadlocked.
Producer/Consumer [94]
while(1) {
pm_wait(not_full);
pm_wait(sem_buffer);
/* put data into buffer */
pm_signal(sem_buffer);
pm_signal(not_empty);
}
Readers/Writer [95]
Problem
many readers OK:
=> data
one writer OK:
<= data
read/write NOT OK: data =>
<= data
questions
answer
Readers/Writer [96]
demo-OK:
Exercise 9a: Readers 1 and 2 read from the file and Writer 3 waits.Then, Writer 3 writes to the file and Readers 1 and 2 wait. All make progress and the shared access to the file is synchronized. The readers see a consistent file.
demo-BAD
Exercise 9b: Writer 3 writes to the file before Readers 1 and 2 are finished reading from the file. The readers do not see a consistent file. Writer 3 needs to use a semaphore.
Readers/Writer [97]
while(1) {
pm_wait(sem_readcount);
readcount++;
if (readcount==1) pm_wait(sem_wrt);
pm_signal(sem_readcount);
...
pm_wait(sem_readcount);
readcount--;
if (readcount==0) pm_signal(sem_wrt);
pm_signal(sem_readcount);
}
while(1) {
pm_wait(sem_wrt);
/* write to file */
pm_signal(sem_wrt);
}
Cigarette Smokers [98]
IIICsmoke
Problem:
questions
answer
Cigarette Smokers [99]
demo-OK:
Exercise 10a: Each smoker gets to smoke in turn. All make progress and the access to resources is synchronized.
demo-BAD:
Exercise 10b: Smoker 1 gets to smoke while the other smokers wait for a signal that Smoker 1 is done. But Smoker 1 is waiting for its turn again. All are deadlocked.
Cigarette Smokers [100]
while(1) {
pm_wait(tobacco);
/* smoke */
pm_signal(tobacco);
}
while(1) {
pm_signal(tobacco);
pm_wait(tobacco);
...
}
IPC Message: send and receive [101]
while(1) {
msg = pm_receive(&pid);
}
pid=7;
while(1) {
pm_send(pid,msg++);
}
while(1) {
msg = pm_receive(&pid);
pm_send(pid,msg++);
}
Message Demo [102]
pid = 2;
while(1) {
pm_wait(sem);
pm_send(pid,msg);
}
demo-OK:
Exercise 11a: Both processes make progress.
demo-BAD:
Exercise 11b: Both processes are deadlocked.
Client/Server [103]
demo-OK:
Ex12a: Server waits in receiving for a message from a client. Each client sends a message and waits in receiving for an answer. The server receives each message, in turn, and sends back an answer.
demo-BAD:
Ex12b: Server receives both messages but does not send back answers to either client. The clients are waiting for answers and the server is waiting for new messages. All processes are deadlocked.
Client/Server [104]
while(1) {
pm_send(server_pid,msg);
msg=pm_receive(&server_pid);
}
Ring Network [105]
Ring:
Ring Network [106]
demo-OK:
Exercise 13a: All processes are deadlocked, waiting for a message. Send, Process 1, message ``0''. The message goes to all processes on the ring.
demo-BAD:
Exercise 13b: The message does not go to any other processes on the ring. All processes are deadlocked waiting for messages.
while(1) {
msg=pm_receive(&pid);
...
pm_send(nextpid,msg);
}
Star Network [107]
Star:
Star Network [108]
demo-OK:
Exercise 14a: All processes are deadlocked, waiting for a message. Send, Process 1, message ``0''. The message goes to all processes on the rim of the star.
demo-BAD:
Exercise 14b: The message does not go to any processes on the rim of the star. All processes are deadlocked waiting for messages.
Star Network [109]
while(1) {
msg=pm_receive(&pid);
if (pid != center_pid)
pm_send(center_pid,msg++);
}
while(1) {
msg=pm\_receive(&pid);
msg++;
for (all rim processes which do not have the message)
pm_send(rim_pid,msg++);
}