Linux
Microsoft Windows

What is a Race Condition in process syncronization?

Answer

Wiki User
12/06/2009

When two processes sharing a common variable try to update it simultaneously, one cannot predict the output of it, this is the race condition.A thread while updating the variable can be preempted by another thread and update it differently .This is why Synchronization mechanisms are used.


Ex:

X=5



Process 1: X=X+1
Process 2: X=X+2


Machine code is





LOAD EAX,MEMORY_X

ADD EAX , 1
MOV MEMORY_X,EAX


While process1 is executing second line it could be preempted ..And process2 takes turn so , MEMORY_X contains value 7,.Now Process1 comes back and starts the remaining lines of code .The memory then contains value 6.
Which is the final answer. But we should have got a value of 8 instead