Operating Systems Module 4: Concurrency Lesson 4.6 15 min

Project: fix the broken bank

A small bank has three concurrency bugs: a lost deposit, an overdraft and a deadlock. Find each one in the simulator, choose the right fix, and prove the money still adds up.

A bank is the classic home of concurrency bugs, because money is the one thing that must never be wrong. Here is a very small bank: a few accounts, and transfer threads that move money between them. It has three bugs, and each one is a different problem from this module.

Each challenge below is a simulator with a goal: the run must end with the total money unchanged, no account below zero, and every transfer finished. Open Try it (it is already open), choose a fix, press Run, and the goal box turns green when the bank is safe. The sim reports what really happened, so a fix that does not help will visibly not help. There are four fixes on the menu, and each challenge needs a different one:

  • A lock around the update: lock only the read, change and write of a balance.
  • A lock around the check and the update: one lock held from the moment a thread looks at a balance until it has acted on what it saw.
  • Lock the accounts in a fixed order: every transfer takes its locks in the same order.
  • No fix: the bug as it is.

The threads take turns one instruction at a time, which is the worst case for these bugs, so you can see each one on the first run.

Bug 1: money vanishes

Two transfers happen at the same time. T1 moves 30 from Ana to Ben, and T2 moves 50 from Cleo to Ben. Each takes money out of its own account, then adds it to Ben’s with the three steps you know: read, add, write. Run it as it is, and look at the total.

Bug 1: a deposit disappears

  1. Start: total money is 250 A bank has three accounts. T1 moves 30 from Ana to Ben and T2 moves 50 from Cleo to Ben, at the same time. Each takes the money out of its own account, then adds it to Ben with the usual three steps: read, add, store. There is no fix yet. The total must stay 250, whatever the threads do.
    Ana
    100
    Ben
    50
    Cleo
    100
    Money in flight
    0
    Total (should stay 250)
    250
  2. T1 takes 30 out of Ana Ana goes down by 30 and the money is now in T1's hands. The total in the accounts is lower for a moment, but the money is not lost: it is in flight.
    Ana
    70
    Ben
    50
    Cleo
    100
    Money in flight
    30
    Total (should stay 250)
    250
  3. T2 takes 50 out of Cleo Cleo goes down by 50 and the money is now in T2's hands. The total in the accounts is lower for a moment, but the money is not lost: it is in flight.
    Ana
    70
    Ben
    50
    Cleo
    50
    Money in flight
    80
    Total (should stay 250)
    250
  4. T1 reads Ben T1 copies Ben's balance into a register: r = 50.
    Ana
    70
    Ben
    50
    Cleo
    50
    Money in flight
    80
    Total (should stay 250)
    250
  5. T2 reads Ben T2 copies Ben's balance into a register: r = 50.
    Ana
    70
    Ben
    50
    Cleo
    50
    Money in flight
    80
    Total (should stay 250)
    250
  6. T1 adds 30 T1 adds 30 in its register: r = 80. The account has not changed yet.
    Ana
    70
    Ben
    50
    Cleo
    50
    Money in flight
    80
    Total (should stay 250)
    250
  7. T2 adds 50 T2 adds 50 in its register: r = 100. The account has not changed yet.
    Ana
    70
    Ben
    50
    Cleo
    50
    Money in flight
    80
    Total (should stay 250)
    250
  8. T1 writes Ben T1 stores r = 80 into Ben. It matches what the account held when T1 read it.
    Ana
    70
    Ben
    80
    Cleo
    50
    Money in flight
    50
    Total (should stay 250)
    250
  9. T2 writes Ben and overwrites a deposit T2 stores r = 100 into Ben. But Ben was 80 by now, and the result should have been 130. The other deposit has been wiped out, and 30 of the bank's money has vanished.
    Ana
    70
    Ben
    100
    Cleo
    50
    Money in flight
    0
    Total (should stay 250)
    220
  10. Money has vanished: 220 instead of 250 Both transfers finished, but the accounts add up to 220, not 250. 30 disappeared because two threads updated the same balance without a lock. Nothing reported an error.
    Ana
    70
    Ben
    100
    Cleo
    50
    Money in flight
    0
    Total (should stay 250)
    220

