Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

File Locking Tricks and Traps

by Dominus (Parson)
on Mar 06, 2003 at 04:26 UTC ( #240805=perlmeditation: print w/ replies, xml ) Need Help??

I just gave a talk on File Locking Ticks and Traps to the Philadelphia area Linux Users' Group. File locking is an area of frequent misery for Perl programmers, and all the examples in my talk are in Perl, so I expect that the slides will be of general interest. Here they are.

--
Mark Dominus
Perl Paraphernalia

Comment on File Locking Tricks and Traps
Re: File Locking Tricks and Traps
by grantm (Parson) on Mar 06, 2003 at 07:33 UTC

    Cool. I've often used flock to ensure only one instance of a program is running, but I never thought of opening $0 and locking that - consequently each of my scripts litters the filesystem with a matching .lck file.

      grantm,
      There is a better reason to use $0 then leaving the filesystem littered with lock files. I pointed out to Nitrox here that in Unix, it is possible to delete a locked file, and that he should use the process table or semaphores instead. He wanted to know if there was a way around this, and I replied with this, which is an attempt at security through obscurity.

      What Dominus is doing is beautiful. By locking $0, it can't be deleted - or rather it can be, but then there wouldn't be a program to run Dominus++.

      Cheers - L~R

Re: File Locking Tricks and Traps
by Anonymous Monk on Mar 06, 2003 at 12:25 UTC

    On slide 5 you state:

    LOCK_UN should be considered for experts only

    Could you provide an example of how it would be beneficial for an 'expert' to use it? Would it not be more appropriate to say "LOCK_UN shouldn't be used. Period.?"

      Could you provide an example of how it would be beneficial for an 'expert' to use it? Would it not be more appropriate to say "LOCK_UN shouldn't be used. Period.?"
      If several scripts are appending to the end of a file multiple times, they might make use of LOCK_UN to take turns locking the file, seeking to the end, writing to, then unlocking the file. Though in most cases its better just to close, reopen, and lock the file again (e.g., for the purpose of writing log files, especially when log files are cycled).

      Another use might be to lock $0 so that no other process can run some part of a script at the same time, but then at some point in the script it might be deemed safe (and beneficial) to allow other processes to run the script and so use LOCK_UN. Again, its a matter of choice whether to use LOCK_UN or just close, reopen, and lock the file. Maybe someone else has better examples...

Re: File Locking Tricks and Traps
by Anonymous Monk on Mar 06, 2003 at 12:45 UTC
    You missed a ton of issues that cause lots of fun. For instance you sometimes cannot lock a file for unexpected reasons. People often don't check $!, and will take a while to track down that. (People who don't check failure are simply hosed.)

    Another biggie is that people close a file while they have locks on it open. This may happen for them unexpectedly (eg they have locked a dbm which closes and opens the file behind their back) or because they don't realize that this loses the lock. (Opening the file on a second filehandle and closing that loses the lock on the first still-open filehandle. So that attempted workaround fails!)

    Otherwise useful if basic.

      Of course it's basic. Did you expect a 10 minute talk to go into incredible detail about file locking? It's just not possible.

      Unfortunately, I prepared 15 minutes' worth of material for a 10 minute talk.

      So that's more than Dominus should have offered.

      John J Reiser
      newrisedesigns.com

Re: File Locking Tricks and Traps
by Nitrox (Chaplain) on Mar 11, 2003 at 16:42 UTC
    On slide 6 this line:
    open SELF, "< $0" or die ...;
    Should be:
    open SELF, "+< $0" or die ...;
    Due to the fact that some OS's won't allow an exclusive lock on a file that has been opened for read only.

    -Nitrox

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://240805]
Approved by blokhead
Front-paged by VSarkiss
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (13)
As of 2014-07-31 09:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (248 votes), past polls