How to use fork to create a new process (child process) in C

2 Answers

0 votes
#include <stdio.h>
#include <unistd.h>

int main() {
    int id = fork();
    
    printf("%d\n", id);

    printf("c programming\n");
    
    return 0;
}



/*
run:

20
c programming
0
c programming

*/

 



answered Jan 5, 2021 by avibootz
0 votes
#include <stdio.h>
#include <unistd.h>

int main() {
    int id = fork();
    
    printf("%d\n", id);
    
    if (id != 0) {
        fork();
    }

    printf("c programming\n");
    
    return 0;
}



/*
run:

0
c programming
20
c programming
20
c programming

*/

 



answered Jan 5, 2021 by avibootz

Related questions

1 answer 184 views
184 views asked May 1, 2021 by avibootz
1 answer 164 views
1 answer 157 views
157 views asked Jan 5, 2021 by avibootz
1 answer 187 views
1 answer 109 views
1 answer 146 views
146 views asked Jun 12, 2023 by avibootz
...