Race Condition in Operating Systems

Race Condition in Operating Systems

A Race Condition in OS is a scenario that occurs in a multithreaded environment due to multiple threads sharing the same resource or executing the same piece of code.
When the concurrently executing threads access the same shared data, it may affect the consistency of the data item. The outcome of the execution depends on the particular order in which the statements are executed.

That must have gone over your head. Let me break things simpler for you.
Firstly, what happens at the CPU level when there is an instruction count = count + 1 ?
It is slightly more than just incrementing the value of count by 1. There are 2 steps involved,
Step 1. temp = count + 1
Step 2. count = temp

In the CPU, initially count + 1 is stored in a temporary variable, let's call it temp and then the value of the temp variable is assigned to count.

Now that we know how increment happens at the CPU level, let us understand the Race Condition through an example,

Screenshot 2022-07-06 211000.png

Suppose there are 4 Aadhaar offices and people at each office want to register their identity into the main database. Let's call this database a Shared resource or Critical Section and let all the 4 offices where the identity of an individual is updated be Threads.
Suppose, the value of the count is initially 10. Now there are requests from A center and B center simultaneously to update a citizen count.
Imagine A's request {count++} was accepted first by the CPU or in our example, the Aaadhar Center database. As per the two steps mentioned above, the value of temp will be 11 i.e.,

temp = count + 1 
temp = 10 + 1 
temp = 11

Right at this point, when the value of temp = 11 and the count variable has not been updated to 11, assume that Context Switch takes place and the CPU will accept B's request for updating the count {count++} of a citizen from the center B.
The following will take place,
temp = count + 1
Note that count's value is still 10 not 11 because context switich took place before the assignment of temp value to count variable
temp = 10 + 1
temp = 11

After both A and B's requests are accepted, count in the database will be updated. count = temp
but the temp value is 11.
count = 11
Now evaluate this situation where there were two requests from persons updating their counts but the final value at the database is 11 which should have been 12.

We can clearly see that we lost the count of one person and imagine there are hundreds of requests coming from all over the country; inaccuracy in the data will be very high. This inconsistency in accessing resources led to data inconsistency.

To sum up, a race condition occurs when two or more threads can access shared data and try to change it at the same time. Because the thread scheduling algorithm can swap between threads at any point, we don't know the order in which threads will attempt to access the shared data (or critical section).
As a result, the change in data is dependent on the thread scheduling algorithm, that is to say, both threads are Racing to access/change the data. This is how the name Race Condition came into the picture.




Thanks for reading till the end. I hope you liked this blog and do share your thoughts in the comments section below.
Have a great day!