summaryrefslogtreecommitdiffstats
path: root/scraps/forktest.c
blob: 40ecb862cb481c9d318b447178fb8e904676bf38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main() {
    printf("forktest pid: %d\n", getpid());
    fflush(stdout);
    pid_t pid = fork();
    if (pid == 0) {
        printf("grandchild pid: %d\n", getpid());
        fflush(stdout);
        // printf("child ppid: %d\n", getppid());
        execl("./bin/grandchild", "./bin/grandchild", (char*)0);
    }
    int status;
    while (1) {
        waitpid(pid, &status, 0);
        if(WIFEXITED(status)) {
            printf("grandchild exited\n");
            return 0;
        }
    }
}