File Locking 1133
As we see from the last line of output, this allowed process A’s blocked lock request
to be granted.
It is important to realize that even though process B’s deadlocked request was
canceled, it still held its other lock, and so process A’s queued lock request
remained blocked. Process A’s lock request is granted only when process B removes
its other lock, bringing about the situation shown in part d of Figure 55-5.
Figure 55-5: State of granted and queued lock requests while running i_fcntl_locking.c
55.3.3 Example: A Library of Locking Functions
Listing 55-3 provides a set of locking functions that we can use in other programs.
These functions are as follows:
z The lockRegion() function uses F_SETLK to place a lock on the open file referred
to by the file descriptor fd. The type argument specifies the lock type (F_RDLCK or
F_WRLCK). The whence, start, and len arguments specify the range of bytes to lock.
These arguments provide the values for the similarly named fields of the
flockstr structure that is used to place the lock.
z The lockRegionWait() function is like lockRegion(), but makes a blocking lock
request; that is, it uses F_SETLKW, rather than F_SETLK.
Granted lock Queued lock
PID=790, type=READ
0 39
PID=800, type=READ
70 99
(^0) File Offset 99
PID=790, type=READ
0 39
PID=800, type=READ
70 99
PID=790, type=WRITE
0 99
a)
b)
PID=790, type=READ
0 39
PID=800, type=READ
70 99
PID=790, type=WRITE
0 99
c)
PID=800, type=WRITE (canceled because of deadlock)
0 99
PID=790, type=WRITE
(^099)
d)