CSC351-Operating System
Week-7 Lecture-14
Semester 5
1
Preamble (Past
lesson brief)
►Algorithm Evaluation
►Deterministic Modelling
►Queuing Modelling
►Simulation
2
Lahore Garrison University
Chapter 5
 Process Synchronization
3
Lahore Garrison University
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.
 They may share
 Variable
 Memory (buffer)
 Code
 Resources (CPU, Printer, Scanners)
Process synchronization problem arises in the case of Cooperative process also
because resources are shared in Cooperative processes.
4
Lahore Garrison University
Process Synchronization
 Example: for sharing a variable
Int shared = 5;
5
P1 P2
Int x= shared; Int y= shared;
x++; y--;
sleep(1); sleep(1);
shared = x; shared = y;
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.
 Several processes access and process the manipulations over the same
data concurrently, then the outcome depends on the particular order in
which the access takes place.
Lahore Garrison University
6
Critical Section
 It is part of program where shared resources are accessed by
various processes
Lahore Garrison University
7
Synchronization Mechanism
Lahore Garrison University
8
Mutual Exclusion
 P2 also wants to enter the same time
then P2 will not be allowed to enter
while P1 is in CS. Its like a lock.
Lahore Garrison University
9
Shared Data
Critical Section
P1
Progress
 P1 is not interested to enter in the critical section but its also
stopping P2 to enter in critical section
 It can be done by mistake or intentionally
 P1 is not progressing nor letting P2 to progress
 So no process is in critical section no progress
 P1 must progress and also let P2
Lahore Garrison University
10
Critical Section
Bounded Waiting
 P1 P2
 1 0
 2 0
 3 0
 . 0
 .
 .
 .
 Infinite
 Then it’s a kind of starvation. there must not be unbounded waiting.
Lahore Garrison University
11
P1
Critical Section
No assumption related to H/W
speed
 Solution should not be hardware dependent
 It must be portable
Lahore Garrison University
12
Peterson’s Solution
Peterson’s Solution is a classical software based solution to the critical section
problem.
 In Peterson’s solution, we have shared variable:
 int turn : The process whose turn is to enter the critical section.
Lahore Garrison University
13
 Two process solution (Software based solution)
 Shared variables:
 int turn;
initially turn = 0
 turn = i  Pi can enter its critical section
Solution : Algorithm 1
 Problem ? , Does it meet all conditions ?
Solution : Algorithm 1
Process P0
do {
while (turn!=0) ;
<CS>
turn = 1;
<RS>
} while (1);
Process P1
do {
while (turn!=1) ;
<CS>
turn = 0;
<RS>
} while (1);
Two process solution (Software based solution)
Shared variables
 boolean flag[2]; // Set to false
 flag [i] = true  Pi ready to enter its critical section
Solution : Algorithm 2
 Problem ? , Does it meet all conditions ?
 Does not satisfy the progress condition, if both set flag at same
time
Solution : Algorithm 2
Process P0
do {
flag[0] = true;
while (flag[1]) ;
<CS>
flag [0] = false;
<RS>
}
Process P1
do {
flag[1] = true;
while (flag[0]) ;
<CS>
flag [1] = false;
<RS>
}
Algorithm 3: Peterson’s Solution
 Two process solution (Software based solution)
 The two processes share two variables:
 int turn;
 Boolean flag[2]
 The variable turn indicates whose turn it is to enter the critical
section.
 The flag array is used to indicate if a process is ready to enter the
critical section. flag[i] = true implies that process Pi is ready
Peterson’s Solution
 The Peterson’s solution for process Pi.
 If both processes try to
enter at the same
time …
 turn will be set to
both i and j at roughly
the same time.
 The eventual value of
