(5) Synchronization Flashcards
What are ‘Critical sections’?
Sequences of instructions that may get incorrect results if executed simultaneously
When do Critical Sections arise?
Read-modify-write pattern. Two threads trying to access a global/heap allocated variable (not local/stack).
What is a race condition?
If the result of the program executing depends on the timing, i.e it is non-deterministic.
What are the requirements for a correct critical section?
Mutual exclusion
(at most one thread in the critical section)
Progress
(If T outside critical section then T can’t prevent S from entering it)
Bounded Waiting
(If T is waiting for critical section it will eventually enter it)
Performance
(The overhead of entering and exiting is small)
How do we implement Mutual Exclusion?
Via hardware - special machine instructions
Via OS support - primitives via syscall
Via Software - entirely by user code
Give Peterson’s Algorithm
(Let ˆı mean 1-i) flag[i] = true; turn = ˆı; while ( flag[ˆı] && turn == ˆı ){} /* critical section */ flag[i] = false;
What are the different mechanisms for building critical sections?
Spinlocks
Semaphores
Monitors
Messages
What is a lock?
A memory object with two operations:
Acquire(): obtain right to enter CritSec
Release(): Give up right to be in CritSec
Acquire() prevents progress of thread until lock can be acquired.
How do we implement spinlocks?
‘Busy wait’ / spin in the acquire method until the lock is released. When a thread loops waiting for a mutex it is called a Spinlock. Acquire/release must be ATOMIC (executes as though it could not be interrupted)