C fork dealing with global variable

前端 未结 5 1219
谎友^
谎友^ 2021-01-05 01:37

I\'m not understanding the output of this program:

#include 
#include 
#include 

int i = 0;

int main()
{
           


        
5条回答
  •  误落风尘
    2021-01-05 02:06

    Change your code to this and the output should make a lot more sense:

    #include 
    #include 
    #include 
    
    int i = 0;
    
    int main()
    {
        while (i < 3)
        {
            fork();
            printf("pid = %d, i = %d\n", getpid(), i);
            ++i;
        }
        return 0;
    }
    

提交回复
热议问题