in reply to Fork + Flock = Who Gets the Lock?

running man flock on my Linux box tells...

Locks created by flock() are associated with a file, or, more precisely, an open file table entry. This means that duplicate file descriptors (created by, for example, fork(2) or dup(2)) refer to the same lock, and this lock may be modified or released using any of these descriptors. Furthermore, the lock is released either by an explicit LOCK_UN operation on any of these duplicate descriptors, or when all such descriptors have been closed.

... so they are shared!

Replies are listed 'Best First'.
Re^2: Fork + Flock = Who Gets the Lock?
by Anonymous Monk on Jun 03, 2005 at 19:59 UTC
    Thanks! So if they share the lock, then would it be safe to say that if the child process closes the file handle then it will no longer have a lock, but the parent will?
      yes, the lock being released only "when all such descriptors have been closed" (in the absense of an explicit unlock) means exactly that.
      yes!

      BTW, I think this behaviour is very OS dependant

        Yes! here too - veeery useful insight there :)