Professional Documents
Culture Documents
Synchronization
Operating System Concepts – 8th Edition,! Silberschatz, Galvin and Gagne ©2009!
Module 6: Process Synchronization
■ Background"
■ The Critical-Section Problem"
■ Peterson’s Solution"
■ Synchronization Hardware"
■ Semaphores"
■ Classic Problems of Synchronization"
■ Synchronization Examples "
"
"
"
Operating System Concepts – 8th Edition! 6.2! Silberschatz, Galvin and Gagne ©2009!
Objectives
Operating System Concepts – 8th Edition! 6.3! Silberschatz, Galvin and Gagne ©2009!
Background
Operating System Concepts – 8th Edition! 6.4! Silberschatz, Galvin and Gagne ©2009!
Background -2-
■ Disjoint threads use disjoint sets of variables, and do not use shared
resources. "
● The progress of one thread is independent of all other threads, to which
it is disjoint. "
Operating System Concepts – 8th Edition! 6.5! Silberschatz, Galvin and Gagne ©2009!
Example -1-
Suppose that we wanted to provide a solution to the consumer-
producer problem that fills all the buffers. We can do so by
having an integer count that keeps track of the number of full
buffers. Initially, count is set to 0. It is incremented by the
producer after it produces a new buffer and is decremented by
the consumer after it consumes a buffer.
Operating System Concepts – 8th Edition! 6.6! Silberschatz, Galvin and Gagne ©2009!
PROCESS SYNCHRONIZATION
■ The Producer Consumer Problem: A producer process "produces"
information "consumed" by a consumer process."
■ Here are the variables needed to define the problem:"
Operating System Concepts – 8th Edition! 6.7! Silberschatz, Galvin and Gagne ©2009!
Producer / Consumer
Producer:!
item nextProduced;"
while (true) {"
/* produce an item and put in nextProduced */"
" while (count == BUFFER_SIZE)"
" " "; // do nothing"
" " buffer [in] = nextProduced;"
" " in = (in + 1) % BUFFER_SIZE;"
! ! count++;!
}"
"
Consumer:
item nextConsumed;
while (true) {"
" while (count == 0)"
" " ; // do nothing"
" " nextConsumed = buffer[out];"
" " out = (out + 1) % BUFFER_SIZE;"
! count--;!
" " "/* consume the item in nextConsumed"
"}" Concepts – 8th Edition!
Operating System 6.8! Silberschatz, Galvin and Gagne ©2009!
Count=0
out in
Producer executes
buffer [in] = nextProduced; à "
in = (in + 1) % BUFFER_SIZE; à in= (0+1) % 10= 1"
Count++ à count=1
out in
Operating System Concepts – 8th Edition! 6.9! Silberschatz, Galvin and Gagne ©2009!
Suppose that my buffer has 4 itemsà count=4
out
in
Consumer executes
nextConsumed = buffer[out];
out = (out + 1) % BUFFER_SIZEà out= (0+1) % 10= 1
count-- à count=3
out in
Operating System Concepts – 8th Edition! 6.10! Silberschatz, Galvin and Gagne ©2009!
Race Condition
■ count++ could be implemented as
register1 = count
register1 = register1 + 1
count = register1"
■ Obviously, we would like to have count++ execute, followed by count-- (or vice
versa)"
" Silberschatz, Galvin and Gagne ©2009!
Operating System Concepts – 8th Edition! 6.11!
"
Race Condition
Time! Count=5"
S0! register1 = count à 5"
S1! register1 = register1 + 1 à 6"
S2! register2 = count à 5"
S3! register2 = register2 – 1 à4"
S4! count = register1 à 6"
S4! count = register2 à4"
Operating System Concepts – 8th Edition! 6.12! Silberschatz, Galvin and Gagne ©2009!
Example -2-
Operating System Concepts – 8th Edition! 6.13! Silberschatz, Galvin and Gagne ©2009!
Race Conditions
Operating System Concepts – 8th Edition! 6.14! Silberschatz, Galvin and Gagne ©2009!
Critical Sections
Operating System Concepts – 8th Edition! 6.15! Silberschatz, Galvin and Gagne ©2009!
Critical Sections -2-
■ A Critical Section Environment contains:"
● Entry Section Code requesting entry into the critical section."
● Critical Section Code in which only one process can execute at any
one time."
● Exit Section The end of the critical section, releasing or allowing others
in."
● Remainder Section Rest of the code AFTER the critical section."
do {"
" " Entry
"" Section;
" " "critical section;"
"
Exit Section;
"
" " "reminder sections;"
"} while (true); "
Operating System Concepts – 8th Edition! 6.16! Silberschatz, Galvin and Gagne ©2009!
Solution to Critical-Section Problem
A solution to the critical-section problem must satisfy the following
requirements:!
1. !Mutual Exclusion - If process Pi is executing in its critical section, then no
other processes can be executing in their critical sections"
2. "Progress - If no process is executing in its critical section and there exist
some processes that wish to enter their critical section, then the selection
of the processes that will enter the critical section next cannot be
postponed indefinitely"
3. "Bounded Waiting - A bound, or limit must exist 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"
Assume that each process executes at a nonzero speed "
No assumption concerning relative speed of the N processes"
Operating System Concepts – 8th Edition! 6.17! Silberschatz, Galvin and Gagne ©2009!
Critical section problem
Operating System Concepts – 8th Edition! 6.18! Silberschatz, Galvin and Gagne ©2009!
Peterson’s Solution
q P1"
Sonali C. 20
Operating System Concepts – 8th Edition! 6.20! Silberschatz, Galvin and Gagne ©2009!
2 process -Algorithm 2
■ Shared variables"
boolean flag[2];
● // “interest” bits
initially flag [0] = flag [1] = false.!
● flag [i] = true ⇒ Pi declares interest in entering its
critical section"
■ Process Pi // where the “other” process is Pj ""
do {
flag[i] = true; // declare your own interest
while (flag[ j]) ; //wait if the other guy is interested
critical section
flag [i] = false; // declare that you lost interest
remainder section // allows other guy to enter
} while (1);
Sonali C. Process 22
Operating System Concepts – 8th Edition! Synchronization
6.22! Silberschatz, Galvin and Gagne ©2009!
TWO-PROCESS -Algorithm 3
■ Combined shared variables & approaches of algorithms
1 and 2."
For Process Pi
do {
flag [i] = true; // declare your interest to enter
turn = j; // assume it is the other’s turn-give PJ a chance
while (flag [ j ] and turn == j) ;
critical section
flag [i] = false;
remainder section
} while (1);
Sonali C. Process 23
Operating System Concepts – 8th Edition! Synchronization
6.23! Silberschatz, Galvin and Gagne ©2009!
TWO-PROCESS-Algorithm 3
■ Meets all three requirements; solves the critical-
section problem for two processes.!
● Turn variable breaks any deadlock possibility of
previous example,
AND prevents “hogging” – Pi setting turn to j gives PJ
a chance after each pass of Pi’s CS"
● Flag[ ] variable prevents getting locked out if other
guy never re-enters or crashes outside and allows CS
consecutive access other not interested in entering."
Operating System Concepts – 8th Edition! 6.25! Silberschatz, Galvin and Gagne ©2009!
Semaphore
"
Operating System Concepts – 8th Edition! 6.27! Silberschatz, Galvin and Gagne ©2009!
Semaphores Usage
Usage1:"
■ Binary semaphores can solve the critical-section problem for n
processes. The n processes share a semaphore, mutex, initialized to 1.
Each process Pi is organized as: "
Semaphore mutex; // initialized to 1"
do {"
"wait (mutex);"
// Critical Section"
signal (mutex);"
" "// remainder section"
} while (TRUE);"
Usage2:"
■ Counting semaphores control access to a given resource of many
instances."
■ Simply initialize the semaphore to K (# of available resources)."
■ This allows K processes to enter their critical-sections and use that
resource at a time."
● Each process wants to use a resource performs wait( ) operation à
decrementing the count of the semaphore."
● Each process releases a resource, it performs signal( ) operation à
incrementing the count of the semaphore."
● When the count =0, all resources are being used. Any process wants to use
a resource will block until count >0."
Operating System Concepts – 8th Edition! 6.29! Silberschatz, Galvin and Gagne ©2009!
Semaphores Usage
Usage3:"
■ To solve synchronization problems."
■ Example:"
● Two concurrent processes: P1 and P2"
● Statement S1 in P1 needs to be performed before statement S3 in P2"
■ Need to make P2 wait until P1 tells “it is OK to proceed”"
● Define a semaphore “synch” à Initialize synch to 0."
Operating System Concepts – 8th Edition! 6.30! Silberschatz, Galvin and Gagne ©2009!
Semaphore Implementation
Operating System Concepts – 8th Edition! 6.31! Silberschatz, Galvin and Gagne ©2009!
Semaphore Implementation with no Busy waiting
v Implementation of wait:"
wait(semaphore *S) { "
" " "S->value--; "
" " "if (S->value < 0) { "
" " " "add this process to S->list; "
" " " "block(); "
" " "} "
" "}"
v Implementation of signal:"
"
" "signal(semaphore *S) { "
" " "S->value++; "
" " "if (S->value <= 0) { "
" " " "remove a process P from S->list; "
" " " "wakeup(P); "
" " "}"
" "} "
Operating System Concepts – 8th Edition! 6.33! Silberschatz, Galvin and Gagne ©2009!
struct semaphore {
int count;
queueType queue;
} ■ A queue is used to
void wait(semaphore s)
{ hold processes
s.count--; waiting on a
if (s.count < 0)
{ semaphore."
place this process in s.queue;
block this process
}
}
void signal(semaphore s)
{
s.count++;
if (s.count <= 0)
{
remove a process P from
s.queue;
place process P on ready list;
Sonali
} C. Process 34
} System Concepts – 8 Edition!
Operating th Synchronization
6.34! Silberschatz, Galvin and Gagne ©2009!
Semaphore
Operating System Concepts – 8th Edition! 6.35! Silberschatz, Galvin and Gagne ©2009!
Deadlock
■ Deadlock – two or more processes are waiting indefinitely for an event that can
be caused by only one of the waiting processes"
■ Let S and Q be two semaphores initialized to 1"
! ! P0 " P1"
" " wait (S); " wait (Q);"
" " wait (Q); " wait (S);"
" ". " "."
" ". " "."
" ". " "."
" " signal (S); " signal (Q);"
" " signal (Q); " signal (S);"
● Suppose that P0 executes wait(S) and P1 then executes wait(Q). "
● When P0 executes wait(Q), it must wait until P1 executes signal(Q) . "
● Similarly, when P1 executes wait(S), it must wait until P0 executes signal
(S) . "
● Since these signal () operations cannot be executed, P0 and P1 are
deadlocked. "
"
Operating System Concepts – 8th Edition! 6.36! Silberschatz, Galvin and Gagne ©2009!
"
Starvation
Operating System Concepts – 8th Edition! 6.37! Silberschatz, Galvin and Gagne ©2009!
Classical Problems of Synchronization
v Bounded-Buffer Problem"
v Bounded buffers P/C can be seen in e.g. streaming filters or packet
switching in networks."
v Dining-Philosophers Problem"
v Dining philosophers could be a sequence of active database transactions
that have a circular wait-for-lock dependence. "
Operating System Concepts – 8th Edition! 6.38! Silberschatz, Galvin and Gagne ©2009!
Bounded-Buffer Problem
Operating System Concepts – 8th Edition! 6.39! Silberschatz, Galvin and Gagne ©2009!
Bounded Buffer Problem (Cont.)
The structure of the producer process"
"
"do {
"
// produce an item in nextp"
"
wait (empty); " " /* decrement empty count */"
wait (mutex); " " /* enter critical region */"
"
// add the item to the buffer"
"
signal (mutex); /* leave critical region */"
signal (full); " /* increment count of full slots */"
} while (TRUE);"
Operating System Concepts – 8th Edition! 6.40! Silberschatz, Galvin and Gagne ©2009!
Bounded Buffer Problem (Cont.)
The structure of the consumer process"
"
do {"
wait (full); " " /* decrement full count */"
wait (mutex); " " /* enter critical region */"
"
// remove an item from buffer to nextc"
"
signal (mutex); " " /* leave critical region */"
signal (empty); " " /* increment count of empty slots */"
"
// consume the item in nextc"
"
} while (TRUE);"
Operating System Concepts – 8th Edition! 6.41! Silberschatz, Galvin and Gagne ©2009!
Classical Problem 2:
The Readers-Writers Problem
X X X
writer reader writer reader
reader
writer reader
reader
reader
writer
reader reader
v Shared Data"
v Data set"
v Semaphore mutex initialized to 1"
v Semaphore wrt initialized to 1"
v Integer readcount initialized to 0"
Operating System Concepts – 8th Edition! 6.43! Silberschatz, Galvin and Gagne ©2009!
Readers-Writers Problem (Cont.)
BINARY_SEMAPHORE wrt = 1;! The structure of a reader process!
BINARY_SEMAPHORE mutex = 1;! "
int readcount = 0;" "do {"
The structure of a writer process! wait (mutex) ; /* Allow 1 reader in entry*/"
readcount ++ ;"
" if (readcount == 1) "
do {" " wait (wrt) ; /* 1st reader locks writer */"
wait (wrt) ;" signal (mutex)"
"
"
// reading is performed"
// writing is performed"
"
" wait (mutex) ;"
readcount - - ;"
signal (wrt) ;"
if (readcount == 0) "
} while (TRUE);" " signal (wrt) ; /*last reader frees writer */"
" signal (mutex) ;"
} while (TRUE);"
"
"
Operating System Concepts – 8th Edition! 6.44! Silberschatz, Galvin and Gagne ©2009!
Dining-Philosophers Problem
Operating System Concepts – 8th Edition! 6.45! Silberschatz, Galvin and Gagne ©2009!
The Structure of Philosopher i
n Philosopher i
while ( true ) {
// get left chopstick
wait(chopStick[i]);
Operating System Concepts – 8th Edition! 6.47! 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.48! Silberschatz, Galvin and Gagne ©2009!
The Sleeping Barber Problem
The following pseudo-code guarantees synchronization
between barber and customer and is deadlock free,
but may lead to starvation of a customer"
"
Semaphore Customers = 0 "
Semaphore Barber = 0 "
Semaphore accessSeats (mutex) = 1"
int NumberOfFreeSeats = N //total number of seats "
Operating System Concepts – 8th Edition! 6.49! Silberschatz, Galvin and Gagne ©2009!
The Sleeping Barber Problem
The Barber (Thread/Process): !
while(true) { //runs in an infinite loop "
wait(Customers) //tries to acquire a customer - if none is available he goes to
sleep"
wait(accessSeats) //at this time he has been awakened - want to modify the
number of available seats"
Operating System Concepts – 8th Edition! 6.50! Silberschatz, Galvin and Gagne ©2009!
The Sleeping Barber Problem
Operating System Concepts – 8th Edition! 6.51! Silberschatz, Galvin and Gagne ©2009!
End of Chapter 6
Operating System Concepts – 8th Edition,! Silberschatz, Galvin and Gagne ©2009!
Synchronization Examples
v Windows XP"
v Linux"
Operating System Concepts – 8th Edition! 6.53! Silberschatz, Galvin and Gagne ©2009!
Windows XP Synchronization
v Uses interrupt masks to protect access to global resources on uniprocessor
systems."
v Uses spinlocks on multiprocessor systems"
v Also provides dispatcher objects which may act as either mutexes and
semaphores"
v Dispatcher objects may also provide events"
v An event acts much like a condition variable(they may notify a waiting
thread when a desired condition occurs)."
"
"
owner thread releases"
Nonsignaled
signaled
mutex lock"
"
"
thread acquires mutex lock "
" Silberschatz, Galvin and Gagne ©2009!
Operating System Concepts – 8th Edition! 6.54!
Linux Synchronization
v Linux:"
v Prior to kernel Version 2.6, a nonpreemptive kernel."
v Version 2.6 and later, fully preemptive"
v Linux provides:"
v semaphores"
v spin locks"
Operating System Concepts – 8th Edition! 6.55! Silberschatz, Galvin and Gagne ©2009!