[rochester-pm-list] flock question

Pat havoc at eznet.net
Thu Jun 17 09:46:27 CDT 1999


By default flock blocks(stops and waits for the lock to be released).
If you wish to not wait, add the flag LOCK_NB and flock will return
immediately.

In my experience LOCK_NB is not needed for most implementations under CGI.
But for heavy usage and/or a robust solution use LOCK_NB and implement a 
wait manually and kill threads that seem to have hung (or any solution
that seems reasonable :-).

Good Luck,

--Patrick Ludwig
Xerox Internet Document Services

On Thu, Jun 17, 1999 at 10:14:15AM -0400, Justin C. Sherrill wrote:
> Here's something I never understood about flock(), and I haven't really had
> the chance to test it out.  Could someone on this list hazard an answer for
> me?  For instance, I have this chunk of code:
> 
> open (DB_FH, ">&=$fd") or print "Cant open $fd : $!\n";
> # I have no clue what +<&= does.  It's in the Cookbook.
> flock (BD_FH, LOCK_EX);
> # do stuff to file here that I took out for space
> $db->sync;
> flock(DB_FH, LOCK_UN);
> close (DB_FH);
> 
> What I'm not sure about is this: I can get an exclusive lock here, which is
> fine.  However, this script is running as part of a CGI I wrote, and so
> multiple instances may run at the same time depending on traffic.  Do I have
> to do some sort of check for an existing exclusive lock before obtaining
> one, or will a new lock coming from another process with this script
> automatically wait until the first process releases the lock?
> 
> Justin C. Sherrill
> Rochester Road Runner Webmaster
> http://www.rochester.rr.com/
> "Think slow, type fats"
> 



More information about the Rochester-pm mailing list