Linux File Lock key points-general Linux technology-Linux programming and kernel information, the following is a detailed description. Du XiaoGang
File lock is one of the most common inter-process synchronization mechanisms in Linux. Compared with sysv semaphores, file locks are easier to use (the trouble of sysv semaphores lies in its key Acquisition Mechanism ).
APUE provides a detailed description of the file lock. Note the following points. For the original article, see section 14.3 of APUE2:
1. The file lock (file_lock) is bound to the file (specifically the file inode) and process. The file_lock structure of the file in the Linux implementation is mounted to the inode structure corresponding to the file, the file_lock structure contains the pid of the process to which the lock belongs. This implementation method has two consequences:
A. When a process exits, all file locks belonging to the process are released.
B. When any fd or file to which the lock process belongs is disabled, the lock is released. This is because the file_lock structure is mounted on inode rather than fd or file (the next stop of fd in the kernel structure), so it is impossible to distinguish the fd or file corresponding to the file lock. Note that two examples are listed on APUE. The first one is fd disabled, and the second one is file disabled. Both cases will cause lock release: