Race condition - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

Race condition

Description:

Race condition The scourge of parallel and distributed computing... Race condition When multiple processes compete for a non-sharable resource With no synchronization ... – PowerPoint PPT presentation

Number of Views:56
Avg rating:3.0/5.0
Slides: 20
Provided by: peopleDs86
Category:

less

Transcript and Presenter's Notes

Title: Race condition


1
Race condition
  • The scourge of parallel and distributed
    computing...

2
Race 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

3
Race condition (cont)
mem
Thread A
Thread B
reg mem reg reg 8 mem reg
reg mem reg reg 1 mem reg
4
Race 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.
5
Race 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
6
Race 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.
7
Non-serialized operations
Resource
8
Serialized 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

9
Synchronization
  • Only one process may execute the critical section
    of code
  • Acquire exclusive rights
  • Execute critical section
  • Release exclusive rights

10
Synchronization
... acquire_permission(s) reg mem reg reg
n mem reg release_permission(s) ...
The entity s controls access to mem.
11
Synchronization
  • 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

12
Semaphore
  • 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

13
Semaphore 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

14
Mutex
  • 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).

15
Monitor
  • 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

16
Unsharable resources
  • Process memory (part of)
  • Files
  • Databases
  • Printers, scanners, cameras, modems

17
Deadlock
  • 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
18
Livelock
  • 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
19
Further fun reading
  • Dijkstra, The Dining Philosophers problem
  • Chandy/Misra solution to DPh problem
  • Producer-consumer problem
  • Sleeping barber problem
  • Readers-writers problem
Write a Comment
User Comments (0)
About PowerShow.com