[Linux] orphan process

 In Linux, if the parent process terminates for some reason while the child process is running, the child process is called an orphan process .

We write the following code:

 The child process is always running, and the parent process is automatically terminated after running for a period of time. Run the program to observe the phenomenon:

 At the beginning, the child process runs concurrently with the parent process. After a while, the parent process terminates and the child process continues:

 It can be found that the parent process disappeared directly. And the PPID of the child process becomes 1 .

 Therefore, we can conclude that after the parent process exits, the OS will make process 1 the new parent process of the child process, and this adopted child process is an orphan process. If the OS does not adopt the orphan process, then the orphan process will never be recycled, and its PCB will always be maintained, occupying memory space.

At the same time, we observed that the state of the orphan process changed from S+ to S , that is, from running in the foreground to running in the background. At this time, we can no longer terminate it by using ctrl + c . We need to use the command killall [process name]  or kill -9 [ PID] to terminate the process.

Guess you like

Origin blog.csdn.net/weixin_74078718/article/details/129326183