Sie sind auf Seite 1von 31

Classical Synchronization Problems

Copyright : University of Illinois CS 241 Staff

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

3. The Sleeping Barber


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

Sleeping Barber solution (1) PROCESSES


/* use your imagination */

CHAP. 2

#define CHAIRS 5 typedef int semaphore; semaphore customers = 0; semaphore barbers = 0; semaphore mutex = 1; int waiting = 0;

/* # chairs for waiting customers */

/* # 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;

Sleeping Barber solution (2)

/* # 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]

Copyright : University of Illinois CS 241 Staff

/* enter critical region */

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

/* use your imagination */

*/

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]

} }

Sleeping Barber solution, plus code comments

up(barbers); up(mutex); cut hair();

* * /* 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 */

Figure 2-21. A solution to the sleeping barber problem.

2.4 PROCESS SCHEDULING of Illinois CS 241 Staff Copyright : University


[From Tanenbaum, Modern Operating Systems]

4. Dining Philosophers: an intellectual game


N philosophers and N forks Philosophers eat/think Eating needs 2 forks Pick one fork at a time
PROCESSES

CHAP. 2

Copyright : University of Illinois CS 241 Staff

4. Dining Philosophers: an intellectual game


(John Stuart Mill, of his own free will, On half a CHAP. 2 of shandy pint was particularly ill...)

PROCESSES

Kravets

Lunch time in the Philosophy Department.

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

Does this CLASSICAL the problem? solve IPC PROBLEMS


/* number of philosophers */ /* i: philosopher number, from 0 to 4 */

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 */

[From Tanenbaum, Modern Operating Systems]

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

(Think about safety control in a nuclear power plant.)

Copyright : University of Illinois CS 241 Staff

10

Necessary and sufficient conditions for deadlock


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

Necessary and sufficient conditions for deadlock

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

Dining Philosophers solution 2 PROCESSES CHAP.


/* number of philosophers */ /* number of is left neighbor */ /* number of is right neighbor */ /* philosopher is thinking */ /* philosopher is trying to get forks */ /* philosopher is eating */ /* semaphores are a special kind of int */ /* array to keep track of everyones state */ /* mutual exclusion for critical regions */ /* one semaphore per philosopher */ /* i: philosopher number, from 0 to N-1 */ /* repeat forever */ /* philosopher is thinking */ /* acquire two forks or block */ /* yum-yum, spaghetti */ /* put both forks back on table */
Copyright : University of Illinois CS 241 Staff 13

#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 */

Dining Philosophers solution


/* i: philosopher number, from 0 to N-1 */ /* enter critical region */ /* record fact that philosopher i is hungry */ /* try to acquire 2 forks */ /* exit critical region */ /* block if forks were not acquired */ /* i: philosopher number, from 0 to N-1 */ /* enter critical region */ /* philosopher has finished eating */ /* see if left neighbor can now eat */ /* see if right neighbor can now eat */ /* exit critical region */

[From Tanenbaum, Modern Operating Systems]

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...

Made picking up both left and right chopsticks an atomic operation?


That works (i.e., prevents deadlock) This is essentially what we just did! That works too!

Or, N philosophers & N+1 chopsticks?

And well see another solution later...


Copyright : University of Illinois CS 241 Staff 15

Summary
Classical synchronization problems

Producer-Consumer Problem Reader-Writer Problem Sleeping Barbers Problem Dining Philosophers Problem

Copyright : University of Illinois CS 241 Staff

16

Deadlocks

Copyright : University of Abdelzaher, Kravets, Copyright : Nahrstedt, Angrave,Illinois CS 241 Staff Gupta

17

Deadlock

Copyright : University of Illinois CS 241 Staff

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

Is deadlock the same as starvation (i.e., indefinitely postponed)?

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

Necessary Conditions for Deadlock

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

Hold-and-wait (a.k.a. wait-for) condition

No preemption condition

Circular wait condition

Dining Philosophers had it all

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

Hold and wait condition

No preemption condition

Circular wait condition

21

Mutual Exclusion

Processes claim exclusive control of the resources they require How to break it?

Grant non-exclusive access only (e.g., read-only)

Copyright : University of Illinois CS 241 Staff

22

Hold and Wait Condition

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

Copyright : University of Illinois CS 241 Staff

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

Copyright : University of Illinois CS 241 Staff

24

Circular Wait Condition

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?

Allow processes to access resources only in increasing order of resource id

Copyright : University of Illinois CS 241 Staff

25

Resource Allocation Graph

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

Resource Allocation Graph

assign

request

(a) (b) (c)

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

Dining Philosophers resource allocation graph


If we use the trivial broken solution... void philosopher(i) { while true { take left fork; take right fork; eat(); put left fork; put right fork; } }
PROCESSES CHAP. 2

Kravets

Lunch time in the Philosophy Department.

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

Dining Philosophers resource allocation graph


If we use the trivial broken solution... One node per philosopher and per fork 1. Everyone tries to pick up left fork (request edges)

Kravets

Copyright : University of Illinois CS 241 Staff

29

Dining Philosophers resource allocation graph


If we use the trivial broken solution... One node per philosopher and per fork 1. Everyone tries to pick up left fork (request edges) 2. Everyone succeeds! (request edges become assignment edges) 3. Everyone tries to pick up right fork (request edges) 4. Cycle => deadlock
Copyright : University of Illinois CS 241 Staff 30

Kravets

Summary

Definition of deadlock 4 conditions for deadlock to happen

How to tell when circular wait condition happens: cycle in resource allocation graph

Next time: How to deal with deadlock

Copyright : University of Illinois CS 241 Staff

31

Das könnte Ihnen auch gefallen