[boulder.pm] open vs. sysopen

Walter Pienciak walter at frii.com
Thu Jan 18 15:17:46 CST 2001


On Thu, 18 Jan 2001, jeff wrote:

> is the lisa tutorial available to the public.
>
> > It turns out that locking a file for writing without leaving a
> > race-condition problem is more difficult than expected, and from year
> > to year I've noticed the "right" advice changing.  The latest I've seen
> > is what I had in the nicevi code:
> >
> > sysopen(LAUNCH_CODES, "$f", O_WRONLY | O_CREAT) or die "Urk!  $!";
> > flock(LAUNCH_CODES, LOCK_EX|LOCK_NB) or die "Aah! $!";
> >
> > # doodle doodle doodle
> >
> > flock(LAUNCH_CODES, LOCK_UN) or die "Yarrgh! $!";
> > close(LAUNCH_CODES);
> >
> > In the above code, you don't need the |LOCK_NB
> > but the flock call will block (the program will hang) until
> > the exclusive lock is obtained if you do that.  Personally,
> > I don't.
> >
> > I don't pretend to understand the minutiae of why exactly that
> > sequence of function calls and flags does the job, but I do trust
> > that Tom Christiansen (I got the above code in a LISA tutorial) has
> > a better understanding of it than I do.  So, yes, it's a bit of
> > cargo-cult programming.
> >
>
> --
> T_r_i_k_M_e_d_i_a
> http://www.trikmedia.com

Sorry, it's copyrighted stuff.  And worthwhile, too, I should add,
if anyone gets the chance to sit in a class someday.  There's a variant
of this code discussed at

http://www.perl.com/pub/doc/manual/html/pod/perlfaq5.html#How_can_I_lock_a_file_

Walter




More information about the Boulder-pm mailing list