Re: fcntl file locking and pthreads

From: Ville Voutilainen (vjv@ee.oulu.fi)
Date: Sun May 04 2003 - 16:56:19 EST


>flock()-based locks work as expected, i.e. only one thread can have an
>exclusive lock at a time.
>What would it take to make fcntl work as flock?

This is because flock locks the inode, not the file descriptor.
fcntl locks the file descriptor. This is why flock does not
work over nfs, I suppose. If you share descriptors, you also
share locks and positions (at least according to the man pages
of fcntl, dup et al). I don't know what exactly is supposed
to happen if you open the file twice in two separate threads
and then lock with fcntl in the first thread. But simply doing

open()
pthread_create()
fcntl()
                        (other thread)
                        fcntl()
will probably result in both threads acquiring the lock
successfully. It would be reasonable IMHO to assume that
a sequence like

open()
pthread_create()
fcntl()

                        (other thread)
                        open()
                        fnctl() /* lock the newly opened fd */

would give you what you're after. The only problem being that
even user space manuals suggest that fcntl can only detect
that other *processes* hold a file lock. Given the muddy
nature of what is a thread/process in Linux, this requires
someone more familiar with the clone stuff to clarify.

Another issue altogether is why you are trying to sync two
threads with file locks, but I digress.

-VJV-
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/



This archive was generated by hypermail 2b29 : Wed May 07 2003 - 22:00:20 EST