Race Condition
Race Condition
A race condition occurs when two or more processes attempt to modify a shared resource simultaneously, leading to unpredictable behavior. In this example, we create two processes that increment a shared counter without proper synchronization, resulting in incorrect values.
•Initially Process P1 starts,
•X Takes value of shared (i.e. 7)
•X increments by 1 (x=8)
•Process P1 sleeps (paused) for 1 second, note that during this time CPU can not be ideal as there is another process P2 ready to execute.
•Note: status record of P1 is stored in PCB (Process control block)
•P2 starts.
• y holds shared values (i.e. 7)
•Y decrements by 1 (y=6)
•P2 sleep for 1 second, as it happens P1 process resumes its execution from step no 4.(Just because of PCB status P1 starts from step4)
•Shared=x (i.e. 8)
•Now P2 resumes back from step 4
•Shared =y (i.e. 6)
•Note: that here neither of values (6 & 8 ) of shared is true because multiple process sharing same resource, the condition called here as race condition. This is occurred because two processes share a resource without synchronization.