I am a bit confused about the value which the function fork returns. I understand that value 0
is for child
process and value >0
is for parent
process.
I have the code below
int main()
{
int pid;
pid = fork();
if(pid == 0)
//DO SOMETHING
else
//DO SOMETHING ELSE
return 0;
}
The valiable pid
after fork is different for each process ?
I can't understand how it switches value. And I have a second part with code
int main()
{
int pid;
if (pid == 0)
{
return 5;
}
printf("parent = %d waits for child = %d ", getpid(), pid);
waitpid(pid, NULL, 0);
printf("child terminates!")
return 0;
}
in which I can't understand why pid on line with first printf
has the value of child. It shouldn't be the id
of parent ?