Question: (a) [6 Points] Consider this pseudocode for a bank account supporting concurrent access. Assume that Lock is a valid locking class, although it is not
![(a) [6 Points] Consider this pseudocode for a bank account supporting](https://s3.amazonaws.com/si.experts.images/answers/2024/09/66de58d05737b_75966de58cfc0cb6.jpg)
(a) [6 Points] Consider this pseudocode for a bank account supporting concurrent access. Assume that Lock is a valid locking class, although it is not in Java 1 class BankAccount 3 private Lock lk new Lock (O; 5 lk.acquireO 7 lk.release) private int balance-6, 4 int getBalance() int ansbalance; return ans; 10 void setBalance(int x) 11 k.acquire) 12 13 14 15 void withdraw(int amount) 16 k.acquire) 17 int b=getBalance(); 18 if(amount > b){ 19 20 21 22setBalance (bamount); 23 lk.release) 24 25J balancex; 13 k.release) lk.release(); throw new WithdrawTooLargeException); If Lock is NOT re-entrant, then the code above is broken. Below is a new version of the withdraw method your friend proposes. Explain why newWithdraw doesn't "block forever" (unlike the original code) even if locks are NOT re-entrant 1 void newwithdraw(int amount) 2 k.acquire); 3 lk.release() int b getBalance(); 5 k.acquire(); 6 if (amount > b) 7 8 lk.release(); throw new WithdrawTooLargeException); 10 k.release) 11setBalance(bamount); 12 k.acquire) 13 lk.release); (b) [7 Points] Show that newwithdraw is incorrect by giving an interleaving of two threads, both calling newWithdraw, in which a withdrawal is forgotten/lost (a) [6 Points] Consider this pseudocode for a bank account supporting concurrent access. Assume that Lock is a valid locking class, although it is not in Java 1 class BankAccount 3 private Lock lk new Lock (O; 5 lk.acquireO 7 lk.release) private int balance-6, 4 int getBalance() int ansbalance; return ans; 10 void setBalance(int x) 11 k.acquire) 12 13 14 15 void withdraw(int amount) 16 k.acquire) 17 int b=getBalance(); 18 if(amount > b){ 19 20 21 22setBalance (bamount); 23 lk.release) 24 25J balancex; 13 k.release) lk.release(); throw new WithdrawTooLargeException); If Lock is NOT re-entrant, then the code above is broken. Below is a new version of the withdraw method your friend proposes. Explain why newWithdraw doesn't "block forever" (unlike the original code) even if locks are NOT re-entrant 1 void newwithdraw(int amount) 2 k.acquire); 3 lk.release() int b getBalance(); 5 k.acquire(); 6 if (amount > b) 7 8 lk.release(); throw new WithdrawTooLargeException); 10 k.release) 11setBalance(bamount); 12 k.acquire) 13 lk.release); (b) [7 Points] Show that newwithdraw is incorrect by giving an interleaving of two threads, both calling newWithdraw, in which a withdrawal is forgotten/lost
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