The total is 220 where it should be 250: 30 has gone. It is exactly the race from the start of the module: two threads working from the same old balance.

Hint. The problem is a critical section on Ben’s balance. Which lock covers it? Try the lock around the update, and check that Ben ends up with the sum of both deposits.

Bug 2: the account goes negative

Ana has 100. Two transfers each try to move 80 to Savings, but only if Ana has at least 80. The rule is that the total stays the same and no account goes below zero. Run it with no fix first.

Bug 2: an overdraft

  1. Start: total money is 100 Ana has 100. T1 and T2 each want to move 80 from Ana to Savings, but only if Ana has at least 80. The rule is that no account may go below zero. There is no fix yet. The total must stay 100, whatever the threads do.
    Ana
    100
    Savings
    0
    Money in flight
    0
    Total (should stay 100)
    100
  2. T1 checks Ana: enough money T1 sees Ana has 100, at least 80, so it goes ahead. Remember what it saw: the check is only true at this moment.
    Ana
    100
    Savings
    0
    Money in flight
    0
    Total (should stay 100)
    100
  3. T2 checks Ana: enough money T2 sees Ana has 100, at least 80, so it goes ahead. Remember what it saw: the check is only true at this moment.
    Ana
    100
    Savings
    0
    Money in flight
    0
    Total (should stay 100)
    100
  4. T1 takes 80 out of Ana Ana goes down by 80 and the money is now in T1's hands. The total in the accounts is lower for a moment, but the money is not lost: it is in flight.
    Ana
    20
    Savings
    0
    Money in flight
    80
    Total (should stay 100)
    100
  5. T2 takes 80 out of Ana Ana goes down by 80 and the money is now in T2's hands. The total in the accounts is lower for a moment, but the money is not lost: it is in flight.
    Ana
    -60
    Savings
    0
    Money in flight
    160
    Total (should stay 100)
    100
  6. T1 puts 80 into Savings Savings goes up by 80. The transfer is complete, and the total is whole again.
    Ana
    -60
    Savings
    80
    Money in flight
    80
    Total (should stay 100)
    100
  7. T2 puts 80 into Savings Savings goes up by 80. The transfer is complete, and the total is whole again.
    Ana
    -60
    Savings
    160
    Money in flight
    0
    Total (should stay 100)
    100
  8. An account went below zero The total is still 100, but Ana has -60. Both threads checked the balance before either one changed it, so both thought there was enough. Checking and acting have to happen under one lock.
    Ana
    -60
    Savings
    160
    Money in flight
    0
    Total (should stay 100)
    100

The total is fine, 100, so a check on the total alone would pass. But Ana is at -60: the bank has paid out money it did not have. Nothing was lost by the increment this time: every update was safe. The bug is that the check and the act were separate, so what the check saw was no longer true when the act happened. This is a check-then-act race.

Hint. If you try “a lock around the update” you will see that it does not help: the check is still outside the lock, so both threads still pass it. The lock has to cover the check and the update, held together. Then the second transfer sees the balance after the first, and refuses.

Bug 3: two transfers freeze

Now Ana and Ben each have 100. T1 moves 10 from Ana to Ben, and T2 moves 20 from Ben to Ana. To be safe, each transfer locks both accounts before moving money: first the one it takes from, then the one it gives to. That fixes the races. Run it.

