Synchronization Examples: Complete
Synchronization Examples: Complete
complete
BESE 24 (A,B,C)
2 Classical Problems of Synchronization
Classical problems used to test newly-proposed
synchronization schemes
Bounded-Buffer Problem
Dining-Philosophers Problem
3 Bounded-Buffer Problem
while (true) {
...
/* produce an item in next_produced */
...
wait(empty);
wait(mutex);
...
/* add next_produced to the buffer */
...
signal(mutex);
signal(full);
}
5 Bounded Buffer Problem (Cont.)
The structure of the consumer process
while (true) {
wait(full);
wait(mutex);
...
/* remove an item from buffer to next_consumed */
...
signal(mutex);
signal(empty);
...
/* consume the item in next_consumed */
...
}
6 Readers-Writers Problem
Several variations of how readers and writers are considered – all involve
some form of priorities
7 Readers-Writers Problem (Cont.)
Shared Data
Data set
Semaphore rw_mutex initialized to 1
Semaphore mutex initialized to 1
Integer read_count initialized to 0
8 Readers-Writers Problem (Cont.)
The structure of a writer process
while (true) {
wait(rw_mutex);
...
/* writing is performed */
...
signal(rw_mutex);
}
9 Readers-Writers Problem (Cont.)
The structure of a reader process
while (true){
wait(mutex);
read_count++;
if (read_count == 1) /* first reader */
wait(rw_mutex);
signal(mutex);
...
/* reading is performed */
...
wait(mutex);
read count--;
signal(mutex);
}
10 Readers-Writers Problem Variations
Both the first and second may result in starvation. leading to even more
variations
signal (chopstick[i] );
signal (chopstick[ (i + 1) % 5] );
}
What is the problem with this algorithm? Deadlock
13 Possible Remedies for deadlock
Allow at most four philosophers to be sitting simultaneously
“Synchronization Examples”