LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 10-31-2007, 08:04 AM   #1
mistretzu
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.

Code:
 #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) {
    		perror("open");
    		exit(1);
    	}
    
    	printf("Press <RETURN> to try to get lock: ");
    	getchar();
    	printf("Trying to get lock...");
    
    	if (fcntl(fd, F_SETLKW, &fl) == -1) {
    		perror("fcntl");
    		exit(1);
    	}
    
    	printf("got lock\n");
    	printf("Press <RETURN> to release lock: ");
    	getchar();
    
    	fl.l_type = F_UNLCK;  /* set to unlock same region */
    
    	if (fcntl(fd, F_SETLK, &fl) == -1) {
    		perror("fcntl");
    		exit(1);
    	}
    
    	printf("Unlocked.\n");
    
    	close(fd);
    }
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
vadiml
Member
 
Registered: Oct 2003
Posts: 42

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
mistretzu
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
mistretzu
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
ta0kira
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.
ta0kira

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
orgcandman
Member
 
Registered: May 2002
Location: dracut MA
Distribution: Ubuntu; PNE-LE; LFS (no book)
Posts: 594

Rep: Reputation: 102Reputation: 102
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.
 
  


Reply


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


All times are GMT -5. The time now is 07:51 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration