linux 文件区域锁

F_GETLK, F_SETLK and F_SETLKW are used to acquire,  release,  and  test
       for  the existence of record locks (also known as file-segment or file-
       region locks).  The third argument, lock, is a pointer to  a  structure
       that has at least the following fields (in unspecified order).

           struct flock {
               ...
               short l_type;    /* Type of lock: F_RDLCK,
                                   F_WRLCK, F_UNLCK */
               short l_whence;  /* How to interpret l_start:
                                   SEEK_SET, SEEK_CUR, SEEK_END */
               off_t l_start;   /* Starting offset for lock */
               off_t l_len;     /* Number of bytes to lock */
               pid_t l_pid;     /* PID of process blocking our lock
                                   (F_GETLK only) */
               ...
           };

Specifying 0  for  l_len  has  the  special
       meaning:  lock all bytes starting at the location specified by l_whence
       and l_start through to the end of file, no matter how  large  the  file
       grows.

Any number of processes may hold a read lock
       (shared lock) on a file region, but only one process may hold  a  write
       lock  (exclusive  lock).

 A single process can hold only one type  of
       lock  on  a  file region;

 

Each byte in a file can have only a single type of lock on it at any one time, and may be locked for shared
access, locked for exclusive access, or unlocked.

 

l_type Either F_RDLCK for a shared (read-only) lock or F_WRLCK for an exclusive
(write) lock

F_RDLCK A shared (or “read”) lock. Many different processes can have a shared lock
on the same (or overlapping) regions of the file. If any process has a shared
lock, then no process will be able to get an exclusive lock on that region. In
order to obtain a shared lock, the file must have been opened with read or
read/write access.
F_UNLCK Unlock; used for clearing locks
F_WRLCK An exclusive (or “write”) lock. Only a single process may have an exclusive
lock on any particular region of a file. Once a process has such a lock, no other
process will be able to get any sort of lock on the region. To obtain an exclusive
lock, the file must have been opened with write or read/write access.

你可能感兴趣的:(linux 文件区域锁)