getpid and getppid return two different values

后端 未结 2 1150
陌清茗
陌清茗 2020-12-14 20:25

When I run the code below

#include 
#include 
//int i=0;
int main(){

int id ;
id = fork() ;
printf(\"id value : %d\\n\",i         


        
相关标签:
2条回答
  • 2020-12-14 20:35

    What's happening is that the parent is terminating before the child runs. this leaves the child as an orphan and it gets adopted by the root process with PID of 1. If you put a delay or read data from stdin rather than letting the parent terminate you'll see the result you expect.

    Process ID 1 is usually the init process primarily responsible for starting and shutting down the system. The init (short for initialization) is a daemon process that is the direct or indirect ancestor of all other processes. wiki link for init

    As user314104 points out the wait() and waitpid() functions are designed to allow a parent process to suspend itself until the state of a child process changes. So a call to wait() in the parent branch of your if statement would cause the parent to wait for the child to terminate.

    0 讨论(0)
  • 2020-12-14 20:36

    Because the parent process runs out and released, its child process became an orphan, The init (short for initialization)whose pid is 1 received the orphan process.

    0 讨论(0)
提交回复
热议问题