#include #include #include #include #include int main() { pid_t child_pid, pid; int status; switch (child_pid=fork()) { case (pid_t)-1: perror("fork"); /* fork fails */ break; case (pid_t)0: printf("Child process created\n"); _exit(15); /* terminate child */ default: printf("Parent process after fork\n"); pid = waitpid(child_pid,&status,WUNTRACED); } if (WIFEXITED(status)) fprintf(stderr, "%d exits: %d \n", child_pid ,WEXITSTATUS(status)); else if (WIFSTOPPED(status)) fprintf(stderr, "%d stopped by %d \n", child_pid,WSTOPSIG(status)); else if (WIFSIGNALED(status)) fprintf(stderr, "%d killed by %d \n", child_pid,WTERMSIG(status)); else perror("waitpid"); _exit(0); } /* The output should be: % cc -o test_waitpid test_waitpid.c % test_waitpid child process created 1354 exits: 15 */