turn decides which
can go.
do {
flag[i] = TRUE;
turn = j;
while ( flag[j] && turn == j);
CRITICAL SECTION
flag[i] = FALSE;
REMAINDER SECTION
} while (TRUE);
If other process (Pj) wishes
to enter the critical section,
it can do so.
Algorithm 3: Peterson’s
Solution
Process P0
do {
flag[0] = true;
turn = 1;
while (flag[1] && turn == 1);
<CS>
flag [0] = false;
<RS>
} while (1);
Process P1
do {
flag[1] = true;
turn = 0;
while (flag[0] && turn ==0);
<CS>
flag [1] = false;
<RS>
} while (1);
Algorithm 3: Peterson’s Solution
 Meets all three requirements:
1. Mutual exclusion is preserved.
 Pi enters its critical section only if either flag[j]==false or turn==i.
 If both processes want to enter their critical sections at the same time, then
flag[i] == flag[j] == true.
 However, the value of turn can be either 0 or 1 but cannot be both.
 Hence, one of the processes must have successfully executed the while
statement (to enter its critical section), and the other process has to wait, till the
process leaves its critical section.  mutual exclusion is preserved.
Algorithm 3: Peterson’s
Solution
2. The progress requirement is satisfied.
 Case 1:
 Pi is ready to enter its critical section.
 If Pj is not ready to enter the critical section (it is in the remainder
section).
 Then flag[j] == false, and Pi can enter its critical section.
 Case 2:
 Pi and Pj are both ready to enter its critical section.
 flag[i] == flag[j] == true.
 Either turn == i or turn == j.
 If turn == i, then Pi will enter the critical section.
 If turn == j, then Pj will enter the critical section.
Algorithm 3: Peterson’s
Solution
3. The bounded-waiting requirement is met.
 Once Pj exits its critical section, it will reset flag[j] to
false, allowing Pi to enter its critical section.
 Even if Pj immediately resets flag[j] to true, it
must also set turn to i.
 Then, Pi will enter the critical section after at most one
entry by Pj.
Reference
 To cover this topics , different reference material has been used for
consultation.
 Operating systems concept by Abraham silberchatz edition 9
 Tutorialspoint.com
 Google.com
24
Lahore Garrison University

