File locking problems

michael sweet sweetmr at SCT60A.SUNYCT.EDU
Fri Jun 14 04:34:00 AEST 1991


In a program we're writing, we have a configuration file which needs to be
locked against changes for two reasons:

 1) If the information in the configuration file is changed during a run,
    the program will bomb.
 2) If two copies of the program are run, the second copy should sense the
    lock placed on the config file by the first copy and exit without
    doing anything.

Unfortunately, the lockf(2) function doesn't seem to work properly (at least
the way we are doing it...)  THe following is a code exerpt from the
program:

  if (fd = open("config_file", O_RDWR, 02644) < 0)
   {
    puts("Can't open configuration file!");
    exit(1);
   };

  lseek(fd, 0L, 0);

  if (lockf(fd, F_TLOCK, 0L) < 0)
   {
    puts("Configuration file is already locked!");
    exit(1);
   };

Can anyone see anything obviously wrong with the above code?  We have tried
running two copies simultaneously, and both seem to lock the file successfully.
The configuration file has 02644 permissions....

 -Mike Sweet (sweetmr at sct60a.sunyct.edu)



More information about the Comp.sys.sgi mailing list