Untitled

mail@pastecode.io avatar
unknown
plain_text
25 days ago
776 B
2
Indexable
Never
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

int main(int argc, char *argv[]) {
    int pid, ppid;

    pid = fork();
    
    if (pid < 0) {
        printf("Error at fork() child\n");
        exit(1);
    } else if (pid != 0) {
        printf("I am parent\n");
        printf("Parent process ID: %d\n", getppid());
    } else {
        wait(NULL);  
        ppid = fork();

        if(ppid < 0) {
            printf("Error at fork for grandchild\n");
            exit(1);
        } else if (ppid != 0) {
            printf("I am grandchild\n");
            printf("Grandchild process ID: %d\n", getpid());
            printf("Parent process ID: %d\n", getppid());
        } else {
            wait(NULL);
        }
    }

    return 0;
}
Leave a Comment