You are on page 1of 18

Chapter 5: Part 2

Classical Problems of
Synchronization

Operating System Concepts – 8th Edition, Silberschatz, Galvin and Gagne ©2009
Deadlock
 Deadlock – two or more processes are  Starvation – indefinite blocking. A
waiting indefinitely for an event that process may never be removed from
can be caused by only one of the the semaphore queue in which it is
waiting processes. suspended.

Operating System Concepts – 8th Edition 6.2 Silberschatz, Galvin and Gagne ©2009
Classical Problems of Synchronization
 Bounded-Buffer Problem
 Bounded buffers P/C can be seen in e.g. streaming filters or packet
switching in networks.

 Readers and Writers Problem


 Database readers and writers: online reservation systems; file systems.

 Dining-Philosophers Problem
 Dining philosophers could be a sequence of active database transactions
that have a circular wait-for-lock dependence.

 The Sleeping Barber problem


 Sleeping Barber is often generally thought of as a client-server relationship.

 The cigarette smoker problem


 Distributing different data across networks.
Operating System Concepts – 8th Edition 6.3 Silberschatz, Galvin and Gagne ©2009
Bounded-Buffer Problem

A finite supply of containers is available.


Producers take an empty container and fill it with
a product. Consumers take a full container,
consume the product and leave an empty
container. The main complexity of this problem is
that we must maintain the count for both the
number of empty and full containers that are
available.

Operating System Concepts – 8th Edition 6.4 Silberschatz, Galvin and Gagne ©2009
Bounded-Buffer Problem

Operating System Concepts – 8th Edition 6.5 Silberschatz, Galvin and Gagne ©2009
Bounded-Buffer Problem

Operating System Concepts – 8th Edition 6.6 Silberschatz, Galvin and Gagne ©2009
The Readers-Writers Problem

Multiple readers or a single writer can use DB.

X X X
writer reader writer reader
reader

writer reader
reader
reader
writer
reader reader

7
Operating System Concepts – 8th Edition 6.7 Silberschatz, Galvin and Gagne ©2009
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

Operating System Concepts – 8th Edition 6.8 Silberschatz, Galvin and Gagne ©2009
Readers-Writers Problem
 If one notebook exists where writers may write information to, only one
writer may write at a time. Confusion may arise if a reader is trying read at
the same as a writer is writing. Since readers only look at the data, but do
not modify the data, we can allow more than one reader to read at the
same time.
 The main complexity with this problems stems from allowing more than
one reader to access the data at the same time.
 Application: A message distribution system is an example of Readers and
Writers. We must keep track of how many messages are in the queue.

Operating System Concepts – 8th Edition 6.9 Silberschatz, Galvin and Gagne ©2009
Readers-Writers Problem

Operating System Concepts – 8th Edition 6.10 Silberschatz, Galvin and Gagne ©2009
Dining-Philosophers Problem

Shared data
Bowl of rice (data set)
Semaphore chopstick [5] initialized to 1

Operating System Concepts – 8th Edition 6.11 Silberschatz, Galvin and Gagne ©2009
Dining-Philosophers Problem
 Five philosophers (the tasks) spend their time thinking and eating spaghetti.
They eat at a round table with five individual seats. To eat, each philosopher
needs two forks (the resources). There are five forks on the table, one to the
left and one to the right of each seat. When a philosopher can not grab both
forks, he sits and waits. Eating takes random time, then the philosopher puts
the forks down and leaves the dining room. After spending some random time
thinking about the nature of the universe, he again becomes hungry, and the
circle repeats itself.
 A Philosopher needs both the fork on his left and on his right to eat. The forks
are shared with the neighbors on either side.
 Application: The dining philosophers problem represents a situation that can
occur in large communities of processes that share a sizeable pool of
resources.

Operating System Concepts – 8th Edition 6.12 Silberschatz, Galvin and Gagne ©2009
Dining-Philosophers Problem

Operating System Concepts – 8th Edition 6.13 Silberschatz, Galvin and Gagne ©2009
The Sleeping Barber Problem
A barbershop consists of a
waiting room with N chairs, and
the barber room containing the
barber chair. If there are no
customers to be served the
barber goes to sleep. If a
customer enters the
barbershop and all chairs are
busy, then the customer leaves
the shop. If the barber is busy,
then the customer sits in one
of the available free chairs. If
the barber is asleep, the
customer wakes the barber up.

Operating System Concepts – 8th Edition 6.14 Silberschatz, Galvin and Gagne ©2009
The Sleeping Barber Problem

Operating System Concepts – 8th Edition 6.15 Silberschatz, Galvin and Gagne ©2009
The Cigarette Smokers
 Four threads are involved: an agent and three smokers. The smokers loop
forever, first waiting for ingredients, then making and smoking cigarettes.
The ingredients are tobacco, paper, and matches.
 We assume that the agent has an infinite supply of all three ingredients, and
each smoker has an infinite supply of one of the ingredients; that is, one
smoker has matches, another has paper, and the third has tobacco. The
agent repeatedly chooses two different ingredients at random and makes
them available to the smokers. Depending on which ingredients are chosen,
the smoker with the complementary ingredient should pick up both
resources and proceed. For example, if the agent puts out tobacco and
paper, the smoker with the matches should pick up both ingredients, make
a cigarette, and then signal the agent.

Operating System Concepts – 8th Edition 6.16 Silberschatz, Galvin and Gagne ©2009
The Cigarette Smokers

Operating System Concepts – 8th Edition 6.17 Silberschatz, Galvin and Gagne ©2009
End of Chapter 6

Operating System Concepts – 8th Edition, Silberschatz, Galvin and Gagne ©2009

You might also like