Race Conditions in Operating System:


Definition and Explanation:

The race condition is the situation where several processes access and manipulate shared data concurrently. The final value of the shared data depends upon which process finishes last. To prevent race conditions, concurrent processes must be synchronized.

Example:


Consider the following simple procedure:


void deposit (int amount)


{


balance = balance + amount;


}


Where we assume that balance is a shared variable. If two processes try to call the process deposit concurrently, something very bad can happen. The single statement "balance = balance + amount" is actually implemented by a sequence of instructions such as:


Load Reg, balance // loads the previous balance in register
Add         Reg, amount //adds the amount
Store Reg, balance // updates the calculated amount back in balance
Suppose process PI calls deposit (10) and process P2 calls deposit (20). If one completes before the other starts, the combined effect is to add 30 to the balance. However, the calls may happen at exactly the same time. Suppose the initial balance is 100, and the two processes run on different CPUs. One possible result is



P1 loads 100 into its register
P2 loads 100 into its register
P1 adds 10 to its register, giving 110
P2 adds 20 to its register, giving 120
P1 stores 110 in balance
P2 stores 120 in balance


and the final effect is to add only 20 to the balance.


This kind of bug is called a race condition. It only occurs under certain timing conditions. It is very difficult to track down it since it may disappear when you try to debug it. It may be nearly impossible to detect from testing since it may occur very rarely. The only way to deal with race conditions is through very careful coding. The systems that support processes contain constructs called synchronization primitives to avoid these problems.

{ 1 comments... read them below or add one }

Blog Archive

Powered by Blogger.

- Copyright © 2013 Taqi Shah Blogspot -Metrominimalist- Powered by Blogger - Designed by Johanes Djogan -