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

OS_CH7

Chapter 7 discusses synchronization in operating systems, focusing on the critical-section problem and concurrency control issues. It introduces classical synchronization problems such as the Bounded-Buffer (producer-consumer) problem, Readers and Writers problem, and the Dining-Philosophers problem, along with their solutions using semaphores. The chapter emphasizes the importance of preventing race conditions and ensuring mutual exclusion among concurrent processes.

Uploaded by

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

OS_CH7

Chapter 7 discusses synchronization in operating systems, focusing on the critical-section problem and concurrency control issues. It introduces classical synchronization problems such as the Bounded-Buffer (producer-consumer) problem, Readers and Writers problem, and the Dining-Philosophers problem, along with their solutions using semaphores. The chapter emphasizes the importance of preventing race conditions and ensuring mutual exclusion among concurrent processes.

Uploaded by

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

Chapter 7: Synchronization

Examples

Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018
Recap
• In the previous chapter

• discussed the critical-section problem and focused on how race conditions can occur when multiple
concurrent processes share data.

• examined several tools that address the critical-section problem by preventing race conditions from
occurring.

• These tools ranged from pure software-based solution written by programmers to low-level hardware
solutions (such as memory barriers and the compare-and-swap and the test_and_set operation) to higher-
level tools (from mutex locks to semaphores).

Operating System Concepts – 10th Edition 2 Silberschatz, Galvin and Gagne ©2018
Classical Problems of Synchronization

• We have studied the Critical Section problem, which is one of the


synchronization problems
• In this chapter, we will discuss examples of concurrency control
problem
• Classical problems used to test newly-proposed synchronization
schemes
• Bounded-Buffer Problem/ producer-consumer problem
• Readers and Writers Problem
• Dining-Philosophers Problem

Operating System Concepts – 10th Edition 3 Silberschatz, Galvin and Gagne ©2018
Producer-Consumer problem
• The producer produces items one by one and places them in the buffer, this
buffer has a limited number of items that it can hold
• Consumer consumes items one by one from the buffer

Operating System Concepts – 10th Edition 4 Silberschatz, Galvin and Gagne ©2018
Producer finds Buffer is full

Operating System Concepts – 10th Edition 5 Silberschatz, Galvin and Gagne ©2018
Consumer finds empty buffer

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

• The producer-consumer problem can be solved using semaphores


• Producer and consumer share the following
• n buffers, each can hold one item
• Semaphore mutex initialized to the value 1
• Semaphore full initialized to the value 0
• Number of items in the buffer
• Semaphore empty initialized to the value n
• Number of empty spaces in the buffer

Operating System Concepts – 10th Edition 7 Silberschatz, Galvin and Gagne ©2018
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);
}

Operating System Concepts – 10th Edition 8 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 9 Silberschatz, Galvin and Gagne ©2018
Producer-consumer

If the buffer is empty, then the consumer waits until a new item is
placed in the buffer. When the producer produces an item it does the
following: wait (empty) will decrement empty variable and increment
variable full by calling signal (full) this will awake the consumer
waiting for full to become positive wait(full)

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

Wait(empty) Decrements
empty variable

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

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

When the buffer is full, empty = 0 , there is no place to write


a new item, the producer waits until there is an available
place.
This happen if the consumer consumes an item, it signals
(empty) which means empty now is > 0

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

The consumer will be waiting until there is an item in the


buffer (full > 0), consumer is blocked on wait(full)
Once there is an item written by the producer, producer will
signal(full) so full becomes > 0, which means consumer can
proceed

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

After the item is consumed, the consumer will


increment the empty semaphore

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

Operating System Concepts – 10th Edition 16 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
• allow multiple readers to read at the same time will not have any bad
affect on the shared dataset
• However, if a writer and another process (either reader or writer) access
the shared dataset simultaneously, inconsistent data might be the result
• Only one single writer can access the shared data at the same time
• A writer should get an exclusive access to the shared object, no other
reader or writer is allowed to get access

Operating System Concepts – 10th Edition 2 Silberschatz, Galvin and Gagne ©2018
Readers-Writers Problem
• Several variations of how readers and writers are considered – all involve
some form of priorities
• The simplest one, referred to as the first readers–writers problem,
• Requires that no reader be kept waiting unless a writer has already
obtained permission to use the shared object.
• In other words, no reader should wait for other readers to finish simply
because a writer is waiting

• The second readers–writers problem requires that, once a writer is ready,


that writer perform its write as soon as possible.
• In other words, if a writer is waiting to access the object, no new
readers may start reading.
• A solution to either problem may result in starvation. In the first case,
writers may starve; in the second case, readers may starve

