DOCUMENT ID: 1491-02 SYNOPSIS: A description of Advisory Locking on Solaris 2.x OS RELEASE: Solaris 2.x PRODUCT: KEYWORDS: advisory lock fnctl file process DESCRIPTION: Description of Advisory Locking on Solaris 2.x SOLUTION: Advisory Locking on Solaris 2.x Advisory locking: The fcntl system call provides the programmer with file and record locking facilities. A library function called lockf(3C) is also provided which implements similar facilities although its underlying mechanisms use fcntl. A lock may be placed on a whole file or on portions (segments) of it. To place a lock, the programmer specifies a byte offset in the file that the lock is to extend to. Thus, to lock an entire file the programmer specifies that the lock begins at byte zero and extends to the end of the file. The system allows a process to lock a file only if the file being referenced is not already locked (permissions permitting). If the file is locked, the process requesting the lock is put to sleep until the lock is removed. VOP_FRLOCK(), an option with fcntl allows the programmer to lock a file without blocking, That is, the programmer can specify that the system call is to return if the file is locked rather than wait for it to become unlocked. The programmer can also specify if a lock is to be enforced for writes, reads, or both. fcntl is also used to remove a lock. A read lock prevents another process from placing a write lock on the locked area within the file. However, several read locks can be placed within the file so long as their segments do not overlap. On the other hand, a write lock is exclusive. That is, it prevents another process from placing a read or write lock on the file. Furthermore, only one write lock is allowed on a segment but the write lock is not allowed if the segment already has a read lock on it. Of course, none of these operations can be performed on a file unless a valid file descriptor has first been obtained for it (i.e., via open, create etc). The file must be opened with the correct modes before a read or write lock on can be placed on a segment within it. For example, to place a write lock on a file, the file must first be opened for writing. Similarly, to place a read lock on a file, the file must first be opened for reading. Locking services provided by fcntl are useful only if the programmer implements them in his or her programs. Therefore, if cooperating programs are expected to access or modify the contents of the same file, this locking service is recommended. It is because of this that the locking features of fcntl are collectively referred to as advisory file and record locking. However, an advisory lock, as its name implies, is "advisory". That is, a program (if programmed to do so) may ignore the lock and read or write the data regardless. DATE APPROVED: 09/05/95