#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/wait.h> int main() { pid_t child, pid; int stat; child = fork(); if (child == -1) { perror("unable to fork"); exit(1); } if (child == 0) { /* child process */ srand(getpid()); exit(rand()); } /* parent process */ pid = wait(&stat); if (pid == child) { if (WIFEXITED(stat)) { printf("exit code of child = %d\n", WEXITSTATUS(stat)); } else { printf("child terminated abnormally\n"); } } else { perror("wait"); } } |