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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
10-31-2007, 08:04 AM
|
#1
|
|
LQ Newbie
Registered: Oct 2007
Posts: 6
Rep:
|
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?
|
|
|
|
10-31-2007, 08:38 AM
|
#2
|
|
Member
Registered: Oct 2003
Posts: 42
Rep:
|
Did you try to open a new file handle (either by opening the same file or by using "dup()" syscall in the second thread?
|
|
|
|
10-31-2007, 09:19 AM
|
#3
|
|
LQ Newbie
Registered: Oct 2007
Posts: 6
Original Poster
Rep:
|
All new handles are created using open().
|
|
|
|
11-01-2007, 01:03 AM
|
#4
|
|
Senior Member
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,780
|
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.
|
|
|
|
11-01-2007, 04:25 AM
|
#5
|
|
LQ Newbie
Registered: Oct 2007
Posts: 6
Original Poster
Rep:
|
After some more thinking I got to the conclusion that combining mutexes and lock files are the best way to do it.
|
|
|
|
11-01-2007, 07:07 AM
|
#6
|
|
Senior Member
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 2,967
Rep: 
|
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.
|
|
|
|
11-05-2007, 12:45 PM
|
#7
|
|
Member
Registered: May 2002
Location: dracut MA
Distribution: Ubuntu; PNE-LE; LFS (no book)
Posts: 593
Rep: 
|
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 |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 02:12 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|