Handling SIGCHLD will return EOF to the father, why?

岁酱吖の 提交于 2019-12-08 08:08:21

问题


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);
}

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?


回答1:


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

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



来源:https://stackoverflow.com/questions/2325711/handling-sigchld-will-return-eof-to-the-father-why

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!