Gu Dequan:Personal homepage
Personal column:"Linux Operating System" "C/C++" "LeedCode Question Writing"
The keyboard is bad, and the annual salary is one million!
With the study of the previous articleblog, we have briefly understood the basic knowledge of the process. Today we will learn two special ones. processes, zombie processes and orphan processes.
1. Zombie process
1. Related concepts
A zombie process is a process that has finished running but still exists in the process table. Specifically, when a process ends, its parent process does not immediately call wait or waitpid to collect the exit status information of the child process. This results in that although the child process has terminated, it still occupies a place in the process table, thus A zombie process is formed. The zombie process is actually a relatively special state, which is also called the dying state (Z-zombie). The exit status of the process must be maintained, because it needs to tell the process that cares about it (the parent process) how well I did the task you gave me. In the actual programming process, you should try to pay attention to the problem of zombie processes because their resources have not been released. You can use the ps
command with grep 'Z'
to find all processes in status Z, that is, zombie processes.
2. Zombie state
Zombies are a relatively special state. When the process exits and the parent process (using the wait() system call, discussed later) does not read the return code of the child process exit, a zombie (zombie) process will be generated. A zombie process remains in the process table in a terminated state and is waiting for the parent process to read the exit status code. Therefore, as long as the child process exits, the parent process is still running, but the parent process does not read the child process status, and the child process enters the Z state.
3. Create a zombie process
Let’s take an example of creating a zombie process that lasts for 30 seconds:
#include <stdio.h>
#include <stdlib.h>
int main()
{
pid_t id = fork();
if(id < 0)
{
perror("fork");
return 1;
}
else if(id > 0)
{ //parent
printf("parent[%d] is sleeping...\n", getpid());
sleep(30);
}
else
{
printf("child[%d] is begin Z...\n", getpid());
sleep(5);
exit(EXIT_SUCCESS);
}
return 0;
}
Compile and monitor in another terminal:
start testing:
See the result:
4. The harm of zombie processes
1. The exit status of the process must be maintained, because it needs to tell the process that cares about it (the parent process) how I did the task you gave me. But if the parentprocess never reads, the child process will always be in Z state?
Yes!
2. Maintaining the exit status itself requires data maintenance, and it is also the basic information of the process, so it is saved in task_struct (PCB). In other words, Z status never exits , PCB always needs maintenance?
Yes!
It will cause a waste of memory resources?
Yes! Because the data structure object itself takes up memory, think about defining a structure variable (object) in C to open up space somewhere in the memory!
4.内存泄漏?
Yes!
5. How to avoid zombie processes
1.Use the wait() or waitpid() function: Call the wait() or waitpid() function in the parent process and wait The child process ends and returns a status code. This ensures that after the child process exits, its resources can be recycled normally and avoids the generation of zombie processes.
2.Use signal handler program: Register the SIGCHLD signal handler in the parent process. When the child process ends, it will notify the parent The process sends this signal, and the parent process can call the wait() or waitpid() function in the signal handler to reclaim the resources of the child process.
3.fork() twice: The child process of the first fork exits directly after the fork is completed, so that the second fork The obtained child process will no longer have a father. It will be automatically adopted by the ancestor init process. Init will be responsible for releasing its resources, so that no "zombie" will be generated.
2. Orphan process
1. Related concepts
Question: If the parent process exits early and the child process exits later and enters Z, what should be done?
The parent process exits first, and the child process is called an "orphan process". The orphan process is adopted by init process No. 1. Of course, it must be recycled by the init process. a>
2. Create an orphan process
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
pid_t id = fork();
if(id < 0)
{
perror("fork");
return 1;
}
else if(id == 0)
{//child
printf("I am child, pid : %d\n", getpid());
sleep(10);
}
else
{//parent
printf("I am parent, pid: %d\n", getpid());
sleep(3);
exit(0);
}
return 0;
}
The specific implementation is the same as above!
3. The harm of orphan processes
An orphan process is a process without a parent process. The important task of the orphan process falls on the init process. The init process is like a civil affairs bureau, responsible for handling the aftermath of the orphan process. Whenever an orphan process appears, the kernel sets the parent process of the orphan process to init, and the init process will cyclically wait() its exited child processes. In this way, when an orphan process ends its life cycle miserably, the init process will handle all its aftermath on behalf of the party and the government.
So there is no harm in the orphan process.
3. Process priority
1. Related concepts
The order in which CPU resources are allocated refers to the priority of the process.
Processes with higher priority have priority execution rights. Configuring process priorities is very useful for Linux in a multi-tasking environment and can improve system performance.
You can also run the process on a designated CPU. In this way, arranging unimportant processes to a certain CPU can greatly improve the overall performance of the system.
2. View system processes
In a linux or unix system, using the ps-l command will output something similar to the following:
We can easily notice several important information, as follows:
UID: represents the identity of the executor
PID: represents the codename of this process
PPID: Represents the process from which this process is derived, that is, the code name of the parent process
PRI: Represents the priority at which this process can be executed. The smaller the value, the earlier it will be executed.
Nl: represents the nice value of this process
3.PRI and NI
4.PRI vs NI
It should be emphasized that the nice value of a process is not the priority of the process. They are not the same concept, but the nice value of the process will affect the priority change of the process.
It can be understood that the nice value is the modified data of the process priority.
Conclusion:The secondary sharing of processes in the Linux system ends here. You can practice the operations without demonstration. I hope that the sharing of this article will be helpful to everyone. The study has brought some help. If you have any questions, you are welcome to leave a message in the comment area~~~