سؤال

I'm trying to implement a file-based exclusive lock for a daemon, which applies on a per-file basis (no inter-thread or intra-process locking). I know it's a common problem, and has some conventions established, but I am having trouble getting it right, or understanding the problem completely.

I've looked at other answers, and currently I'm using something very close to this: https://stackoverflow.com/a/1643134, ie. using flock to create an advisory lock on program start. However, this doesn't do what I want; the call to flock always succeeds.

I'm not sure if my code is incorrect, or if I've misunderstood, and flock isn't meant to work across separate processes (?).

This is C++ (11) code, tested on Linux 2.6.32 (CentOS VM) and 3.12.9 (Arch), both ext4 filesystems.

هل كانت مفيدة؟

المحلول

I was closing the file descriptor at the end of the acquire routine. Whoops.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top