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

The internals of flock()

by Anonymous Monk
on Dec 25, 2001 at 12:23 UTC ( [id://134270]=perlquestion: print w/replies, xml ) Need Help??

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

I want to use flock more often in my programming. I have thought that it would be nice to try to flock the file more than once. Because if i use flock(FH,1) or die.. then i'm afraid it will cause the program to error when it simply can't get the lock on the first try.

so I've been trying to think of control structures to allow for 10 trys, or more. I'm not sure I know a good number of times to loop back over, because all of this could happen in a fraction of a milisecond, and I will have tried to flock the file 1000 times and it still won't work.

now I'm also very ignorant when it comes to this, and I've heard that flock will try continously until it gets the lock, (if the file is already locked)... is this true.. how could I go about doing this?

Thanks monks

Replies are listed 'Best First'.
Re: The internals of flock()
by Aristotle (Chancellor) on Dec 25, 2001 at 22:24 UTC
    What you don't seem to realize is that flock blocks. That is, if you call it at a point when it cannot get a lock for normal reasons (ie the file is already locked by someone else), it will wait indefinitely. Your program will be stuck at the flock and won't continue executing until it has obtained the lock. When it continues, you will always have a lock.

    Except if something very strange happened, in which case it is good to die noisily at that point because that is a clear indicator that something is very wrong with the system. You're not likely to be able to function properly aftewards anyway.

    You can make flock nonblocking by adding a flag to the function call. However, this is only useful in specific circumstances. You shouldn't try it unless you understand why it is necessary.

    And a sidenote: please use Fcntl; and write your flock calls using constants. flock(FILE, LOCK_SH); is much clearer than flock(FILE, 1);. Also, you may want to look up the difference between LOCK_SH and LOCK_EX; it's important to know of the existance of both and to understand when to use which.

      True and good advise.

      The programmer can also use time outs and alarms to walk out of a blocked IO call. Examples of this exist in the perlipc documentation (man perlipc ; perldoc perlipc). (might only work safely on UNIX though).

      This will solve your retry problem too. On failing a blocking IO operation, the cautious always pause before trying again. Also be careful of the advisory nature of the flock. A program is not forced to wait unless they lock and unlock voluntarily at the correct place in their operations.
      -- termix

Re: The internals of flock()
by merlyn (Sage) on Dec 25, 2001 at 20:15 UTC
Re: The internals of flock()
by Alex the Serb (Monk) on Dec 25, 2001 at 12:55 UTC
    flock combined with LOCK_NB (Non-Blocked) and (LOCK_SH (Shared Lock) or LOCK_EX (Exclusive Lock)) would cause flock to return imediately, like that it would not wait infinately. You could also fork so that the parent would wait to get the lock and the child should do other things and you should be aware that fork does not create another instance of a lock. And if you are doing something using NFS (Network File System) with mounted disks and exported directories with files and if you need locking you can't use flock, there is another one called fcntl.

    Hope that this helped a little, if not .. sorry dude! I'm ignorant too!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2024-04-16 09:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found