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

int
main(int argc, char *argv[])
{
    int i;
    int status;
    pid_t pid;

    for (i = 0; i < 3; i++) {
        switch (fork()) {
        case -1:
            err(EXIT_FAILURE, "fork");

        case 0:         // child process
            printf(" > ID=%ld created\n", (long) getpid());
            _exit((long) getpid() % 100);

        default:
           pid = wait(&status);
           if (pid == -1)
                err(EXIT_FAILURE, "wait");
           printf(">>PID=%ld terminates with status=%d\n",
                   (long) pid, WEXITSTATUS(status));
        }
    }

    if (wait(NULL) == -1)
        err(EXIT_FAILURE, "no unwaited children");
    exit(EXIT_SUCCESS);
}
