Professional Documents
Culture Documents
Chapter 7: Synchronization Examples: Silberschatz, Galvin and Gagne ©2018 Operating System Concepts - 10 Edition
Chapter 7: Synchronization Examples: Silberschatz, Galvin and Gagne ©2018 Operating System Concepts - 10 Edition
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
• Bounded-Buffer Problem
• Readers and Writers Problem
• Dining-Philosophers 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 (Cont.)
while (true) {
...
/* produce an item in next_produced */
...
wait(empty);
wait(mutex);
...
/* add next produced to the buffer */
...
signal(mutex);
signal(full);
}
Operating System Concepts – 10th Edition 7.5 Silberschatz, Galvin and Gagne ©2018
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 */
...
}
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
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
Operating System Concepts – 10th Edition 7.8 Silberschatz, Galvin and Gagne ©2018
Readers-Writers Problem (Cont.)
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.)
Operating System Concepts – 10th Edition 7.10 Silberschatz, Galvin and Gagne ©2018
Readers-Writers Problem Variations
Operating System Concepts – 10th Edition 7.11 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.12 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.13 Silberschatz, Galvin and Gagne ©2018
Monitor Solution to Dining Philosophers
monitor DiningPhilosophers
{
enum { THINKING; HUNGRY, EATING) state [5] ;
condition self [5];
Operating System Concepts – 10th Edition 7.14 Silberschatz, Galvin and Gagne ©2018
Solution to Dining Philosophers (Cont.)
initialization_code() {
for (int i = 0; i < 5; i++)
state[i] = THINKING;
}
}
Operating System Concepts – 10th Edition 7.15 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);
DiningPhilosophers.putdown(i);
Operating System Concepts – 10th Edition 7.16 Silberschatz, Galvin and Gagne ©2018
Kernel Synchronization - Windows
Operating System Concepts – 10th Edition 7.17 Silberschatz, Galvin and Gagne ©2018
Kernel Synchronization - Windows
Operating System Concepts – 10th Edition 7.18 Silberschatz, Galvin and Gagne ©2018
Linux Synchronization
Linux:
• Prior to kernel Version 2.6, disables interrupts to implement
short critical sections
• Version 2.6 and later, fully preemptive
Linux provides:
• Semaphores
• Atomic integers
• Spinlocks
• Reader-writer versions of both
On single-CPU system, spinlocks replaced by enabling and
disabling kernel preemption
Operating System Concepts – 10th Edition 7.19 Silberschatz, Galvin and Gagne ©2018
Linux Synchronization
Atomic variables
atomic_t counter;
int value;
Operating System Concepts – 10th Edition 7.20 Silberschatz, Galvin and Gagne ©2018
POSIX Synchronization
Operating System Concepts – 10th Edition 7.21 Silberschatz, Galvin and Gagne ©2018
POSIX Mutex Locks
Operating System Concepts – 10th Edition 7.22 Silberschatz, Galvin and Gagne ©2018
POSIX Semaphores
Operating System Concepts – 10th Edition 7.23 Silberschatz, Galvin and Gagne ©2018
POSIX Named Semaphores
Another process can access the semaphore by referring to its name SEM.
Acquiring and releasing the semaphore:
Operating System Concepts – 10th Edition 7.24 Silberschatz, Galvin and Gagne ©2018
POSIX Unnamed Semaphores
Operating System Concepts – 10th Edition 7.25 Silberschatz, Galvin and Gagne ©2018
POSIX Condition Variables
Operating System Concepts – 10th Edition 7.26 Silberschatz, Galvin and Gagne ©2018
POSIX Condition Variables
Operating System Concepts – 10th Edition 7.27 Silberschatz, Galvin and Gagne ©2018
Java Synchronization
Operating System Concepts – 10th Edition 7.28 Silberschatz, Galvin and Gagne ©2018
Java Monitors
Operating System Concepts – 10th Edition 7.29 Silberschatz, Galvin and Gagne ©2018
Bounded Buffer – Java Synchronization
Operating System Concepts – 10th Edition 7.30 Silberschatz, Galvin and Gagne ©2018
Java Synchronization
Operating System Concepts – 10th Edition 7.31 Silberschatz, Galvin and Gagne ©2018
Java Synchronization
Operating System Concepts – 10th Edition 7.32 Silberschatz, Galvin and Gagne ©2018
Java Synchronization
Operating System Concepts – 10th Edition 7.33 Silberschatz, Galvin and Gagne ©2018
Bounded Buffer – Java Synchronization
Operating System Concepts – 10th Edition 7.34 Silberschatz, Galvin and Gagne ©2018
Bounded Buffer – Java Synchronization
Operating System Concepts – 10th Edition 7.35 Silberschatz, Galvin and Gagne ©2018
Java Reentrant Locks
Operating System Concepts – 10th Edition 7.36 Silberschatz, Galvin and Gagne ©2018
Java Semaphores
Constructor:
Usage:
Operating System Concepts – 10th Edition 7.37 Silberschatz, Galvin and Gagne ©2018
Java Condition Variables
Operating System Concepts – 10th Edition 7.38 Silberschatz, Galvin and Gagne ©2018
Java Condition Variables
Example:
Five threads numbered 0 .. 4
Shared variable turn indicating which thread’s turn it is.
Thread calls doWork() when it wishes to do some work. (But it may only
do work if it is their turn.
If not their turn, wait
If their turn, do some work for awhile …...
When completed, notify the thread whose turn is next.
Necessary data structures:
Operating System Concepts – 10th Edition 7.39 Silberschatz, Galvin and Gagne ©2018
Java Condition Variables
Operating System Concepts – 10th Edition 7.40 Silberschatz, Galvin and Gagne ©2018
Alternative Approaches
Transactional Memory
OpenMP
Functional Programming Languages
Operating System Concepts – 10th Edition 7.41 Silberschatz, Galvin and Gagne ©2018
Transactional Memory
Consider a function update() that must be called atomically. One
option is to use mutex locks:
Operating System Concepts – 10th Edition 7.42 Silberschatz, Galvin and Gagne ©2018
OpenMP
OpenMP is a set of compiler directives and API that support
parallel progamming.
Operating System Concepts – 10th Edition 7.43 Silberschatz, Galvin and Gagne ©2018
Functional Programming Languages
Operating System Concepts – 10th Edition 7.44 Silberschatz, Galvin and Gagne ©2018
End of Chapter 7
Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018