I try to create a process tree as in the first screenshot. My code is not working correctly. The result of my code is shown in the second screenshot. The problem is that I do not know how to create the latest generation of processes.

#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/wait.h> int main() { int i; int j; pid_t ppid; pid_t cpid; ppid = getpid(); printf("I'm the parent, my PID is: %d\n", ppid); for (i = 0; i < 4; i++) { ppid = fork(); if (ppid == 0) { printf("Hello, my PID is: %d, my parent's PID is %d\n", getpid(), getppid()); for(j = 0; j < 2; j++) { ppid = fork(); if (ppid == 0) { printf("Hello, my PID is: %d, my parent's PID is %d\n", getpid(), getppid()); sleep(60); printf("I'm process %d and I'm done\n", getpid()); exit(0); } } sleep(60); printf("I'm process %d and I'm done\n", getpid()); exit(0); } } sleep(1); printf("I'm process %d. Waiting for one of my children to complete", getpid()); wait(NULL); printf("Eltern: I'm done\n"); printf("... and bye. \n"); }
fork()works? - PinkTux