From: utzoo!decvax!harpo!floyd!cmcl2!philabs!sdcsvax!sdchema!jwp Newsgroups: net.unix-wizards Title: mutual exclusion between processes Article-I.D.: sdchema.349 Posted: Wed Jan 5 09:53:30 1983 Received: Thu Jan 6 07:32:31 1983 Randy Bentson (csu-cs!bentson) points out, correctly, that using "creat(LOCKFILE, 0)" fails to guarantee exclusion since the 'creat' works for the superuser even if LOCKFILE already exists and has mode 0. The following does not have that problem: myfile = mktemp(TEMPLATE); /* mktemp() guarantees a unique */ /* file name for each invocation */ /* of the program */ creat(myfile, ANYDESIREDMODE); if(link(myfile, LOCKFILE) == 0) /* LOCKFILE is a name known by */ { /* all invocations of the program. *//* The link will fail if LOCKFILE */ unlink(LOCKFILE); /* exists even if the process is */ } /* being run with uid == 0. */ else { } unlink(myfile); I use this in a variety of places, one of them being a sort of general purpose "spool stuff up for this device/file" program where the alogrithm is essentially: Get temporary file name Copy input to the temporary file name Try to link temporary file name to lockfile name If the link succeeds, start daemon and exit If the link fails, exit (the daemon is already running) John Pierce, Chemistry, UC San Diego {ucbvax, philabs}!sdcsvax!sdchema!jwp