On 23/11/2025 06:48, Nahor via Cygwin wrote:
On Fri, Nov 21, 2025 at 2:00 AM Takashi Yano <[email protected]> wrote:
IIUC, flock() locks file itself, but not file descriptor. Usually,
flock() is used for inter-process file protection, isn't it?
It's kind of fuzzy:
- on one hand: "Locks created by flock() are associated with an open
file description" (Linux man page)
- on the other: "Locks are on files, not file descriptors" (BSD man page)
Either way, they both follow up by saying that if the descriptor is
duplicated/forked/etc, the copies will share the lock. But then that's
not my issue.
From my experience with MS-family file locks, I would say that the locks
are applied to the underlying file/inode as seen by the OS, but are each
held by the file descriptor through which they were "taken".  Thus
closing that file descriptor by any means (close(), dup2() on top,
execve(), exit(), kill() etc.) will implicitly release the lock. to
(dup(), inherit etc.) a file descriptor that holds a lock may or may
not cause that lock to be held by the set of mutually duplicate
descriptors, as the lock is really held by one of the internal objects
that implement the file descriptor concept, and more than one file
descriptor may refer to that same internal object which in turn holds
a lock on the underlying file/inode.

Put another way, each numeric file descriptor (such as fd=4 in pid=12345)
points to an underlying shared "file descriptor object", which in turn
points to other such objects, until reaching the object that owns the
lock and points further down, until finally looking at a disk inode.

--
Jakob Bohm, CIO, partner, WiseMo A/S. https://www.wisemo.com
Transformervej 29, 2860 Soborg, Denmark. direct: +45 31 13 16 10 <tel:+4531131610>
This message is only for its intended recipient, delete if misaddressed.
WiseMo - Remote Service Management for PCs, Phones and Embedded

--
Problem reports:      https://cygwin.com/problems.html
FAQ:                  https://cygwin.com/faq/
Documentation:        https://cygwin.com/docs.html
Unsubscribe info:     https://cygwin.com/ml/#unsubscribe-simple

Reply via email to