[rochester-pm-list] flock question

Pat havoc at eznet.net
Thu Jun 17 09:48:42 CDT 1999


Can't believe I didn't spot that, Brian is absolutely correct, never
unlock files, just close 'em.

--pat


On Thu, Jun 17, 1999 at 09:50:20AM -0400, Brian Mathis wrote:
> Yes, if your file is flock()ed by another process, your current process
> will just wait until it's free.  It will wait at the flock().
> 
> As a side note, you don't want to unlock the file, just close it.
> Unlocking then closing actually creates a race condition, because not all
> data is flushed to the file until the close.  A close() automatically
> unlocks a file as well.
> 
> Brian Mathis
> 
> 
> On Thu, 17 Jun 1999, 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