杀死分叉的孩子会杀死父母吗? [英] Killing forked child kills parent?

查看:68
本文介绍了杀死分叉的孩子会杀死父母吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我进入了这个奇怪的行为,在那里我有我的主程序和一个分叉的孩子.它们的管道是这样的(数字是文件描述符):

I am into this weird behaviour where I have my main program and a forked child. They are piped like this(the numbers are file descriptors):

 ___parent___
|            |                     ____child_____
| 0 stdin    |                    |              |
| 1 pipe1[1]----------.           |  1 stdout    |
| 2 pipe2[1]----------.\          |  2 stderr    |
|____________|         \`----------> 3 pipe1[0]  | 
                        `----------> 5 pipe2[0]  |
                                  |______________|

因此父级从 stdin 获取输入,但将 stdout stderr 重定向到两个管道.该孩子已关闭其 stdin ,并改用管道的读取端.

So parent gets input from stdin but redirects stdout and stderr to two pipes. The child has closed its stdin and uses the read ends of the pipes instead.

然后我有一个功能可以杀死孩子:

Then I have a function to just kill the child:

void killChild(){
  printf("Killing %d\n", (int)childID);
  fflush(stdout);
  kill(childID, SIGKILL);
  waitpid(childID, NULL, 0);   // getting rid of the zombie
}

孩子被成功杀害,但问题是父母本身也被杀害.我检查了孩子的PID,它是正确的.

The child gets succesfully killed but the problem is that the parent itself gets killed as well. I checked the PID of the child and it's correct.

那父母为什么死了?

推荐答案

父级在子级退出后尝试写入其fd 1或fd 2的任何尝试都会导致内核将SIGPIPE发送给父级. SIGPIPE的默认行为是进程终止.可能就是这样.

Any attempt by the parent to write to its fd 1 or fd 2 after the child exits will result in the kernel sending SIGPIPE to the parent. The default behavior for SIGPIPE is process termination. That's probably what's happening.

这篇关于杀死分叉的孩子会杀死父母吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