Lecture 11: Synchronization 2 Flashcards
Algorithm 1: Strict Alternation
Each thread takes turns getting access to its CS 1. Mutual Exclusion? • Thread only has access to critical section when turn has appropriate value • With n threads, thread i waits for at most n 1 threads to enter/exit their critical sections 2. Bounded Wait? 3. Progress? No
Mutex
Synchronization mechanism for enforcing limits on access to the CS
Algorithm 2: “After You”
Thread specifies interest in entering critical section. Can enter if other thread is not interested
“Mutual Exclusion: Yes
• Thread only has access to critical section when other threads’ flag == 0
Progress: No
• Both threads could express interest with neither allowed
entry into critical section
Bounded Wait: No
• Possible for threads to starve forever”
Algorithm 3: Peterson’s Algorithm
Combines algorithms 1 and 2. Threads take turns, but a thread skips its turn if it’s not interested and another thread is
“• thread A gets access to C/S if (flag1” “== FALSE) or (turn” “== 0)”
“• thread B gets access to C/S if (flag0” “== FALSE) or (turn” “== 1)”
Disabling Interrupts
“Thread code works as follows …”
“while (TRUE) {
disable interrupts
enable interrupts
}”
+: Easy to see that it works
–:Overkill
1. Disables all interrupts (e.g.: I/O)
2. Disallows concurrency with threads in non
critical sections
–: Doesn’t work for multicore systems
(Each CPU has own interrupts: threads on different
CPU’s can access their critical sections at same time)
Test and Set ( TS)
"New AL instruction ( Atomic) TS (i) = 1. temporarily saves value of Mem[i] 2. sets Mem[i] := TRUE 3. returns original value of Mem[i]"
can be used to implement “:Locking”