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

Lectur 7 Synchronizaton Problems

famous synchronization problems in OS

Uploaded by

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

Lectur 7 Synchronizaton Problems

famous synchronization problems in OS

Uploaded by

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

CSC 323 – Principles of Operating Systems

Instructor: Dr. M. Hasan Jamal


Lecture# 07: Classic Synchronization Problems

1
Producer-Consumer Problem
• Producer-consumer problem is a common paradigm for cooperating processes.
Producer process produces information that is used by the consumer
process.
• Shared buffer used that is filled by the producer and emptied by the consumer
concurrently.
• Must be synchronized so that the consumer does not try to consume an item that
has not yet been produced. Consumer has to wait for the producer
• Unbounded buffer places no particular limit on the size of the buffer
• Bounded buffers assumes that there is a fixed buffer size

Shared Buffer
Consume
Producer
r 2
Producer-Consumer Problem
Producer Consumer
while (TRUE) { while (TRUE) {
// produce an item while (count == 0);
nextProduced = ...
nextConsumed = buffer[out];
while (count == BUFFER_SIZE); out = (out + 1)% BUFFER_SIZE;
count--;
buffer[in] = nextProduced
in = (in + 1)% BUFFER_SIZE; //consume item nextConsumed
count++ }
}

3
Classical Problems of Synchronization
• Classical problems used to test newly-proposed synchronization schemes
• Bounded-Buffer Problem
• Readers-Writers Problem
• Dining-Philosophers Problem

4
Bounded-Buffer Problem
Producer Consumer
while (TRUE) { while (TRUE) {
// produce an item wait(full);
nextProduced = ... wait(mutex);

wait(empty); // remove item from buffer


wait(mutex); nextConsumed = buffer[out];
// Add item to buffer out = (out + 1)% BUFFER_SIZE;
buffer[in] = nextProduced
in = (in + 1)% BUFFER_SIZE; signal(mutex);
signal(empty);
signal(mutex);
signal(full); //consume item nextConsumed
} }

semaphore mutex = 1 5
semaphore full = 0 // 0 slots are full
semaphore empty = N // All slots are empty initially
Bounded-Buffer Problem

Full

BUF_SIZE
Producer
Sleeps

0 time
Consumer
Sleeps

6
Bounded-Buffer Problem

Ensured by synchronization mechanisms: * Red is always less than Blue


Pt – Ct ≤ BUF_SIZE * (Blue – Red) can never be
Pt – Ct ≥ 0 greater than BUF_SIZE

all items produced (Pt)

BUF_SIZE

times
all items consumed (Ct)

7
Readers-Writers Problem
• A data set is shared among a number of concurrent processes
• Readers – can 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
rea
rea writ
der
der er

Data Set rea


writ der 8
er writ rea
er der
Bounded-Buffer Problem
Writer Reader
while (TRUE) { while (TRUE) {
wait(rw_mutex); wait(rd_mutex);
reader_count++;
// Writing is performed
if(reader_count == 1) // 1st Reader
signal(rw_mutex);
wait(rw_mutex);
}
signal(rd_mutex);
// Reading is performed

wait(rd_mutex);
reader_count--;
if(reader_count == 0) // Last Reader
signal(rw_mutex);
signal(rd_mutex);
}
9
int reader_count = 0
semaphore rd_mutex = 1 // protect reader_count variable
semaphore rw_mutex = 1 // protects the dataset
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.
• No reader kept waiting unless writer has permission to use shared object
• No reader should wait for other readers to finish just because a writer is waiting
• Writers may starve

• 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”.
• Once writer is ready, it performs write ASAP
• If writer is waiting, then no new reader may be allowed to read

10
Dining-Philosophers Problem
• N philosophers’ sit at a round table with a bowel of rice in the middle.

• Problem – 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
(Chopsticks are like resources). While a philosopher is holding a chopstick,
another one can not have it.

a resource

a process

11
Dining-Philosophers Problem
• Is not a real problem, but a lot of real resource allocation problems look like
this. If we can solve this problem effectively and efficiently, we can also solve
the real problems.

• From a satisfactory solution:


• We want to have concurrency: two philosophers that are not sitting next to each
other on the table should be able to eat concurrently.
• We don’t want deadlock: waiting for each other indefinitely.
• We don’t want starvation: no philosopher waits forever.

12
Dining-Philosophers Problem
semaphore chopstick[N] = 1
semaphore mutex = 1

while (TRUE) {
// think for a while

wait(mutex);
wait(chopstick[i]);
wait(chopstick[(i+1)%N]);
signal(mutex);

// eat for a while

signal(chopstick[i]);
signal(chopstick[(i+1)%N]);
} 13
Dining-Philosophers Problem Variations
• Allow to pick up the chopsticks only if both are available (pick up must be in
critical section)

• An odd philosopher picks up first his left chopstick and then his right, whereas
an even philosopher picks up his right chopstick first and then his left.

14

You might also like