09 Ch7Synchronization Examples
09 Ch7Synchronization Examples
Examples
Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018
Outline
Operating System Concepts – 10th Edition 7.2 Silberschatz, Galvin and Gagne ©2018
Classical Problems of Synchronization
Classical problems used to test newly-proposed synchronization
schemes(solutions)
• Bounded-Buffer Problem
• Readers and Writers Problem
• Dining-Philosophers Problem
These problems are used for testing nearly every newly proposed
synchronization scheme
Operating System Concepts – 10th Edition 7.3 Silberschatz, Galvin and Gagne ©2018
Bounded-Buffer Problem
The Bounded-Buffer Problem (also known as the producer consumer
problem), is one of the classic problems of synchronization.
There is a buffer of n slots and each slot is capable of storing one
unit of data
There are two processes running, namely, Producer and Consumer,
which are operating on the buffer.
• The producer tries to insert data into the buffer
• The consumer to remove data from the buffer
• The producer must not insert data when the buffer is full
• The consumer must not remove data when the buffer is empty
• The producer and consumer should not insert and remove data
simultaneously
Operating System Concepts – 10th Edition 7.4 Silberschatz, Galvin and Gagne ©2018
Solution to the Bounded-Buffer Problem using Semaphores
Operating System Concepts – 10th Edition 7.5 Silberschatz, Galvin and Gagne ©2018
Bounded Buffer Problem (Cont.)
while (true) {
...
/* produce an item in next_produced */
...
wait(empty); //wait until empty>0 and then decrement ‘empty’
wait(mutex); //acquire lock
...
/* add next produced data to the buffer */
...
signal(mutex); //release lock
signal(full); //increment ‘full’
}
Notes:
1. empty>0 if there exist an empty slot
Operating System Concepts – 10th Edition 7.6 Silberschatz, Galvin and Gagne ©2018
Bounded Buffer Problem (Cont.)
The structure of the consumer process
while (true) {
wait(full); //wait until full>0 and then decrement ‘full’
wait(mutex); //acquire lock
...
/* remove an item from buffer to next_consumed */
...
signal(mutex); //release lock
signal(empty); //increment empty
...
/* consume the item in next consumed */
...
}
Operating System Concepts – 10th Edition 7.7 Silberschatz, Galvin and Gagne ©2018
Readers-Writers Problem
Suppose that A data set (database or file )is shared among a number of
concurrent processes. Some of these processes may want only to
read the database, whereas others may want to update (that is,
read and write)
• Readers – only read the data set; they do not perform any updates
• Writers – can both read and write
if two readers access the shared data simultaneously, there is no
problem. However, if a writer and some other process (either a
reader or a writer) access the database simultaneously, there may
be problems.
Example1 Example2
Operating System Concepts – 10th Edition 7.9 Silberschatz, Galvin and Gagne ©2018
Readers-Writers Problem (Cont.)
In the solution to the first readers–writers problem, the reader processes
share the following data structures:
Data set
• Semaphore rw_mutex initialized to 1
binary semaphore
common to both reader and writer processes
rw_mutex functions as a mutual exclusion semaphore for the
writers. It is also used by the first or last reader that enters or
exits the critical section. It is not used by readers that enter or
exit while other readers are in their critical sections.
while (true) {
wait(rw_mutex);
...
/* writing is performed */
...
signal(rw_mutex);
}
Operating System Concepts – 10th Edition 7.11 Silberschatz, Galvin and Gagne ©2018
Readers-Writers Problem (Cont.)
The structure of a reader process
while (true){
wait(mutex); //to write to read_count
read_count++;
if (read_count == 1) /* first reader */
wait(rw_mutex); //to prevent the writer to
enter
signal(mutex);
...
/* reading is performed */
...
wait(mutex);
read_ount--;
if (read_count == 0) /* last reader */
signal(rw_mutex);
signal(mutex);
}
Operating System Concepts – 10th Edition 7.12 Silberschatz, Galvin and Gagne ©2018
Readers-Writers Problem Variations
Operating System Concepts – 10th Edition 7.13 Silberschatz, Galvin and Gagne ©2018
Dining-Philosophers Problem
N philosophers’ sit at a round table with a bowel of rice in the middle.
Operating System Concepts – 10th Edition 7.14 Silberschatz, Galvin and Gagne ©2018
Dining-Philosophers Problem Algorithm
Semaphore Solution
The structure of Philosopher i :
while (true){
wait (chopstick[i] );
wait (chopStick[ (i + 1) % 5] );
signal (chopstick[i] );
signal (chopstick[ (i + 1) % 5] );
}
What is the problem with this algorithm?
Operating System Concepts – 10th Edition 7.15 Silberschatz, Galvin and Gagne ©2018
The problem with this algorithm is that it can lead to deadlock in the
Dining Philosophers Problem.
Deadlock HappensDeadlock Definition: Deadlock occurs when multiple
processes (philosophers) are waiting for resources (chopsticks), but none
can proceed because the resources they need are held by other
processes
How Deadlock Occurs in This Algorithm:
• Each philosopher first waits for their left chopstick (wait(chopstick[i]))
• and then their right chopstick (wait(chopstick[(i + 1) % 5])).
• If all five philosophers simultaneously pick up their left chopstick,
none of them can proceed to pick up the right chopstick, because all
the chopsticks are already being held.
• As a result, all philosophers are stuck waiting indefinitely, causing a
deadlock.
Operating System Concepts – 10th Edition 7.16 Silberschatz, Galvin and Gagne ©2018