#include #include #include #include #include #include int main() { int status; void *ret; pid_t pid, p2; int pp[2]; if (pipe(pp)) { perror("pipe"); exit(EXIT_FAILURE); } switch (pid=fork()) { case -1: perror("fork"); exit(EXIT_FAILURE); case 0: // Child close(pp[0]); switch (pid=fork()) { case -1: perror("fork"); exit(EXIT_FAILURE); case 0: // Child sleep(10); break; default: exit(EXIT_SUCCESS); } exit(EXIT_SUCCESS); default: // Parent close(pp[1]); ret=signal(SIGCHLD, SIG_IGN); if (ret==SIG_ERR) { perror("signal"); } else if (ret==SIG_IGN) { fputs("was: sig_ign\n", stderr); } else if (ret==SIG_DFL) { fputs("was: sig_dfl\n", stderr); } else { fputs("unknown initial signal handler\n", stderr); } break; } sleep(1); for ( ; 1; ) { char buf; switch (read(pp[0], &buf, sizeof buf)) { case 0: // EOF exit(EXIT_SUCCESS); case -1: perror("read"); exit(EXIT_FAILURE); case 1: continue; } } for ( ; 1; ) { p2=waitpid(pid, &status, WUNTRACED); if (p2==-1) { if (errno==ECHILD) break; perror("wait"); exit(EXIT_FAILURE); } if (WIFEXITED(status)) { status=WEXITSTATUS(status); if (status==0) { puts("Exited successfully"); } else { printf("Exited with status: %d\n", status); } } else if (WIFSIGNALED(status)) { #ifndef WCOREDUMP #define WCOREDUMP 0 #endif printf("Killed by signal: %d%s\n", WTERMSIG(status), WCOREDUMP(status)?"; dumped core":""); } else if (WIFSTOPPED(status)) { status=WSTOPSIG(status); printf("Stopped by signal: %d\n", status); // kill(getpid(), status); } } exit(EXIT_SUCCESS); }