Penguin

Differences between current version and predecessor to the previous major change of flock(2).

Other diffs: Previous Revision, Previous Author, or view the Annotated Edit History

Newer page: version 2 Last edited on Sunday, November 3, 2002 10:23:04 pm by WikiAdmin
Older page: version 1 Last edited on Tuesday, June 4, 2002 12:23:40 am by perry Revert
@@ -1,107 +1,119 @@
-FLOCK  
-!!!FLOCK  
-NAME  
-SYNOPSIS  
-DESCRIPTION  
-RETURN VALUE  
-ERRORS  
-CONFORMING TO  
-NOTES  
-SEE ALSO  
-----  
 !!NAME 
+flock - apply or remove an advisory lock on an open file  
  
-  
-flock - apply or remove an advisory lock on an open file  
 !!SYNOPSIS 
+ __#include <sys/file.h>__  
+ __int flock(int__ fd__, int__ operation__)__  
  
-  
-__#include __  
-  
-  
-__int flock(int__ fd__, int__  
-operation__)__  
 !!DESCRIPTION 
+Apply or remove an advisory lock on an open file. The file is specified by ''fd''. Valid operations are given below:  
+;LOCK_SH: Shared lock. More than one process may hold a shared lock for a given file at a given time.  
+;LOCK_EX: Exclusive lock. Only one process may hold an exclusive lock for a given file at a given time.  
+;LOCK_UN: Unlock.  
  
+;LOCK_NB: Don't block when locking. May be specified (by ''or'''ing) along with one of the other operations.  
+;:A single file may not simultaneously have both shared and exclusive locks.  
+;: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.  
  
-Apply or remove an advisory lock on an open file. The file  
-is specified by ''fd ''. Valid operations are given  
-below:  
+!!RETURN VALUE  
+On success, zero is returned. On error, -1 is returned, and ''errno '' is set appropriately
  
+!!ERRORS  
+;[EWOULDBLOCK]: The file is locked and the __LOCK_NB__ flag was selected.  
  
-LOCK_SH  
+!!CONFORMING TO  
+4.4BSD (the flock(2) call first appeared in 4.2BSD).  
  
+!!NOTES  
+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.  
  
-Shared lock. More than one process may hold a shared lock  
-for a given file at a given time
+flock(2) and fcntl(2) locks have different semantics with respect to forked processes and dup(2)
  
+!!EXAMPLE  
+ /*  
+ * This program creates and locks /tmp/flock.example, waiting for the user  
+ * before unlocking the file again.  
+ *  
+ * By running this program twice simultaniously, locking can be demonstrated.  
+ *  
+ */  
  
-LOCK _EX  
+ #include <sys/file.h> /* for flock(2) */  
+ #include <sys/stat.h> /* for S _* constants */  
+ #include <string.h> /* for strerror(3) prototype */  
+ #include <stdio.h> /* for fprintf(3),printf(3),stderr protype */  
+ #include <errno.h> /* for errno prototype */  
+ #include <unistd.h> /* for close(2) prototypes */  
  
+ #define FILENAME "/tmp/flock.example"  
  
-Exclusive lock. Only one process may hold an exclusive lock  
-for a given file at a given time.  
+ int main(int argc,char **argv)  
+ {  
+ int fd;  
+ char buf;  
  
+ fd = open(FILENAME,O_RDWR|O_CREAT,S_IRUSR|S_IWUSR);  
+ if (fd==-1) {  
+ fprintf(  
+ stderr,  
+ "open(\"%s\",O_RDWR,S_IRUSR|S_IWUSR): %s (%i)\n",  
+ FILENAME,  
+ strerror(errno),  
+ errno);  
+ return 1;  
+ }  
  
-LOCK_UN  
+ printf("Aquiring lock...");  
+ fflush(stdout);  
  
+ /* Aquire an exclusive lock */  
+ if (flock(fd,LOCK_EX) == -1) {  
+ fprintf(  
+ stderr,  
+ "flock(fd,LOCK_EX): %s (%i)\n",  
+ strerror(errno),  
+ errno);  
+ return 1;  
+ }  
+ printf("Succeeded!\n");  
  
-Unlock
+ printf("Press enter to release the lock .\n");  
  
+ if (read(0,&buf,sizeof(buf)) == -1) {  
+ printf("read(0,&buf,sizeof(buf)): %s (%i)\n",  
+ strerror(errno),  
+ errno  
+ );  
+ return 1;  
+ }  
  
-LOCK_NB  
+ printf("Releasing lock...");  
+ fflush(stdout);  
  
+ if (flock(fd,LOCK_UN)==-1) {  
+ fprintf(  
+ stderr,  
+ "flock(fd,LOCK_UN): %s (%i)\n",  
+ strerror(errno),  
+ errno);  
+ return 1;  
+ }  
  
-Don't block when locking. May be specified (by  
-''or'''ing ) along with one of the other  
-operations.  
+ printf ("Released!\n" );  
  
+ if (close(fd)==-1) {  
+ fprintf(  
+ stderr,  
+ "close(fd): %s (%i)\n",  
+ strerror(errno),  
+ errno);  
+ return 1;  
+ }  
  
-A single file may not simultaneously have both shared and  
-exclusive locks.  
+ return ;  
+ }  
  
-  
-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.  
-!!RETURN VALUE  
-  
-  
-On success, zero is returned. On error, -1 is returned, and  
-''errno'' is set appropriately.  
-!!ERRORS  
-  
-  
-__EWOULDBLOCK__  
-  
-  
-The file is locked and the __LOCK_NB__ flag was  
-selected.  
-!!CONFORMING TO  
-  
-  
-4.4BSD (the flock(2) call first appeared in  
-4.2BSD).  
-!!NOTES  
-  
-  
-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.  
-  
-  
-flock(2) and fcntl(2) locks have different  
-semantics with respect to forked processes and  
-dup(2).  
 !!SEE ALSO 
+open(2), close(2), dup(2), execve(2), fcntl(2), fork(2), lockf(3)  
  
-  
-open(2), close(2), dup(2),  
-execve(2), fcntl(2), fork(2),  
-lockf(3)  
-  
-  
- There are also ''locks.txt'' and ''mandatory.txt'' in  
- ''/usr/src/linux/Documentation''.  
-----  
+There are also ''locks.txt'' and ''mandatory.txt'' in ''/usr/src/linux/Documentation''. 
This page is a man page (or other imported legacy content). We are unable to automatically determine the license status of this page.