#include "csapp.h"

volatile sig_atomic_t ccount = 0;

void handler(int sig) {
    while (wait(NULL) > 0)
        ccount--;
}

int main(void) {
    int i;
    struct sigaction sa;

    Sigemptyset(&sa.sa_mask);
    sa.sa_flags = SA_RESTART;
    sa.sa_handler = handler;
    Sigaction(SIGCHLD, &sa, NULL);

    for (i = 0 ; i < 5; i++) {
        if (Fork() == 0)
            exit(0);  // Child
        else
            ccount++; // Parent
    }
    printf("ccount = %d\n", ccount);
    while (ccount > 0)
        ;
    return 0;
}
