Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

file locking problem!

by kiseok7 (Beadle)
on Jun 29, 2001 at 18:09 UTC ( #92620=perlquestion: print w/replies, xml ) Need Help??

kiseok7 has asked for the wisdom of the Perl Monks concerning the following question:

this is my file locking function..


it seem to be failure often.

what is problem with my function...!?

and how about you, all?

show me the best file locking example..


#!perl my $file = "hanul.txt"; lock($file); open FILE, ">$file"; print FILE "you're locking...\n"; close FILE; unlock($file); # LoCk sub lock{ my $lFile = shift; my $LockFile = "$lFile.lock"; my $EndTime = time + 45; while (-e $LockFile && time < $EndTime) { } if (!open (LOCK,">$LockFile")){ return 0; } else{ chmod 0666, $LockFile; $locked = 'Y'; } } # uNLocK sub unlock { my $lFile = shift; my $LockFile; if ($locked eq 'Y'){ $LockFile = "$lFile.lock"; close (LOCK); if (-e $LockFile) { unlink ($LockFile); } $locked = 'N'; } }

Replies are listed 'Best First'.
Re: file locking problem!
by LD2 (Curate) on Jun 29, 2001 at 18:27 UTC
    In the future, a good place to start searching for anything here is using Super Search. To give you an understanding and a good introduction on File Locking, check out turnstep's tutorial File Locking.
Re: file locking problem!
by RatArsed (Monk) on Jun 29, 2001 at 18:22 UTC
      thanks:) I know flock but my OS is windows :(
        Still works; at least under ActiveState's distribution (for me, at least)


Re: file locking problem!
by Anonymous Monk on Jun 29, 2001 at 21:53 UTC
    This is a race condition. There is no reason to believe that because a file did not exist when the previous instruction was executed that it still does not exist (your operating system could have run another proccess in between). So when your while (-e $LockFile && time < $EndTime) loop ends there is no guarantee that the file does not exist (infact, even if there were no race condition, you have a time limit on this loop but never check to see if the loop terminated because time was up or if it terminated because the file doesn't exist -- but like I said, checking to see if the file doesn't exist would still be a bug). If you are unable to use flock(), you could see if you're able to use sysopen with the O_EXCL flag which will guarantee that the file does not exist when opened (if it does exist, the sysopen call will fail and you'll probably want to handle this rather than simply returning from the function)
Re: file locking problem!
by Chady (Priest) on Jun 29, 2001 at 18:23 UTC

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://92620]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2023-03-25 08:38 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (62 votes). Check out past polls.