Using fcntl() record locks with threads

Using fcntl() record locks with threads

Post by Alan Murph » Sat, 31 Mar 2001 20:37:39



Hi,

whats mechanism can be used to take a record lock on a file (using the
fcntl() call), in a posix multi threaded application.  Seems to me that
these locks are process based, and therfore multiple threads within the same
process are treated as the same thing.

Any pointer would be appreciated

Thanks Alan

 
 
 

Using fcntl() record locks with threads

Post by Dave Butenho » Sat, 31 Mar 2001 23:25:18



> whats mechanism can be used to take a record lock on a file (using the
> fcntl() call), in a posix multi threaded application.  Seems to me that
> these locks are process based, and therfore multiple threads within the same
> process are treated as the same thing.

> Any pointer would be appreciated

This has been discussed several times before. Yes, fcntl() locks are
process-based, for a number of reasons historical and pragmatic. Some people
have successfully built a two-level file locking strategy that uses mutexes
between threads within a process and fcntl() between processes. Essentially,
you reference count the fcntl() lock(s) so that the process holds an fcntl()
lock whenever any thread within the process has an area locked; if more than
one thread within the process is interested in the same file area, they
synchronized among themselves using a mutex. I believe that sample code may
have been posted. Search the newsgroup archives, if you can find a good server.
(I don't know what the state of Deja is now; it was always a good one, and may
be again if the transfer of control has been straightened out.)


| Compaq Computer Corporation              POSIX Thread Architect |
|     My book: http://www.awl.com/cseng/titles/0-201-63392-2/     |
\-----[ http://home.earthlink.net/~anneart/family/dave.html ]-----/

 
 
 

1. Irix 6.3 & Pthreads: fcntl and file locking

Hi all!

I tried to use fcntl to lock files in a pthreads application
to prevent other threads and processes affecting the contents
of the file. Now it seems that when a thread has locked the
file, another thread is unable to get the information about
the lock by calling fcntl(fd, F_GETLK, &lock).

Do I have to create a mutex for every open file descriptor
and handle the thread synchronization by my self? Or what?

BTW. Isn't it strange that a process can create several
locks with same properties, but have them lost with one
call? IMHO, the OS shouldn't allow it.

--
Dado Colussi                    Xenetic Oy

     suora  (09) 6689 6485           fax      (09) 6689 6489
     gsm    040 500 5501             osoite   Pursimiehenkatu 29-31 B
                                              00150 HELSINKI

2. Date Validation...!!! urgent

3. record locking & threads

4. MACazine

5. File locking (fcntl.h), VC++ 6.0

6. DBFCDX question

7. pthreads and fcntl file locking

8. World Wide Free Internet Access .

9. pthreads, fcntl and threads

10. Question thread safe fcntl() and pthreads on Ultrix 4.4

11. Threads using Qt (not Qt using Threads)

12. thread-safe creation of singleton with one locking per thread - what's wrong?

13. Caps Lock / Num Lock / Scroll Lock