Linux environment programming-orphan process, zombie process, daemon process

We have introduced the previous process control one after another. Today we will introduce orphan processes, zombie processes, and daemon processes.

So what are they? ? Let's take a video to understand who the blogger is.

1. Orphan Process

If the parent process ends before the child process, the child process becomes an orphan process. The resources of the parent process are reclaimed by the init process. The child process that is not finished is adopted by the init process. The init process can be regarded as the dean of the orphanage.

2. Zombie process:

The parent process is still running, and the child process has ended [It is common sense that when the process ends, the resources he opened up will be released. At that time, I want to remind everyone that not all resources will be released. The PCB (process Control block) will not be released. Although the PCB occupies about ten bytes, a lot of resources will be wasted if there are more zombie processes ], Note: kill -9 will not reclaim the PCB remaining in the kernel, it just A command to terminate the process, the zombie process has been terminated, that is, the PCB in the kernel has not been recycled.

Thinking:

1. Why is there no PCB waste in the orphan process without the end of the parent process?

Because the parent process can be regarded as a tree root, this tree root is managed by the init process, and the operating system will automatically reclaim the PCB remaining in the kernel after the process ends.

 

2. How to reclaim PCB resources left by the zombie process in the kernel? [ Code implementation is provided in the following chapters, only introduction here ]

Method 1: You can kill the parent process to make the child process an orphan, and let the init process reclaim the PCB of the child process

Method 2: You can modify the parent process of the process so that the child process is directly managed by the init process

Method 3: You can use the function provided by glibc [wait, waitpid (callback function triggered by signal (and signal trap processing))] to recover the PCB left in the kernel by the child process (commonly used)

 

Three. [ Key ] Daemon [The code implementation of the daemon is provided later]

The Daemon process is a background service process in Linux, which is usually independent of the terminal and periodically performs certain operations or waits for certain events to occur. Generally, names ending in d are used.

Some system service processes in the linux background have no control terminal and cannot directly interact with users. They are not affected by user login and logout. They are always running. They are all daemon processes. Such as: the realization of the pre-reading input and slow output mechanism; ftp server; nfs server, etc.

 

One more thing: you can add the ./daemon daemon through .bashrc, so that the daemon can be started automatically after booting./daemon

 

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/qq_44065088/article/details/108660219