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

Example: a print spooler. When a process wants to print a file, it enters the file name in a special spooler directory.

Another process, the printer daemon, periodically checks to see if there are any files to be printed, and if there are, it prints them and then removes their names from the directory.

Consider our 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.


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 variable 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 sets in to 8. The spooler directory is now internally consistent, so the printer daemon will not notice anything wrong, but process B will never receive any output.