467,907 Members | 1,430 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,907 developers. It's quick & easy.

file locking

KDB
Hi,
I am trying the following code to get a write lock on a file.

#include <unistd.h>
#include <iostream.h>
#include <fcntl.h>

main()
{
int fd = open("file",O_RDWR);

/** lock file */
flock lock = { F_WRLCK, SEEK_SET, 0, 0, 0 };
fcntl(fd, F_SETLKW, &lock);

/** do some processing here */

/** unlock file */
flock lock1 = { F_UNLCK, SEEK_SET, 0, 0, 0 };
fcntl(fd, F_SETLKW, &lock1);

printf("file unlocked\n");

close(fd);
}

the problem i am facing is that this program blocks while locking the
file ( if the file is already locked by another process).

can anyone suggest how to get a lock in non blocking mode so that if
the lock can not be obtained the function should return indicating
that it could not lock the file.

Thanks in advance.
Nov 13 '05 #1
  • viewed: 16156
Share:
4 Replies
KDB wrote:
/** lock file */
flock lock = { F_WRLCK, SEEK_SET, 0, 0, 0 };
fcntl(fd, F_SETLKW, &lock);

can anyone suggest how to get a lock in non blocking mode so that if
the lock can not be obtained the function should return indicating
that it could not lock the file.


Use F_SETLK instead of F_SETLKW (the W means Wait)

--
Martijn
http://www.sereneconcepts.nl
Nov 13 '05 #2
ka*****@delhi.tcs.co.in (KDB) wrote:
I am trying the following code to get a write lock on a file.

#include <unistd.h>
#include <iostream.h>
#include <fcntl.h> the problem i am facing is that this program blocks


No, the problem you're facing is that this isn't ISO C - unistd.h and
fcntl.h are POSIX, and iostream.h is some kind of C++. I'm not sure
which newsgroup you'd need for this, a POSIX or a C++ one, but given the
nature of your problem my first guess would be comp.unix.programmer.

Richard
Nov 13 '05 #3
Martijn wrote:
KDB wrote:
/** lock file */
flock lock = { F_WRLCK, SEEK_SET, 0, 0, 0 };
fcntl(fd, F_SETLKW, &lock);

can anyone suggest how to get a lock in non blocking mode so
that if the lock can not be obtained the function should return
indicating that it could not lock the file.


Use F_SETLK instead of F_SETLKW (the W means Wait)


Please do not answer off topic questions with something other
than a redirection to an appropriate group. There is (in theory)
nobody here to criticize your answers and to correct possible
errors.

--
Replies should be to the newsgroup
Chuck Falconer, on vacation.
Nov 13 '05 #4
LibraryUser wrote:
/** lock file */
flock lock = { F_WRLCK, SEEK_SET, 0, 0, 0 };
fcntl(fd, F_SETLKW, &lock);

can anyone suggest how to get a lock in non blocking mode so
that if the lock can not be obtained the function should return
indicating that it could not lock the file.


Use F_SETLK instead of F_SETLKW (the W means Wait)


Please do not answer off topic questions with something other
than a redirection to an appropriate group. There is (in theory)
nobody here to criticize your answers and to correct possible
errors.


He says while, "Lo and Behold", he's engaging in an off-topic discussing in
another thread.

The question in question is so simple in nature that the user can easily
verify the correctness of the proposed solution himself.

--
Martijn
http://www.sereneconcepts.nl
Nov 13 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Geoffrey | last post: by
2 posts views Thread by Kamus of Kadizhar | last post: by
4 posts views Thread by Stanley Cheung | last post: by
4 posts views Thread by darrel | last post: by
2 posts views Thread by Dr. Who | last post: by
15 posts views Thread by Matt Brandt | last post: by
1 post views Thread by =?Utf-8?B?REZTIEhFTFAgV0FOVEVE?= | last post: by
2 posts views Thread by Sparky | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.