What exactly does fork return?

后端 未结 8 1119
时光取名叫无心
时光取名叫无心 2020-12-02 13:51

On success, the PID of the child process is returned in the parent’s thread of execution, and a 0 is returned in the child’s thread of e

8条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-02 14:05

    Once fork is executed, you have two processes. The call returns different values to each process.

    If you do something like this

    int f;
    f = fork();
    if (f == 0) {
      printf("I am the child\n");
    } else {
      printf("I am the parent and the childs pid is %d\n",f);
    
    }
    

    You will see both the messages printed. They're being printed by two separate processes. This is they way you can differentiate between the two processes created.

提交回复
热议问题