Portable Message Queues

Brandon S. Allbery allbery at ncoast.UUCP
Thu Sep 15 09:05:56 AEST 1988


As quoted from <7317 at umn-cs.cs.umn.edu> by randy at umn-cs.cs.umn.edu (Randy Orrison):
+---------------
| I expect that the best I can do will be just writing messages to a file and
| using some form of file locking to ensure that the file doesn't get mangled.
| 
| Is there a portable way of achieving file locking under all versions of Unix?
| 
| I believe that using open(...O_CREAT|O_EXCL) on a lock file is the best there
| is.  Is this always true, and is there nothing better?
+---------------

Is O_EXCL portable?  I know System V has it, but possibly not BSD.

The easiest and most portable way is non-obvious:

		while (link("datafile", "lockfile") != 0)
			(void) sleep(30);
		/* do your processing */
		(void) unlink("lockfile");

After all, link() is available on all versions of *nix, and on all versions
of *nix the destination pathname must not exist.  (This is used in various
places in System V-compatible (pre-fcntl-locking) programs, but in fact works
as an advisory lock on all systems.)

++Brandon
-- 
Brandon S. Allbery, uunet!marque!ncoast!allbery			DELPHI: ALLBERY
	    For comp.sources.misc send mail to ncoast!sources-misc
"Don't discount flying pigs before you have good air defense." -- jvh at clinet.FI



More information about the Comp.unix.wizards mailing list