Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > file locking

Reply
Thread Tools

file locking

 
 
Richard Bos
Guest
Posts: n/a
 
      09-09-2003
(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
 
Reply With Quote
 
 
 
 
KDB
Guest
Posts: n/a
 
      09-09-2003
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.
 
Reply With Quote
 
 
 
 
Martijn
Guest
Posts: n/a
 
      09-09-2003
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


 
Reply With Quote
 
LibraryUser
Guest
Posts: n/a
 
      09-09-2003
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.
 
Reply With Quote
 
Martijn
Guest
Posts: n/a
 
      09-10-2003
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Locking locking resolution Frontpage raiderhawk General Computer Support 0 01-08-2008 01:42 AM
increase in cpu usage on locking and locking the system sowmya.rangineni@gmail.com Computer Support 0 06-15-2007 12:06 PM
Application locking to support optimisitc locking ? Timasmith Java 4 11-01-2006 12:42 AM
Designer locking mdb file... (file and dir GRANTS FULL CONTROL FOR "EVERYONE") Praveen ASP .Net 1 04-16-2005 06:01 AM
Confused about locking a file via file.flock(File::LOCK_EX) Ludwigi Beethoven Ruby 5 07-26-2003 03:26 PM



Advertisments