void main()
{
int pid = fork();
if (pid < 0 ) {
printf("Child process doesn't be created!");
return(-1);
}
if (pid == 0) { // A father process
int pid1 = fork(); // fork another child process
...
... // same judge
if (pid1 > 0) { // B child process
int pid3 = fork(); // B child process fork Dgrandchild process
}
if (pid > 0) { // C child process
printf("C child process pid is %d\n", pid);
int pid2 = fork(); // C child process fork F grandchild process
if (pid2 > 0) { // F grandchild process
printf(" F grandchild process pid is %d\n", pid2);
}
if (pid == 0) {
int pid4 = fork()
if (pid4 > 0) { // G grandchild process
printf(" G grandchild process pid is %d\n", pid4);
}
}
}
}
}
}
when you call the fork function , if the return value is large than 0, it is father process and if the return value is equal to 0, it is the child process. The fathar and child process will execute the same statement.
for (i = 0; i < 2; i ++) {
child_id = fork();
if (child_id != 0) {
printf("father A :child %d,%c,id = %d\n",
i, flag[i +1], child_id);
stat ++;
}else{
break;
}
}
if (child_id == 0) {
for (i = 0; i < 2; i ++) {
child_id = fork();
if (child_id != 0) {
printf("father %c :child %d,%c,id = %d\n",
flag[1 + stat], i, flag[i + stat * 2 + 3],child_id);
}else{
break;
}
}
}
wait( &stat);
}