Chapter 7: Synchronization
Examples
Instructor: Dr Farzana
Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018
Classical Problems of Synchronization
▪ Classical problems used to test newly-proposed
synchronization schemes
• Bounded-Buffer Problem
• Readers and Writers Problem
• Dining-Philosophers Problem
Operating System Concepts – 10th Edition 7.2 Silberschatz, Galvin and Gagne ©2018
Bounded Buffer Problem
Operating System Concepts – 10th Edition 7.3 Silberschatz, Galvin and Gagne ©2018
Bounded Buffer Problem
Operating System Concepts – 10th Edition 7.4 Silberschatz, Galvin and Gagne ©2018
Bounded-Buffer Problem
▪ Solution ??
Operating System Concepts – 10th Edition 7.5 Silberschatz, Galvin and Gagne ©2018
Bounded Buffer Problem(sol)
Operating System Concepts – 10th Edition 7.6 Silberschatz, Galvin and Gagne ©2018
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
Operating System Concepts – 10th Edition 7.7 Silberschatz, Galvin and Gagne ©2018
Operating System Concepts – 10th Edition 7.8 Silberschatz, Galvin and Gagne ©2018
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.9 Silberschatz, Galvin and Gagne ©2018
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--;
if (read_count == 0) /* last reader */
signal(rw_mutex);
signal(mutex);
}
Operating System Concepts – 10th Edition 7.10 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 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.11 Silberschatz, Galvin and Gagne ©2018
Today’s Lecture
Operating System Concepts – 10th Edition 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
Operating System Concepts – 10th Edition 7.13 Silberschatz, Galvin and Gagne ©2018
Dining-Philosophers Problem
Operating System Concepts – 10th Edition 7.14 Silberschatz, Galvin and Gagne ©2018
Dining-Philosophers Problem (sol.)
Operating System Concepts – 10th Edition 7.15 Silberschatz, Galvin and Gagne ©2018
Semaphores
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.16 Silberschatz, Galvin and Gagne ©2018
Semaphores
Operating System Concepts – 10th Edition 7.17 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.18 Silberschatz, Galvin and Gagne ©2018
Problem with semaphores solution
Operating System Concepts – 10th Edition 7.19 Silberschatz, Galvin and Gagne ©2018
Dinning Philosopher's Problem
Operating System Concepts – 10th Edition 7.20 Silberschatz, Galvin and Gagne ©2018
Quiz
Operating System Concepts – 10th Edition 7.21 Silberschatz, Galvin and Gagne ©2018
Monitors
Operating System Concepts – 10th Edition 7.22 Silberschatz, Galvin and Gagne ©2018
Monitors
Operating System Concepts – 10th Edition 7.23 Silberschatz, Galvin and Gagne ©2018
Monitors
Operating System Concepts – 10th Edition 7.24 Silberschatz, Galvin and Gagne ©2018
Monitors
Operating System Concepts – 10th Edition 7.25 Silberschatz, Galvin and Gagne ©2018
Solution to Dining Philosophers (Cont.)
▪ Each philosopher “i” invokes the operations pickup() and
putdown() in the following sequence:
DiningPhilosophers.pickup(i);
/** EAT **/
DiningPhilosophers.putdown(i);
▪ No deadlock, but starvation is possible
Operating System Concepts – 10th Edition 7.26 Silberschatz, Galvin and Gagne ©2018
Monitor Solution to Dining Philosophers
monitor DiningPhilosophers
{
enum { THINKING; HUNGRY, EATING) state [5] ;
condition self [5];
void pickup (int i) {
state[i] = HUNGRY;
test(i);
if (state[i] != EATING) self[i].wait;
}
void putdown (int i) {
state[i] = THINKING;
// test left and right neighbors
test((i + 4) % 5);
test((i + 1) % 5);
}
Operating System Concepts – 10th Edition 7.27 Silberschatz, Galvin and Gagne ©2018
Solution to Dining Philosophers (Cont.)
void test (int i) {
if ((state[(i + 4) % 5] != EATING) &&
(state[i] == HUNGRY) &&
(state[(i + 1) % 5] != EATING) ) {
state[i] = EATING ;
self[i].signal () ;
}
}
initialization_code() {
for (int i = 0; i < 5; i++)
state[i] = THINKING;
}
}
Operating System Concepts – 10th Edition 7.28 Silberschatz, Galvin and Gagne ©2018