Handling SIGCHLD will return EOF to the father, wh

2019-05-31 12:57发布

问题:

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.



标签: c fork wait eof