Understanding Lock Types

There are two basic forms of file locking. They are

  • Lock files

  • Locked regions

The first form requires that a process create and open a lock file before it writes to the protected data file. If a process fails to create the lock file, then it sleeps for a while and tries again.

For example, if the data file database.dat is the data file, then the lock file might be named database.lck. The contents of the lock file are not important to the procedure, and it may even be empty. When the updating process has finished with its update of database.dat, then the lock file database.lck is released. This method works only when all processes cooperate and obey this procedure.

The UNIX kernel also will permit a process to lock regions of a data file. A region consists of one or more bytes at a specified starting offset. The offset can extend beyond the end of the current file size. In this way, all processes agree to tell the kernel which regions of the file they are about to update. If a requested lock region is in conflict with presently granted locks on that file, the requesting process is put to sleep until the conflict is removed. When all processes obey this procedure, the integrity of the file is preserved. Figure 5.1 shows four processes that want to update one data file concurrently.

Figure 5.1. Three locked file regions and one pending request to lock a file region.


Process IDs 1000, 1001, and 1002 in Figure 5.1 have their regions locked within the data file. The UNIX kernel grants these locks because they do not overlap. This allows these three processes to update the same file concurrently. Notice that process ID 1003 has requested a lock for a region that conflicts with a presently locked Region 3, owned by process ID 1002. Consequently, process 1003 sleeps until Region 3 becomes unlocked.

File locking under UNIX occurs under one of two lock enforcement models:

Advisory locking—No enforcement
Mandatory locking—Enforced locking

The lock file and lock region methods just discussed require process cooperation to maintain the integrity of the data file. Cooperative locking methods are known as advisory locking. The UNIX kernel cannot enforce such cooperative methods. Consequently, when advisory locking methods are employed, processes that disobey the locking convention can corrupt the data file.

Many UNIX kernels also support mandatory locking of files. When a process attempts to write to a region of a file that has enforced locking enabled, all other processes are prevented from interfering. Similarly, the writing process is blocked from executing until its conflicts with other processes have vanished.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.161.222