130 likes | 249 Views
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.
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.