[Linux learning path] Exploring the zombie process of the process state | Orphan process

insert image description here

1. Overview of process status

The process state refers to the different running states of a process in the operating system, and the process state determines what task the process will perform next. Common process states are as follows:

  • New state : The process is created but has not been accepted and allocated resources by the operating system.

  • Ready state : The process has obtained the required resources and is waiting to be scheduled for execution.

  • Running state : The process is executing instructions and occupying CPU resources.

  • Blocked state : The process temporarily stops executing due to waiting for an event (such as an IO operation), and releases resources such as CPU.

  • Termination state : The process execution is completed or terminated, and all resources are released.

1.1 Detailed explanation of running status

In the previous article [Linux Learning Path] The father and son of the secret process mentioned that there is only one CPU in a general computer, but there may be many processes, which is destined to be a small amount of resources for all CPUs. For a process, the essence of running is to put it on the CPU, so each CPU maintains a running queue, and the CPU schedules the process in the form of a queue. All processes must be queued in the run queue to run, and the PCB objects of each process are involved in the queue. All the processes in the running queue are in the running state (R state).

insert image description here
A process only needs to put itself on the CPU to start running, and it doesn't have to finish executing before putting itself down. If a process is put on the CPU until the execution is completed, it will put itself down and continue to execute other processes. Then when we write a while loop in our program, other applications will be stuck when running the program. . But the reality is not like this, we wrote a while infinite loop, other programs can still run normally. In order to avoid such a situation where a process occupies CPU resources for a long time, the concept of time slice is proposed.

Time slice is an idea of ​​the task scheduling algorithm in the operating system, which divides the execution time of the CPU into fixed-length time periods, and each time period is called a time slice. In each time slice, the operating system allocates the CPU to a task for execution, and when the time slice is exhausted, the operating system interrupts the current task and allocates the CPU to the next task. The time slice is generally about 10 milliseconds, so all process codes will be executed within a time period. We call this situation concurrent execution. In this case, there will be a large number of actions to put the process on the CPU and take it off the CPU, which is called process switching.

1.2 Detailed Explanation of Blocking Status

The most common blocking state is when a process needs to read data through the keyboard. A process is blocked when it is waiting for input from the keyboard. The keyboard is a kind of hardware, which belongs to the input device (peripheral) in the von Neumann structure system. The management of hardware resources by the operating system is to describe and then organize. Therefore, each piece of hardware will correspond to a structure object, and the structure object A waiting queue must be maintained in the system. When a process needs to use the hardware resource, the PCB object of the process will be linked into the waiting queue, and the process is in a blocked state at this time.

insert image description here

Small Tips : There may be hundreds of waiting queues in the operating system, not only for each type of hardware, but also for processes. There may be a process waiting for another process to finish before continuing to run. Different operating systems have different scheduling algorithms.

1.3 Detailed Suspend Status

There will also be a hang state on some operating system textbooks. Regardless of whether it is running or blocked, when a process is not scheduled by the CPU, its code and data are idle, that is, not used. I said before that a process has its own code and data in memory, as well as its own PCB object. When the memory space is running out, the operating system will put the code and data of these processes that are not scheduled by the CPU into the disk first. Storage, leaving only the PCB object of the process queued in the queue, this process is in a suspended state.

insert image description here
The above-mentioned theoretical knowledge belongs to the operating system discipline. Different operating systems may have different implementation schemes. Let's take a closer look at the process states in the specific Linux operating system.

Small Tips : The suspended state is invisible to the user, which is a behavior of the operating system. Just like we deposit money in the bank, we don’t know what the bank is doing with our money. The bank may have lent our money or paid employees wages, etc. We, as customers, don’t know. I only know that if the deposit is a demand deposit, you can go to the bank to withdraw the money at any time. If the deposit is a death deposit, you can only withdraw it when it expires.

Two, the specific process status in the Linux operating system

In order to figure out what a running process means, we need to know the different states of the process. A process can have several states (in the Linux kernel, processes are sometimes called tasks).

2.1 Linux kernel source code

