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 (possibly in a kernel data structure) 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 inter-process 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. 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.
- Imagine that 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.
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. 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 or writing some shared data and the final result depends on who runs precisely when are called race conditions.
Debugging programs containing race conditions is no fun at all.
The results of most test runs are fine, but once in a rare while something weird and unexplained happens.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.