Perl fork and kill - kill(0, $pid) always returns 1, and can't kill the child

后端 未结 6 878
孤独总比滥情好
孤独总比滥情好 2021-01-06 07:16

I am running a perl script on a Linux host. I\'m trying to write a script that forks, where the child starts a program that takes forever and the parent times out after 5 s

6条回答
  •  一向
    一向 (楼主)
    2021-01-06 07:45

    From perldoc fork:

    If you fork without ever waiting on your children, you will accumulate zombies. On some systems, you can avoid this by setting $SIG{CHLD} to "IGNORE" .

    I was able to get the desired behavior when adding $SIG{CHLD} = 'IGNORE'; to the top of your code.

    [ben@imac ~]$ perl test.pl
    numKilled: 1
    [ben@imac ~]$ 
    

    Alternatively, adding waitpid($childPid, 0); after kill did the trick as well.

提交回复
热议问题