[boulder.pm] open vs. sysopen

Walter Pienciak walter at frii.com
Wed Jan 17 20:39:50 CST 2001


On Wed, 17 Jan 2001, jeff saenz wrote:

> i saw an example  in the perl tutorial on flocking that implied you should use
> sysopen in combo with flock to write to a locked file.
>
> but I saw another example in the flock reference that used open...

> Justin Simoni wrote:
>
> > I'm going to ask a stupid question, but its been said that there aren't any
> > stupid questions, just stupid people,
> >
> > Then again, there's something that says philosophers create proverbs, fools
> > repeat them,
> >
> > saying that,
> >
> > what is the difference between the two built in Perl functions, open() and
> > sysopen() in terms of performance, cross platform usage, etc? To my
> > knowledge, open() is Perl's own way of opening thingies, and sysopen() is
> > straight from a C library. I know (think) also that sysopen() allows me to
> > use default permissions to a file, like this:
> >
> > sysopen(LIST, "$path/$file_name", O_RDWR|O_CREAT, $file_chmod)or die
> > "couldn't open $path/$file_name for reading: $!\n";
> >
> > I think I started sysopen on a project when I thought open() wouldn't work.
> > I think that the problem lay elsewhere, but I still have sysopen() peppered
> > in the program. any thoughts on these two functions? My program only runs on
> > *nix, although I would like to see NT supported with as little effort as
> > possible from me.
> >
> > Any help would be super appreciated,
> >
> > thanks,
> >
> > Justin

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.

The (new? I never noticed it before 5.6.0) perlopentut manpage
covers the open/sysopen thing in more detail than most people want.
Basically, sysopen gives you more granularity, but once the file
is open, it's open -- you don't need to use sysread just because
you used sysopen, for example.  If cross-platform compatibility is
an issue, be aware that nonstandard flags can be passed to sysopen,
and they'll work on your development machine but bomb on platform X.
Check out your fcntl manpages for details on your flag options.
(In my manpages, fnctl(2) isn't it, but fnctl(5) is.)

Walter





More information about the Boulder-pm mailing list