Ivan Balashov
Ivan Balashov

Reputation: 1963

Choice of filehandle for flock utility

The flock utility man page gives the following usage example:

(
    flock -s 200
    # ... commands executed under lock ...
) 200>/var/lock/mylockfile

Assuming 200 is the filehandle of the lockfile, is there a possibility that during some run it fails, because the same filehandle is already in use by other process? If so, are there any tricks to make sure locking with flock works reliably?

Upvotes: 0

Views: 467

Answers (1)

Jonathan Leffler
Jonathan Leffler

Reputation: 754530

It doesn't matter in the slightest whether another process is also using file descriptor 200. Think about it; every process on the system is entitled to have file descriptors 0, 1, 2 pointing to somewhere, and they do not all point to the same place. All that matters is that your processes won't get upset about file descriptor 200 being used, and very few processes will notice, much less care.

Given that, there aren't any tricks needed - you simply have to ensure that all the processes that need to use the lock file actually do use it.

Upvotes: 2

Related Questions