At most 1 running copy

John Summerfield debian at herakles.homelinux.org
Sun Nov 28 01:47:47 UTC 2004


On Saturday 27 November 2004 22:46, fredex wrote:
> What I've done in C programs is to create the lock file and put my
> PID in it. Whenever the program starts, if the lock file is not present
> it's safe to start (assumed). If it's present, open it and read the PID
> value. Look up the PID to see if it still exists. If it does, assume it
> is the correct process (which it may not since PIDs do get recycled in
> time) and abort the program. If the file exists and the PID doesn't,
> the program probably died without cleaning up, so just delete the lock
> file then create a new one with your own PID. During the process of
> creating the file, you need to lock it to avoid race conditions. In
> C I'd use fcntl or lockf or some such. I don't know what you'd do in
> PERL but there surely is a mechanism.


That's quite a tricky (though widely practiced) scheme to paper over Unix's 
traditional lack of proper locking semantics.

I've never understood why it was omitted; perhaps the original designers 
didn't understand the need. I was using OS Rel 20 or so about then, and it 
did have a straight-forward mechanism.

Now we have proper locking, I think we should use it. Even though it's not as 
good as I had bask then.




-- 
Cheers
John




More information about the fedora-list mailing list