Beruflich Dokumente
Kultur Dokumente
This lecture
Goals:
Introduce classical synchronization problems Producer-Consumer Problem Reader-Writer Problem Dining Philosophers Problem Sleeping Barbers Problem
Copyright : University of Illinois CS 241 Staff 2
Topics
N customer chairs (waiting chairs) One barber who can cut one customers hair at any time No waiting customer => barber sleeps Customer enters =>
If all waiting chairs full, customer leaves Otherwise, if barber asleep, wake up barber and make him work Otherwise, barber is busy wait in a chair Copyright : University of Illinois CS 241 Staff
82
CHAP. 2
#define CHAIRS 5 typedef int semaphore; semaphore customers = 0; semaphore barbers = 0; semaphore mutex = 1; int waiting = 0;
/* # of customers waiting for service */ /* # of barbers waiting for customers */ /* for mutual exclusion */ /* customers are waiting (not being cut) */
void barber(void) { while (TRUE) { down(customers); /* go to sleep if # of customers is 0 */ down(mutex); /* acquire access to waiting */ waiting = waiting - 1; /* decrement count of waiting customers */ up(barbers); /* one barber is now ready to cut hair */ Copyright : University of release waiting 4 up(mutex); /* Illinois CS 241 Staff */ [From Tanenbaum, Modern Operating Systems]
#define CHAIRS 5 typedef int semaphore; semaphore customers = 0; semaphore barbers = 0; semaphore mutex = 1; int waiting = 0;
/* # chairs fordown(mutex); waiting customers */ waiting = waiting - 1; /* use your imagination */ up(barbers); up(mutex); /* # of customers hair(); for service */ cut waiting /* # of }barbers waiting for customers */ /* for }mutual exclusion */ /* customers are waiting (not being cut) */
void customer(void) { down(mutex); /* go to sleep if # of customers is 0 */ if (waiting < CHAIRS) { /* acquire access to waiting */ 1; waiting = waiting + /* decrement up(customers); customers */ count of waiting /* one barberup(mutex); is now ready to cut hair */ /* release waiting */ down(barbers); /* cut hair (outside critical region) */ get haircut(); } else { up(mutex); } }
Figure 2-21. A solution
void barber(void) { while (TRUE) { down(customers); down(mutex); waiting = waiting - 1; up(barbers); up(mutex); cut hair(); } }
Note: down customer(void) void means semWait { up means semSignal down(mutex); [From Tanenbaum, Modern Operating Systems]
typedef int semaphore; semaphore customers = 0; semaphore barbers = 0; semaphore mutex = 1; int waiting = 0;
/* of customers waiting Sleeping Barber#solution, forfor service *// /* # of barbers waiting customers * /* for mutual plus code comments exclusion */(not being cut) / customers are waiting
*/
void barber(void) { while (TRUE) { down(customers); down(mutex); waiting = waiting - 1; up(barbers); up(mutex); cut hair(); } }
/* go to sleep if # of customers is 0 */ /* acquire access to waiting */ /* decrement count of waiting customers */ /* one barber is now ready to cut hair */ /* release waiting */ /* cut hair (outside critical region) */
void customer(void) { down(mutex); /* enter critical region */ if (waiting < CHAIRS) { /* if there are no free chairs, leave */ Copyright : University of Illinois CS 241 Staff 6 waiting = waiting + 1; /* increment count of waiting customers */ [From Tanenbaum, Modern Operating Systems]
} }
* * /* one barber is now ready to cut hair */ /* release waiting */ /* cut hair (outside critical region) */
void customer(void) { down(mutex); if (waiting < CHAIRS) { waiting = waiting + 1; up(customers); up(mutex); down(barbers); get haircut(); } else { up(mutex); } }
/* enter critical region */ /* if there are no free chairs, leave */ /* increment count of waiting customers */ /* wake up barber if necessary */ /* release access to waiting */ /* go to sleep if # of free barbers is 0 */ /* be seated and be serviced */ /* shop is full; do not wait */
N philosophers and N forks Philosophers eat/think Eating needs 2 forks Pick one fork at a time
PROCESSES
CHAP. 2
PROCESSES
Kravets
The life of a philosopher consists of alternate periods of eating and thinking. (This is something of an abstraction, even for philosophers, but the other activities Copyright : University of Illinois CS she tries are irrelevant here.) When a philosopher gets hungry,241 Staff to acquire her left
SEC. 2.3
77
#define N 5 void philosopher(int i) { while (TRUE) { think(); take fork(i); take fork((i+1) % N); eat(); put fork(i); put fork((i+1) % N); } }
/* philosopher is thinking */ /* take left fork */ /* take right fork; % is modulo operator */ /* yum-yum, spaghetti */ /* put left fork back on the table */ /* put right fork back on the table */
Figure 2-17. A A non-solution to nonsolution to the dining philosophers problem.problem the dining philosophers that everything would continue in lockstep for even an hour is very small. This observation is true, but in some applications up their prefer fork first, Deadlock: everyone picks one would left a solution that always works and cannot fail due tofor unlikely fork random numbers. then waits an right series of
10
Mutual exclusion Hold and wait No preemption Circular wait Dining Philosophers has all four of these properties.
Copyright : University of Illinois CS 241 Staff 11
Mutual exclusion: Exclusive use of chopsticks Hold and wait: Hold 1 chopstick, wait for next No preemption: cannot force another to release held resource Circular wait: Each waits for next neighbor to put down chopstick
Copyright : University of Illinois CS 241 Staff 12
78
#define N 5 #define LEFT (i-1)%N #define RIGHT (i+1)%N #define THINKING 0 #define HUNGRY 1 #define EATING 2 typedef int semaphore; int state[N]; semaphore mutex = 1; semaphore s[N]; void philosopher(int i) { while (TRUE) { think(); take forks(i); eat(); put forks(i); } }
[From Tanenbaum, Modern Operating Systems]
{ while (TRUE) { think(); take forks(i); eat(); put forks(i); } } void take forks(int i) { down(&mutex); state[i] = HUNGRY; test(i); up(&mutex); down(&s[i]); } void put forks(i) { down(&mutex); state[i] = THINKING; test(LEFT); test(RIGHT); up(&mutex); } /* repeat forever */ /* philosopher is thinking */ /* acquire two forks or block */ /* yum-yum, spaghetti */ /* put both forks back on table */
void test(i) /* i: philosopher number, from 0 to N-1 */ { if (state[i] == HUNGRY && state[LEFT] != EATING && state[RIGHT] != EATING) { state[i] = EATING; up(&s[i]); } Copyright : University of Illinois CS 241 Staff }
14
What if...
That works (i.e., prevents deadlock) This is essentially what we just did! That works too!
Summary
Classical synchronization problems
Producer-Consumer Problem Reader-Writer Problem Sleeping Barbers Problem Dining Philosophers Problem
16
Deadlocks
Copyright : University of Abdelzaher, Kravets, Copyright : Nahrstedt, Angrave,Illinois CS 241 Staff Gupta
17
Deadlock
18
Deadlock Definition
A process is deadlocked if it is waiting for an event that will never occur. Typically, but not necessarily, more than one process will be involved together in a deadlock
A process is indefinitely postponed if it is delayed repeatedly over a long period of time while the attention of the system is given to other processes. (Logically the process may proceed but the system never gives it the CPU.)
Copyright : University of Illinois CS 241 Staff 19
Mutual exclusion
Processes claim exclusive control of the resources they require Processes hold resources already allocated to them while waiting for additional resources Resources cannot be removed from the processes holding them until used to completion A circular chain of processes exists in which each process holds one or more resources that are requested by the next process in the chain
Copyright : University of Illinois CS 241 Staff 20
No preemption condition
Mutual exclusion
Exclusive use of chopsticks Hold 1 chopstick, wait for next Cannot force another to undo their hold Each waits for next neighbor to put down chopstick
Copyright : University of Illinois CS 241 Staff
No preemption condition
21
Mutual Exclusion
Processes claim exclusive control of the resources they require How to break it?
22
Processes hold resources already allocated to them while waiting for additional resources How to break it?
Allow processes to either access all its required resources at once, or none of them
23
No Preemption Condition
Resources cannot be removed from the processes holding them until used to completion How to break it?
Allow processes to be pre-empted and forced to abort themselves or release held resources
24
A circular chain of processes exists in which each process holds one or more resources that are requested by the next process in the chain How to break it?
25
Nodes
Processes Resources From resource to process = resource assigned to process From process to resource = process requests (and is waiting for) resource
Copyright : University of Illinois CS 241 Staff 26
Arcs
assign
request
resource R assigned to process A process B is requesting/waiting for resource S process C and D are in deadlock over resources T and U
Copyright : University of Illinois CS 241 Staff 27
Kravets
The life of a philosopher consists of alternate periods of eating and thinking. (This is something of an abstraction, even for philosophers, but the other activities Copyright : irrelevantof Illinois When aStaff 28 are University here.) CS 241 philosopher gets hungry, she tries to acquire her left
Kravets
29
Kravets
Summary
How to tell when circular wait condition happens: cycle in resource allocation graph
31