/**
 * File: waitpid.c
 * ----------------
 * Here's is a program where a parent waits for its child.
 */

#include <unistd.h>
#include <stdio.h>
#include <sys/wait.h>

static const int kForkFailed = 1;
static const int kWaitFailed = 2;

int main(int argc, char *argv[]) {
    printf("Before.\n");
    pid_t pidOrZero = fork();

    // Optional error checking
    if (pidOrZero == -1) {
        fprintf(stderr, "Error: fork failed\n");
        return kForkFailed;
    }
    
    if (pidOrZero == 0) {
        sleep(2);
        printf("I (the child) slept and the parent still waited up for me.\n");
    } else {
        pid_t result = waitpid(pidOrZero, NULL, 0);

        // Optional error checking
        if (result != pidOrZero) {
            fprintf(stderr, "Error: waitpid failed\n");
            return kWaitFailed;
        }

        printf("I (the parent) finished waiting for the child.  This always prints last.\n");
    }

    return 0;
}
