I've a very specific question about fork system call. I've this piece of code:int main (void) {    for (int i = 0; i < 10; i++) {        pid_t pid = fork ();        if ( !pid ) {            printf(CHILD | PID: %d, PPID: %d\n, getpid(), getppid());            _exit(i + 1);        }    }    for (int i = 0; i < 10; i++) {        int status;        waitpid(-1, &status, 0);        if (WIFEXITED(status)) {            printf(IM %d AND CHILD WITH EXIT CODE %d TERMINATED\n,                    getpid(), WEXITSTATUS(status));        }         else {            printf(ERROR: CHILD NOT EXITED\n);        }    }    return 0;}which produces this output:CHILD | PID: 3565, PPID: 3564CHILD | PID: 3566, PPID: 3564IM 3564 AND CHILD WITH EXIT CODE 1 TERMINATEDIM 3564 AND CHILD WITH EXIT CODE 2 TERMINATEDCHILD | PID: 3573, PPID: 3564CHILD | PID: 3567, PPID: 3564IM 3564 AND CHILD WITH EXIT CODE 9 TERMINATEDIM 3564 AND CHILD WITH EXIT CODE 3 TERMINATEDCHILD | PID: 3568, PPID: 3564IM 3564 AND CHILD WITH EXIT CODE 4 TERMINATEDCHILD | PID: 3569, PPID: 3564IM 3564 AND CHILD WITH EXIT CODE 5 TERMINATEDCHILD | PID: 3570, PPID: 3564IM 3564 AND CHILD WITH EXIT CODE 6 TERMINATEDCHILD | PID: 3571, PPID: 3564IM 3564 AND CHILD WITH EXIT CODE 7 TERMINATEDCHILD | PID: 3572, PPID: 3564IM 3564 AND CHILD WITH EXIT CODE 8 TERMINATEDCHILD | PID: 3574, PPID: 3564IM 3564 AND CHILD WITH EXIT CODE 10 TERMINATEDwhich makes me wonder how fork really works and what code new processes really execute. Looking at the above output results, I can not understand:How does second for cycle prints before first for finish all iterations? I've also noticed this second for is always executed by parent process, which makes me wonder if while on first for cycle, if pid != 0 (which means parent process calling) second for cycle is executed (?)Why does CHILD processes are not sorted printed by PID?So, bottom line, how does fork really works and who executes what?
How does fork system call really works
c;system calls;fork
When you fork, the kernel creates a new process which is a copy of the forking process, and both processes continue executing after the fork (with the return code showing whether an error occurred, and whether the running code is the parent or the child). This continue executing part doesnt necessarily happen straight away: the kernel just adds the new process to the run queue, and it will eventually be scheduled and run, but not necessarily immediately.This explains both behaviours youre asking about:since new processes arent necessarily scheduled immediately, the parent might continue running before any child gets a chance to run;creation order doesnt have much (if any) impact in the run queue, so theres no guarantee the child processes will run in the order theyre created.