Lock Files From Shell Scripts

Liam R. E. Quin lee at sq.sq.com
Fri Nov 10 12:34:16 AEST 1989


In article <25561EFF.29662 at ateng.com> chip at ateng.com (Chip Salzenberg) writes:
>BZZT!  You lose the System V Sweepstakes.
>
>SysV's "ln" will remove an existing file, rather like "cp" will.  You have
>to write your own "link" program.  Those nice C News people did just that.

True.
This is why mkdir can be better...

In my mail to the original poster, I suggested using mkdir in a loop.
A file in the directory can contain the process' PID, and you can then
do simple collision detection, using kill -0 to see if the other process
is running.
Obviously a file containing the Process ID can't be used as a semaphore,
but the directory suffices, because only the process that created the
directory can put the file there.
It is harder to recover from the case of an empty directory, though.

On the whole, it is probably simplest to write a "makelock" program,
but one can't always do that (e.g. no C compiler).

Lee
--
Liam R. Quin, Unixsys (UK) Ltd
lee at sq.com (whilst visiting Toronto from England)
lee at anduk.co.uk (after Christmas)



More information about the Comp.unix.questions mailing list