/*
* The task state array is a strange "bitmap" of
* reasons to sleep. Thus "running" is zero, and
* you can test for combinations of others with
* simple bit tests.
*/
static const char * const task_state_array[] = {
    
    
	"R (running)", /* 0 */
	"S (sleeping)", /* 1 */
	"D (disk sleep)", /* 2 */
	"T (stopped)", /* 4 */
	"t (tracing stop)", /* 8 */
	"X (dead)", /* 16 */
	"Z (zombie)", /* 32 */
};
  • R running status (running) : It does not mean that the process must be running, it indicates that the process is either running or in the run queue.

  • S sleeping state (sleeping) : It means that the process is waiting for the event to complete (the sleep here is sometimes called interruptible sleep (interruptible sleep)), which corresponds to the blocking state in the operating system theory.

  • D Disk sleep state (Disk sleep) : sometimes called uninterruptible sleep state (uninterruptible sleep), the process in this state usually waits for the end of IO.

  • T stop state (stopped) : The process can be (T) by sending a SIGSTOP signal to the process. The suspended process can be resumed by sending the SIGCONT signal.

  • X dead state (dead) : This state is just a return state, you will not see this state in the task list.

2.2 View process status

Let's take a look at the process status after the following code is executed.

int main()                                                    
{
    
                                                                 
    while(1)                                                  
    {
    
        
         printf("Hello Linux\n!");                                                           
    }    
    return 0;    
}  

insert image description here
It can be seen that the process state after this code is executed is S sleep state, remove the printing in the while loop and then execute the code to see the process state.

int main()                                                    
{
    
                                                                 
    while(1);                                                  
    return 0;    
}  

insert image description here
At this time, there is only a while loop in the code, to execute this code, the process status becomes the R running status. Why is this happening? The reason is that the execution speed of the CPU is very fast. The printf in the first piece of code is to frequently access the display device, and our display may not be directly written by the process, so the process spends most of its time in The waiting queue of the display is waiting for the display device to be ready, so the final process state detected is the S sleep state. When we remove printf, the process will not access the display device, and will always be in the running queue, so the final detected process status is the R running status.

Small Tips : The + displayed in the query result indicates that the process is running in the foreground, which means that there will be no response when we enter the command on the bash command line at this time. You can add & after the input command, which means that the process is allowed The process runs in the background, and the only way to terminate the process is through instructions kill -9 进程PID.

2.3 D disk sleep state (Disk sleep)

The D state is also a blocking state. At the Linux system level, we call it deep sleep, and the S state is called light sleep. Light sleep can be woken up, that is, it can respond to external changes. We can terminate the light sleep process through the kill command (other processes). The following is a sitcom to introduce why there is a D state and the role of the D state.

There is a scenario where a process needs to write a large amount of data to disk. Under normal circumstances, when data is written to the disk, the process needs to wait. After the disk is written, a signal is given to the process, and then the process can continue to run. One day, process A was writing a large amount of data to the disk. During the writing process, process A was cocking its legs in the memory, eating melon seeds, waiting for the disk to finish writing and sending it a message. At this time, the passing operation The system found process A, and it said to process A: "My memory pressure is too high, but you are good, you are taking up memory and not doing business, and you are still eating melon seeds here!". So the operating system kills process A. At this time, the disk was dumbfounded, and the process disappeared halfway through the data writing. Because the process was gone, the disk deleted the written data, and the final result was that the data was not written to the disk. Who actually caused this tragedy? So the judge came out, it first interrogated the operating system, the process was killed by you, how do you explain it? The operating system said, my life is miserable. I just finished my job. In order to provide users with a smooth running environment, it is my responsibility to kill some processes. This is not my problem. Then the judge came to ask the disk again, you lost the data, how do you explain it? The disk said, my ancestors have worked like this for generations. The process let me write data, but it disappeared. Other disks also discarded the data when they encountered this situation. If you convict me, it is not mine. Both father and mother are guilty. Finally, the judge came to ask Process A. Before the judge could speak, Process plopped down on his knees and said, Your Honor, you know the truth. I was the one who was killed. I belong to the victim. How can I be guilty. The judge listened for a while and felt that everyone was not guilty. In the end, the judge pronounced that the three of you are not guilty. It is a system problem. I will change the operating system when I go back. When the process is writing data to the disk, no one can Kill the process. And thus D-State was born. When a process is in the D state, it will not respond to any request, and no one or the operating system can kill the process.

Small Tips : There are two ways to end the D state, one is to wait for a certain condition to be met, such as waiting for the data to be written, and the other is to directly cut off the power. If the process in the D state is found by the user, it indicates that the operating system is not far from crashing. So there will be a D state, but generally the time of appearance is very short.

2.4 T stop state (stopped)

In the Linux kernel source code, we can see that there are two T states, one is T and the other is t. We can think that these two T states are the same. For a process, we can set it to stop state.

kill -19 进程PID

insert image description here
The stop state can be ended by the following command.

kill -18 进程PID//

