Week 3 - Mutual Exclusion Flashcards
Critical Sections
Set of steps algorithim that must be executed to update a shared data strucuture
repeat
«critical section»
«do other work»
forever;
Race condition
outcome of two or more threads competing to modify shared data, where interference between threads results in erroneous results or a corrupted data structure.
Mutual exclusion
methods to ensure only one thread does a particular activity, such as executing their critical section, at a time. i.e., all other threads are excluded from doing that activity.
Atomicity
Mutual exclusion used to ensure that critical sections excute atomically.
An concurrent programming, code fragment is a piece of code that executes as a whole without interruption. Where no other code in other threads can interfere with execution.
Race Conditions are Evil
Noxious kind of programming error.
They occur non-deterministically, causing intermittent faults.
- Program could be tested and never exhibit race condition.
- When program does crash randomly, and nobody knows why.
Avoiding a Race Condtion
repeat
… do something to begin mutual exclusion…
«critical section»
… do something to end mutual exclusion…
«do normal work»
forever;
Attempt at Mutal Exclusion (LOCK)
New shared boolean, lock, initialized to false, specifies whether thread is in its critical section.
lock idea
when first thread is ready to enter critical section, its wait loop terminates immediately, lock is set to true, and critical section starts to execute.
if second thread wants critical section it will see lock is true, and wait loop iterates until the first thread leaves its critical section and sets lock back to false.
Attempt at turn
a new shared variable called turn specifies whose turn it is to enter cirital section next.
turn idea
turn is initalised to 0.
thread 0 is allowed to execute first cirital section.
- if thread 1 tries to enter its own critical section before thread 0 has finished, it waits in loop doing nothing.
when thread 0 leaves critical sction, turn is set to 1.
- if thread 1 was waiting in a loop, it then ends.
roles are now swapped, thread 0 cannot reenter the critical region until thread 1 has had its turn.
problem with turn idea
Thread 1 may be blocked forever.
Guarantees safety but not progress.
Algorthim interested
Two shared variables, interested [0] and interested [1]
they are boolean variables, either true or false.
when true interested[i] wants to enter critical section
interested idea
both interested[0] and interested[1] set to false intialization.
thread sets to interested variable when wants enter into ciritcal region.
- if other interested it has to wait in loop until thread is finished crirtical section.
leave crirtical section, unset interested variavle, so other thread can have acess.
interested problem
Doesnt establish mutual exlusion.
both threads now loop block forever.
guarntees safety, not progress.
Petersons Algorithim
Discovered until 1981.
Late for such a fundamental and simple looking algorithm.