Optimal lock file method

后端 未结 6 1407
离开以前
离开以前 2020-11-30 23:25

Windows has an option to open a file with exclusive access rights. Unix doesn\'t.

In order to ensure exclusive access to some file or device, it is common practice

6条回答
  •  攒了一身酷
    2020-12-01 00:15

    You should probably be using flock(), as in

    fd = open(filename, O_RDWR | O_CREAT, 0666); // open or create lockfile
    //check open success...
    rc = flock(fd, LOCK_EX | LOCK_NB); // grab exclusive lock, fail if can't obtain.
    if (rc)
    {
        // fail
    }
    

提交回复
热议问题