Operating System Concepts – 10th Edition 3 Silberschatz, Galvin and Gagne ©2018
Readers-Writers Problem/reader preference
• The reader-writer problem can be solved using semaphores
• Shared Data
• Data set/ what is to be shared
• Semaphore rw_mutex initialized to 1
• compete between readers and writers
• Semaphore mutex initialized to 1
• To lock the read count
• Integer read_count initialized to 0
• keeps track of how many processes are currently reading the object

Operating System Concepts – 10th Edition 4 Silberschatz, Galvin and Gagne ©2018
Readers-Writers Problem (Cont.)

• The structure of a writer process


• The writer will call wait(rw_mutex),
while (true) { if its is the first process then it will
wait(rw_mutex); lock the rw_mutex semaphore
... • Wait make rw_mutex = 0
/* writing is performed */ • No other writer or reader will be
... allowed, any process trying to call
signal(rw_mutex); wait(rw_mutex) will be blocked until
the first writer signal the rw_mutex
}

Operating System Concepts – 10th Edition 5 Silberschatz, Galvin and Gagne ©2018
Readers-Writers Problem (Cont.)
• The structure of a reader process
while (true){ • This is necessary to lock the
wait(mutex);
read_count++; read count, if many readers
if (read_count == 1) are trying to read at the same
wait(rw_mutex); time, then we need to
signal(mutex); synchronize how they
... increment the read_count
/* reading is performed */ variable
...
wait(mutex); • Other reader can start after
read count--;
if (read_count == 0) the one locking the mutex
signal(rw_mutex); release it using signal(mutex)
signal(mutex);
}

Operating System Concepts – 10th Edition 6 Silberschatz, Galvin and Gagne ©2018
Readers-Writers Problem (Cont.)
• The structure of a reader process
while (true){ • When the first reader comes, it
wait(mutex);
read_count++; updates the read_count, so it is
if (read_count == 1) equal to 1. then it will call
wait(rw_mutex); wait(rw_mutex) to block the
signal(mutex); writer. Othor readers don’t
... need to lock the writer, it is
/* reading is performed */
already locked by the first
...
reader
wait(mutex);
read count--;
if (read_count == 0) • The last reader release the lock
signal(rw_mutex); to enable the writer to resume
signal(mutex); (read_count ==0)
}
• First-reader competes with the
writer and last reader signal the
writer

Operating System Concepts – 10th Edition 7 Silberschatz, Galvin and Gagne ©2018
Readers-Writers Problem/ analyze this code

Readers will be
blocked until all
writers finish

The first writer will


block the reader:
wait(rd)

Last writer will


signal the reader
signal (rd)

Operating System Concepts – 10th Edition 8 Silberschatz, Galvin and Gagne ©2018
Readers-Writers Problem Variations

• First variation (reader preference) – no reader kept waiting


unless writer has permission to use shared object
• Second variation (writer preference) – once writer is ready,
it performs the write ASAP
• 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 9 Silberschatz, Galvin and Gagne ©2018
Classical Problems of Synchronization
• We have studied the Critical Section problem, which is one of the
synchronization problems
• In this chapter, we will discuss examples of concurrency control
problem
• Classical problems used to test newly-proposed synchronization
schemes
• Bounded-Buffer Problem/ producer-consumer problem
• Readers and Writers Problem
• Dining-Philosophers Problem

Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018
Solution to Bounded-Buffer Problem
•Producer must not insert data when the buffer is full
•Consumer must not remove data when buffer is empty
•The producer and consumer must not insert and remove data
simultaneously.

•The producer-consumer problem can be solved using semaphores


•Producer and consumer share the following 3 semaphores:

•mutex: a binary semaphore which is used to acquire and release


the lock.
•empty: a counting semaphore whose initial value is the number
of slots in the buffer, since, initially all slots are empty.
•full: a counting semaphore whose initial value is 0.

Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018
Bounded Buffer Problem (Cont.)
•The structure of the producer process

while (true) {
...
/* produce an item in next_produced */
...
wait(empty); // wait until empty > 0 and then decrement
‘empty’
wait(mutex); // acquire lock
...
/* add next produced to the buffer */
...
signal(mutex); // release lock
signal(full); // increment ‘full’
}

Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018
Bounded Buffer Problem (Cont.)
•The structure of the consumer process

while (true) {
wait(full); // wait until full>0 and then decrement ‘full’
wait(mutex); // acquire lock
...
/* remove an item from buffer to next_consumed */
...
signal(mutex); // release lock
signal(empty); // increment ‘empty’
...
/* consume the item in next consumed */
...
}

