Untitled
unknown
plain_text
a year ago
776 B
8
Indexable
#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;
}
Editor is loading...
Leave a Comment