Examples of race conditions
CS 273 (OS), Fall 2019
Race conditions in Dining Philosophers algorithm 3
Within loop
philosopher p=1 philosopher p=2
+>
| ...
| // myturn[p] IS FALSE (p=2)
| think
| if (!myturn[p])
<+
... |
myturn[p] = false //p=1 |
myturn[next] = true |
wakeup(next) // WAKEUP p=2 |
repeat forever |
think |
... |
if (!myturn[p]) |
sleep() // BLOCK! |
+>
| sleep() // BLOCK!
Involving initialization
philosopher p=1 philosopher p=2
<+
if (p == 1) |
myturn[p] = true |
else |
myturn[p] = false |
next = (p+1) % N |
prev = (p+N-1) % N |
repeat forever |
think |
if (!myturn[p]) // FALSE |
sleep() |
pick up left fork |
... |
myturn[p] = false |
myturn[next] = true |
+>
| if (p == 1)
| myturn[p] = true
| else
| myturn[p] = false
| // myturn[p] IS NOW FALSE
| next = (p+1) % N
| prev = (p+N-1) % N
<+
wakeup(next) // WAKEUP p=2 |
repeat forever |
think |
... |
if (!myturn[p]) |
sleep() // BLOCK! |
+>
| repeat forever
| think
| if (!myturn[p])
| sleep() // BLOCK!
A race condition in producer/consumer algorithm
producer consumer
+>
| repeat forever
| ...
| // ASSUME count=0 NOW
| if (count == 0)
<+
produce an item |
if (count == MAX) |
sleep() |
insert an item |
count = count + 1 |
if (count == 1) |
wakeup(consumer) |
produce an item |
... |
+>
| sleep() // BLOCK!
<+
... |
// ASSUME count=MAX NOW |
produce an item |
if (count == MAX) |
sleep() // BLOCK! |