Chapter 55: File Locking
Previous chapters have covered various techniques that processes can use to syn-
chronize their actions, including signals (Chapters 20 to 22) and semaphores
(Chapters 47 and 53). In this chapter, we look at further synchronization tech-
niques designed specifically for use with files.
55.1 Overview
A frequent application requirement is to read data from a file, make some change
to that data, and then write it back to the file. As long as just one process at a time
ever uses a file in this way, then there are no problems. However, problems can
arise if multiple processes are simultaneously updating a file. Suppose, for example,
that each process performs the following steps to update a file containing a
sequence number:
- Read the sequence number from the file.
- Use the sequence number for some application-defined purpose.
- Increment the sequence number and write it back to the file.
The problem here is that, in the absence of any synchronization technique, two
processes could perform the above steps at the same time with (for example) the
consequences shown in Figure 55-1 (here, we assume that the initial value of the
sequence number is 1000).