C fork dealing with global variable
c, fork, global-variables, process
Solution
Fork will make a copy of the process. An independent copy of the process. So, if a global variable contains 3 at the time you fork, each copy of the process gets their very own 3. And if they modify, their modifications are completely independent.
Problem
I'm not understanding the output of this program: ``` #include <pthread.h> #include <stdio.h> #include <unistd.h> int i = 0; int main() { while(i<3) { fork(); printf("%d\n",i); ++i; } } ``` The output is: ``` 0 1 2 2 1 2 0 1 2 2 2 1 2 2 ``` Can please someone tell me how I should tackle this issue in order to fully understand why I'm getting this output?