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?
If child's
stdout
(or any other stream) is connected to parent'sstdin
, it is natural that parent receives theEOF
.Alternatively parent may die from
SIGPIPE
(broken pipe) signal, if parent doesn't don't handle it.