Xref: utzoo comp.unix.wizards:22307 comp.unix.i386:5627 comp.unix.xenix:11913 Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!uunet!mcsun!unido!mikros!mwtech!martin From: martin@mwtech.UUCP (Martin Weitzel) Newsgroups: comp.unix.wizards,comp.unix.i386,comp.unix.xenix Subject: Locking from shell scripts (was Re: lpadmin(8) question) Keywords: fifo, semaphor, locking, shell Message-ID: <779@mwtech.UUCP> Date: 5 Jun 90 14:23:43 GMT References: <453@van-bc.UUCP> <1492@mitisft.Convergent.COM> <475@van-bc.wimsey.bc.ca> Reply-To: martin@mwtech.UUCP (Martin Weitzel) Followup-To: comp.unix.wizards,comp.unix.questions Organization: MIKROS Systemware, Darmstadt/W-Germany Lines: 94 In article <475@van-bc.wimsey.bc.ca> sl@van-bc.wimsey.bc.ca (Stuart Lynne) writes: [about locking from interface programs of the SysV spool system] > >This script shows how this could be done, except that there is a race >condition to create the lock file. > >A C program would be a much better solution. > [script deleted, basically the existence of some file was used as lock condition; because `test -f lockfile was done before `echo ... >lockfile', there remained a small chance for two simultaneous executions of the script both to "think" they have won - or technically spoken, they succeeded locking the resource] As the poster allready pointed out, there is a race condition with this method. Without changing much, this could be avoided by `link'ing to the lockfile instead of creating it. Eg. write to a temporary file first, then place the lock with `/etc/link'; this operation will replace the test for existence in the script. (Note that for some newer "ln"-commands it is no error if the destination file exists, though there's a "-f" option which seems at first glance to give the choice to the user ... :-(). Linking (and unlinking) provide some "semaphor"-mechanism for shell scripts, which works even for the priviledged processes. (Note that the latter is *not* the case for the more common technique of creating a file without access permissions.) If your system supports FIFOs, there's another interresting technique, which even avoids the usual trade off of "busy waiting" vs. "quick response". (This is not so much a problem in the spooler example, but may be in other applications.) Locking with FIFOs works by creating a named pipe with some daemon process, then write a single new-line character into it and sleep forever. If you don't want to program in C, the latter may be done by executing repeated `sleep 30000' from a never ending loop. It is essential that the daemon keeps the FIFO open for read and write, so that the character in it is never discarded! All in all it's a bit tricky to get this started, as you can open a FIFO from the shell only for read OR for write and each request would wait for the other, but you can get around this with: mknod LOCKFIFO p echo >LOCKFIFO & while sleep 30000 do :; done