ALL > Computer and Education > courses > university courses > undergraduate courses > Operating System > ZSTU-(2020-2021)-1 > student homework > 2018529627020 JE >
HOMEWORK 5 Version 0
👤 Author: by kagenzaclaudiengmailcom 2020-10-27 07:18:28
PROBLEM EXAMPLE

Race conditions are possible in many computer systems. Consider a banking system with two methods: deposit (amount) and withdraw (amount). These two methods are passed the amount that is to be deposited or withdrawn from a bank account. Assume that a husband and wife share a bank account and that concurrently the husband calls the withdraw () method and the wife calls deposit(). Describe how a race condition is possible and what might be done to prevent the race condition from occurring. Write the pseudo code for the solution.

SOLUTION TO THIS PROBLEM
The "race" is not between husband and wife, but between the withdrawl() method and the deposit() method.  Since they each have access to the "Current Balance" variable (a shared variable), and they either reduce it or increase it, synchronization is imperative.


Here is an example of the race condition:  The Current Balance is $500.  The husband's program calls withdrawl($100) and accesses the Current Balance of $500.  But, before the new Current Balance is stored, the wife's program calls deposit($100) and accesses the Current Balance of $500.  Then, the husband's program changes the shared variable Current Balance to $400, followed by the wife's program changing the Current Balance variable to $600.  With a withdrawl($100) and a deposit($100), the Current Balance should again be $500, but it is $600 (note: it could have gone the other way).


The solution is to create a "Lock" on the Current Balance variable (or any shared memory).  By agreement, 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.  [note: If, somehow the variable gets "locked" for a long period of time, this results in a "deadlock" condition and one process or the other must be cancelled.]


THE FOLLOWING ARE PSEUDOCODE:


  method withdrawl(Amt)

      If BalanceLock is locked, then WAIT

      Lock (BalanceLock)

          Assign (Balance - Amt) to Balance

     UnLock (BalanceLock)

  end withdrawl


  method deposit(Amt)

      If BalanceLock is locked, then WAIT

      Lock (BalanceLock)

         ASSIGN (Balance - Amt) to Balance

      Unlock (BalanceLock)

  end deposit

Please login to reply. Login

Reversion History

Loading...
No reversions found.