130 likes | 270 Vues
This text delves into classic interprocess communication (IPC) problems, including the Dining Philosophers, Readers and Writers, and Sleeping Barber. The Dining Philosophers problem illustrates issues like deadlock and starvation when multiple processes (philosophers) need to access limited resources (forks). Readers and Writers focus on the challenge of allowing multiple readers while restricting writers. Meanwhile, the Sleeping Barber problem depicts a barber who sleeps when there are no customers, highlighting queue-related issues. Solutions for these classic problems illustrate the complexities of parallel processing.
E N D
2.4 Classic IPC Problems • Dining philosophers • Readers and writers • Sleeping barber
Philosphers eat and think. To eat, they must first acquire a left fork and then a right fork (or vice versa). Then they eat. Then they put down the forks. Then they think. Go to 1. Dining philosphers
Dining philosphers • Problems: • Deadlock • Starvation
Dining philosophers (poor solution) down(mutex); up(mutex); Too much blocking – not very parallel but it works.
Dining philosophers (non solution) void philosopher ( int i ) { for ( ; ; ) { think(); for ( ; ; ) { take_fork( i ); if (try_take_fork( (i+1)%N )) { put_fork( i ); break; } } eat(); put_fork( i ); put_fork( (i+1)%N ); } } How does this lead to starvation? problem
Readers and writers • Multiple readers can concurrently read from the data base. • But when updating the db, there can only be one writer (i.e., no other writers and no readers either)
Readers and writers • Suffers from starvation (as long as there are readers, a writer is starved).
Sleeping barber problem • One barber, one barber chair, and N seats for waiting. • No customers so barber sleeps. • Customer comes in & wakes up barber. • More customers come in. • If there is an empty seat, they take a seat. • Otherwise, they leave.