On Mon, Aug 19, 2013 at 11:38 AM, Jan Alexander Steffens <jan.steffens@gmail.com> wrote:
How about this?
* Open lock file with O_RDWR | O_APPEND | O_CREAT and read. -> If file is empty, we can run. -> If file contains a non-existing PID and the current hostname on its last line, we can run. * If we can run, write new line containing current PID and hostname. * Rewind file and read again. Verify content is as expected.
Correction: Close and reopen instead of rewinding, to catch the lock file being removed right after the open().
* Do database operation. * Remove lock file.
Addendum: The file is only removed iff the lock was taken successfully.
I'm not sure how well it works over a network. Maybe one or more fsyncs can be introduced to ensure the appending and reading is synchronized with the server?