0% found this document useful (0 votes)
4 views

CH7

Uploaded by

221020
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

CH7

Uploaded by

221020
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Chapter 7: Synchronization

Examples

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
Semaphore
 Synchronization tool that provides more sophisticated ways (than Mutex Locks)
 Keeping track of the availability of the required resources

 Semaphore S (protected integer variable) can only be accessed via two atomic
operations: wait()and signal()

 Definition of the wait()operation


wait(S){
while (S == 0)
; // busy wait
S--;
}

 Definition of the signal() operation


signal(S){
S++;
}

Operating System Concepts – 10th Edition 7.3 Silberschatz, Galvin and Gagne ©2018
Bounded-Buffer Problem

 n buffers, each can hold one item


 Semaphore mutex initialized to the value 1
 Semaphore full initialized to the value 0
 Semaphore empty initialized to the value n

Operating System Concepts – 10th Edition 7.4 Silberschatz, Galvin and Gagne ©2018
Bounded Buffer Problem (Cont.)
 The structure of the producer process
 The structure of the consumer
(bounded)
process (bounded)
full =0; empty =n;
full =0; empty =n;
do {
Do {
...
/* produce an item in wait(full);
next_produced */ wait(mutex);
wait(empty); ...
wait(mutex); /* remove an item from
buffer to next_consumed */
...
/* add next produced ...
to the buffer */ signal(mutex);
... signal(empty);
signal(mutex);
signal(full); /* consume the item in
next consumed */
...
} while (true);
} while (true);

Operating System Concepts – 10th Edition 7.5 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
 If a writer and some other process (either a reader or a writer) access
the data set simultaneously, problems may occur
 The writers should have exclusive access to the data set while writing

 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.6 Silberschatz, Galvin and Gagne ©2018
Readers-Writers Problem Variations

 First variation – no reader is kept waiting unless writer has


permission to use shared object (i.e., no reader should wait for other
readers to finish simply because a writer is waiting)

 Second variation – once writer is ready, it performs the write ASAP


(i.e., if a writer is waiting to access the object, no new readers may
start reading)

 Both may have starvation leading to even more variations

 Problem is solved on some systems by kernel providing reader-writer


locks

Operating System Concepts – 10th Edition 7.7 Silberschatz, Galvin and Gagne ©2018
Readers-Writers Problem (Cont.)
 The structure of a reader process
 The structure of a writer process do {
wait(mutex);
do { read_count++;
wait(rw_mutex); if (read_count == 1)
... wait(rw_mutex);
/* writing is performed */ signal(mutex);
... ...
/* reading is performed */
signal(rw_mutex);
...
wait(mutex);
} while (true);
read count--;
if (read_count == 0)
signal(rw_mutex);
signal(mutex);
} while (true);

Operating System Concepts – 10th Edition 7.8 Silberschatz, Galvin and Gagne ©2018
Dining-Philosophers Problem

 Philosophers spend their lives alternating thinking and eating


 Don’t 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
 Shared data
 Bowl of rice (data set)
 Semaphore chopstick[5] initialized to 1

Operating System Concepts – 10th Edition 7.9 Silberschatz, Galvin and Gagne ©2018
Dining-Philosophers Problem Algorithm
 The structure of Philosopher i:
do {
wait (chopstick[i]);
wait (chopstick[(i + 1) % 5]);

// eat

signal (chopstick[i]);
signal (chopstick[(i + 1) % 5]);

// think

} while (TRUE);

 What is the problem with this algorithm?

Operating System Concepts – 10th Edition 7.10 Silberschatz, Galvin and Gagne ©2018
End of Chapter 7

Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018

You might also like