处理SIGCHLD会将EOF退还给父亲,为什么? [英] Handling SIGCHLD will return EOF to the father, why?

查看:91
本文介绍了处理SIGCHLD会将EOF退还给父亲,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小的外壳,可以创建子对象(使用fork()),并使其通过execvp执行一些命令。
它还支持&选项,以便父亲可以同时运行其他命令。
当一个孩子死了时,我想在控制台上以及孩子pid上写它,所以我为SIGCHLD定义了信号处理程序:

I have a small shell that creates children (with fork()) and make them execute some commands with execvp. It support also the & option so the father can run other commands in the meanwhile. When a child die i want to write that on the console and also the child pid, so i defined a sighandler for SIGCHLD:

void backdeadchild(int sig)
{
 int pid, exitstat;
 pid = wait(&exitstat);
 printf("Child with pid: %d died", pid);
}

问题在于,在处理程序打印消息后,父亲也会关闭。
我已经设法理解SIGCHLD在stdin中返回EOF(并且每当父亲收到EOF时,它就会关闭),但是SIGCHLD为什么返回EOF?
如果是正常的话..如何避免父亲倒闭?

The problem is that after the handler prints the message also the father closes. I've managed to understand that SIGCHLD returns an EOF in stdin (and whenever father receives EOF it closes), but why SIGCHLD returns EOF? If it's normal.. how to avoid that the father closes?

推荐答案

如果孩子的 stdout (或任何其他流)连接到父级的 stdin ,父级自然会收到 EOF

If child's stdout (or any other stream) is connected to parent's stdin, it is natural that parent receives the EOF.

或者,如果父母没有,父母可能会死于 SIGPIPE (断线)信号不要处理它。

Alternatively parent may die from SIGPIPE (broken pipe) signal, if parent doesn't don't handle it.

这篇关于处理SIGCHLD会将EOF退还给父亲,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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