next up previous contents
Next: Deadlock Up: Process Management Previous: Process Synchronization: Semaphores   Contents

Classical Problems [87]





Dining Philosophers [88]

Problem:


\begin{picture}(245,116)(10,705)
\thicklines\put( 30,810){\circle{22}}
\put( 30,...
...705){\makebox(0,0)[lb]{\raisebox{0pt}[0pt][0pt]{\bf 1 chopstick}}}
\end{picture}



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:

1.
FORK: semaphore array [0..n-1], initially all available

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:

Producer: Consumer:
buffer
data => \framebox[1cm][l]{\rule[-.4cm]{0cm}{1cm}}\framebox[1cm][l]{\rule[-.4cm]{0cm}{1cm}}\framebox[1cm][l]{\rule[-.4cm]{0cm}{1cm}}\framebox[1cm][l]{\rule[-.4cm]{0cm}{1cm}}\framebox[1cm][l]{\rule[-.4cm]{0cm}{1cm}} => 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: \framebox[2.5cm]{\rule[-0.8cm]{0cm}{2.0cm}FILE} => data

one writer OK: \framebox[2.5cm]{\rule[-0.8cm]{0cm}{2.0cm}FILE} <= data

read/write NOT OK: data => \framebox[2.5cm]{\rule[-0.8cm]{0cm}{2.0cm}FILE} <= 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:


\begin{picture}(376,232)(4,604)
\thicklines\put(300,720){\oval(92,72)}
\put(300,...
...
\put(295,635){\makebox(0,0)[lb]{\raisebox{0pt}[0pt][0pt]{\bf 3}}}
\end{picture}



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:


\begin{picture}(135,90)(15,735)
\thicklines\put( 20,820){\circle*{10}}
\put( 20,...
...t( 30,740){\vector( 1, 0){ 10}}
\put( 70,740){\vector( 1, 0){ 10}}
\end{picture}



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:


\begin{picture}(100,90)(5,735)
\thicklines\put( 20,820){\circle*{10}}
\put( 20,7...
...ut( 5,780){\vector( 1, 0){ 35}}
\put( 65,780){\vector( 1, 0){ 15}}
\end{picture}



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


next up previous contents
Next: Deadlock Up: Process Management Previous: Process Synchronization: Semaphores   Contents
Ted Billard 2001-11-17