0% found this document useful (0 votes)
83 views14 pages

Synchronization Examples: Complete

The document discusses three classical synchronization problems: [1] the bounded buffer problem, [2] the readers-writers problem, and [3] the dining philosophers problem. For each problem, it provides an overview and describes the key shared resources and processes involved. It also outlines potential solutions using semaphores. The dining philosophers problem algorithm presented risks deadlock if all philosophers pick up chopsticks simultaneously. Possible remedies include limiting the number of eating philosophers or requiring both chopsticks to be available before picking up.

Uploaded by

WEBSITE NINJA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views14 pages

Synchronization Examples: Complete

The document discusses three classical synchronization problems: [1] the bounded buffer problem, [2] the readers-writers problem, and [3] the dining philosophers problem. For each problem, it provides an overview and describes the key shared resources and processes involved. It also outlines potential solutions using semaphores. The dining philosophers problem algorithm presented risks deadlock if all philosophers pick up chopsticks simultaneously. Possible remedies include limiting the number of eating philosophers or requiring both chopsticks to be available before picking up.

Uploaded by

WEBSITE NINJA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Synchronization Examples

complete

Instructor: Asst Prof Mobeena Shahzad

BESE 24 (A,B,C)
2 Classical Problems of Synchronization
 Classical problems used to test newly-proposed
synchronization schemes
 Bounded-Buffer Problem

 Readers and Writers Problem

 Dining-Philosophers Problem
3 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
4 Bounded Buffer Problem (Cont.)
 The structure of the producer process

while (true) {
...
/* produce an item in next_produced */
...
wait(empty);
wait(mutex);
...
/* add next_produced to the buffer */
...
signal(mutex);
signal(full);
}
5 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 */
...
}
6 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
7 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
8 Readers-Writers Problem (Cont.)
 The structure of a writer process

while (true) {

wait(rw_mutex);
...
/* writing is performed */
...
signal(rw_mutex);
}
9 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);
}
10 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


11 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
12 Dining-Philosophers Problem Algorithm
 Semaphore Solution
 The structure of Philosopher i :
while (true){
wait (chopstick[i] );
wait (chopStick[ (i + 1) % 5] );

/* eat for a while */

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

/* think for a while */

}
 What is the problem with this algorithm? Deadlock
13 Possible Remedies for deadlock
 Allow at most four philosophers to be sitting simultaneously

 Allow a philosopher to pick up his chopstick only if both chopsticks


are available

 Use an asymmetric solution


End of Lecture 7

“Synchronization Examples”

You might also like