LPM: Neat alarm trick

Frank Price fprice at upended.org
Wed Oct 4 18:51:19 CDT 2000


On Wed, Oct 04, 2000 at 02:32:49PM -0400, Mik Firestone wrote:
> 
> I have a few suggestions.  The system call is a convenience - it is really
> just forks a child and spins the parent into a wait loop while the child
> exec's the shell call.  What if you were to do it yourself?  

OK, seems like it should work.  A few questions:

>     while ( 1 ) {
> 	if ( $spid = fork() ) {
> 	    # I am the parent, do a few things and waitpid
> 	    select undef,undef,undef,0.5;  # Keep my loop loose

So this 4-arg form of select does the same thing as the alarm, right?
Not quite sure why it's necessary if the waitpid doesn't block.

> 	    $result = waitpid( $spid, 1 ); # non-blocking
> 	    last LOOP if ( $result == $spid );  # Child process exited
> 	    if ( ++$counter > 20 ) {	       # 10 seconds
> 		$timeout = 1;
> 		kill HUP, $spid;

Is kill HUP sufficient or should it be kill TERM?

> This WILL NOT work if the exec causes a shell to spawn - you will then be at
> least a few PIDs removed from the original child process.  See perlfunc:exec
> for a complete explanation of when exec spawns shells and when it won't.  This
> is also going to be somewhat dependant on the quirks of your execvp(3) call.

In that case kill -$$ actually might be best, it seems, since it
should kill all the related processes.  Think that's right?

> HTH ( but no promises :)

Sure!  Thanks,

-Frank.
-- 
Frank Price | fprice at upended.org | www.upended.org/fprice/
GPG key: www.upended.org/fprice/gpg.asc | E Pluribus Unix




More information about the Lexington-pm mailing list