home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / unix / shell / 3116 < prev    next >
Encoding:
Text File  |  1992-07-24  |  3.0 KB  |  87 lines

  1. Newsgroups: comp.unix.shell
  2. Path: sparky!uunet!pipex!ibmpcug!kate.ibmpcug.co.uk!dylan
  3. From: dylan@ibmpcug.co.uk (Matthew Farwell)
  4. Subject: Re: file locking in shell scripts
  5. Organization: The IBM PC User Group, UK.
  6. Date: Fri, 24 Jul 1992 14:21:56 GMT
  7. Message-ID: <1992Jul24.142156.2903@ibmpcug.co.uk>
  8. References: <1992Jul23.124327.20843@cv.ruu.nl> <FRIEDMAN.92Jul24055302@nutrimat.gnu.ai.mit.edu>
  9. Lines: 76
  10.  
  11. In article <FRIEDMAN.92Jul24055302@nutrimat.gnu.ai.mit.edu> friedman@gnu.ai.mit.edu (Noah Friedman) writes:
  12. >In article <1992Jul23.124327.20843@cv.ruu.nl> rvloon@cv.ruu.nl (Ronald van Loon) writes:
  13. >>Somewhere on our system is a log-file. This logfile is used to gather
  14. >>statistical information. However, it is possible that people write to the file
  15. >>at the same time, thus resulting in either the merge of the input-streams or
  16. >>total garbage in the file.
  17. >>
  18. >>How can I - within a shell script - lock the file exclusive for writing ? If
  19. >>that's not possible, what do I need to use to do so from C, for example ?
  20. >   If you can design the locking protocol yourself, and make sure that
  21. >every interface people will use to write the log file will follow that
  22. >protocol, you can specify when a file is locked by creating a separate
  23. >lockfile.  Other programs seeing this file will know someone else has a
  24. >lock on the log file (many mail programs use this "advisory" locking
  25. >method).  You can easily create a lock file in a C program using
  26. >
  27. >            open (lockfile_path, O_CREAT|O_EXCL|..., mode);
  28. >
  29. >(appropriate error checking omitted, since that part is obvious).  This
  30. >means that open will only create the file if it does not already exist.
  31. >This is an atomic operation, so you shouldn't have to worry about any race
  32. >conditions (locally, anyway.. I don't know if anything is guaranteed over
  33. >NFS, including other locking methods!)
  34.  
  35. Nothing is guaranteed over NFS, apart from link(2)[*].  There was a
  36. thread about a week ago which covered this exact subject.  You could use
  37. ln(1) for this purpose, but Sys V has broken ln(1), so you have to write
  38. your own little locking program, or use the lock program provided below
  39. (which is 'newslock' from the c-news distribution.) You have to have
  40. something like this in your script:
  41.  
  42. LOCKFILE=LOCKFILE
  43. TMP=L.$$
  44.  
  45. echo $$ > $TMP
  46. if newslock $TMP $LOCKFILE
  47. then
  48.     # you have the lock
  49. else
  50.     # you dont have the lock
  51. fi
  52.  
  53. And this is the program:
  54.  
  55. /*
  56.  * newslock - simple, unbroken version of ln(1) for shell-program locking
  57.  *
  58.  * (System V has broken ln(1) itself.)
  59.  */
  60. #include <stdio.h>
  61.  
  62. main(argc, argv)
  63. int argc;
  64. char *argv[];
  65. {
  66.     if (argc != 3) {
  67.         fprintf(stderr, "Usage: %s tempname lockname\n", argv[0]);
  68.         exit(2);
  69.     }
  70.  
  71.     if (link(argv[1], argv[2]) < 0)
  72.         exit(1);
  73.     else
  74.         exit(0);
  75.     /* NOTREACHED */
  76. }
  77.  
  78. Can someone please add this to the FAQ?
  79.  
  80. Dylan.
  81. ------
  82. [*] With NFS, the locking is atomic, but you can find yourself with
  83. stale locks hanging around, if you're unlucky.
  84. -- 
  85. It is no coincidence that in no known language does the phrase 'As
  86. pretty as an Airport' appear -- Douglas Adams
  87.