randy@umn-cs.cs.umn.edu (Randy Orrison) (09/07/88)
For the application I'm working on, I would like to be able to use System V message queues, however, I have reason to believe that they are somewhat less than universally available. Since a major goal in this project is for it to be portable, I need to find a similar, but more portable, mechanism. 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? Please e-mail if the answer is non-trivial, simple, and obvious. Please post if you think others may be interested. (What does POSIX say?) Thanks in advance... -randy -- Randy Orrison, Chemical Computer Thinking Battery -- randy@cctb.mn.org randy@ux.acss.umn.edu {bungia, uunet!hi-csc, rutgers, sun}!umn-cs!randy "Good morning. Welcome to reality. I'm afraid all our guides are busy this millenium, so you'll have to take the self-guided tour."
allbery@ncoast.UUCP (Brandon S. Allbery) (09/15/88)
As quoted from <7317@umn-cs.cs.umn.edu> by randy@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@clinet.FI
guy@gorodish.Sun.COM (Guy Harris) (09/16/88)
> Is O_EXCL portable? I know System V has it, but possibly not BSD.
4.2BSD and subsequent BSD releases have all the O_* flags that S5R2 had,
including O_EXCL. (O_NDELAY has some differences in its behavior, however.)
No current BSD release has the O_EXCL flag that first appeared in a release
after S5R2 (there was a bit that did some of O_EXCL in earlier releases, but it
was for use inside the kernel only, and was, as I remember missing some pieces
that were necessary to make it useful; I guess they put in the pieces and
advertised the flag). It's probably not hard to add to BSD.