Professional Documents
Culture Documents
Operating Systems
Operating System Concepts – 9th Edit9on Silberschatz, Galvin and Gagne ©2013
Chapter 5: Process
Synchronization
Operating System Concepts – 8th Edition Silberschatz, Galvin and Gagne ©2009
Process Synchronization
Operating System Concepts – 8th Edition 6.3 Silberschatz, Galvin and Gagne ©2009
Critical Section Problem
Informally, a critical section is a code segment that accesses
shared variables and has to be executed as an atomic action.
Operating System Concepts – 8th Edition 6.4 Silberschatz, Galvin and Gagne ©2009
Critical Section Problem
Consider system of n processes {p0, p1, … pn-1}
Each process has critical section segment of code
Process may be changing common variables, updating table,
writing file, etc
When one process in critical section, no other may be in its
critical section
Critical section problem is to design protocol to solve this
Each process must ask permission to enter critical section in
entry section, may follow critical section with exit section, then
remainder section
Especially challenging with preemptive kernels.
Operating System Concepts – 8th Edition 6.5 Silberschatz, Galvin and Gagne ©2009
Critical Section
General structure of process pi is
Operating System Concepts – 8th Edition 6.6 Silberschatz, Galvin and Gagne ©2009
Solution to Critical-Section Problem
Solution to the Critical Section Problem must meet three conditions...
Operating System Concepts – 8th Edition 6.7 Silberschatz, Galvin and Gagne ©2009
Solution to Critical-Section Problem
3. Bounded Waiting - there must exist a bound on the number of times that
other processes are allowed to enter their critical sections after a process
has made a request to enter its critical section and before that request is
granted
• The wait is the time from when a process makes a request to enter its
critical section until that request is granted.
• in practice, once a process enters its critical section, it does not get another
turn until a waiting process gets a turn (managed as a queue).
Peterson’s Solution
Peterson’s Solution is a classical software based solution to the critical
section problem.
In Peterson’s solution, we have two shared variables:
boolean flag[i] : Initialized to FALSE, initially no one is interested in
entering the critical section
int turn : The process whose turn is to enter the critical section.
Operating System Concepts – 8th Edition 6.9 Silberschatz, Galvin and Gagne ©2009
Peterson’s Solution
Peterson’s Solution preserves all three conditions :
1. Mutual Exclusion is assured as only one process can access the
critical section at any time.
2. Progress is also assured, as a process outside the critical section
does not blocks other processes from entering the critical
section.
3. Bounded Waiting is preserved as every process gets a fair
chance.
Disadvantages of Peterson’s Solution
It involves Busy waiting
It is limited to 2 processes.
Operating System Concepts – 8th Edition 6.10 Silberschatz, Galvin and Gagne ©2009
Semaphore
Semaphore is a simply a variable. This variable is used to solve critical
section problem and to achieve process synchronization in the multi
processing environment.
Semaphore S – integer variable
Two standard operations modify S: wait() and signal()
Originally called P() and V()
Can only be accessed via two indivisible (atomic) operations
wait (S) {
while S <= 0
; // no-op
S--;
}
signal (S) { S++;
}
Operating System Concepts – 8th Edition 6.11 Silberschatz, Galvin and Gagne ©2009
Semaphore
P(Semaphore s)
{
s = s - 1;
if (s < 0) {
// add process to queue
block();
}
}
V(Semaphore s)
{
s = s + 1;
if (s >= 0) {
// remove process p from queue
wakeup(p);
}
}
Operating System Concepts – 8th Edition 6.12 Silberschatz, Galvin and Gagne ©2009
Classical Problems of Synchronization
Classical problems used to test newly-proposed synchronization
schemes:
Bounded-Buffer Problem
Readers and Writers Problem
Dining-Philosophers Problem
Cigarette Smokers problem
Sleeping barber problem
Operating System Concepts – 8th Edition 6.13 Silberschatz, Galvin and Gagne ©2009
Bounded-Buffer Problem
Also known as producer-consumer problem
The problem is to make sure that the producer won't try to add data
into the buffer if it's full and that the consumer won't try to remove
data from an empty buffer.
Operating System Concepts – 8th Edition 6.14 Silberschatz, Galvin and Gagne ©2009
Bounded-Buffer Problem
Operating System Concepts – 8th Edition 6.15 Silberschatz, Galvin and Gagne ©2009
Assignment : Bounded-Buffer Problem
Implement Producer Consumer Problem in C programming Language.
Operating System Concepts – 8th Edition 6.16 Silberschatz, Galvin and Gagne ©2009
Readers-Writers Problem
Operating System Concepts – 8th Edition 6.17 Silberschatz, Galvin and Gagne ©2009
Readers-Writers Problem
Problem parameters:
• One set of data is shared among a number of processes
• Once a writer is ready, it performs its write. Only one writer
may write at a time
• If a process is writing, no other process can read it
• If at least one reader is reading, no other process can write
• Readers may not write and only read
Operating System Concepts – 8th Edition 6.18 Silberschatz, Galvin and Gagne ©2009
Readers-Writers Problem (Cont.)
The structure of a writer process
do {
wait (wrt) ;
// writing is performed
signal (wrt) ;
} while (TRUE);
Operating System Concepts – 8th Edition 6.19 Silberschatz, Galvin and Gagne ©2009
Dining-Philosophers Problem
Consider there are five philosophers sitting around a circular dining
table. The dining table has five chopsticks and a bowl of rice in the
middle as shown in the below figure.
Operating System Concepts – 8th Edition 6.21 Silberschatz, Galvin and Gagne ©2009
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);
Operating System Concepts – 8th Edition 6.22 Silberschatz, Galvin and Gagne ©2009
Dining-Philosophers Problem
The possible solutions for this are:
A philosopher must be allowed to pick up the chopsticks
only if both the left and right chopsticks are available.
Allow only four philosophers to sit at the table. That
way, if all the four philosophers pick up four chopsticks,
there will be one chopstick left on the table. So, one
philosopher can start eating and eventually, two
chopsticks will be available. In this way, deadlocks can
be avoided.
Operating System Concepts – 8th Edition 6.23 Silberschatz, Galvin and Gagne ©2009
The Cigarette-Smokers Problem
Consider a system with three smoker processes and one
agent process. Each smoker continuously rolls a cigarette
and then smokes it. But to roll and smoke a cigarette the
smoker needs three ingredients: tobacco, paper and
matches. One of the smoker processes has paper another
has tobacco and the third has matches. The agent has an
infinite supply of all three materials. The agent places two of
the ingredients on the table. The smoker who has the
remaining the ingredient then makes and smokes a cigarette,
signaling the agent on completion. The agent then puts out
another two of the three ingredients, and the cycle repeats.
Write a program to synchronize the agent and the smokers
using Java synchronization.
Operating System Concepts – 8th Edition 6.24 Silberschatz, Galvin and Gagne ©2009
The Sleeping-Barber Problem
A barbershop consists of a waiting room with n chairs and a
barber room with one 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
occupied, then the customer leaves the shop. If the barber
is busy but chairs are available, then the customer sits in
one of the free chairs. If the barber is asleep, the customer
wakes up the barber. Write a program to coordinate the
barber and the customers
Operating System Concepts – 8th Edition 6.25 Silberschatz, Galvin and Gagne ©2009
Windows XP Synchronization
Uses interrupt masks to protect access to global resources on uniprocessor systems
Also provides dispatcher objects user-land which may act mutexes, semaphores,
events, and timers
Events
An event acts much like a condition variable
Timers notify one or more thread when time expired
Dispatcher objects either signaled-state (object available) or non-signaled state
(thread will block)
Operating System Concepts – 8th Edition 6.26 Silberschatz, Galvin and Gagne ©2009
Linux Synchronization
Linux:
Prior to kernel Version 2.6, disables interrupts to implement short
critical sections
Version 2.6 and later, fully preemptive
Linux provides:
semaphores
spinlocks
reader-writer versions of both
Operating System Concepts – 8th Edition 6.27 Silberschatz, Galvin and Gagne ©2009
End of Chapter
6
Operating System Concepts – 8th Edition Silberschatz, Galvin and Gagne ©2009