How many processes are created in this snippet?


Main()
{
Fork();
Fork() && fork () || fork ();
Fork ();
}
help would be appreciated..
Depends, if what you have written in exactly right, i.e. if main() has been written like what you have written i.e. Main().
Then it won't compile. ;)
Now, considering that was a typo, code is still ambiguous. It's because of the way you wrote Fork();. Fork(); is different function than fork();. fork(); creates a new process. But we don't know what Fork(); does because you have not posted the code for it.
Last edited on
Assuming Fork() == fork(),

fork() returns the PID of the child process or -1 (on error) to the parent. Either way, use
of fork() in a boolean context with those return codes results in conversion to 'true'.

C++ does short circuit evaluation, meaning that in the expression "A || B", if A is true, then
B is never evaluated because anything OR true is true. Likewise, in the expression "A && B",
if A is false, then B is never evaluated because anything AND false is false.

Since && and || have the same precedence, they are evaluated left-to-right.

With this information, you should be able to figure it out yourself.


Topic archived. No new replies allowed.