Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Exercise 5.7 (in 9^th edition of the textbook). Race conditions are possible in

ID: 3844630 • Letter: E

Question

Exercise 5.7 (in 9^th edition of the textbook). Race conditions are possible in many computer systems. Consider a banking system that maintains an account balance with two functions: deposit(amount) and withdraw(amount). These two functions are passed the amount that is to be deposited or withdrawn from the bank account balance. Assume that a husband and wife share a bank account. 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

A race condition occurs inside critical section. In OS terms critical section is any part of program which are accessed by multiple threads simultaneously and their sequence of execution can affect the final result.

In this case husband and wife are two threads executing some funcitons which finally change the overall balance in the account. So updating the account balance here is the critical section. What could possibly happen:

Let us first write the two functions as:

wirhdraw()

1.read balance from memory to register

2.reduce value of register by withdraw amount

3.write register to memory

deposit()

1.read balance from memory to register

2.increase value of register by deposit amount

3.write register to memory

Let initial balance be $500,

Husband wants to withdraw $200, so he calls withdraw(200) and concurrently wife wants to deposit $300 so she calls deposit(300). Based on this the final balance should be $600

Now let's see how incorrect balance is updated in memory due to race condition.

The execution of two threads(let thread H denote husband and thread W denote Wife) will be interleaved and the following seuence of operation can take place:

H: read balance(500) from memory to register A

W:read balance(500) from memory to register B

H:subtract 200 amount from register A (500-200=300)

H:write register A value (300) balance to memory

W:add 300 to register B (500+300=800)

W:write register B value(800) to memory

In the execution sequence above both thread reads $500 from memory and stores in their registers, then they add or subtract balance to the balance and write the result back to memory. But due the above sequence instead of $600, $800 will be stored in memory, which is incorrect.

Above example shows demo of race condition.

To prevent race condition, it should be made sure that only one thread enters critical section at a time. Locks should be implemented in critical section. Once a thread enters critical section, it should be locked and unless this thread releases the lock, other threads should not be able to access the critical section , this would guarantee that only one thread executes at a time in critical sectoin, thus preventing race condition from occuring.

Please give a thumbs up if you liked the solution