Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,158 questions

40,712 answers

573 users

How to call fork multiple times C

1 Answer

0 votes
#include <stdio.h>
#include <sys/wait.h>
#include <unistd.h>
#include <errno.h>

int main() {
    int id1 = fork();
    int id2 = fork();
    
    if (id1 == 0) {
        if (id2 == 0) {
            printf("process a\n");
        } else {
            printf("process b\n");
        }
    } else {
        if (id2 == 0) {
            printf("process c\n");
        } else {
            printf("parent process\n");
        }
    }
    while (wait(NULL) != -1 || errno != ECHILD) {
        printf("wait for child process to finish\n");
    }

    return 0;
}




/*
run:

process c
process a
process b
wait for child process to finish
parent process
wait for child process to finish
wait for child process to finish

*/

 





answered Jan 5, 2021 by avibootz
...