On Wed, Oct 31, 2001 at 01:26:47AM -0500, Jona Andersen wrote:
> > On Tue, Oct 30, 2001 at 07:40:47PM -0500, John Tobey wrote:
> > > I would guess so, but I would be more confident if you added ||
> > > die("LOCK_EX: $!") after flock.
> >
> > Two potentially non-obvious but traditional
> > "flock" semantics are that it waits indefinitely
> > until the lock is granted, and ...
>
> In that case, wouldn't an eval/alarm block work?
>
> I mean,
>
> eval {
> local $SIG{ALRM} = sub { die "Hurg. Can't lock file" };
>
> alarm $seconds_to_wait;
> flock($fh, LOCK_EX);
> alarm 0;
> }
If by "work" you mean do something useful, yes, this might "work".
However, it misses my point (that flock() returns a value that one
ought not ignore). It may fail and return undef if the filesystem
does not support locks, because of some resource shortage, or because
the Yankees won. It is really easy in Perl to check a return value
that you assume to be always true: add "or die".
I've used flock() in perhaps half a dozen Perl programs over my
career, and every time I reread perldoc -f flock.
By the way, before your program goes to the trouble of setting an
alarm, it might try flock($fh, LOCK_EX|LOCK_NB). (Then you *really*
have to check what it returns.)
-John