Sorry if I answer a dead question:
After locking the file, open another copy, fstat both copies and check the inode number, for example:
lockfile = "/tmp/some_name.lock"; while(1) { fd = open(lockfile, O_CREAT); flock(fd, LOCK_EX); fstat(fd, &st0); stat(lockfile, &st1); if(st0.st_ino == st1.st_ino) break; close(fd); } do_something(); unlink(lockfile); flock(fd, LOCK_UN);
This prevents the race condition, because if the program has a file lock that is still in the file system, every other program that has the remaining file will have the wrong inode number.
I actually proved this in a state-machine model using the following properties:
If P_i has a handle locked in the file system, then no other process is in the critical section.
If P_i after the stat with the correct index or in the critical section, it has a handle locked in the file system.
user2769258 Sep 11 '13 at 15:25 2013-09-11 15:25
source share