14- Process Synchronization.pptx

  • 1.
  • 2.
    Preamble (Past lesson brief) ►AlgorithmEvaluation ►Deterministic Modelling ►Queuing Modelling ►Simulation 2 Lahore Garrison University
  • 3.
    Chapter 5  ProcessSynchronization 3 Lahore Garrison University
  • 4.
    Process Synchronization  Onthe 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.  They may share  Variable  Memory (buffer)  Code  Resources (CPU, Printer, Scanners) Process synchronization problem arises in the case of Cooperative process also because resources are shared in Cooperative processes. 4 Lahore Garrison University
  • 5.
    Process Synchronization  Example:for sharing a variable Int shared = 5; 5 P1 P2 Int x= shared; Int y= shared; x++; y--; sleep(1); sleep(1); shared = x; shared = y;
  • 6.
    Race Condition  Whenmore 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.  Several processes access and process the manipulations over the same data concurrently, then the outcome depends on the particular order in which the access takes place. Lahore Garrison University 6
  • 7.
    Critical Section  Itis part of program where shared resources are accessed by various processes Lahore Garrison University 7
  • 8.
  • 9.
    Mutual Exclusion  P2also wants to enter the same time then P2 will not be allowed to enter while P1 is in CS. Its like a lock. Lahore Garrison University 9 Shared Data Critical Section P1
  • 10.
    Progress  P1 isnot interested to enter in the critical section but its also stopping P2 to enter in critical section  It can be done by mistake or intentionally  P1 is not progressing nor letting P2 to progress  So no process is in critical section no progress  P1 must progress and also let P2 Lahore Garrison University 10 Critical Section
  • 11.
    Bounded Waiting  P1P2  1 0  2 0  3 0  . 0  .  .  .  Infinite  Then it’s a kind of starvation. there must not be unbounded waiting. Lahore Garrison University 11 P1 Critical Section
  • 12.
    No assumption relatedto H/W speed  Solution should not be hardware dependent  It must be portable Lahore Garrison University 12
  • 13.
    Peterson’s Solution Peterson’s Solutionis a classical software based solution to the critical section problem.  In Peterson’s solution, we have shared variable:  int turn : The process whose turn is to enter the critical section. Lahore Garrison University 13
  • 14.
     Two processsolution (Software based solution)  Shared variables:  int turn; initially turn = 0  turn = i  Pi can enter its critical section Solution : Algorithm 1
  • 15.
     Problem ?, Does it meet all conditions ? Solution : Algorithm 1 Process P0 do { while (turn!=0) ; <CS> turn = 1; <RS> } while (1); Process P1 do { while (turn!=1) ; <CS> turn = 0; <RS> } while (1);
  • 16.
    Two process solution(Software based solution) Shared variables  boolean flag[2]; // Set to false  flag [i] = true  Pi ready to enter its critical section Solution : Algorithm 2
  • 17.
     Problem ?, Does it meet all conditions ?  Does not satisfy the progress condition, if both set flag at same time Solution : Algorithm 2 Process P0 do { flag[0] = true; while (flag[1]) ; <CS> flag [0] = false; <RS> } Process P1 do { flag[1] = true; while (flag[0]) ; <CS> flag [1] = false; <RS> }
  • 18.
    Algorithm 3: Peterson’sSolution  Two process solution (Software based solution)  The two processes share two variables:  int turn;  Boolean flag[2]  The variable turn indicates whose turn it is to enter the critical section.  The flag array is used to indicate if a process is ready to enter the critical section. flag[i] = true implies that process Pi is ready
  • 19.
    Peterson’s Solution  ThePeterson’s solution for process Pi.  If both processes try to enter at the same time …  turn will be set to both i and j at roughly the same time.  The eventual value of turn decides which can go. do { flag[i] = TRUE; turn = j; while ( flag[j] && turn == j); CRITICAL SECTION flag[i] = FALSE; REMAINDER SECTION } while (TRUE); If other process (Pj) wishes to enter the critical section, it can do so.
  • 20.
    Algorithm 3: Peterson’s Solution ProcessP0 do { flag[0] = true; turn = 1; while (flag[1] && turn == 1); <CS> flag [0] = false; <RS> } while (1); Process P1 do { flag[1] = true; turn = 0; while (flag[0] && turn ==0); <CS> flag [1] = false; <RS> } while (1);
  • 21.
    Algorithm 3: Peterson’sSolution  Meets all three requirements: 1. Mutual exclusion is preserved.  Pi enters its critical section only if either flag[j]==false or turn==i.  If both processes want to enter their critical sections at the same time, then flag[i] == flag[j] == true.  However, the value of turn can be either 0 or 1 but cannot be both.  Hence, one of the processes must have successfully executed the while statement (to enter its critical section), and the other process has to wait, till the process leaves its critical section.  mutual exclusion is preserved.
  • 22.
    Algorithm 3: Peterson’s Solution 2.The progress requirement is satisfied.  Case 1:  Pi is ready to enter its critical section.  If Pj is not ready to enter the critical section (it is in the remainder section).  Then flag[j] == false, and Pi can enter its critical section.  Case 2:  Pi and Pj are both ready to enter its critical section.  flag[i] == flag[j] == true.  Either turn == i or turn == j.  If turn == i, then Pi will enter the critical section.  If turn == j, then Pj will enter the critical section.
  • 23.
    Algorithm 3: Peterson’s Solution 3.The bounded-waiting requirement is met.  Once Pj exits its critical section, it will reset flag[j] to false, allowing Pi to enter its critical section.  Even if Pj immediately resets flag[j] to true, it must also set turn to i.  Then, Pi will enter the critical section after at most one entry by Pj.
  • 24.
    Reference  To coverthis topics , different reference material has been used for consultation.  Operating systems concept by Abraham silberchatz edition 9  Tutorialspoint.com  Google.com 24 Lahore Garrison University