I have been trying to understand fork() behavior. This time in a for-loop. Observe the following code:
#include
void main()
{
i++ is executed after the call of fork, because that's the way the for loop works.fork may fail.A little explanation on the second one:
for (i = 0;i < 3; i++)
{
fork();
}
is similar to:
i = 0;
while (i < 3)
{
fork();
i++;
}
So i in the forked processes(both parent and child) is the value before increment. However, the increment is executed immediately after fork(), so in my opinion, the diagram could be treat as correct.