I have this c code
/* SIGCHLD handler. */
static void sigchld_hdl (int sig)
{
    /* Wait for all dead processes.
     * We use a non-blocking call to be sure this signal handler will not
     * block if a child was cleaned up in another part of the program. */
     //printf("Inside handler:  \n");
    while (waitpid(-1, NULL, WNOHANG) > 0) {
        printf("reaped\n");
    }
     //printf("Outside handler:  \n");
}
int main (int argc, char *argv[])
{
    struct sigaction act;
    int i;
    memset (&act, 0, sizeof(act));
    act.sa_handler = sigchld_hdl;
    if (sigaction(SIGCHLD, &act, 0)) {
        perror ("sigaction");
        return 1;
    }
    /* Make some children. */
    for (i = 0; i < 5; i++) {
        switch (fork()) {
            case -1:
                perror ("fork");
                return 1;
            case 0:
                 exit(0);
                return 0;
            default:
                //printf("inside parent\n");
                printf("Created child %d\n", i);
        }
    }
    /* Wait until we get a sleep() call that is not interrupted by a signal. */
    while (sleep(1)) {
        printf("inside parent while loop\n");
    }
    return 0;
}
I am calling fork 5 times, so I expect total 5 child process and 1 parent process. When I run this code, I get this output
Created child 0
Created child 1
reaped
reaped
Created child 2
Created child 3
Created child 3
reaped
reaped
Created child 4
reaped
inside parent while loop
I can't quite figure out why I am seeing child3 twice. There should not be any duplication with regards to i as i is incrementing in every iteration.
Can somebody explain why I am seeing child3 twice?
 
    