1 course syllabus 1. introduction - history; views; concepts; structure 2. process management -...
TRANSCRIPT
1
Course Syllabus
1. Introduction - History; Views; Concepts; Structure2. Process Management - Processes; State + Resources;
Threads; Unix implementation of Processes3. Process Scheduling – Paradigms; Unix; Modeling4. Process Synchronization - Synchronization primitives and
their equivalence; Deadlocks5. Memory Management - Virtual memory; Page replacement
algorithms; Segmentation 6. File Systems - Implementation; Directory and space
management; Unix file system; Distributed file systems (NFS)
7. Security – General policies and mechanisms; protection models; authentication
8. Distributed issues – Synchronization; Mutual exclusion
Operating Systems, 2011, Danny Hendler & Amnon Meisels
Operating Systems, 2011, Danny Hendler & Amnon Meisels 2
Mutual exclusion: motivation
Race Conditions:Example: Spooler directory with slots; index
variables; two processes attempt concurrent access. In points to the next empty slot, Out points to entry holding name of file to print next.
. . . . . .abc.doc f.ps paper.ps
Process A
Process B
4 5 6 7
Out = 4 In=7
Operating Systems, 2011, Danny Hendler & Amnon Meisels 3
Code using reads/writes only
Add-file-to-print-spool (char *name)1. Local-in := In2. StoreName(Spool-dir[local-in], name)3. In := (local-in+1) mod DIR-LEN
A scenario proving the above code wrong• Process A performs line 1• Process A is preempted by Process B• Process B performs Add-file-to-print-spool to completion• Process A is re-scheduled, completes lines 2-3.
Process B's file is never printed
Operating Systems, 2011, Danny Hendler & Amnon Meisels 4
The mutual exclusion problem (Dijkstra, 1965)
We need to devise a protocol that guarantees
mutually exclusive access by processes to a
shared resource (such as a file, printer, etc.)
How can we avoid such race conditions?
Operating Systems, 2011, Danny Hendler & Amnon Meisels 5
The problem model§ Multiple processes
§ Processes can apply read, write, or stronger operations to shared memory
§ Completely asynchronous
§ We assume processes do not fail-stop
Operating Systems, 2011, Danny Hendler & Amnon Meisels 6
Mutual exclusion: formal definition
loop foreverRemainder codeEntry codeCritical section
(CS)Exit code
end loop
Remainder code
Entry code
Exit code
CS
Operating Systems, 2011, Danny Hendler & Amnon Meisels 7
Mutex Requirements
Mutual exclusion: No two processes are at the critical section (CS) at the same time
Deadlock-freedom: If processes are trying to enter their critical section, then some process eventually enters the critical section
aka Progress
Starvation-freedom: If a process is trying to enter its critical section, then this process must eventually enter its critical section
also called Bounded waiting
Operating Systems, 2011, Danny Hendler & Amnon Meisels 8
Mutual exclusion using critical regions
Operating Systems, 2011, Danny Hendler & Amnon Meisels 9
Brute force solution: disabling interrupts
Disable interruptsDo your stuffEnable interrupts
Problems User processes should not be allowed to disable interrupts
Disabling interrupts must be done for a very short period of time
Does not solve the problem in a multi-processor system
Operating Systems, 2011, Danny Hendler & Amnon Meisels 10
Mutual Exclusion with a lock variable
Program for both processes
1. await lock=02. lock:=13. CS4. lock:=0
initially: lock=0
Does the algorithm satisfy mutex?
Does it satisfy deadlock-freedom?
Does it satisfy starvation-freedom?
NoYesNo
Operating Systems, 2011, Danny Hendler & Amnon Meisels 11
Mutual Exclusion: strict alternation
Does the algorithm satisfy mutex?
Does it satisfy deadlock-freedom?
Does it satisfy starvation-freedom?
YesNoNo
Program for process 0
1. await turn=02. CS3. turn:=1
Program for process 1
1. await turn=12. CS3. turn:=0
initially: turn=0
Operating Systems, 2011, Danny Hendler & Amnon Meisels 12
Mutual Exclusion: flag array
Does the algorithm satisfy mutex?
Does it satisfy deadlock-freedom?
Does it satisfy starvation-freedom?
YesNoNo
Program for process 01. flags[0]:=true2. await flags[1]=false3. CS4. flags[0]:=false
Program for process 1
1. flags[1]:=true2. await flags[0]=false3. CS4. flags[1]:=false
bool flags[2] initially {false, false}
Operating Systems, 2011, Danny Hendler & Amnon Meisels 13
Peterson’s 2-process algorithm (Peterson, 1981)
initially: b[0]:=false, b[1]:=false, value of turn immaterial
Program for process 0
1. b[0]:=true2. turn:=03. await (b[1]=false or
turn=1)4. CS5. b[0]:=false
Program for process 1
1. b[1]:=true2. turn:=13. await (b[0]=false or
turn=0)4. CS5. b[1]:=false
Operating Systems, 2011, Danny Hendler & Amnon Meisels 14
Schema for Peterson’s 2-process algorithm
Indicate participationb[i]:=true
Barrierturn:=i
Is there contention?b[1-i]=true?
yes
no
Critical Section
Exit codeb[i]:=false
First to cross barrier?turn=1-i?
no, maybe
yes
Synchronization Algorithms and Concurrent Programming , Gadi Taubenfeld © 2006
Operating Systems, 2011, Danny Hendler & Amnon Meisels 15
Peterson’s 2-process algorithm satisfies mutual-exclusion
and deadlock-freedomand starvation-freedom
Operating Systems, 2011, Danny Hendler & Amnon Meisels 16
Observations Shared register: turn (2-valued)
read & write by both processes Two boolean registers: b[0], b[1]
process 0 can write b[0], process 1 can write b[1]both can read b[0] & b[1]
Can the algorithm be modified to use only single-writer registers ?
Operating Systems, 2011, Danny Hendler & Amnon Meisels 17
Kessels’ 2-process algorithm (1982)
encode turn=0 as turn[0] = turn[1]turn=1 as turn[0] ≠ turn[1]
initially: b[0]:=false, b[1]:=false, value of turn[i] immaterial
Program for process 01. b[0]:=true2. local[0]:=turn[1]3. turn[0]:=local[0]4. await (b[1]=false or
local[0] ≠ turn[1])5. CS6. b[0]:=false
Program for process 11. b[1]:=true2. local[1]:=1 – turn[0]3. turn[1]:=local[1]4. await (b[0]=false or
local[1] = turn[0])5. CS6. b[1]:=false
Operating Systems, 2011, Danny Hendler & Amnon Meisels 18
Mutual exclusion for n processes: A tournament tree
0
0 1
0 1 2 3
0 1 2 3 4 5 6 7
Level 0
Level 1
Level 2
Processes
A tree-node is identified by: [level, node#]
Synchronization Algorithms and Concurrent Programming , Gadi Taubenfeld © 2006
Operating Systems, 2011, Danny Hendler & Amnon Meisels 19
N-process mutual exclusion: a tournament tree
Program for process i1. node:=i2. For level = 0 to log n-1 do3. id:=node mod 24. node:= node/2 5. b[level,2node+id]:=true6. turn[level,node]:=id7. await (b[level,2node+1-id]=false or
turn[level,node]=1-id)8. od 9. CS10. for level=log n –1 downto 0 do11. node:= i/2level+1 12. b[level,node]:=false13. od
VariablesPer node: b[level, 2node], b[level, 2node+1], turn[level,node]Per process (local): level, node, id
Operating Systems, 2011, Danny Hendler & Amnon Meisels 20
Fairness: First in First Out (FIFO)
entry code
exit code
criticalsection
remainder Mutual Exclusion
Deadlock-freedom
Starvation-freedom
doorway
waiting
• FIFO: if process p is waiting and process q has not yet started the doorway, then q will not enter the CS before p
Synchronization Algorithms and Concurrent Programming , Gadi Taubenfeld © 2006
Operating Systems, 2011, Danny Hendler & Amnon Meisels 21
time
Lamport’s Bakery Algorithm
0 0 0 0 0 0
doorway
1 2 3 4 5 n
CS
exit
1
1
2 2
2 2
1
1
0
2
2
0
3
3
2
2
0
4
4waiting
en
try
remainder
Synchronization Algorithms and Concurrent Programming , Gadi Taubenfeld © 2006
Operating Systems, 2011, Danny Hendler & Amnon Meisels 22
Implementation 1code of process i , i {1 ,..., n}
number[i] := 1 + max {number[j] | (1 j n)}for j := 1 to n (<> i) { await (number[j] = 0) (number[j] > number[i])}critical sectionnumber[i] := 0
1 2 3 4 n
number integer0 0 0 0 0 0
Answer: No, it can deadlock!
Does this implementation work?
Synchronization Algorithms and Concurrent Programming , Gadi Taubenfeld © 2006
Operating Systems, 2011, Danny Hendler & Amnon Meisels 23
time
Implementation 1: deadlock
0 0 0 0 0 0
doorway
1 2 3 4 5 n
CS
exit
1
1
2 2
2 2
1
1
0
waiting
en
try
remainder
deadlock
Synchronization Algorithms and Concurrent Programming , Gadi Taubenfeld © 2006
Operating Systems, 2011, Danny Hendler & Amnon Meisels 24
number[i] := 1 + max {number[j] | (1 j n)}for j := 1 to n (<> i) { await (number[j] = 0) (number[j],j) < number[i],i)
// lexicographical order
}critical sectionnumber[i] := 0
1 2 3 4 n
number integer0 0 0 0 0 0
Answer: It does not satisfy mutual exclusion!
Implementation 2code of process i , i {1 ,..., n}
Does this implementation work?
Synchronization Algorithms and Concurrent Programming , Gadi Taubenfeld © 2006
Operating Systems, 2011, Danny Hendler & Amnon Meisels 25
time
Implementation 2: no mutual exclusion
0 0 0 0 0
doorway
1 2 3 4 5 n
CS
exit
0
1
0 0
2 2
1
1
0
2 2
waiting
en
try
remainder
1 2 2
0
Synchronization Algorithms and Concurrent Programming , Gadi Taubenfeld © 2006
Operating Systems, 2011, Danny Hendler & Amnon Meisels 26
The Bakery Algorithmcode of process i , i {1 ,..., n}
1: choosing[i] := true2: number[i] := 1 + max {number[j] | (1 j n)}3: choosing[i] := false4: for j := 1 to n do5: await choosing[j] = false 6: await (number[j] = 0) (number[j],j) (number[i],i)7: od8: critical section9: number[i] := 0
1 2 3 4 n
choosing bitsfalse
number integer0 0 0 0 0 0
false false false false false
Doorway
Waiting Bakery
Operating Systems, 2011, Danny Hendler & Amnon Meisels 27
Implementing the choice of number[]
local1 := 0;for local2:= 1 to n { local3 := number[local2] ; if local1 < local3 then local1 := local3;}number[i] := 1 + local1;
If the value of number[k] does not change while process i is computing the maximum, then number[i] will have a value greater than number[k]
Synchronization Algorithms and Concurrent Programming , Gadi Taubenfeld © 2006
Operating Systems, 2011, Danny Hendler & Amnon Meisels 28
Correctness of the Bakery algorithm§ Doorway – lines 1-3; Bakery – lines 4-8 Process i is in its CS and process k is in the bakery T[i,5] – last time process i read choosing[k] (line 5)§ T[i,6] – last time process i executed line 6 for j=k§ T[k,1] – last time process k executed line 1 (entered doorway)§ T[k,3] – last time process k executed line 3 (left
doorway)§ Either T[i,5] < T[k,1] or T[k,3] < T[i,5]§ First case: number[i] < number[k]§ Second: (number[i],i) < (number[k],k)§ If process i is in the CS and process k is in
the bakery then (number[i],i) < (number[k],k)
Operating Systems, 2011, Danny Hendler & Amnon Meisels 29
Lamport’s bakery algorithm satisfies mutual-exclusion,
starvation-freedom, and FIFO
Operating Systems, 2011, Danny Hendler & Amnon Meisels 30
Test-and-set(w)do atomically prev:=w w:=1 return prev
The test-and-set instruction
Program for process i1. await test&set(v) = 02. Critical Section3. v:=0
initially: v:=0
Mutual exclusion?Yes
Deadlock-freedom?
NoStarvation-freedom?
Yes
Operating Systems, 2011, Danny Hendler & Amnon Meisels 31
Starvation-free mutex using test-and-set
program for process i
1. interested[i]:=true2. await ( (test-and-set(lock) = 0) || (interested[i]=false) )3. CS4. interested[i]:=false5. j:=(i+1) % n6. while (j != i && !interested[j]) j:=++j % n7. if (j=i)8. lock:=09. else10. interested[j]:=false
boolean lock initially 0, interested[n] initially false