我尝试过无限循环的简单程序,我使用PERL脚本运行它并在10次睡眠后试图杀死它,但是我的程序没有杀死子进程并且在无限循环中运行.
// C代码:$ cat loop.c
#include <stdio.h>
int main(){
while(1){
printf("Infinite loop\n");
}
return 0;
}
$ gcc loop.c -o loop.bin
Run Code Online (Sandbox Code Playgroud)
// PERL CODE $ cat timeout.pl
#!/usr/bin/perl
use strict;
use warnings;
use POSIX ":sys_wait_h";
use Time::HiRes qw(sleep);
if(!defined( my $pid = fork())) {
die "Cannot fork a child: $!";
} elsif ($pid == 0) {
print "Printed by child process\n";
system("./loop.bin");
} else {
print "Printed by parent process\n";
sleep(10);
my $ret = waitpid($pid, WNOHANG);
if ($ret == 0){ …Run Code Online (Sandbox Code Playgroud) perl ×1