Perl 5.002, Linux 1.2.13:
I came up with a really nice piece of Perl code for forking
off an external process and being able to check, reliably, at any
time, whether that process has died... basically, the parent
does a double-fork, and the grandchild establishes an exclusive
lock (LOCK_EX) on a file descriptor opened by the parent. The parent
then checks on the child simply by trying to get a lock
(LOCK_SH|LOCK_NB) on that file descriptor.
Now, in Unix, we have the following:
* Locks aren't preserved across a fork (which is why
I open the LOCK_EX in the grandchild)
* Locks *ARE* preserved across an exec(), and...
* Open file descriptors *ARE* preserved across both
a fork and an exec().
However, in my Perl code, it's clear that the grandchild has the
lock ONLY SO LONG AS IT DOESN'T EXEC ANOTHER PROGRAM... once I
call exec(), like this:
exec $progpath, $arg1, ..., $argn;
I lose the lock! It doesn't matter whether the thing I exec
is a Perl or /bin/sh script. And yes, I did check, and the
close-on-exec flag is *not* set.
Can someone tell me if there's something BLINDINGLY OBVIOUS
I'm missing, or does Perl do a silent fork() (which would lose
the lock) inside exec(), or what?
I notice that under Linux, vfork() is a synonym for fork(), which
is listed (rightly so) as a bug (they behave very differently)...
could this issue be screwing Perl up???
/ __/ _/ / / , / Hughes STX, NASA/Goddard Space Flight Cntr.
/___/_/ \ /\ /___
/_/ /_____/ http://www.mcs.net/~eryq