Race conditions are possible in many computer systems. Consider a banking system with two functions:
deposit (amount) and withdraw (amount).
These two functions are past 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().
1. Describe how a race condition is possible, and what might be done to prevent the race condition from occurring?

Respuesta :

Answer:

A race condition is actually possible in this scenario.

Explanation:

A race condition is an undesirable situation that occurs when a device or system attempts to perform two or more operations at the same time. In order to prevent the race condition from occurring the operations must be done in the proper sequence to be done correctly.

Here is an example of the race condition:  Let's assume the Current Balance of the shared bank account is $600.  The husband's program calls the withdraw($100) function and accesses the Current Balance of $600.  But right before it stores the new Current Balance, the wife's program calls function deposit($100) function and accesses the Current Balance of $600.  At this point, the husband's program changes the shared variable Current Balance to $500, followed by the wife's program changing the Current Balance variable to $700.  Based on calling both withdraw($100) and deposit($100) functions, the Current Balance should again be $600, but it is $700. Alternatively, the  Current Balance could also have been $500.

To prevent the race condition from occurring, the solution will be to create a method to "Lock" on the Current Balance variable.  Each method must "Lock" the variable before accessing it and changing it.  If a method determines that the variable is "Locked," then it must wait before accessing it.

ACCESS MORE