Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 10-31-2007, 08:04 AM   #1
LQ Newbie
Registered: Oct 2007
Posts: 6

Rep: Reputation: 0
Arrow How to lock files using fcntl() and to work between threads of the same process

I need to look a file and for this I'm using fcntl() function like in the sample bellow.
fcntl() locks are taken into consideration only by other process.

 #include <stdio.h>
    #include <stdlib.h>
    #include <errno.h>
    #include <fcntl.h>
    #include <unistd.h>
    int main(int argc, char *argv[])
    	/* l_type   l_whence  l_start  l_len  l_pid   */
    	struct flock fl = { F_WRLCK, SEEK_SET, 0,       0,     0 };
    	int fd;
        fl.l_pid = getpid();
    	if (argc > 1) 
    		fl.l_type = F_RDLCK;
    	if ((fd = open("lockdemo.c", O_RDWR)) == -1) {
    	printf("Press <RETURN> to try to get lock: ");
    	printf("Trying to get lock...");
    	if (fcntl(fd, F_SETLKW, &fl) == -1) {
    	printf("got lock\n");
    	printf("Press <RETURN> to release lock: ");
    	fl.l_type = F_UNLCK;  /* set to unlock same region */
    	if (fcntl(fd, F_SETLK, &fl) == -1) {
If I acquire a exclusive lock on a file from thread A, thread B will also succeed in obtaining an exclusive lock on the same file.
How can I make thread B to detect that the specified file is already locked?
Old 10-31-2007, 08:38 AM   #2
Registered: Oct 2003
Posts: 44

Rep: Reputation: 19
Did you try to open a new file handle (either by opening the same file or by using "dup()" syscall in the second thread?
Old 10-31-2007, 09:19 AM   #3
LQ Newbie
Registered: Oct 2007
Posts: 6

Original Poster
Rep: Reputation: 0
All new handles are created using open().
Old 11-01-2007, 01:03 AM   #4
Wim Sturkenboom
Senior Member
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
Have you considered the use of mutexes. See man pthread_mutex_init
If necessary, you can combine it with a lockfile.

Last edited by Wim Sturkenboom; 11-01-2007 at 01:04 AM.
Old 11-01-2007, 04:25 AM   #5
LQ Newbie
Registered: Oct 2007
Posts: 6

Original Poster
Rep: Reputation: 0
After some more thinking I got to the conclusion that combining mutexes and lock files are the best way to do it.
Old 11-01-2007, 07:07 AM   #6
Senior Member
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
File locks aren't mandatory by default. They only work when you check for them first or try to set one where it isn't allowed, or at least that's how your program should look look at it. Rather than try to set it and let it fail in the other threads, you should check for the lock's existence first and then wait if it exists. Unlike interprocess locks you can't just use the wait flag; you'd have to create your own spin-lock.

EDIT: You can make locks mandatory by mounting with the "mand" mount option and setting g-x and g+s on the file, but I don't know if that works within the same process. Might still need to do what I advised above, or maybe use separate file descriptors for each thread. You shouldn't count on the file being set up for mandatory locks, though. Mandatory locks seem like an administrator's tool rather than a programmer's.

Last edited by ta0kira; 11-01-2007 at 08:20 AM.
Old 11-05-2007, 12:45 PM   #7
Registered: May 2002
Location: dracut MA
Distribution: Fedora, RHEL
Posts: 600

Rep: Reputation: 109Reputation: 109
for multi-threaded access to a file, it is best to use an ADT that contains a reference to the file handle, and a mutex lock that you pass around, or have a file writing thread which other threads ask to write to the file for them. Each carries it's own advantages and drawbacks.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
what happens to threads if i kill the process ? psh2001 Programming 4 08-11-2007 11:21 PM
Do the threads lock on Meesage queues before writing to it? asarkar Programming 2 09-22-2006 05:06 PM
Caps Lock and Num Lock leds dont work! npc Linux - Hardware 2 11-08-2005 10:40 AM
How to View Threads per Process Shashi Linux - Software 0 08-01-2003 01:37 AM
Threads And Process Penguinizer Programming 1 02-18-2003 09:39 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:00 PM.

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