Open File Description Locks confusion

317 views Asked by At

As in - https://www.gnu.org/software/libc/manual/html_node/Open-File-Description-Locks.html#Open-File-Description-Locks

fcntl(F_OFD_SETLK) locks on an open file table entry, (usually obtained by open()). Easy to understand.

But in the following example :

https://www.gnu.org/software/libc/manual/html_node/Open-File-Description-Locks-Example.html#Open-File-Description-Locks-Example.

In its example process, each thread calls open(), so each file descriptor should point to a different open file table entry.

Then doing fcntl (fd, F_OFD_SETLKW, &lck) in each thread is just getting a lock on a different open file table entry, which means this locking is completely wrong.

But I tested on Ubuntu, and it works for some reason. What am I missing?

1

There are 1 answers

0
Abhi Agarwal On

Old question but: multiple file descriptors can point to the same open file description. In this case, the OFD-style calls set advisory locks on the description itself (and thus is non-portable as it is a Linux concept) rather than the descriptor. Subtle difference.