Transfer from http://www.cnblogs.com/hjslovewcl/archive/2011/03/14/2314333.html
There are three different file locks, three of which are "advisory", which means they rely on the
Cooperation, so it is very important for all programs in a project to blockade policy to be consistent when your program requires
You should take extra care when sharing files with third-party software.
Some programs use file lock files such as Filename.lock, and then simply test for the existence of such files. This method is obviously not very good, because when the file-generating process is killed, the lock file still exists, so that the file may be permanently locked. The process number PID of the generated file is stored in the UUCP file, but this is still not insured, because the use of PID is recycled.
Here are three file lock functions:
Flock ();
LOCKF ();
Fcntl ();
Flock () is derived from BSD, but is now available on most Unix systems, in a single master
On-machine flock () is simple and effective, but it cannot work on NFS. There's a little bit confusing in Perl, too.
Flock () function, but it is implemented inside Perl.
Fcntl () is the only POSIX-compliant file lock implementation and is therefore the only portable. It is also the same
Is the most powerful file lock--and the hardest to use. On an NFS file system, the FCNTL () request is passed
To the daemon called RPC.LOCKD, which is then responsible for the LOCKD conversation with the host side, and Flock ()
Different, fcntl () can implement blocking on the record layer.
LOCKF () is just a simplified fcntl () file lock interface.
Regardless of which file lock you use, be sure to remember to update all your files with sync before the lock takes effect
Input/output.
- Lock (FD);
- Write_to (Some_function_of (FD));
- Flush_output_to (FD); / * Rinse the output before going to the lock * /
- Unlock (FD);
- Do_something_else; / * Maybe another process will update it * /
- Lock (FD);
- Seek (fd, somewhere); / * Because the original file pointer is not secure * /
- Do_something_with (FD); ...
- Some useful fcntl () blocking methods (to omit error handling for brevity):
- #include <fcntl.h>;
- #include <unistd.h>;
- Read_lock (int fd)/ * A shared file lock on the entire file * /
- {
- Fcntl (FD, F_SETLKW, File_lock (F_rdlck, Seek_set));
- }
- Write_lock (int fd)/ * An exclusive file lock on the entire file * /
- {
- Fcntl (FD, F_SETLKW, File_lock (F_wrlck, Seek_set));
- }
- Append_lock (int fd)/ * A lock that closes the end of the file,
- Other processes can access existing content */
- {
- Fcntl (FD, F_SETLKW, File_lock (F_wrlck, seek_end));
- }
- The File_lock function used earlier is as follows:
- struct flock* file_lock (shorttype, short whence)
- {
- static struct flock ret;
- Ret.l_type = type;
- Ret.l_start = 0;
- Ret.l_whence = whence;
- Ret.l_len = 0;
- Ret.l_pid = Getpid ();
- return &ret;
- }
- Lock.c
- #include <stdio.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <string.h>
- struct flock* file_lock (shorttype, short whence)
- {
- static struct flock ret;
- Ret.l_type = type;
- Ret.l_start = 0;
- Ret.l_whence = whence;
- Ret.l_len = 0;
- Ret.l_pid = Getpid ();
- return &ret;
- }
- int main ()
- {
- int fd = open ("1.txt", o_wronly| O_append);
- For (int i=0; i<1000; ++i) {
- Fcntl (FD, F_SETLKW, File_lock (F_wrlck, Seek_set));
- char buf[1024] = {0};
- sprintf (buf, "Hello World%d/n", I);
- int len = strlen (BUF);
- Write (FD, buf, Len);
- Fcntl (FD, F_SETLKW, File_lock (F_unlck, Seek_set));
- Sleep (1);
- }
- Close (FD);
- }
- Lock2.c ... Compared with LOCK.C, only modified the next BUF content
- #include <stdio.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <string.h>
- struct flock* file_lock (shorttype, short whence)
- {
- static struct flock ret;
- Ret.l_type = type;
- Ret.l_start = 0;
- Ret.l_whence = whence;
- Ret.l_len = 0;
- Ret.l_pid = Getpid ();
- return &ret;
- }
- int main ()
- {
- int fd = open ("1.txt", o_wronly| O_append);
- For (int i=0; i<1000; ++i) {
- Fcntl (FD, F_SETLKW, File_lock (F_wrlck, Seek_set));
- char buf[1024] = {0};
- sprintf (buf, "China%d/n", i);
- int len = strlen (BUF);
- Write (FD, buf, Len);
- Fcntl (FD, F_SETLKW, File_lock (F_unlck, Seek_set));
- Sleep (1);
- }
- Close (FD);
- }
- g++ Lock.c-o 1
- g++ Lock2.c-o 2
- Execute two programs to see the mutex effect.
Unix/linux inter-process file lock