05 - synchCritSec Args HW
05 - synchCritSec Args HW
t if MDFO < SafetyMargin then [brake] if MDFO < SafetyMargin then [brake]
Problem: need to ensure that each thread is executing its critical section (e.g
read&update MDFO) exclusively /atomically
(*) Oversimplified example, to get a feeling of context
if SMAF > MaxAllowedFlow then [change flaps’ orientation] if SMAF > MaxAllowedFlow then [change flaps’ orientation]
t
Oooops RACE CONDITION
SMAF is still 20, no flaps changed orientation ……
Problem: need to ensure that each thread is executing its critical section (e.g
read&update SMAF) exclusively /atomically
Problem formulation:
A solution must provide the entry and exit sections
It must ensure:
1. Mutual Exclusion. Only one process/thread at a time is allowed to
execute in its critical section.
2. Progress (no deadlock/no livelock). If no process/thread is in its critical
section and some is/are trying, some must enter its critical section in
bounded time.
3. Fairness / Bounded Waiting / no starvation. Variety of formulations, eg
– FCFS; or bounded #bypasses, …
/thread
B waits
/thread
A in CS
Bin CS
Can Dstep(B) happen here
i.e. see flagA == false? Pstep(B) sets
YES=> B decides to enter CS flagB to true
Time
too, before A exits !!!
B in CS
Can Dstep(B) happen here
i.e. see turn == idB ? Time
NO, only A can set turn to idB, i.e.
B must wait
[MP] – OS 05_ Process/thread synch: CS & argumentation; primitives & objects 15
“check-order”: progress property?
Can the algo violate the progress requirement?
Shared var turn: (A..B) // initially eg A
i.e. Can it happen that there is a case when no thread is in its CS and some is trying, and has // indicates whose turn it is to
to wait for unbounded time? enter CS
Thread A // B is symmetric
• Assume it can and w.l.o.g. consider B waiting in its while loop:
repeat
• B sees turn == idA => it needs to wait while turn ≠ idA do [nothing] Dstep
• Only A can set turn to idB [critical section]
turn ←idB
• if A is in its Remainder Section for unboubded time, B will wait for unbounded time [remainder section]
• i.e. we proved, using a counterexample that the algo can violate the progress requirement forever
A executes in its
remainder section
for unbounded time
.....
in CS
Fstep(B) sets Tstep(B) sets B’s check will see flagA == true
flagB to true turn to idA and turn == idA
Hence B must WAIT! 19 (But we are not done, we
have one more case…)
Dstep(A) sees
Tstep(A) turn == idA
sets turn to idB A decides to enter its CS
in CS
Fstep(A)
sets flagA to true Time
in CS
Time
in CS in CS
Time
3. Bounded waiting:
Key argument: wlog: If A waits for B, upon B re-requesting, B sets turn to A, hence B will wait for A to go ahead => A
can be bypassed at most once by B.
• This way of thinking and arguing, when we are presented with a solution to a synchronization
problem/race-condition (ie try to “attack” it, try to find if it does not work), is practical and useful (and
thus very common)
• It is very frequently used when arguing about correctness/incorrectness of proposed solutions. This way
of arguing helps us to:
(i) give a proof by contradiction that a proposition in focus is _correct_, e.g. that mutual exclusion is
preserved;
– see about proofs by way of contradiction @ eg http://cgm.cs.mcgill.ca/~godfried/teaching/dm-reading-
assignments/Contradiction-Proofs.pdf p 1-4 or
http://web.stanford.edu/class/archive/cs/cs103/cs103.1152/lectures/02/Small02.pdf p 45-47
(ii) OR identify a counter-example, which implies that the proposition in focus is _incorrect_ (i.e. it
_disproves_ the proposition)
– see also https://mathforlove.com/lesson/counterexamples/ and/or https://study.com/academy/lesson/counterexample-in-
math-definition-examples.html and/or http://web.stanford.edu/class/archive/cs/cs103/cs103.1152/lectures/02/Small02.pdf
Now:
• Help from other hardware synchronization primitives; n-thread CS
• Semaphores
Interrupt_disable
[critical section]
Interrupt_enable
fig: http://preshing.com/20120612/an-
introduction-to-lock-free-programming/
Simple, right?
Homework training: argue as we did for Peterson’s algo regarding correctness (mutual exclusion, progress) ;
what about fairness???
fig: http://preshing.com/20120612/an-
introduction-to-lock-free-programming/
Now:
• Help from other hardware synchronization primitives; n-thread CS
– Let’s reflect on busy waiting (aka spinning) …..
• Semaphores
Interrupt
LP thread in CS
Time
• Semaphores
Thread i
repeat
wait(mutex_sem);
[critical section]
signal(mutex_sem);
[remainder section]
forever;
Homework training: (i) argue as we did for Peterson’s algo; (ii) compare with the methods that use TAS or CAS
Q: how to execute code segment B in TB only after code segment A has been executed in TA:
Now:
• Help from other hardware synchronization primitives; n-thread CS
– Discussion on busy-waiting
• Semaphores
• semaphores
• spin locks for multiprocessors (for short critical sections): why?
– [hint: wastes CPU time and can cause deadlocks if used with strict priority
scheduling on uniprocessor]
• futex
– try_lock through 1 invocation of RMW instruction; if already locked, then process
enters blocked queue; why?
[Balance the cost of entering a queue directly with the cost of spinning on the
RMW instruction]
– https://man7.org/linux/man-pages/man2/futex.2.html
• condition variables