flock(): removing locked file without race condition?

后端 未结 3 710
情深已故
情深已故 2020-12-01 12:04

I\'m using flock() for inter-process named mutexes (i.e. some process can decide to hold a lock on \"some_name\", which is implemented by locking a file named \"some_name\"

3条回答
  •  日久生厌
    2020-12-01 12:55

    Sorry if I reply to a dead question:

    After locking the file, open another copy of it, fstat both copies and check the inode number, like this:

    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 a program holds a lock on a file that is still on the file system, every other program that has a leftover file will have a wrong inode number.

    I actually proved it in the state-machine model, using the following properties:

    If P_i has a descriptor locked on the filesystem then no other process is in the critical section.

    If P_i is after the stat with the right inode or in the critical section it has the descriptor locked on the filesystem.

提交回复
热议问题