// modified p2.c to check child exit status #include #include #include #include int main(int argc, char *argv[]) { printf("hello world (pid:%d)\n", (int) getpid()); int rc = fork(); if (rc < 0) { // fork failed; exit fprintf(stderr, "fork failed\n"); exit(1); } else if (rc == 0) { // child (new process) printf("hello, I am child (pid:%d)\n", (int) getpid()); sleep(1); return 123; } else { // parent goes down this path (original process) int wstatus, wc = wait( &wstatus); printf("hello, I am parent of %d (wc:%d) (pid:%d)\n", rc, wc, (int) getpid()); printf( "child exit status = %i\n", WEXITSTATUS(wstatus) ); } return 0; } /* sample run: $ ./p2 hello world (pid:1595) hello, I am child (pid:1596) hello, I am parent of 1596 (wc:1596) (pid:1595) child exit status = 123 $ */