Deep analysis of the results of a parent-child thread, process termination sequence _c language

Source: Internet
Author: User
Tags terminates

Linux programming, threading, process exit sequence problems are confusing, if the parent process/thread before the child process/thread termination, what the system will do? Conversely, what will the system do if the subprocess/thread terminates before the parent process/thread? Here are some of my personal notes on the symbolic summary, if there is doubt, can be raised, I always think that suspicion is the fundamental human progress.

One, thread
The Linux thread creation function is Pthread_create (), the default rule is who creates the child thread, who is responsible for the child thread's resource collection, and when the parent thread exits, the child thread exits. Therefore, in general, when the parent thread exits, it is necessary to ensure that the child thread has exited, so the Pthread_join () function is used to block the exit signal/identity of the waiting child thread.
The function of the Pthread_detach (ThreadID) function is to make a thread with a thread ID threadid in a detached state (which can be a non parent-child relationship), and once the thread is detached, the underlying resource is reclaimed immediately when the thread terminates Otherwise, the state of the terminating child thread will hold the resource of the system until the main thread calls Pthread_join (Threadid,null) to get the exit state. The created child thread can also detach itself, and the child thread calls Pthread_detach (Pthread_self ()) to detach itself, because pthread_self () This function returns its own thread ID.
1 The parent thread terminates before the child thread
If the parent thread is preceded by a child thread, then the child thread exits abnormally, which is definitely not using the blocking non-detach function pthread_join, divided into 2 different cases:
A the child thread has been detached from the parent thread, such as calling the thread detach function Pthread_detach, and the resource is automatically reclaimed for release.
b) The child thread is not detached from the parent thread, the resource cannot be freed, caused the waste of resources and the system is bloated (in this case, I see some information that the system can also automatically release the child thread resources, such as closing the descriptor, free memory space, etc., but the individual has done some testing, such as in the child thread allocated a lot of space, etc., after the process exits Top view memory state still exists).
2) The child thread terminates before the parent thread
Also divided into 2 kinds of situations:
A normal situation: the child thread calls the thread detach function Ptread_detach (), or the parent thread calls the wait thread End Function Pthread_join ().
(b) Exception: If both are called, the resource allocated for the child thread cannot be freed.

Ii. process
An existing process can call the fork function to create a new process. A new process created by Fork is called a subprocess (child process). The fork function was called once but returned two times. The only difference that is returned two times is that the child process returns a value of 0 and the child process ID is returned in the parent process.
1 The parent process terminates before the child process
When the parent process exits first, the system lets the init process take over the child process , which becomes the orphan process.
2 the child process terminates before the parent process
Divided into 2 kinds of situations:
A normal situation: the parent process calls the wait function (the Waitpid function is used by the non-parent process), and the parent process waits for the child process to finish.
b The parent process does not call the wait function (the Waitpid function is not called by a parent-child process), in which case the child process enters the zombie state, which is the zombies process, and continues until the system reboots. when a child process is in a zombie state, the kernel saves only some of the necessary information for the process in case the parent process needs it. At this point, the child process always occupies the resource, while also reducing the maximum number of processes that the system can create.
Zombie state: A process that has been terminated, but whose parent process has not yet been dealt with (getting information about terminating the child process, releasing the resources it still occupies) is called a Zombie process (zombie). The PS command prints the state of the zombie process as Z.

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.