| Rev | Author | # | Line |
|---|---|---|---|
| 1 | perry | 1 | !!NAME |
| 2 | WikiAdmin | 2 | flock - apply or remove an advisory lock on an open file |
| 1 | perry | 3 | |
| 4 | !!SYNOPSIS | ||
| 2 | WikiAdmin | 5 | __#include <sys/file.h>__ |
| 6 | __int flock(int__ fd__, int__ operation__)__ | ||
| 1 | perry | 7 | |
| 8 | !!DESCRIPTION | ||
| 2 | WikiAdmin | 9 | Apply or remove an advisory lock on an open file. The file is specified by ''fd''. Valid operations are given below: |
| 10 | ;LOCK_SH: Shared lock. More than one process may hold a shared lock for a given file at a given time. | ||
| 11 | ;LOCK_EX: Exclusive lock. Only one process may hold an exclusive lock for a given file at a given time. | ||
| 12 | ;LOCK_UN: Unlock. | ||
| 1 | perry | 13 | |
| 2 | WikiAdmin | 14 | ;LOCK_NB: Don't block when locking. May be specified (by ''or'''ing) along with one of the other operations. |
| 15 | ;:A single file may not simultaneously have both shared and exclusive locks. | ||
| 16 | ;:A file is locked (i.e., the inode), ''not'' the file descriptor. So, dup(2) and fork(2) do not create multiple instances of a lock. | ||
| 1 | perry | 17 | |
| 2 | WikiAdmin | 18 | !!RETURN VALUE |
| 19 | On success, zero is returned. On error, -1 is returned, and ''errno'' is set appropriately. | ||
| 1 | perry | 20 | |
| 2 | WikiAdmin | 21 | !!ERRORS |
| 22 | ;[EWOULDBLOCK]: The file is locked and the __LOCK_NB__ flag was selected. | ||
| 1 | perry | 23 | |
| 2 | WikiAdmin | 24 | !!CONFORMING TO |
| 25 | 4.4BSD (the flock(2) call first appeared in 4.2BSD). | ||
| 1 | perry | 26 | |
| 2 | WikiAdmin | 27 | !!NOTES |
| 28 | flock(2) does not lock files over NFS. Use fcntl(2) instead: that does work over NFS, given a sufficiently recent version of Linux and a server which supports locking. | ||
| 1 | perry | 29 | |
| 2 | WikiAdmin | 30 | flock(2) and fcntl(2) locks have different semantics with respect to forked processes and dup(2). |
| 1 | perry | 31 | |
| 2 | WikiAdmin | 32 | !!EXAMPLE |
| 33 | /* | ||
| 34 | * This program creates and locks /tmp/flock.example, waiting for the user | ||
| 35 | * before unlocking the file again. | ||
| 36 | * | ||
| 37 | * By running this program twice simultaniously, locking can be demonstrated. | ||
| 38 | * | ||
| 39 | */ | ||
| 1 | perry | 40 | |
| 2 | WikiAdmin | 41 | #include <sys/file.h> /* for flock(2) */ |
| 42 | #include <sys/stat.h> /* for S_* constants */ | ||
| 43 | #include <string.h> /* for strerror(3) prototype */ | ||
| 44 | #include <stdio.h> /* for fprintf(3),printf(3),stderr protype */ | ||
| 45 | #include <errno.h> /* for errno prototype */ | ||
| 46 | #include <unistd.h> /* for close(2) prototypes */ | ||
| 1 | perry | 47 | |
| 2 | WikiAdmin | 48 | #define FILENAME "/tmp/flock.example" |
| 1 | perry | 49 | |
| 2 | WikiAdmin | 50 | int main(int argc,char **argv) |
| 51 | { | ||
| 52 | int fd; | ||
| 53 | char buf; | ||
| 1 | perry | 54 | |
| 2 | WikiAdmin | 55 | fd = open(FILENAME,O_RDWR|O_CREAT,S_IRUSR|S_IWUSR); |
| 56 | if (fd==-1) { | ||
| 57 | fprintf( | ||
| 58 | stderr, | ||
| 59 | "open(\"%s\",O_RDWR,S_IRUSR|S_IWUSR): %s (%i)\n", | ||
| 60 | FILENAME, | ||
| 61 | strerror(errno), | ||
| 62 | errno); | ||
| 63 | return 1; | ||
| 64 | } | ||
| 1 | perry | 65 | |
| 2 | WikiAdmin | 66 | printf("Aquiring lock..."); |
| 67 | fflush(stdout); | ||
| 1 | perry | 68 | |
| 2 | WikiAdmin | 69 | /* Aquire an exclusive lock */ |
| 70 | if (flock(fd,LOCK_EX) == -1) { | ||
| 71 | fprintf( | ||
| 72 | stderr, | ||
| 73 | "flock(fd,LOCK_EX): %s (%i)\n", | ||
| 74 | strerror(errno), | ||
| 75 | errno); | ||
| 76 | return 1; | ||
| 77 | } | ||
| 78 | printf("Succeeded!\n"); | ||
| 1 | perry | 79 | |
| 2 | WikiAdmin | 80 | printf("Press enter to release the lock.\n"); |
| 1 | perry | 81 | |
| 2 | WikiAdmin | 82 | if (read(0,&buf,sizeof(buf)) == -1) { |
| 83 | printf("read(0,&buf,sizeof(buf)): %s (%i)\n", | ||
| 84 | strerror(errno), | ||
| 85 | errno | ||
| 86 | ); | ||
| 87 | return 1; | ||
| 88 | } | ||
| 1 | perry | 89 | |
| 2 | WikiAdmin | 90 | printf("Releasing lock..."); |
| 91 | fflush(stdout); | ||
| 1 | perry | 92 | |
| 2 | WikiAdmin | 93 | if (flock(fd,LOCK_UN)==-1) { |
| 94 | fprintf( | ||
| 95 | stderr, | ||
| 96 | "flock(fd,LOCK_UN): %s (%i)\n", | ||
| 97 | strerror(errno), | ||
| 98 | errno); | ||
| 99 | return 1; | ||
| 100 | } | ||
| 1 | perry | 101 | |
| 2 | WikiAdmin | 102 | printf("Released!\n"); |
| 1 | perry | 103 | |
| 2 | WikiAdmin | 104 | if (close(fd)==-1) { |
| 105 | fprintf( | ||
| 106 | stderr, | ||
| 107 | "close(fd): %s (%i)\n", | ||
| 108 | strerror(errno), | ||
| 109 | errno); | ||
| 110 | return 1; | ||
| 111 | } | ||
| 1 | perry | 112 | |
| 2 | WikiAdmin | 113 | return 0; |
| 114 | } | ||
| 1 | perry | 115 | |
| 116 | !!SEE ALSO | ||
| 2 | WikiAdmin | 117 | open(2), close(2), dup(2), execve(2), fcntl(2), fork(2), lockf(3) |
| 1 | perry | 118 | |
| 2 | WikiAdmin | 119 | There are also ''locks.txt'' and ''mandatory.txt'' in ''/usr/src/linux/Documentation''. |
lib/blame.php:177: Warning: Invalid argument supplied for foreach() (...repeated 4 times)