Operating System Concepts – 10th Edition 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 – update data (both read and write)
• allow multiple readers to read at the same time will not have any bad
affect on the shared dataset
• However, if a writer and another process (either reader or writer) access
the shared dataset simultaneously, inconsistent data might be the result
• Only one single writer can access the shared data at the same time
• A writer should get an exclusive access to the shared object, no other
reader or writer is allowed to get access

Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018
Readers-Writers Problem/reader preference

• The reader-writer problem can be solved using 2 semaphores and an


integer variable
• Shared Data
• Data set/ what is to be shared
• Semaphore rw_mutex initialized to 1
• compete between readers and writers
• Shared between reader and writer
• Semaphore mutex initialized to 1
• Is used to ensure mutual exclusion when read_count is
updated i.e. when any reader enters or exit from CS
• To lock the read count
• no two processes make update on read_count
• Integer read_count initialized to 0
• keeps track of how many processes are currently reading the
object
• How many readers are reading the same data

Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018
Readers-Writers Problem (Cont.)

• The structure of a writer process


• The writer will call wait(rw_mutex),
while (true) {
wait(rw_mutex); //if wr_mutex is if its is the first process then it will
not held or used by other process, it will lock the rw_mutex semaphore
enter the CS
... • Wait make rw_mutex = 0
/* writing is performed */ • No other writer or reader will be
...
allowed, any process trying to call
signal(rw_mutex); // leaves the CS
} wait(rw_mutex) will be blocked until
the first writer signal the rw_mutex

Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018
Readers-Writers Problem (Cont.)
• The structure of a reader process
while (true){ • This is necessary to lock the
wait(mutex);
read_count++; // acquire a lock when reader want to update read count, if many readers
read_count are trying to read at the same
time, then we need to
if (read_count == 1) synchronize how they
wait(rw_mutex); // to ensure no writer can enter if there is increment the read_count
even one reader variable
signal(mutex); // other readers can enter while this current
reader is inside the CS • Other reader can start after
... the one locking the mutex
/* reading is performed */ release it using signal(mutex)
...

wait(mutex);
read count--;// a reader wants to leave
if (read_count == 0) // no reader is left in CS

signal(rw_mutex); // writers can enter

signal(mutex); // reader leaves

Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018
Readers-Writers Problem Variations

• First variation (reader preference) – no reader kept waiting unless


writer has permission to use shared object
• Second variation (writer preference) – once writer is ready, it
performs the write ASAP
• 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 Silberschatz, Galvin and Gagne ©2018
Dining-Philosophers Problem

• Five philosophers are sitting around a table. Each philosopher has a plate in front of
him/her.
• Between each two plates there is a chopstick.
• A philosopher is either “thinking” or “eating”.
• When a philosopher gets hungry he/she tries to get the 2 chopsticks on his/her left
and right.
• Having obtained both chopsticks the philosopher can eat.
• They cannot have both chopstick at the same time; pick the left then the right
• When finished eating, the philosopher puts both chopsticks down, and resumes
thinking.

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

• One simple solution is to represent each chopstick with a semaphore.


• A philosopher tries to grab a chopstick by executing a wait() operation on that
semaphore.
• She releases her chopsticks by executing the signal() operation on the
appropriate semaphores.
• Thus, the shared data are
• semaphore chopstick[5];

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

• In the case of 5 philosophers


• Shared data
• Bowl of rice (data set)
• Semaphore chopstick [5] initialized to 1
• semaphore chopstick[5]={1,1,1,1,1};

Operating System Concepts – 10th Edition 13 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 14 Silberschatz, Galvin and Gagne ©2018
Dining-Philosophers Problem

• This solution guarantees that no two neighbors are eating simultaneously, it


nevertheless must be rejected because it could create a deadlock.
• Suppose that all five philosophers become hungry at the same time
• and each grabs her left chopstick.
• All the elements of chopstick array (semaphores) will now be equal to 0.
• When each philosopher tries to grab her right chopstick, she will be
delayed forever.

Operating System Concepts – 10th Edition 15 Silberschatz, Galvin and Gagne ©2018
Think of these cases

• What about deadlock in the following scenarios


• Allow at most four philosophers to be sitting simultaneously at the table
while maintaining five chopsticks!
• Allow a philosopher to pick up his/her chopsticks only if both chopsticks are
available.

Operating System Concepts – 10th Edition 16 Silberschatz, Galvin and Gagne ©2018
Think of these cases

• Several possible remedies to the deadlock problem are the following:


• Allow at most four philosophers to be sitting simultaneously at the table.
• Allow a philosopher to pick up her chopsticks only if both chopsticks are
available (to do this, she must pick them up in a critical section).
• Use an asymmetric solution—that is, an odd-numbered philosopher picks
up first her left chopstick and then her right chopstick, whereas an
evennumbered philosopher picks up her right chopstick and then her left
chopstick.

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

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

You might also like