You are on page 1of 28

Process Synchronization

Operating System Concepts – 8th Edition Silberschatz, Galvin and Gagne ©2009
Process Synchronization
 On the basis of synchronization, processes are categorized as one of the
following two types:
 Independent Process : Execution of one process does not affects the
execution of other processes.
 Cooperative Process : Execution of one process affects the execution of
other processes.
Process synchronization problem arises in the case of Cooperative process
also because resources are shared in Cooperative processes.

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

When more than one processes are executing the same code or accessing the
same memory or any shared variable in that condition there is a possibility that
the output or the value of the shared variable is wrong so for that all the
processes doing race to say that my output is correct this condition known as
race condition.

Operating System Concepts – 8th Edition 6.3 Silberschatz, Galvin and Gagne ©2009
B is a shared variable with initial value 2. You can execute
P1 and P2 in any way. How many different values of B can
have?

P1() P2
{ {
C=B-1; D=2*B;
B=2*C; B=D-1;
} }

Operating System Concepts – 8th Edition 6.4 Silberschatz, Galvin and Gagne ©2009
Critical Section Problem

Critical Section is a code segment that accesses shared


variables and has to be executed as an atomic action. It
means that in a group of cooperating processes, at a given
point of time, only one process must be executing its critical
section. If any other process also wants to execute its critical
section, it must wait until the first one finishes.

Operating System Concepts – 8th Edition 6.5 Silberschatz, Galvin and Gagne ©2009
Operating System Concepts – 8th Edition 6.6 Silberschatz, Galvin and Gagne ©2009
Solution to Critical-Section Problem
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 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.7 Silberschatz, Galvin and Gagne ©2009
 P0  P1

while(1) while(1)
{ {
while(turn!=0); while(turn!=1);
critical section critical section
turn=1; turn=0;
remainder section remainder section
} }

Operating System Concepts – 8th Edition 6.8 Silberschatz, Galvin and Gagne ©2009
 Fulfils mutual exclusion
 Do not fulfil progress

Next approach can be fulfilling progress

Operating System Concepts – 8th Edition 6.9 Silberschatz, Galvin and Gagne ©2009
USING FLAG VARIABLE

P0 P1
while(1) while(1)
{ {
flag[0]=T; flag[1]=T;
while(flag[1]); while(flag[0]);
critical section critical section
flag[0]=F flag[1]=F
} }

Operating System Concepts – 8th Edition 6.10 Silberschatz, Galvin and Gagne ©2009
 Fulfils ME and progress(in some instance)
 Fails to maintain progress at every step

Operating System Concepts – 8th Edition 6.11 Silberschatz, Galvin and Gagne ©2009
Peterson’s Solution
 P0  P1

while(1) while(1)
{ {
flag[0]=T flag[1]=T
turn=1 turn=0
while(turn==1&&flag[1]=T); while(turn==0&&flag[0]=T);
Critical section Critical section
Flag[0]=F; Flag[1]=F;
} }

Operating System Concepts – 8th Edition 6.12 Silberschatz, Galvin and Gagne ©2009
Semaphores
 Peterson solution have the drawback of providing solution to only 2
processes to overcome this Semaphores are used that provide solution to n
processes.
 A semaphore is an integer variable
 The initialization value of semaphore decides that which problem are we
solving with semaphore.
 Eg. For critical section (S=1) where S is a semaphore variable.

Operating System Concepts – 8th Edition 6.13 Silberschatz, Galvin and Gagne ©2009
Major Applications of Semaphores
 Critical section problem
 To decide order of execution of processes
 Resource management

Operating System Concepts – 8th Edition 6.14 Silberschatz, Galvin and Gagne ©2009
 In critical section after initialization of semaphore variable we cannot directly
access it. I t can be accessed using two atomic operations:
 Wait(S)
 Signal(S)
 wait (S)
{
while S <= 0
; // no-op
S--;
}

 signal (S)
{
S++;
}
Operating System Concepts – 8th Edition 6.15 Silberschatz, Galvin and Gagne ©2009
Solving Critical Section
do
{
Wait(S);
Critical section
Signal(S);
//Remainder section
}while(T)

