Classical Problems of Synchronization
Classical Problems of Synchronization
Synchronization
Classical Problems of Synchronization
• Classical problems used to test newly-
proposed synchronization schemes
– Bounded-Buffer Problem
– Readers and Writers Problem
– Dining-Philosophers Problem
Classical Problems of Synchronization
do {
...
/* produce an item in next_produced */
...
wait(empty);
wait(mutex);
...
/* add next produced to the buffer */
...
signal(mutex);
signal(full);
} while (true);
Contd..
The structure of the consumer process
Do {
wait(full);
wait(mutex);
...
/* remove an item from buffer to next_consumed */
...
signal(mutex);
signal(empty);
...
/* consume the item in next consumed */
...
} while (true);
Readers-Writers Problem
• A data set is shared among a number of concurrent processes
– Readers – only read the data set; they do not perform any updates
– Writers – can both read and write
• Problem – allow multiple readers to read at the same time
– Only one single writer can access the shared data at the same time
• Several variations of how readers and writers are considered –
all involve some form of priorities
• Shared Data
– Data set
– Semaphore rw_mutex initialized to 1
– Semaphore mutex initialized to 1
– Integer read_count initialized to 0
Readers-Writers Problem Variations
• First variation – no reader kept waiting unless
writer has permission to use shared object
• Second variation – once writer is ready, it
performs the write ASAP
• Both may have starvation leading to even
more variations
• Problem is solved on some systems by kernel
providing reader-writer locks
Dining-Philosophers Problem
monitor monitor-name
{
// shared variable declarations
procedure P1 (…) { …. }
initialization_code() {
for (int i = 0; i < 5; i++)
state[i] = THINKING;
}
}
Contd..
DiningPhilosophers.pickup(i);
EAT
DiningPhilosophers.putdown(i);