Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Is it needed to lock open file for write?

Reply
Thread Tools

Is it needed to lock open file for write?

 
 
news.t-com.hr
Guest
Posts: n/a
 
      12-01-2005
Is it needed to lock open file for write?

use Fcntl qw(:flock);
open(FH, ">codes.txt") or die "can't open codes.txt $!";
flock(FH, LOCK_EX) or die "can't flock codes.txt $!";

Or is also good only to use command open

open(FH, ">codes.txt") or die "can't open codes.txt $!";


TNX


 
Reply With Quote
 
 
 
 
it_says_BALLS_on_your forehead
Guest
Posts: n/a
 
      12-01-2005

news.t-com.hr wrote:
> Is it needed to lock open file for write?
>
> use Fcntl qw(:flock);
> open(FH, ">codes.txt") or die "can't open codes.txt $!";
> flock(FH, LOCK_EX) or die "can't flock codes.txt $!";
>
> Or is also good only to use command open
>
> open(FH, ">codes.txt") or die "can't open codes.txt $!";
>


AFAIK, locking is really only necessary if there is a possibility of
two processes operating on the same file at the same time--in your case
the operation would be a file write.

 
Reply With Quote
 
 
 
 
Rhugga
Guest
Posts: n/a
 
      12-01-2005
The only problem with file locking is that not all utilities/commands
honor file locking.

For example, let's say your perl program is running and has an
exlcusive lock on /etc/password.

Then let's say, for example, that yppasswd doesn't honor exclusive
locks, and just does it's thing regardless. The user jsmith decides to
change his password while your program is doing it's thing. You now run
into the risk of 1) corrupting the file completely or 2) losing your
changes to the file, or 3) the user's password change being lost.

You'd be surprised at the number of standard commands that don't honor
exclusive locks. I think these commands are become more rare, but all
it takes is 1 to make things go pear shaped. For Solaris, somewhere on
bigadmin or sunsolve there is a list of commands and/or libraries that
fall into this category. I think the list has been decreasing over the
years but I'm sure there are still some.

This is one reason why its recommended when tweaking /etc/passwd or
/etc/shadow from a script that you make a copy of the file, modify the
copy, the move the modifed copy into place. (even this has its risks
but they are very minimized)

In your case, however, if your code will be the only one ever modifying
the file, and your locking just to prevent 2+ running copies from
stepping on each other, than it would work for you.

 
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
Read-Write Lock vs primitive Lock() k3xji Python 7 12-30-2008 10:19 PM
Application.Lock()/UnLock() or lock(Application) nano2k ASP .Net Web Services 2 08-09-2007 09:31 AM
Threading - Why Not Lock Objects Rather than lock the interpreter Fuzzyman Python 3 12-05-2003 10:43 PM
RE: Threading - Why Not Lock Objects Rather than lock theinterpreter Robert Brewer Python 0 12-05-2003 05:33 PM
Best way to lock and open a file Ron Vecchi ASP .Net 3 07-28-2003 01:24 AM



Advertisments