2. p2.c fork() and wait() example

parent waits for child to finish first
$ cat -n ostep/code/cpu-api/p2.c
     1	#include <stdio.h>
     2	#include <stdlib.h>
     3	#include <unistd.h>
     4	#include <sys/wait.h>
     5
     6	int
     7	main(int argc, char *argv[])
     8	{
     9	    printf("hello world (pid:%d)\n", (int) getpid());
    10	    int rc = fork();
    11	    if (rc < 0) {
    12	        // fork failed; exit
    13	        fprintf(stderr, "fork failed\n");
    14	        exit(1);
    15	    } else if (rc == 0) {
    16	        // child (new process)
    17	        printf("hello, I am child (pid:%d)\n", (int) getpid());
    18		sleep(1);
    19	    } else {
    20	        // parent goes down this path (original process)
    21	        int wc = wait(NULL);
    22	        printf("hello, I am parent of %d (wc:%d) (pid:%d)\n",
    23		       rc, wc, (int) getpid());
    24	    }
    25	    return 0;
    26	}
$ ./p2
hello world (pid:21937)
hello, I am child (pid:21938)
hello, I am parent of 21938 (wc:21938) (pid:21937)
$ ./p2
hello world (pid:21939)
hello, I am child (pid:21940)
hello, I am parent of 21940 (wc:21940) (pid:21939)
$
--> try checking the child exit status