n***l 发帖数: 9 | 1 我有下面简单的code
pid = fork();
if (pid == 0) {
printf("Child process running\n");
} else if (pid < 0) {
printf("Failed on fork()\n");
exit(0);
} else {
... ...
if (kill(pid, SIGKILL) < 0) {
perror("Failed on kill()");
}
while(1){ ... ... };
}
运行后发现kill失败了。比如Child process的PID是4726,
在命令行“ps 4726”,结果是空的。但是用“ps”可以看到
child process。在命令行用“kill -9 4726”,系统报
错:No such process。
谁知道这到底是怎么会事?Thanks. | T********r 发帖数: 6210 | 2 What's the code after that else {/*parent process*/}? If there's no
code after that, the child process will exit and become zombie after
the printf statement. You cannot kill the child process any more. To
exit the child process cleanly, you should call wait() in parent
process. To kill the child process, you should let child alive, i.e.
sleep infinitely.
【在 n***l 的大作中提到】 : 我有下面简单的code : pid = fork(); : if (pid == 0) { : printf("Child process running\n"); : } else if (pid < 0) { : printf("Failed on fork()\n"); : exit(0); : } else { : ... ... : if (kill(pid, SIGKILL) < 0) {
|
|