Chapter 7: Synchronization
Examples
Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018
Outline
Explain the bounded-buffer synchronization problem
Explain the readers-writers synchronization problem
Explain and dining-philosophers synchronization problems
Describe the tools used by Linux and Windows to solve
synchronization problems.
Illustrate how POSIX and Java can be used to solve process
synchronization problems
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
We will use three semaphores:
• Semaphore mutex initialized to the value 1
Recall, mutex is a binary semaphore which is used
to acquire and release the lock
• Semaphore full: A counting semaphore initialized to the value 0
it stores the number of the filled slots in the buffer
• Semaphore empty initialized to the value n
Empty is a counting semaphore whose initial value is the
number of the slots in the buffer, since, initially all slots are
empty.
Empty stores the count of the empty slots in the buffer
Operating System Concepts – 10th Edition 7.5 Silberschatz, Galvin and Gagne ©2018
Bounded Buffer Problem (Cont.)
The structure of the producer process
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.
To ensure that these difficulties do not arise, we require that the
writers
have exclusive access to the shared database while writing to the
database. This synchronization problem is referred to as the
readers–writers problem.
• Since it was originally stated, it has been used to test nearly
every new synchronization primitive.
Main points in the first type of readers–write rs problem :
• If writer has started a writing process, then
No additional or other writer can perform write function
No reader is allowed to read
• If one or more readers are reading, then
Other readers may read at the same time
No writer may perform write function until all readers have
finished reading
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.
• Semaphore mutex initialized to 1
binary semaphore
The mutex semaphore is used to ensure mutual exclusion when
the variable read_count is updated.
• Integer read_count initialized to 0
is a counting semaphore
keeps track of how many processes are currently reading the
Readers-Writers Problem (Cont.)
The structure of a writer process
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
The solution in previous slide can result in a situation where
a writer process never writes. It is referred to as the “First
reader-writer” problem.
The “Second reader-writer” problem is a variation of the
first reader-writer problem that state:
• Once a writer is ready to write, no “newly arrived
reader” is allowed to read.
Both the first and second may result in starvation leading to
even more variations
Problem is solved on some systems by kernel providing
reader-writer locks
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.
They spend their lives alternating thinking and eating.
They do not interact with their neighbors.
Occasionally try to pick up 2 chopsticks (one at a time) to eat from bowl
• Need both to eat, then release both when done
In the case of 5 philosophers, the shared data
Bowl of rice (data set)
Semaphore chopstick [5] initialized to 1
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] );
/* eat for awhile */
signal (chopstick[i] );
signal (chopstick[ (i + 1) % 5] );
/* think for awhile */
}
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