I just need to understand this statement:
if (fork() && !fork())
shouldn’t it always be false? I mean, if I write:
if (a && !a)
It’s always false so the first should always be false too, am I wrong? Of course I am, but I’m hoping someone can explain this strange thing to me.
I’m studying C for an exam and I had to resolve this code:
int main(){
if(fork && !fork()){
printf("a\n");
}
else printf("b\n");
}
Every calls to the unix process creation system call fork() returns twice. First it returns with the PID of the child to the parent(the process which called fork()). Second it returns to 0 to the newly created child.
from man pages:
Return Value
On success, the PID of the child process is returned in the parent, and 0 is returned in the child. On failure, -1 is returned in the parent, no child process is created, and errno is set appropriately.
in your case
The statement inside
if, calls fork twice. So what will happen is following :Now first call to
fork()will return in both A and B. In A it will be nonzero and in B it will be zero.Second call to fork() will be evoked only from A. because first fork returned 0 to B, it will not Evoke a second
fork(). its because&&short circuits the evaluation if first operand is found non zero. Thanks to Daniel for pointing this out.So we can make a table out of this:
So from the chart, Process C’s
ifwill be evaluated to TRUEIts important to remember,
fork()1didn’t returned to C . it got the copy of Already evaluated expression from its parent.I hope this explains your question.