niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » Unix internals
File Locking
Post new topic   Reply to topic Page 1 of 1 [9 Posts] View previous topic :: View next topic
Author Message
Nitzan Shaked
*nix forums beginner


Joined: 27 Jun 2005
Posts: 8

PostPosted: Sat Jul 02, 2005 6:22 am    Post subject: File Locking Reply with quote

(Hope this is not considered off-topic)

I wish to perform a seemingly simple task, but all the FAQ reading and
example browsing don't seem to offer a solution.

I wish to create mutual exclusion on a resource I have which several
processes might want to change, let's assume for now that all are on
the same machine.

I do *not* wish to leave my lock files hanging around the file-system
any longer than necessary (I have hundreds of thousands, if not more,
such resouces).

The "text book" solution might be:
1) fd = open( "my.lock", O_RDONLY | O_CREAT );
2) flock( fd, LOCK_EX );
3) do_something_with_the_resource();
4) unlink( "my.lock" );
5) close( fd );

This FAILS for a subtle reason: after having done unlink(), other
processes trying to run the above code will create a new file by the
same new and will lock *that* file (which has a different file-table
entry, of course). ANOTHER process, which trying to get flock() before
the unlink, will also succeed right after the close(), so now 2
processes think they have exclusive lock.

Removing the unlink() call solves this, but leaves my lock files in the
file-system for all eternity.

SysV IPC, multiple lock files, etc -- all don't seem to offer solace :)

What's the right way to do this?

tia,
Nitzan
Back to top
Barry Margolin
*nix forums Guru


Joined: 24 Feb 2005
Posts: 323

PostPosted: Sat Jul 02, 2005 3:14 pm    Post subject: Re: File Locking Reply with quote

In article <1120292556.251595.173690@o13g2000cwo.googlegroups.com>,
nitzan.shaked@gmail.com wrote:

Quote:
(Hope this is not considered off-topic)

I wish to perform a seemingly simple task, but all the FAQ reading and
example browsing don't seem to offer a solution.

I wish to create mutual exclusion on a resource I have which several
processes might want to change, let's assume for now that all are on
the same machine.

I do *not* wish to leave my lock files hanging around the file-system
any longer than necessary (I have hundreds of thousands, if not more,
such resouces).

The "text book" solution might be:
1) fd = open( "my.lock", O_RDONLY | O_CREAT );
2) flock( fd, LOCK_EX );
3) do_something_with_the_resource();
4) unlink( "my.lock" );
5) close( fd );

This FAILS for a subtle reason: after having done unlink(), other
processes trying to run the above code will create a new file by the
same new and will lock *that* file (which has a different file-table
entry, of course). ANOTHER process, which trying to get flock() before
the unlink, will also succeed right after the close(), so now 2
processes think they have exclusive lock.

Removing the unlink() call solves this, but leaves my lock files in the
file-system for all eternity.

I must be missing something. Why don't you just swap the order of the
close() and unlink() calls?

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
Back to top
Nitzan Shaked
*nix forums beginner


Joined: 27 Jun 2005
Posts: 8

PostPosted: Sat Jul 02, 2005 5:48 pm    Post subject: Re: File Locking Reply with quote

Thanks for the reply, but this simply causes another race: if I close()
first, then I am releasing the lock. By the time I will unlink()
another process has already got the lock (and long since has opened the
file, btw, since this is done prior to locking) and I will be
unlink()-ing its file.
Back to top
Barry Margolin
*nix forums Guru


Joined: 24 Feb 2005
Posts: 323

PostPosted: Sat Jul 02, 2005 9:09 pm    Post subject: Re: File Locking Reply with quote

In article <1120333718.686226.150700@o13g2000cwo.googlegroups.com>,
"Nitzan Shaked" <nitzan.shaked@gmail.com> wrote:

Quote:
Thanks for the reply, but this simply causes another race: if I close()
first, then I am releasing the lock. By the time I will unlink()
another process has already got the lock (and long since has opened the
file, btw, since this is done prior to locking) and I will be
unlink()-ing its file.

Good point. I guess the solution is to leave the file around forever.
Think of it like a required configuration file that's needed whenever
the application is used, and it won't feel so bad to have it around
permanently.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
Back to top
Andrew Smallshaw
*nix forums beginner


Joined: 23 Feb 2005
Posts: 26

