flock(2) System Calls Manual flock(2) flock - (advisory) LIBRARY Standard C library (libc, -lc) #include int flock(int fd, int operation); , fd. operation : LOCK_SH (shared) . . LOCK_EX (exclusive) . . LOCK_UN , . flock() , . , LOCK_NB ( ) . . , flock(), ( open(2)). , (, , fork(2) dup(2)) , . , LOCK_UN , . open(2) ( ) , flock() . - , . ( ). flock() . , flock(), execve(2). , . On success, zero is returned. On error, -1 is returned, and errno is set to indicate the error. EBADF fd . EINTR , ; signal(7). EINVAL operation. ENOLCK . EWOULDBLOCK LOCK_NB. Since Linux 2.0, flock() is implemented as a system call in its own right rather than being emulated in the GNU C library as a call to fcntl(2). With this implementation, there is no interaction between the types of lock placed by flock() and fcntl(2), and flock() does not detect deadlock. (Note, however, that on some systems, such as the modern BSDs, flock() and fcntl(2) locks do interact with one another.) CIFS Up to Linux 5.4, flock() is not propagated over SMB. A file with such locks will not appear locked for remote clients. Since Linux 5.5, flock() locks are emulated with SMB byte-range locks on the entire file. Similarly to NFS, this means that fcntl(2) and flock() locks interact with one another. Another important side-effect is that the locks are not advisory anymore: any IO on a locked file will always fail with EACCES when done from a separate file descriptor. This difference originates from the design of locks in the SMB protocol, which provides mandatory locking semantics. Remote and mandatory locking semantics may vary with SMB protocol, mount options and server type. See mount.cifs(8) for additional information. BSD. 4.4BSD ( flock() 4.2BSD). flock() UNIX, fcntl(2). NFS Up to Linux 2.6.11, flock() does not lock files over NFS (i.e., the scope of locks was limited to the local system). Instead, one could use fcntl(2) byte-range locking, which does work over NFS, given a sufficiently recent version of Linux and a server which supports locking. Linux 2.6.12, NFS flock(), fcntl(2) . , fcntl(2) flock() NFS. , . Linux 2.6.37, , flock() ( fcntl(2)) ; local_lock nfs(5). flock() ; , flock() /. flock() fcntl(2) dup(2). , flock() fcntl(2), flock() . ( ) : , . , , , , , LOCK_NB ( BSD ). . flock(1), close(2), dup(2), execve(2), fcntl(2), fork(2), open(2), lockf(3), lslocks(8) Documentation/filesystem/locks.txt Linux ( -- Documentation/locks.txt) Azamat Hackimov , Dmitry Bolkhovskikh , Yuri Kozlov ; GNU 3 , . . , , . Linux man-pages 6.06 31 2023 . flock(2)