Operating System Concepts – 8th Edition 6.16 Silberschatz, Galvin and Gagne ©2009
Operating System Concepts – 8th Edition 6.17 Silberschatz, Galvin and Gagne ©2009
Operating System Concepts – 8th Edition 6.18 Silberschatz, Galvin and Gagne ©2009
Types of Semaphores
There are two main types of semaphores i.e. counting semaphores and binary
semaphores. Details about these are given as follows:

Counting Semaphores
These are integer value semaphores and have an unrestricted value domain. These
semaphores are used to coordinate the resource access, where the semaphore
count is the number of available resources. If the resources are added, semaphore
count automatically incremented and if the resources are removed, the count is
decremented.

Binary Semaphores
The binary semaphores are like counting semaphores but their value is restricted to
0 and 1. The wait operation only works when the semaphore is 1 and the signal
operation succeeds when semaphore is 0. It is sometimes easier to implement
binary semaphores than counting semaphores.

Operating System Concepts – 8th Edition 6.19 Silberschatz, Galvin and Gagne ©2009
Semaphore as
General Synchronization Tool
 Counting semaphore – integer value can range over an unrestricted domain
 Binary semaphore – integer value can range only between 0
and 1; can be simpler to implement
 Also known as mutex locks
 Can implement a counting semaphore S as a binary semaphore
 Provides mutual exclusion

Semaphore mutex; // initialized to 1


do {
wait (mutex);
// Critical Section
signal (mutex);
// remainder section
} while (TRUE);

Operating System Concepts – 8th Edition 6.20 Silberschatz, Galvin and Gagne ©2009
Semaphore Implementation
 Must guarantee that no two processes can execute wait () and
signal () on the same semaphore at the same time
 Thus, implementation becomes the critical section problem
where the wait and signal code are placed in the critical section.
 Could now have busy waiting in critical section
implementation
 But implementation code is short
 Little busy waiting if critical section rarely occupied
 Note that applications may spend lots of time in critical sections
and therefore this is not a good solution.

Operating System Concepts – 8th Edition 6.21 Silberschatz, Galvin and Gagne ©2009
Semaphore Implementation
with no Busy waiting

 With each semaphore there is an associated waiting queue.


Each entry in a waiting queue has two data items:
 value (of type integer)
 pointer to next record in the list
 Two operations:
 block – place the process invoking the operation on the
appropriate waiting queue.
 wakeup – remove one of processes in the waiting queue
and place it in the ready queue.

Operating System Concepts – 8th Edition 6.22 Silberschatz, Galvin and Gagne ©2009
Semaphore Implementation with
no Busy waiting (Cont.)
 Implementation of wait:
wait(semaphore *S) {
S->value--;
if (S->value < 0) {
add this process to S->list;
block();
}
}
 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.23 Silberschatz, Galvin and Gagne ©2009
Deadlock and Starvation
 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);
 Starvation – indefinite blocking. A process may never be removed
from the semaphore queue in which it is suspended
 Priority Inversion – Scheduling problem when lower-priority process
holds a lock needed by higher-priority process

Operating System Concepts – 8th Edition 6.24 Silberschatz, Galvin and Gagne ©2009
Classical Problems of Synchronization

 Bounded-Buffer Problem

 Readers and Writers Problem

 Dining-Philosophers Problem

Operating System Concepts – 8th Edition 6.25 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.
 Shared Data
 Data set
 Semaphore mutex initialized to 1 (controls access to
readcount)
 Semaphore wrt initialized to 1 (writer access)
 Integer readcount initialized to 0 (how many processes are
reading object)

Operating System Concepts – 8th Edition 6.26 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.27 Silberschatz, Galvin and Gagne ©2009
Readers-Writers Problem (Cont.)
 The structure of a reader process
do {
wait (mutex) ;
readcount ++ ;
if (readcount == 1)
wait (wrt) ;
signal (mutex)
// reading is performed
wait (mutex) ;
readcount - - ;
if (readcount == 0)
signal (wrt) ;
signal (mutex) ;
} while (TRUE);

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

You might also like