473,320 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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
4 16537
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Geoffrey | last post by:
We have developed a python class that can read data files created from another application. These target files are C-ISAM files used for accounting applications so the "primary" application may be...
2
by: Kamus of Kadizhar | last post by:
Thanks to Robert Brewer, I got enough insight into logging to make it work.... Now I have another issue: file locking. Sorry if this is a very basic question, but I can't find a handy reference...
3
by: Hugz | last post by:
Hello, And Thank you for taking time to help me.Myself Hugz.I am new to perl and want to write my own Scripts.But i can't understand how perl "rename" function and "file locking" works. ...
2
by: Scott Bryce | last post by:
I am creating a CGI application in Perl that uses an Access database. It will be hosted on an NT server. I have used flat file DBMs (tied hashes) on UNIX servers, but I am not familiar with how...
4
by: Stanley Cheung | last post by:
Hi all, Anyone have a "File Locking" experience? I want to ask if the application can prevent 2 user access (concurrently) to file for content editing... any lock and unlock mechanism...
4
by: darrel | last post by:
I've been dealing with a file locking issue for a while. Our CMS spits out a new XML file each time an item in the DB is updated. This XML file is basically our site menu, and is what we use on...
2
by: Dr. Who | last post by:
I'm always disappointed when I find something that Python doesn't handle in a platform independent way. It seems to me that file locking is in that boat. 1. I don't see a way to atomically open...
15
by: Matt Brandt | last post by:
I am trying to get multiple threads to lock specific regions of a file. However, since each thread has the same PID, it appears that a lock by one thread does not block another thread from the same...
1
by: =?Utf-8?B?REZTIEhFTFAgV0FOVEVE?= | last post by:
We are currently testing the Microsoft technology of Distributed File System Replication with DFS Namespaces which is working perfectly, however from the Microsoft documentation surrounding these...
2
by: Sparky | last post by:
Hello! I am writing some software that will have many users accessing the same file resource at once for reading purposes only. I am programming on (Ubuntu) Linux and my question is in Windows, can...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.