Article View: comp.unix.shell
Article #134Re: file locking in shell scripts
From: Matthew Farwell
Date: Fri, 24 Jul 1992 14:21
Date: Fri, 24 Jul 1992 14:21
76 lines
3136 bytes
3136 bytes
In article <FRIEDMAN.92Jul24055302@nutrimat.gnu.ai.mit.edu> friedman@gnu.ai.mit.edu (Noah Friedman) writes:
>In article <1992Jul23.124327.20843@cv.ruu.nl> rvloon@cv.ruu.nl (Ronald van Loon) writes:
>>Somewhere on our system is a log-file. This logfile is used to gather
>>statistical information. However, it is possible that people write to the file
>>at the same time, thus resulting in either the merge of the input-streams or
>>total garbage in the file.
>>
>>How can I - within a shell script - lock the file exclusive for writing ? If
>>that's not possible, what do I need to use to do so from C, for example ?
> If you can design the locking protocol yourself, and make sure that
>every interface people will use to write the log file will follow that
>protocol, you can specify when a file is locked by creating a separate
>lockfile. Other programs seeing this file will know someone else has a
>lock on the log file (many mail programs use this "advisory" locking
>method). You can easily create a lock file in a C program using
>
> open (lockfile_path, O_CREAT|O_EXCL|..., mode);
>
>(appropriate error checking omitted, since that part is obvious). This
>means that open will only create the file if it does not already exist.
>This is an atomic operation, so you shouldn't have to worry about any race
>conditions (locally, anyway.. I don't know if anything is guaranteed over
>NFS, including other locking methods!)
Nothing is guaranteed over NFS, apart from link(2)[*]. There was a
thread about a week ago which covered this exact subject. You could use
ln(1) for this purpose, but Sys V has broken ln(1), so you have to write
your own little locking program, or use the lock program provided below
(which is 'newslock' from the c-news distribution.) You have to have
something like this in your script:
LOCKFILE=LOCKFILE
TMP=L.$$
echo $$ > $TMP
if newslock $TMP $LOCKFILE
then
# you have the lock
else
# you dont have the lock
fi
And this is the program:
/*
* newslock - simple, unbroken version of ln(1) for shell-program locking
*
* (System V has broken ln(1) itself.)
*/
#include <stdio.h>
main(argc, argv)
int argc;
char *argv[];
{
if (argc != 3) {
fprintf(stderr, "Usage: %s tempname lockname\n", argv[0]);
exit(2);
}
if (link(argv[1], argv[2]) < 0)
exit(1);
else
exit(0);
/* NOTREACHED */
}
Can someone please add this to the FAQ?
Dylan.
------
[*] With NFS, the locking is atomic, but you can find yourself with
stale locks hanging around, if you're unlucky.
--
It is no coincidence that in no known language does the phrase 'As
pretty as an Airport' appear -- Douglas Adams
Technical Details
Message-ID:
<1992Jul24.142156.2903@ibmpcug.co.uk>Path:
arc1-ca.pugleaf.net!sparky!uunet!pipex!ibmpcug!kate.ibmpcug.co.uk!dylanReferences:
<1992Jul23.124327.20843@cv.ruu.nl> <FRIEDMAN.92Jul24055302@nutrimat.gnu.ai.mit.edu>Show Full Headers
Complete Article Headers
Newsgroups: comp.unix.shell Path: arc1-ca.pugleaf.net!sparky!uunet!pipex!ibmpcug!kate.ibmpcug.co.uk!dylan From: dylan@ibmpcug.co.uk (Matthew Farwell) Subject: Re: file locking in shell scripts Organization: The IBM PC User Group, UK. Date: Fri, 24 Jul 1992 14:21:56 GMT Message-ID: <1992Jul24.142156.2903@ibmpcug.co.uk> References: <1992Jul23.124327.20843@cv.ruu.nl> <FRIEDMAN.92Jul24055302@nutrimat.gnu.ai.mit.edu> Lines: 76