Is the parent process init not a zombie process?

Source: Internet
Author: User

Is the parent process init not a zombie process?

Is the parent process init not a zombie process?
-- Lvyilong316

There was a "Z" (zombie) process found when I was operating the online environment for several times at work. The first thought at that time was to kill the parent process of the process. However, in ps, the parent process of the process is init. This situation is confusing to me, because in general, once the init process takes over the Z state process, it will call wait to recycle it. This is an important way to avoid zombie processes. How can this happen?

Later, I thought about the process of terminating the process and found out the problem. We know that the kernel function do_exit will be called when any process ends. This function has two important tasks to complete:

(1) (as a parent process) find a new parent process for its child process (if any;

(2) notify the parent process (as a sub-process) to "collect dead ";

The first thing is relatively simple. If you want to exit a multi-threaded process, you can entrust the sub-process to your brother thread. If there is no such thread, it is entrusted to the init process;

The second thing is relatively simple for single-threaded processes, but multi-threaded processes will be more complex. Only the main thread in the thread group is eligible to notify the parent process. When other threads in the thread group terminate, the parent process is not notified, and there is no need to reserve resources to enter the botnet state, call the release_task function to release all resources.

Because the parent process only recognizes the main thread of the child process, if the main thread is terminated in the thread group but there are other threads in the thread group, the parent process will not be notified to collect the dead, until the last thread in the thread group exits. In other words: at the user level, you can call pthread_exit to make the main thread "dead" first, but in the kernel state, the task_struct of the main thread must be kept, even if it becomes a zombie.

So when the main thread exits but other threads are still running, the main thread will change to Z state, even if init takes over the main thread.

L test code

 
 
  1. # Include
  2. # Include
  3. # Include
  4. # Include
  5. Void thread_fun (void)
  6. {
  7. Printf ("begin thread... \ n ");
  8. Sleep (100 );
  9. Printf ("end thread... \ n ");
  10. }
  11. Int main (int argc, char * argv [])
  12. {
  13. Pthread_t tid;
  14. Int ret;
  15. Daemon (0, 0); // call deamon to let init take over the main thread
  16. Printf ("process begin... \ n ");
  17. Ret = pthread_create (& tid, NULL, (void *) thread_fun, NULL );
  18. If (ret! = 0)
  19. {
  20. Printf ("pthread error \ n ");
  21. Exit (1 );
  22. }
  23. Printf ("main thread is end... \ n ");
  24. Pthread_exit (0 );
  25. Printf ("never exce \ n ");
  26. }

We create a thread to execute sleep, and then the main thread exits. When we use ps to view the status of the main thread, the results are as follows:

18099 1 Zsl a. out 0.0 0.0

As you can see, although init takes over the main thread, the main thread is still in Z state. There are two ways to make the Z process disappear: one is to kill the entire process, that is, kill 18099, and the other is ps-eLf to find other threads and kill them.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.