What fork() does is cause a new *process* to execute in parallel to the one running your program. And the new process ALSO continues running your program. So you have effectively made a cloned copy of your running program. You can detect which copy you are 'in' by examining the return value from the fork() function. Each copy gets a different value.
And no, the child process is not a thread. But it is running concurrently.
if(pid > 0 ){
printf("I am the parent of pid = %d\n",pid);
}elseif(pid == 0){
printf("I am the baby!\n");
}elseif(-1 == pid){
perror("fork");
}
return 0;
So if it is parent process, then if (pid > 0) is true so "I am the parent of pid = 2492" is printed out.
Then if it is the child process, then else if(pid == 0) is true so "I am the baby!" is printed out.
Usually when we do forking, we would want the parent process to wait for the child process to finish doing what they are doing exit and then the parent process terminates. But of cuz depending on your business requirements, you may not want to follow this common pattern though.