Good daytime to all
I am confused with fork(). Does fork() forks child process or only parent?
please help on some examples
#include <unistd.h>
#include <stdio.h>
int main() {
if (fork()) {
fork();
printf(" X\n");
}
return 0;
}
is this how i suppose to build the processes:
parent
|
/ \
Parent Child
so the output would be
X X X
another example is
#include <unistd.h>
#include <stdio.h>
int main() {
fork();
fork();
fork();
printf(" X \n");
return 0;
}
the graph would be like this?
Fork start
/ \
Parent Child 1st fork done output 2 of X
/ \ / \
P C P C 2nd fork done output 4 of X
/ \ / \ /\ /\
P C P C P C P C 3rd fork done output 8 of X
another question how can I draw
if (fork() || fork() || fork())
fork();
or
if (fork() && fork())
fork();
I would be glad if someone correct me.
forkdoesn’t “create parent and child process”, no. It creates a copy of the current process (the copy being the child and the current process being the parent). The distinction is done based on theforkreturn value.