#include <unistd.h> #include <fcntl.h> int fcntl(int fd , int cmd ); int fcntl(int fd , int cmd , long arg ); int fcntl(int fd , int cmd , struct flock * lock );
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) */
...
};
The
l_whence ", " l_start ", and " l_len fields of this structure specify the range of bytes we wish to lock.
l_start is the starting offset for the lock, and is interpreted
relative to either:
the start of the file (if
l_whence is
SEEK_SET ); the current file offset (if
l_whence is
SEEK_CUR ); or the end of the file (if
l_whence is
SEEK_END ). In the final two cases,
l_start can be a negative number provided the
offset does not lie before the start of the file.
l_len is a non-negative integer (but see the NOTES below) specifying
the number of bytes to be locked.
Bytes past the end of the file may be locked,
but not bytes before the start of the file.
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.
The
l_type field can be used to place a read
or a write
lock on a file.
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). An exclusive lock excludes all other locks,
both shared and exclusive.
A single process can hold only one type of lock on a file region;
if a new lock is applied to an already-locked region,
then the existing lock is converted to the the new lock type.
(Such conversions may involve splitting, shrinking, or coalescing with
an existing lock if the byte range specified by the new lock does not
precisely coincide with the range of the existing lock.)
| ---- | |
| lB l. | |
| Bit | Description (event in directory) |
| DN_ACCESS | A file was accessed (read, pread, readv) |
| DN_MODIFY | A file was modified (write, pwrite, |
| writev, truncate, ftruncate) | |
| DN_CREATE | A file was created (open, creat, mknod, |
| mkdir, link, symlink, rename) | |
| DN_DELETE | A file was unlinked (unlink, rename to |
| another directory, rmdir) | |
| DN_RENAME | A file was renamed within this |
| directory (rename) | |
| DN_ATTRIB | The attributes of a file were changed |
| (chown, chmod, utime[s]) |