LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-21-2006, 12:47 AM   #1
rajesh_b
Member
 
Registered: Sep 2004
Location: Hyderabad.
Posts: 83

Rep: Reputation: 15
Question Difference between flockfile and flock


hi,
I want to know the difference between flockfile and flock. After seeing man pages i came to know about the following points.

1. flockfile is thread safe. It is associated with FILE object.
2. flock is associated with process. It is associated with file descriptor.

so this means flockfile will be taken care by c library and flock will be taken care by OS. Am i correct or not? Can any body clarify my doubt.
Thanks in advance.


Regards,
Rajesh
 
Old 06-22-2006, 05:41 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Sounds like you already answered your own question :-)

To clarify a bit more:

All actual reading/writing to files is done by the system calls read(2)/write(2), pread(2)/pwrite(2) and mmap(2). The libc functions like printf(3), puts(3), etc. actually use the system calls (read(2), write(2),..) to do the actual reading/writing. But the libc functions are often more convenient because they (among other things like formatted IO) do the buffering for you, and do a good job at choosing optimal buffer sizes.

OTOH one drawback of this buffering is that you do not control when data is actually read/written, and how much is done at a time. This makes the libc (stdio) IO-functions incompatible with low-level locking functions like fcntl(2), lockf(2) and flock(2), even though it is possible to get the file-descriptor used by a FILE*. To be able to do file locking together with the stdio functions, flockfile(3) is provided by libc (stdio), which implements it's own locking. This stdio-file locking is entirely seperated from the low-level locking with fcntl(2), flock(2) and lockf(3) and the kernel does not even 'know' about the locks from libc (stdio). This makes it possible to make flockfile(3) thread-safe, which is impossible/difficult to do with the system-call (kernel-level) lock-functions (see this question).

The use of flockfile(3) to lock files or parts of files, you should not get a file-descriptor for the FILE* and use the system calls for IO (read(2), write(2),..) because those functions have no way to be aware of the stdio-locks.
 
Old 06-22-2006, 11:20 PM   #3
rajesh_b
Member
 
Registered: Sep 2004
Location: Hyderabad.
Posts: 83

Original Poster
Rep: Reputation: 15
hi Hko,
Thanks for the reply. It help me in understanding. I have one more question.
Implementing a lock in kernel levle is easy, because we have several atomic functions provided by the kernel api which can test and set the bit atomically.
But how can API can set and implementing that lock atomically.
Hope i am clear.


Regards,
rajesh
 
Old 06-23-2006, 03:57 AM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
I don't know. If you really want to know you should read the sources of glibc.

But I can imagine some possible strategies, like maintaining a global hash-table with file-paths + pid + thread-id as keys. Then making sure the changes to the hash-table entries are atomic by using IPC-semaphores or the like.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Flock releases 0.7 beta LXer Syndicated Linux News 0 06-16-2006 02:54 AM
flock problem climbingmerlin Programming 3 10-20-2005 09:36 AM
do we have concept of flock in solaris invinciblemk Solaris / OpenSolaris 3 05-23-2005 08:02 PM
how to use the flock system call in sun os 5.07 ?? gajaykrishnan Solaris / OpenSolaris 2 03-27-2005 03:08 PM
flock and touch Arcades Programming 6 03-11-2005 08:13 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:57 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration