(Usage hints for this presentation)
IT Systems, Summer Term 2024
Dr. Jens Lechtenbörger (License Information)
(Based on Chapter 4 of (Hailperin 2019))
The above is covered in this introduction to transaction processing.
Race (condition): a technical term
Concurrent activities on shared resources
Analogy
“Ferrari Kiss” by Antoine Valentini under CC BY-SA 2.0; from flickr
Assert(false)
),
you demonstrate a race conditionConsider the following piece of Java code (from Sec. 4.2 of (Hailperin 2019)) to sell tickets as long as seats are available. (That code is embedded in this sample program, which you can execute to see races yourself.)
if (seatsRemaining > 0) {
dispenseTicket();
seatsRemaining = seatsRemaining - 1;
} else displaySorrySoldOut();
Inspired by the Deadlock Empire, find and explain race conditions
involving the counter seatsRemaining
, which leads to “too many”
tickets being sold.
Races generally result from non-atomic executions
Even “single” instructions such as i += 1
are not atomic
A context switch may happen after any of these machine instructions, i.e., “in the middle” of a high-level instruction
“Interleaved execution of threads with MX for code from Sec. 4.2 of book by Max Hailperin, CC BY-SA 3.0.” by Jens Lechtenbörger under CC BY-SA 4.0; from GitLab
synchronized
turns Java method into CS with MX!Major synchronization challenges arise
seatsRemaining
Above solutions restrict entry to CS
Thus, they restrict access to the resource CPU
Deadlock
Starvation (related to (un-) fairness)
Mutexes
Figure © 2016 Julia Evans, all rights reserved; from julia's drawings. Displayed here with personal permission.
Object with atomic methods
lock()
or acquire()
: Lock/acquire/take the object
lock()
’ed by one thread at a timelock()
are blocked until
unlock()
unlock()
or release()
: Unlock/release the object
lock()
’ed again afterwards
“Figure 4.4 of cite:Hai17” by Max Hailperin under CC BY-SA 3.0; converted from GitHub
seatlock.lock();
if (seatsRemaining > 0) {
dispenseTicket();
seatsRemaining = seatsRemaining - 1;
} else displaySorrySoldOut();
seatlock.unlock();
up()
and down()
man futex
Typically based on hardware support for atomicity guarantees
Source files are available on GitLab (check out embedded submodules) under free licenses. Icons of custom controls are by @fontawesome, released under CC BY 4.0.
Except where otherwise noted, the work “Mutual Exclusion”, © 2017-2024 Jens Lechtenbörger, is published under the Creative Commons license CC BY-SA 4.0.