Race conditions are possible in many computer systems. Consider a banking system
ID: 3623092 • Letter: R
Question
Race conditions are possible in many computer systems. Consider a banking system with thefollowing two functions: deposit(amount) and withdraw(amount). These two functions are passed the
amount that is to be deposited or withdrawn from a bank account. Assume a shared bank account
exists between a husband and wife and concurrently the husband calls the withdraw() function and the
wife calls deposit(). Describe how a race condition is possible and what might be done to prevent the
race condition from occurring
Explanation / Answer
Say there is a variable 'balance' which records the current balance of the account. It is likely that the withdraw() function contains a line such as 'balance -= amount' and that the deposit() function contains a line such as 'balance += amount'. A race condition is possible because the lower level statements may be like: 1. reg1 = amount1 #deposit amount 2. reg2 = amount2 #withdraw amount 3. reg3 = balance #current balance 4. reg4 = balance #current balance 5. reg1 = reg1 + reg3 #perform deposit 6. reg2 = reg3 - reg2 #perform withdrawal 7. balance = reg1 #balance after deposit 8. balance = reg2 #balance after withdrawal Notice steps 7 and 8 lead to an incorrect state. We can prevent the race condition by treating statements [1, 3, 5, 7] and [2, 4, 6, 8] as atomic instructions to keep a consistant state across transactions.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.