The answer is
if (fork ())
may or may determine whether a child process was created.
Go to the man page:
http://man7.org/linux/man-pages/man2/fork.2.html
We find that fork () returns three types of values:
- -1 => fork() failed
- 0 => return value in the child process
- a positive value => success and the PID of the child process.
Thus the test
if (fork ())
which is the same as
if (fork () != 0)
may succeed whether or not a child process was created. A competently written question would have said
if (fork () > 0)
Assuming everything works correctly:
int main()
{
int v=0;
if(fork()) // Creates a child process that does nothing.
{
v++;
if(!fork()) // Creates a child process that creates a child process (that then does nothing but decrement v).
{
fork();
v--;
}
}
}