I'm revising for an operating systems exam and currently trying to understand this processes question:
int main()
{
int v=0;
if(fork())
{
v++;
if(!fork())
{
fork();
v--;
}
}
}
So the question asks to
Draw a tree reflecting the parent-child hierarchy of processes created when the program above is run.
How many separate copies of the integer variable v are created? Discuss what is the value of v at the end of the program for each of the processes in the tree.
The main thing I'm having an issue with is the
if(fork())
line in the code. From looking at other stack overflows I realised that
if(fork()) = if(fork() != 0)
but I'm still having difficulty understanding whether the first if statement creates a child process? Because thats the only way that the second if statement:
(!fork())
can be executed?
This is how far I've got with my current understanding of the question.
screenshot of attempt
Hopefully the diagram is legible! I'd be really grateful for any hints or pointers with this.