PostPosted: Sat Jul 02, 2005 11:39 pm    Post subject: Re: File Locking Reply with quote

On 2005-07-02, Nitzan Shaked <nitzan.shaked@gmail.com> wrote:
Quote:
Thanks for the reply, but this simply causes another race: if I close()
first, then I am releasing the lock. By the time I will unlink()
another process has already got the lock (and long since has opened the
file, btw, since this is done prior to locking) and I will be
unlink()-ing its file.

Why not avoid the locking issue entirely?

fd = open("my.lock", O_RDONLY | O_CREAT | O_EXCL);

The O_EXCL causes the open to fail if the file exists.

--
Andrew Smallshaw
andrews@sdf.lonestar.org
Back to top
Nitzan Shaked
*nix forums beginner


Joined: 27 Jun 2005
Posts: 8

PostPosted: Sun Jul 03, 2005 5:02 am    Post subject: Re: File Locking Reply with quote

Smile I've already almost "given in" to the not-feeling-bad strategy,
though I think that this morning on my way to work I've found a
solution. If it works out I'll post it.
In any case, I have hundreds of thousands of files, if not more. At the
very least this is a small pinch in my tender heart Smile
Back to top
Nitzan Shaked
*nix forums beginner


Joined: 27 Jun 2005
Posts: 8

PostPosted: Sun Jul 03, 2005 5:03 am    Post subject: Re: File Locking Reply with quote

As I said before, O_EXCL is not good because I am using the file for
actually locking something else. Thus I want to blocked (sleep while
waiting), and O_EXCL does not give me that. It will force polling.
Back to top
Andrew Smallshaw
*nix forums beginner


Joined: 23 Feb 2005
Posts: 26

PostPosted: Mon Jul 04, 2005 9:17 pm    Post subject: Re: File Locking Reply with quote

On 2005-07-03, Nitzan Shaked <nitzan.shaked@gmail.com> wrote:
Quote:
As I said before, O_EXCL is not good because I am using the file for
actually locking something else. Thus I want to blocked (sleep while
waiting), and O_EXCL does not give me that. It will force polling.

The more you describe this problem, the more it strikes me as a case of
finding the questions to the answers - you hadn't mentioned it must block
BTW. What is it you're trying to do? Lets see:

+ Hundreds of thousands of 'resources'

+ Presumably many or at least relevant ones are going to be locked at any
given time, and for an extended period.

+ Can't afford to spin on opening a file.

Are you sure an DBMS isn't more suitable for your purposes? Programmers
have routinely abused the filesystem as a cheap database for years: it
often works, but sometimes not and it strikes me that you're using the
wrong tool for this particular job.

--
Andrew Smallshaw
andrews@sdf.lonestar.org
Back to top
venku
*nix forums beginner


Joined: 07 Jul 2005
Posts: 5

PostPosted: Thu Jul 07, 2005 12:37 pm    Post subject: Re: File Locking Reply with quote

Hi,
I feel that on the following steps ..
1) after having done unlink()
2) other processes trying to run the above code will create a new file
by the same new and will lock *that* file (which has a different
file-table entry, of course)
3)ANOTHER process, which trying to get flock() before the unlink, will
also succeed right after the close(), so now 2 processes think they
have exclusive lock

2 & 3 would not happen both at same time because
if 3) i.e if another process waiting (which should increment file ref
count of my.lock), there no chance of having new file creation which
is 2.
Am i misunderstood ???
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [9 Posts] View previous topic :: View next topic
The time now is Fri Jan 09, 2009 6:25 am | All times are GMT
navigation Forum index » Programming » Unix internals
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts Running php file everyday on scheduled time sachin PHP 1 Fri Jul 21, 2006 12:49 pm
No new posts Regarding thesaurus iso file Srikanth modules 0 Fri Jul 21, 2006 10:42 am
No new posts how can i get a file descriptor not used? mars system 0 Fri Jul 21, 2006 7:41 am
No new posts small GTK "Open file" dialog David Siroky Debian 0 Fri Jul 21, 2006 7:30 am
No new posts Trouble Declaring 3D Array in Header File free2klim C++ 1 Fri Jul 21, 2006 4:07 am

Debt Consolidation | Mobile Phones | Deaf | Loans | Cash Advance Loan
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.2387s ][ Queries: 16 (0.1466s) ][ GZIP on - Debug on ]