Below example I create 3 another process and I have also 1 main process. So totally, there are 4 process which is executing. My question that I can check the which process is parent and which process is child by controlling the return value of the fork
system call function. However how can I detect the main process execution? And what is the difference between a main process and parent process?
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main()
{
int a =fork();
int b =fork();
if (a == 0)
printf("Hello from Child(A)!\n");
// parent process because return value non-zero.
else
printf("Hello from Parent(A)!\n");
if (b == 0)
printf("Hello from Child(B)!\n");
// parent process because return value non-zero.
else
printf("Hello from Parent(B)!\n");
return 0;
}
Your code creates 4 processes :
(a > 0) && (b > 0)
: the original process(a == 0) && (b > 0)
: the first child process of the original process (child A)(a > 0) && (b == 0)
: the second child process of the original process (child B)(a == 0) && (b == 0)
: the first child process of child A (child AA)Remember that fork
creates a child process, and returns the pid of this child process in the parent process, and returns 0 in the child process.