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

int main(void) {
    pid_t pid;

    if ((pid = fork()) == -1) {
        perror("fork");
        return 1;
    }

    if (pid == 0) {
        if ((pid = fork()) == -1) {
            perror("fork child");
            return 1;
        }

        if (pid == 0) {
            printf("in child's child, pid = %d, parent = %d\n", getpid(), getppid());
        } else {
            sleep(1);
            printf("in child, pid = %d, parent = %d\n", getpid(), getppid());
        }
    } else {
        sleep(1);
        printf("in parent, pid = %d\n", getpid());
    }
    printf("parent end\n");

    return 0;
}
