OS L7 PSync 2017
OS L7 PSync 2017
LECTURE - 7
K.ARIVUSELVAN
Assistant Professor (SG) (SITE)
VIT University
PROCESS SYNCHRONIZATION
Introduction to Cooperating Processes
PROCESS A:
PROCESS B:
A1. LOAD R1, BALANCE // load Balance from memory into Register
1 (R1)
A2. SUB R1, 100 // Subtract 100 from R1
A3. STORE BALANCE, R1 // Store R1s contents back to the
memory location of Balance.
B1. LOAD R1, BALANCE // load Balance from memory into Register
1 (R1)
B2. SUB R1, 200 // Subtract 200 from R1
B3. STORE BALANCE, R1 // Store R1s contents back to the
memory location of Balance.
Observe: In a time-shared or multi-processing system the exact
instruction execution order cannot be predicted!
Race Condition:
do {
entry section
critical section
exit session
remainder section
} while (TRUE);
Solution to Critical-Section Problem
(1) Mutual
Exclusion
(2) Progress
(3) Bounded
Waiting
Solution to Critical-Section Problem
do {
entry section
critical section
exit section
remainder section
} while (1);
Shared variables:
int turn;
initially turn = 0 (or ) 1
Structure of Process Pi :
repeat
while (turn != i) do nothing; /*busy wait*/
critical section
turn = i;
remainder section
Until false;
Algorithm 1
boolean flag[2];
initially flag [0] = flag [1] = false
Structure of Process Pi :
repeat
flag[ i ] := true;
while (flag[ j ]) do nothing;
critical section
flag [j] = false;
remainder section
Until false;
Algorithm 2
Process Pi
do {
flag [i]:= true;
turn = j;
while (flag [j] and turn = j) do nothing;
critical section
flag [i] = false;
remainder section
} while (1);