insert image description here
Small Tips : The process that ends the stop state will run in the background, and the only way to terminate this process is through kill -9instructions. The T state is very similar to the S state, in which the process in the S state must be waiting for a certain resource, and the process in the T state may be waiting for a certain resource, or it may be controlled by other processes. When we debug a piece of code at the breakpoint, the process will be in the T state.

insert image description here

3. Zombie process

When a process exits, it does not immediately release all its resources. When a process exits, the operating system will maintain various information of the current process for a period of time. This state is called the Z zombie state. The maintenance information is for the "person" who cares about it, that is, the parent process to view. If the parent process has never cared about the exiting child process, then the child process will be in the Z state for a long time.

int main()    
{
    
        
    pid_t id = fork();    
    if(id == 0)    
    {
    
        
        int cnt = 5;    
        while(cnt)    
        {
    
        
            printf("我是子进程,PID是:%d,PPID:%d,cnt:%d\n",getpid(),getppid(),cnt);                    
            sleep(1);    
            cnt--;    
        }    
       _exit(0);    
    }    
    else 
    {
    
    
        while(1)
        {
    
    
            printf("我是父进程,PID是:%d,PPID:%d\n",getpid(),getppid());
            sleep(1);
        }
    }
    return 0;
}

The above code creates a child process by calling the fork interface in the process process. The child process will be terminated after five times of printing. The exit function is used to terminate a process, and the parent process will continue to run.

insert image description here
After the child process prints 5 times, it is in the Z state and followed by a word defunct, which means that it is dead and does not exist, but it is still waiting for the parent process to reclaim its resources. The related resources of the process in the Z state, especially the task_struct structure, cannot be released. Only when the parent process reclaims the relevant resources of the child process, the child process can become X dead. We call this process in the Z state a zombie process. If the parent process does not recycle, this process will occupy memory resources for a long time and cause memory leaks.

3.1 Summary of zombie process hazards

  1. The exit status of the process must be maintained, because it needs to tell the process (parent process) that cares about it, the task you entrusted to me, and how I am doing. But if the parent process has not read it, the child process will always be in the Z state.

  2. Maintaining the exit status itself requires data maintenance, which also belongs to the basic information of the process, so it is stored in the PCB object. In other words, the Z state does not exit, and the PCB must always be maintained.

  3. If a parent process creates a lot of child processes, it will not be recycled, which will cause a waste of memory resources, because the PCB object itself will occupy memory.

  4. cause a memory leak.

4. Orphan process

Above we let the child process exit first, and the parent process keeps running. Next, we let the parent process exit first, and the child process keeps running, to see what the result will be.

int main()    
{
    
        
    pid_t id = fork();    
    if(id == 0)    
    {
    
    
    	//子进程    
        int cnt = 500;    
        while(cnt)    
        {
    
        
            printf("我是子进程,PID是:%d,PPID:%d,cnt:%d\n",getpid(),getppid(),cnt);    
            sleep(1);    
            cnt--;    
        }    
       _exit(0);    
    }    
    else    
    {
    
    
    	//父进程    
        int cnt = 5;
        //这里的cnt是5,意味着父进程会先执行结束    
        while(cnt--)    
        {
    
        
            printf("我是父进程,PID是:%d,PPID:%d,cnt:%d\n",getpid(),getppid(),cnt);
            sleep(1);                                                                        
        }    
    }    
    return 0;    
}

insert image description here
It can be seen that after the execution of the parent process, only the child process is left. Why is the parent process not in the Z zombie state? The answer is that the parent process is also a child process of bash. After the parent process finishes executing, its parent process bash will recycle it, and the process is very fast, so we don't see the parent process in the Z zombie state. Secondly, we found that when the parent process ends, the parent process of its child process will become process No. 1, which is the operating system. We call the process whose parent process is process No. 1 an orphan process, which is adopted by the system. Because the orphan process will also exit in the future and be freed, it needs to be adopted.

Small Tips : All processes are only responsible for its "son", that is, the child process, and will not be responsible for its grandson process, because there is only logic for creating child processes in the code, and there is no logic for creating grandchild processes, so it's not that you don't want to Let the grandpa process reclaim the resources of the grandson process because the grandpa process does not have this ability, and the operating system will recycle directly from the kernel level, so when the parent process of a process ends, it will hand over the process to the operating system to let the operation system to act as its parent process.

V. Conclusion

Today's sharing is over here! If you think the article is not bad, you can support it three times . There are many interesting articles on Chunren's homepage . Friends are welcome to comment. Your support is the driving force for Chunren to move forward!

insert image description here

Guess you like

Origin blog.csdn.net/weixin_63115236/article/details/132274130