Unix/linux inter-process file lock

Source: Internet
Author: User
Tags flock sprintf

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.

  1. Lock (FD);
  2. Write_to (Some_function_of (FD));
  3. Flush_output_to (FD); / * Rinse the output before going to the lock * /
  4. Unlock (FD);
  5. Do_something_else; / * Maybe another process will update it * /
  6. Lock (FD);
  7. Seek (fd, somewhere); / * Because the original file pointer is not secure * /
  8. Do_something_with (FD); ...
  9. Some useful fcntl () blocking methods (to omit error handling for brevity):
  10. #include <fcntl.h>;
  11. #include <unistd.h>;
  12. Read_lock (int fd)/ * A shared file lock on the entire file * /
  13. {
  14. Fcntl (FD, F_SETLKW, File_lock (F_rdlck, Seek_set));
  15. }
  16. Write_lock (int fd)/ * An exclusive file lock on the entire file * /
  17. {
  18. Fcntl (FD, F_SETLKW, File_lock (F_wrlck, Seek_set));
  19. }
  20. Append_lock (int fd)/ * A lock that closes the end of the file,
  21. Other processes can access existing content */
  22. {
  23. Fcntl (FD, F_SETLKW, File_lock (F_wrlck, seek_end));
  24. }
  25. The File_lock function used earlier is as follows:
  26. struct flock* file_lock (shorttype, short whence)
  27. {
  28. static struct flock ret;
  29. Ret.l_type = type;
  30. Ret.l_start = 0;
  31. Ret.l_whence = whence;
  32. Ret.l_len = 0;
  33. Ret.l_pid = Getpid ();
  34. return &ret;
  35. }
  1. Lock.c
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <fcntl.h>
  5. #include <string.h>
  6. struct flock* file_lock (shorttype, short whence)
  7. {
  8. static struct flock ret;
  9. Ret.l_type = type;
  10. Ret.l_start = 0;
  11. Ret.l_whence = whence;
  12. Ret.l_len = 0;
  13. Ret.l_pid = Getpid ();
  14. return &ret;
  15. }
  16. int main ()
  17. {
  18. int fd = open ("1.txt", o_wronly|  O_append);
  19. For (int i=0; i<1000; ++i) {
  20. Fcntl (FD, F_SETLKW, File_lock (F_wrlck, Seek_set));
  21. char buf[1024] = {0};
  22. sprintf (buf, "Hello World%d/n", I);
  23. int len = strlen (BUF);
  24. Write (FD, buf, Len);
  25. Fcntl (FD, F_SETLKW, File_lock (F_unlck, Seek_set));
  26. Sleep (1);
  27. }
  28. Close (FD);
  29. }
  30. Lock2.c ... Compared with LOCK.C, only modified the next BUF content
  31. #include <stdio.h>
  32. #include <unistd.h>
  33. #include <fcntl.h>
  34. #include <string.h>
  35. struct flock* file_lock (shorttype, short whence)
  36. {
  37. static struct flock ret;
  38. Ret.l_type = type;
  39. Ret.l_start = 0;
  40. Ret.l_whence = whence;
  41. Ret.l_len = 0;
  42. Ret.l_pid = Getpid ();
  43. return &ret;
  44. }
  45. int main ()
  46. {
  47. int fd = open ("1.txt", o_wronly|  O_append);
  48. For (int i=0; i<1000; ++i) {
  49. Fcntl (FD, F_SETLKW, File_lock (F_wrlck, Seek_set));
  50. char buf[1024] = {0};
  51. sprintf (buf, "China%d/n", i);
  52. int len = strlen (BUF);
  53. Write (FD, buf, Len);
  54. Fcntl (FD, F_SETLKW, File_lock (F_unlck, Seek_set));
  55. Sleep (1);
  56. }
  57. Close (FD);
  58. }
  59. g++ Lock.c-o 1
  60. g++ Lock2.c-o 2
  61. Execute two programs to see the mutex effect.

Unix/linux inter-process file lock

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.