Handling SIGCHLD will return EOF to the father, wh

2019-05-31 13:10发布

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?

标签: c fork wait eof
1条回答
女痞
2楼-- · 2019-05-31 13:15

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.

查看更多
登录 后发表回答