Title: Race condition
1Race condition
- The scourge of parallel and distributed
computing...
2Race condition
- When multiple processes compete for a
non-sharable resource - With no synchronization there is a race to who
claims/modifies the resource - Read-Modify-Write (safe)
- Read-Modify-Read-Write-Modify-Write
3Race condition (cont)
mem
Thread A
Thread B
reg mem reg reg 8 mem reg
reg mem reg reg 1 mem reg
4Race condition (cont)
Time reg of A reg of B mem
1 0
2 0 (regmem) 0 (regmem)
3 1 (reg reg1) 8 (reg reg8)
4 ?
5
6
7
reg mem reg reg n mem reg
The race to write to mem creates a wrong result.
5Race condition (cont)
Time reg of A reg of B mem
1 0
2 0 (regmem) 0
3 1 (reg reg1) 0 (regmem)
4 8 (reg reg8) 1 (memA.reg)
5 8 (memB.reg)
6
7
reg mem reg reg n mem reg
The update from process A is lost because it was
not seen by B
6Race condition (cont)
Time reg of A reg of B mem
1 0
2 0 (regmem)
3 1 (reg reg1)
4 1 (memA.reg)
5 1 (regmem)
6 9 (reg reg8)
7 9 (memB.reg)
reg mem reg reg n mem reg
The two updates do not interact, result is as
expected. By sheer luck, operations were
serialized.
7Non-serialized operations
Resource
8Serialized operations
Resource
Change request 1
Change request 2
Change request 3
- The resource is protected by a mechanism which
- enforces serial access to it
- Operating system
- Database manager
- Programming language synchronization
9Synchronization
- Only one process may execute the critical section
of code - Acquire exclusive rights
- Execute critical section
- Release exclusive rights
10Synchronization
... acquire_permission(s) reg mem reg reg
n mem reg release_permission(s) ...
The entity s controls access to mem.
11Synchronization
- Semaphore guards n resources
- Any thread can return a resource
- Mutex guards 1 resource
- Only the currently owning thread can return the
resource - Threads block until the resource is granted
12Semaphore
- Counts the nof available resources
- Acquire decrement, Release increment
- No allocation or identification of resources
- An atomically modifiable counter
- Race conditions over the semaphore are prevented
by - virtual machine / pgm language interpreter
- operating system
- hardware
13Semaphore caveats
- Processes (i.e. programmers) must follow the
protocol and not - Forget to return a resource after use
- Return a resource that was not requested
- Hold a resource for too long
- Use a resource anyway
14Mutex
- A binary semaphore (one resource)
- The process acquiring the resource is the only
one that can return it (ownership) - The synchronized keyword in Java use any object
as a monitor (a kind of mutex).
15Monitor
- A mutex with a thread queue
- Threads queue for the mutex
- Threads can yield the resource (wait)
- Threads can alert other threads (notify)
- In Java, every object has a monitor
- The synchronized keyword
16Unsharable resources
- Process memory (part of)
- Files
- Databases
- Printers, scanners, cameras, modems
17Deadlock
- Processes wait forever for all required resources
to be released
Waiting for R1 to be released
R1
P2
P1
R2
Waiting for R2 to be released
18Livelock
- Processes attempt to break the deadlock by
time-out and release, but no-one wins
Waiting for R1 to be released
R1
P2
P1
R2
Waiting for R2 to be released
19Further fun reading
- Dijkstra, The Dining Philosophers problem
- Chandy/Misra solution to DPh problem
- Producer-consumer problem
- Sleeping barber problem
- Readers-writers problem