interested[1]

Operating Systems, 371-1-1631
Winter Semester 2011
Practical Session 5, Synchronization
1
Motivation
• Multiprocessing needs some tools for
managing shared resources
– Printers
– Files
– Data Bases
2
Conditions for a good Solution
1. Mutual Exclusion – No two processes are in
the critical section (CS) at the same time
2. Deadlock Freedom – If two or more
processes are trying to enter a CS one will
eventually enter it.
3. Starvation Freedom – A process trying to
enter a CS will eventually manage to enter it.
3
Solution Archetypes
• Busy wait
– Wastes CPU
– Priority inversion:
Task L (low priority) runs and gains exclusive use
of resource R. H (high priority) task is introduced
and attempts to acquire R – blocked. M (medium
priority) becomes runnable before L releases R.
Result: L can’t run and release R, H is waiting for R
(and L) as long as M keeps running.
• Sleep & Wake up
4
Peterson’s Solution
N=2; interested[0]=interested[1]=FALSE;
int turn;
/* Should be named NOT_MY_TURN
int interested[N];
/* all initially 0 (FALSE) */
*/
void enter_region(int process){ /* who is entering 0 or 1 ? */
int other = 1- process; /* opposite of process */
interested[process] = TRUE; /* signal that you're interested */
turn = process;
/* set flag – NOT my turn */
while (turn == process &&
interested[other] == TRUE); /* null statement */
}
void leave_region(int process){
interested[process] = FALSE;
}5
/* who is leaving 0 or 1 ? */
/* departure from critical region */
Peterson’s Solution
Process = 0
Process = 1
interested[0]=interested[1]=FALSE
int turn;
int interested[2];
interested[0]=interested[1]=FALSE
int turn;
int interested[2];
void enter_region(int process){
int other = 1;
interested[0] = TRUE;
turn = 0;
while (turn == 0 &&
interested[1] == TRUE);
}
void enter_region(int process){
int other = 1- process;
interested[1] = TRUE;
turn = 1;
while (turn == 1 &&
interested[0] == TRUE);
}
void leave_region(int process){
interested[0] = FALSE;}
void leave_region(int process){
interested[1] = FALSE;}
6
Question 1
N=1; interested[0]=interested[1]=FALSE;
int turn;
/* Should be named NOT_MY_TURN */
int interested[N];
/* all initially 0 (FALSE) */
void enter_region(int process){
/* who is entering 0 or 1 ? */
int other = 1- process;
/* opposite of process */
turn = process;
/* set flag – NOT my turn */
interested[process] = TRUE; /* signal that you're interested */
while (turn == process &&
interested[other] == TRUE); /* null statement */
}
void leave_region(int process){
/* who is leaving 0 or 1 ? */
interested[process] = FALSE;
/* departure from critical region */
}
What happens when the two red lines are in this
order?
7
Peterson’s Solution
Process = 0
Process = 1
interested[0]=interested[1]=FALSE
int turn;
int interested[2];
void enter_region(int process){
int other = 1;
turn = 0;
interested[0] = TRUE;
while (turn == 0 &&
interested[1] == TRUE);
}
void enter_region(int process){
int other = 1- process;
turn = 1;
interested[1] = TRUE;
while (turn == 1 &&
interested[0] == TRUE);
}
void leave_region(int process){
interested[0] = FALSE;}
void leave_region(int process){
interested[1] = FALSE;}
8
Answer
We will get a mutual exclusion violation:
P1 does turn:=1;
P0 does turn:=0;
P0 does interested[0]:=true;
P0 does while(turn == 0 && interested [1]); // (#t and #f)  #f
P0 enters the CS.
P1 does interested [1]:=true;
P1 does while(turn == 1 && interested [0]); // (#f and #t)  #f
P1 enters the CS.
now both processes are in the critical section.
9
Question 2
Assume the while statement in Peterson's
solution is changed to:
while (turn != process && interested[other] == TRUE)
Describe a scheduling scenario in which we will
receive a Mutual Exclusion and one in which
we will NOT receive a Mutual Exclusion
violation.
10
Question 2
N=2; interested[0]=interested[1]=FALSE;
int turn;
/* Should be named NOT_MY_TURN */
int interested[N];
/* all initially 0 (FALSE) */
void enter_region(int process){ /* who is entering 0 or 1 ? */
int other = 1- process; /* opposite of process */
interested[process] = TRUE; /* signal that you're interested */
turn = process;
/* set flag – NOT my turn */
while (turn != process &&
interested[other] == TRUE) /* null statement */
}
void leave_region(int process){
interested[process] = FALSE;
}
11
/* who is leaving 0 or 1 ? */
/* departure from critical region */
Peterson’s Solution
Process = 0
Process = 1
interested[0]=interested[1]=FALSE
int turn;
int interested[2];
void enter_region(int process){
int other = 1;
interested[0] = TRUE;
turn = 0;
while (turn != 0 &&
interested[1] == TRUE);
}
void enter_region(int process){
int other = 1- process;
interested[1] = TRUE;
turn = 1;
while (turn != 1 &&
interested[0] == TRUE);
}
void leave_region(int process){
interested[0] = FALSE;}
void leave_region(int process){
interested[1] = FALSE;}
12
Answer for Q.2
No Mutex violation:
1. One enters and exits and only afterwards the
second receives a time quanta.
2. P0 – interested[0]=true,
P0 – turn = 0,
P1- interested[1]=true,
P1 – turn =1,
P1 passes while loop and enters CS,
P0 – stuck in while loop.
13
Answer for Q.2
Mutex violation:
Process A executes:
`interested [process] = TRUE', `turn = process‘
and falls through the while loop shown above.
While in the critical section, the timer fires and process B
executes:
`interested [process] = TRUE', `turn = process'
and falls through the while loop shown above.
Both processes are in the critical section.
14
Bakery Algorithm
int value[N]={0, 0,…,0};
int busy[N]={FALSE,…, FALSE};
/* processes get “waiting numbers” */
/* just a flag... */
void enter_region(int i ) /* process i entering.. */
{
busy[i] = TRUE;
/* guard the value selection */
value[i] = max(value[0], value[1], …, value[N-1]) + 1; /* LAST in line … */
busy[i] = FALSE;
for(k=0; k < N; k ++){
while (busy[k] == TRUE) ; /* wait before checking */
while((value[k] != 0) && ((value[k], k) < (value[i], i))); /* wait */
}
}
void leave_region(int i ) {
value[i ] = 0;}
15
Semaphores
Two atomic operations: up & down.
down(S)
if S≤0 block process.
S-up(S)
S++
if there are blocked processes wake one up.
NOTE: Semaphore’s interface doesn’t enforce the
implementation of starvation freedom.
16