You are on page 1of 15

 Several processes access & manipulate the

same data concurrently & the outcome of the


execution depends on the particular order in
which the access takes place.
 Consider a system consisting of n processes
{P0,P1,….Pn}. Each process has a segment of
code called a critical section,
 In which process may be changing common
variables, updating a table, writing files & so
on.
 When one process is executing in its critical
section, no other process is allowed to
execute in its critical section.
 No 2 processes are executing in their critical
section at the same time.
 Do
 {
 entry section
 critical section
 Exit section
 Remainder section
 ) while(true)
 Each process must request permission to
enter its critical section.
 Section of code implementing this request is
entry section.
 Critical section is followed by exit section.
 Remaining code is remainder section.
 Structure of process Pi
 Mutual exclusion
 Progress
 Bounded waiting
1. Mutual Exclusion – If process Pi is executing in its
critical section, then no other processes can be
executing in their critical sections.

A. Frank - P. Weisberg
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 process that will enter the critical section
next cannot be postponed indefinitely:

A. Frank - P. Weisberg
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.

A. Frank - P. Weisberg
 2 process pi & pj
 Alg 1:
 Both share a common variable turn=i or j.
 If turn ==i process Pi allowed to execute in
critical section.
 Do
 {
 while(turn !=i)
 Critical section;
 Turn =j;
 Remainder section;
 }while(1);
 Drawback: does not satisfy progress
requirements(state of process). Only
remembers about process which was allowed
to enter in its critical section.
 Prob 1 solved by replacing variable turn with
boolean = flag[2].
 Element of array initialized to false. If flag[i]
=true, value indicates pi ready to execute in
its critical section.
 Structure of process pi
 Do
 {
 flag[i]=true ;
 While(flag[i])
 Critical section;
 flag[i]=false;
 Remainder section;
 }while(1);
 Combine alg1 and alg2. process shares 2
variables: Boolean flag[2], int turn.
 Initially flag[0] = flag[1]=false & value of
turn is immaterial(0 or 1).
 Do
 {
 flag[i]=true ;
 turn = j;
 While(flag[i] && turn ==j)
 Critical section;
 flag[i]=false;
 Remainder section;
 }while(1);

It is also called Peterson’s solution

You might also like