In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Linux allows a process to query the kernel for the PID of its parent process, or the execution status of any of its child processes. For example, a process can create a child process to perform a specific task, and then call some library functions such as wait () to check whether the child process is terminated. If the child process has been terminated, its termination code tells the parent whether the task has completed successfully.
To follow these design principles, the Linux kernel does not allow the data contained in the process descriptor field to be discarded as soon as the process terminates. This is allowed only after the parent process issues a wait () class system call related to the terminated process. This is why zombies are introduced: although the process is technically dead, its descriptor must be saved until the parent process is notified.
If a process has been terminated, but its parent process has not called wait () or waitpid () to clean it, the process state is called zombie state, and the process in zombie state is called zombie process. Any process is a zombie process when it is terminated, and normally, the zombie process is immediately cleaned up by the parent process.
How zombie processes are created
In the UNIX system, a process ends, but its parent process does not wait for him (calling wait / waitpid), then he will become a zombie process. Check its flag with defunct through the ps command. A zombie process is a process that is long dead, but still occupies a slot in the processs table.
But if the parent process of the process has ended first, then the process will not become a zombie process. Because when each process ends, the system will scan all the processes running in the current system to see if any process is a child of the process that has just ended. If so, the Init process will take over him and become his parent process, thus ensuring that each process will have a parent process. The Init process automatically wait its children, so all processes taken over by Init do not become zombie processes.
In order to observe the zombie process, we write an abnormal program ourselves. The parent process fork the child process, the child process terminates, and the parent process neither terminates nor calls the wait cleaning child process:
# include # include # include int main (void) {int I = 100; pid_t pid=fork (); if (pid)
< 0) { perror("fork failed."); exit(1); } if(pid >0) {printf ("This is the parent process. My PID is% d.\ n", getpid ()); for (; I > 0; I -) {sleep (1);}} else if (pid = = 0) {printf ("This is the child process. My PID is:% d. My PPID is:% d.\ n", getpid (), getppid ());} return 0;}
Save the above code to the file zomprocdemo.c file and execute the following command to compile:
$gcc zomprocdemo.c-o zomprocdemo
Then run the compiled zomprocdemo program:
$. / zomprocdemo
By this time the child process has exited, but the parent process has not exited and the child process has not been processed through the wait () call. We use the ps command to see the status of the process:
The uppercase "Z" in the red box above indicates that the process with a PID of 112712 is in a dead state at this time.
Let's move on! The parent process exits after the end of the sleep. When the parent process exits, the child process becomes an orphan process, and it is adopted by an administrative process. This management process varies from system to system. In the early days, it was generally the init process, upstart on Ubuntu, and more recently, Systemd. But they all accomplish the same task: wiat (), these orphan processes, and eventually release the resources in the system schedule that they occupy. In this way, these dead orphan processes are completely eliminated.
The harm of zombie process
When a process exits, the kernel releases all the resources of the process, including open files, occupied memory, and so on. But still keep some information for it (including process number PID, exit status the termination status of the process, run time the amount of CPU time taken by the process, etc.). Not released until the parent process fetches it through wait / waitpid.
If the process does not call wait / waitpid, then the retained information will not be released, and its process number will always be occupied, but the process number that the system can use is limited. If a large number of dead processes are generated, the system will not be able to generate new processes because there is no available process number.
How to handle zombie processes
Zombie processes occur because the parent process does not have a wait () child process. So if we write our own programs, we must use wait () in the parent process to avoid zombie processes.
When there is a zombie process in the system, we can't clean it up with the kill command. But we can kill its parent process, make it an orphan process, and be further adopted and cleaned up by the process that manages the orphan process in the system.
In the following demo, the parent process waits for the child process to end through wait ():
# include # include int main (void) {pid_t pid; pid = fork (); if (pid
< 0) { perror("fork failed"); exit(1); } if (pid == 0) { int i; for (i = 3; i >{printf ("This is the child\ n"); sleep (1);} / / exit with code 3 for test. Exit (3);} else {int stat_val; wait (& stat_val); if (WIFEXITED (stat_val)) {printf ("Child exited with code% d\ n", WEXITSTATUS (stat_val));}} return 0;}
In demo, the parent process not only waits for the child process to finish, but also gets the exit code of the child process through the WEXITSTATUS macro.
The above is the whole content of this article, I hope it will be helpful to your study, and I also hope that you will support it.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.