Reputation: 25
The following will output :
Start of script
PID=29688
Start of script
PID=0
Running child process 1
Done with child process
PID=29689
Start of script
PID=0
Running child process 1
Done with child process
It's working as intended, however I would like to kill the previous child PID.
How can a kill the PID of the child with out kiling the MAIN ?
Thank you !
my $bla = 1;
while (1) {
print "Start of script\n";
run_sleep();
}
sub run_sleep {
sleep(3);
my $pid = fork;
return if $pid; # in the parent process
print("PID=" . $pid . "\n");
print "Running child process " . $bla++ . "\n";
exit(0); # end child process
}
Upvotes: 1
Views: 1438
Reputation: 3484
When you fork a child, and then fail to wait() on it, it will become a defunct process (a zombie in Unix parlance) when it exits. You'll notice that its parent process ID becomes 1, and it will not go away until the OS rebooted.
So the traditional pseudocode for forking looks something like this:
if ($pid = fork()) {
# pid is non-zero, this is the parent
waitpid($pid) # tell OS that we care about the child
do other parental stuff
}
else {
# pid is 0 so this is the child process
do_childish_things()
}
Your code doesn't do that, so you're probably getting zombies, and then getting frustrated that you can't get rid of them.
Upvotes: 2