Race Condition

Q1. What is meant by ‘Race Condition’? How do they occur?

Ans. In some operating systems, processes that are working together may share some common storage that each one can read and write. The shared storage may be in main memory or it may be a shared file. The location of the shared memory does not change the nature of the communication or the problems that arise.

To see how interprocess communication works in practice, let us consider a simple but common example: a print spooler. When a process wants to print a file, it enters the file name in a special spooler directory.

Imagine that out spooler directory has a very large number of slots, numbered 0, 1, 2, ……, each one capable of holding a file name. also imagine that there are two shared variables, out, which points to the next file to be printed, and in, which points to the next free slot in the directory. These two variables might well be kept on a two-word file available to all processes. At a certain instant, slots 0 to 3 are empty and slots 4 to 6 are full. More or less simultaneously, processes A and B decide they want to queue a file for printing.

race condition

Figure: Two processes want to access shared memory at the same time


Process A reads ‘in’ and stores the value, 7, in a local variable called next_free_slot. Just then a clock interrupt occurs and the CPU decides that process A has run long enough, so it switches to process B. process B also reads ‘in’, and also gets a 7. It too stores it in its local vatiable next_free_slot. At this instant both processes think that the next available slot is 7.

Process B now continues to run. It stores the name of its file in slot 7 and updates ‘in’ to be an 8. Then it goes off and does other things.

Eventually, process A runs again, starting from the place it left off. It looks at next_free_slot, finds a 7 there, and writes its file name in slot 7, erasing the name that process B just put there. Then it computes next_free_slot+1, which is 8, and set ‘in’ to 8. The spooler directory is now internally consistent, so the printer will not notice anything wrong, but process B will never receive any output. User B will hang around the printer room for years, wistfully hoping for output that never comes.

Situations like this, where two or more processes are reading for writing some shared data and the final result depends on who runs precisely when, are called race conditions.

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: