[Jerusalem.pm] $SIG{CHLD}

Ephraim Dan ephraim.dan at exlibris.co.il
Thu Nov 4 01:20:12 PST 2004


Eitan,

Use exec() instead of system(), and then the child process will be replaced
by the system call, instead of forking a separate process.

Replace:

    system "csh -f external_program";

with:

    exec "csh", "-f", "external_program"
        or die "Can't exec() ($!)";
    
Note that I used the list form of exec(), which is much safer since you
avoid the shell and it's associated problems.  And you should make sure the
exec() doesn't fail.

- Ephraim


> -----Original Message-----
> From: Eitan Schuler [mailto:Eitan.schuler at exlibris.co.il]
> Sent: Thursday, November 04, 2004 9:18 AM
> To: Jerusalem pm (E-mail); 'perl at perl.org.il'
> Subject: [Jerusalem.pm] $SIG{CHLD}
> 
> People,
> I would like to run an external program (with system) in a child process.
> If
> the time elapsed is more than 5 seconds I pretend that the child process
> is
> stuck and I would like to kill it from the parent. I wrote this code:
> 
> 	$SIG{CHLD} = sub {wait ()};
> 
> 	$pid = fork ();
> 	if ($pid == 0){
> 	    system "csh -f external_program";
> 	}
> 	else{
> 	    if ($pid > 0){
> 	        sleep 5;
> 	        kill 9 => $pid;
> 	        exit;
> 	    }else {
> 	        die "cannot fork";
> 	    }
> 	}
> 
> This works nice, the 'kill' kills the child perl process. BUT the "csh -f
> "
> becomes a zombie, because I killed it's parent (of course).
> I would like to kill it as well, but I don't know it's pid.
> Anybody has an idea on
> 	- how to get the "csh -f" 's pid
> 	- other solution for the problem
> ?
> 
> Thanks
> Eitan
> 
> _______________________________________________
> Jerusalem mailing list
> Jerusalem at perl.org.il
> http://perl.org.il/mailman/listinfo/jerusalem



More information about the Jerusalem mailing list