flock is associated with open-file-description, and open(2) creates new open-file-description. So invoking flock(1) in two separate shells will open the lock file separately and result in two open-file-descriptions. How could flock(1) know the lock status associated with each open-file-description? I am confused... Also, does flock(1) treat path name and file descriptor differently?


The flock I am talking about is the one under Linux.

In my understanding, the struct representing flock is system-wide unique. One or more open-file-descriptions may reference to one flock struct (like the relationship between open-file-description and i-node). Is my 'guess' correct?

1 Answer 1


No, flock, which is a wrapper around the system call flock(), locks on files, not file descriptors.

The OpenBSD manual for flock() says (my emphasis):

flock() applies or removes an advisory lock on the file associated with the file descriptor fd.


Locks are on files, not file descriptors. That is, file descriptors duplicated through dup(2) or fork(2) do not result in multiple instances of a lock, but rather multiple references to a single lock. If a process holding a lock on a file forks and the child explicitly unlocks the file, the parent will lose its lock.

flock() is a system call; the kernel keeps track of locks.

  • The questioner didn't actually say descriptors, note. Xe said descriptions. And xe quite likely took that from the Linux manual page for flock() which specifically notes that such locks are associated with file descriptions. Linux and the BSDs differ on this subject.
    – JdeBP
    Commented May 9, 2018 at 10:36
  • @JdeBP So they do.
    – Kusalananda
    Commented May 9, 2018 at 12:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .