CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Frequently Asked Questions:
What can't I just open(FH, ">file.lock")?

 



Jasmine
Administrator

Jan 26, 2001, 10:24 AM

Post #1 of 1 (1339 views)
What can't I just open(FH, ">file.lock")? Can't Post

(From the Perl FAQ)

What can't I just open(FH, ">file.lock")?

A common bit of code NOT TO USE is this:

Code
    sleep(3) while -e "file.lock";      # PLEASE DO NOT USE 
open(LCK, "> file.lock"); # THIS BROKEN CODE

This is a classic race condition: you take two steps to do something which must be done in one. That's why computer hardware provides an atomic test-and-set instruction. In theory, this ``ought'' to work:

Code
    sysopen(FH, "file.lock", O_WRONLY|O_EXCL|O_CREAT) 
or die "can't open file.lock: $!":

except that lamentably, file creation (and deletion) is not atomic over NFS, so this won't work (at least, not every time) over the net. Various schemes involving involving link() have been suggested, but these tend to involve busy-wait, which is also subdesirable.



 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives