if (pid==0)
{
//child
}
else
{
//parent
...
}
Why is the 'else' part necessary? isn't it implied that we are in the parent ?
if (pid==0)
{
//child
}
else
{
//parent
...
}
Why is the 'else' part necessary? isn't it implied that we are in the parent ?
isn't it implied that we are in the parent ?
No. After a fork()
call, there are two processes that run from exactly that same point in the code. Only one is the parent. The code you put in the else
block is run only by the parent, just as the code inside the if
block is run only by the child. Code that comes after the entire if
/else
statement is executed by both the child and the parent.
It's not necessary unless you want to do something with both the original(parent) and the new(child) processes.
if((pid = fork()) == 0){
//child
}else{
//parent
}