ptg10805159
Section 14.3 RecordLocking 495
Assuming that we are at end of file when we perform the firstwrite,this operation
will extend the file by one byte, and that byte will be locked. The unlock operation that
follows has the effect of removing the locks for futurewrites that append data to the
file, but it leaves a lock on the last byte in the file. When the second write occurs, the
end of file is extended by one byte, but this byte is not locked. The state of the file locks
for this sequence of steps is shown in Figure14.10.
first
byte
written
locked
state of file after firstwrite
first
byte
written
second
byte
written
locked
state of file after secondwrite
Figure 14.10File range lock diagram
When a portion of a file is locked, the kernel converts the offset specified into an
absolute file offset. In addition to specifying an absolute file offset (SEEK_SET),fcntl
allows us to specify this offset relative to a point in the file: current (SEEK_CUR) or end
of file (SEEK_END). The kernel needs to remember the locks independent of the current
file offset or end of file, because the current offset and end of file can change, and
changes to these attributes shouldn’t affect the state of existing locks.
If we intended to remove the lock covering the byte we wrote in the first write, we
could have specified the length as−1. Negative length values represent the bytes before
the specified offset.
Advisor y versus MandatoryLocking
Consider a library of database access routines. If all the functions in the library handle
recordlocking in a consistent way,then we say that any set of processes using these
functions to access a database arecooperating processes.It is feasible for these database
access functions to use advisory locking if they arethe only ones being used to access
the database. But advisory locking doesn’t prevent some other process that has write
permission for the database file from writing whatever it wants to the database file.
This rogue process would be an uncooperating process, since it’s not using the accepted
method (the library of database functions) to access the database.
Mandatory locking causes the kernel to check everyopen,read,andwriteto
verify that the calling process isn’t violating a lock on the file being accessed.
Mandatory locking is sometimes calledenforcement-mode locking.