Bug 3: a deadlock

  1. Start: total money is 200 Ana and Ben each have 100. T1 moves 10 from Ana to Ben, and T2 moves 20 from Ben to Ana. To be safe, each transfer locks both accounts, the one it takes from first. There is no fix yet. The total must stay 200, whatever the threads do.
    Ana
    100
    Ben
    100
    Money in flight
    0
    Total (should stay 200)
    200
  2. T1 locks Ana T1 takes the lock on Ana. Nobody else can lock it until T1 lets go.
    Ana
    100
    Ben
    100
    Money in flight
    0
    Total (should stay 200)
    200
  3. T2 locks Ben T2 takes the lock on Ben. Nobody else can lock it until T2 lets go.
    Ana
    100
    Ben
    100
    Money in flight
    0
    Total (should stay 200)
    200
  4. T1 waits for the lock on Ben T1 wants the lock on Ben, but T2 holds it. T1 sleeps until it is handed over.
    Ana
    100
    Ben
    100
    Money in flight
    0
    Total (should stay 200)
    200
  5. T2 waits for the lock on Ana T2 wants the lock on Ana, but T1 holds it. T2 sleeps until it is handed over.
    Ana
    100
    Ben
    100
    Money in flight
    0
    Total (should stay 200)
    200
  6. Deadlock: both transfers are stuck Each transfer holds one lock and waits for the one the other holds. Neither can ever move again, and the money stays where it is. Locks fixed the data races but created a new problem: waiting in a circle.
    Ana
    100
    Ben
    100
    Money in flight
    0
    Total (should stay 200)
    200

The locks protected the data, and in doing so created a new bug: a deadlock. It is the same circle as the five philosophers, with two threads instead of five. Making the locks bigger does not help. Try the other two fixes to confirm: the stuck state comes from the order of the locks, not from how long they are held.

Hint. Pick the fix that makes both transfers take Ana’s lock first and Ben’s lock second, whichever way the money moves. If every thread takes locks in the same order, nobody can end up holding the second one while waiting for the first.

What you built

Look back at the three fixes. Each one answers a different question about a critical section:

  1. How big is the critical section? For Bug 1, it was the read, the add and the write of one balance.
  2. What does the lock have to include? For Bug 2, the check as well as the act, because the decision depends on the data it looked at.
  3. In what order are locks taken? For Bug 3, always the same order, or the locks themselves become a trap.

Notice also that a fix for one bug did nothing for another. A lock around an update did not stop the overdraft, and lock ordering did not stop the lost deposit. When you fix concurrency bugs you need to know which bug you have, and a test that passes proves very little: the simulator had to show you the unlucky schedule on purpose. Real programs make that schedule rare, which is why these bugs survive testing.

The third bug points ahead. Lock ordering worked, but we have not said why a circle of waiting threads is a deadlock, how to spot one, or what else could be done. That is Module 5.

Quick check

Quick check

  1. Why did a lock around the update alone not fix the overdraft?
  2. What is a check-then-act race?
  3. Two transfers lock their two accounts in opposite orders and freeze. What is the simplest fix?
  4. Which bug in the project did NOT change the total amount of money?

Review: what you learned earlier

Coming back to an idea after a break is what makes it stick. These questions come from earlier modules, starting with any you missed. Answer from memory before you look anything up.

Spaced review

This review picks questions for you, so it needs JavaScript.

Module 4 recap

You have seen that count++ is three instructions and that an unlucky interleaving loses an update, which makes a race condition hard to find because it depends on timing. A lock gives mutual exclusion, built on atomic instructions like test-and-set and compare-and-swap, and a waiting thread may spin or sleep. Semaphores count resources and signal events, and condition variables wait for a condition, always inside a while loop. The classic problems taught the bounded buffer, readers and writers, and the dining philosophers, whose circle of waiting is a deadlock. Separate processes talk through the kernel with pipes, signals, shared memory and sockets. In the project you fixed a lost update, a check-then-act race and a lock-order deadlock.

Next, Module 5: the bank froze because each transfer held one lock and wanted another. You will learn the four conditions that make deadlock possible, and how to avoid it, detect it and recover.

Your progress is saved in this browser. Sign in to keep it on every device. Free, no password.

Join the discussion