In depth analysis of the results of different parent and child threads process termination order

  • 2020-04-02 00:47:30
  • OfStack

Programming under Linux, the thread, process exit order issues are very confusing, if the parent process/thread terminated before the child process/thread, what will the system do? Conversely, if the child/thread terminates before the parent, what does the system do? Here are some of the symbolic summaries I've made in my notes, and if you have any questions, you can ask them. I've always believed that doubt is fundamental to human progress.

A thread,
The Linux thread creation function is pthread_create(). The default rule is that whoever creates the child thread is responsible for the resource recovery of the child thread. When the parent thread exits, the child thread exits with it. So, in general, the parent thread exits to make sure that the child thread exits, so the pthread_join() function blocks the exit signal/id waiting for the child thread.
The pthread_detach(threadid) function keeps the threadid threadid in a separate state (it can be a non-parent relationship), and once the thread is in a separate state, the underlying resource is immediately recovered when the thread terminates. Otherwise, the status of the terminated child thread will hold the occupied system resources until the main thread calls pthread_join(threadid,NULL) to obtain the exit status of the thread. The created child thread can also detach itself. The child thread calls pthread_detach(pthread_self()) to detach itself, because the function pthread_self() returns its own thread ID.
1) parent thread terminates before child thread
If the parent thread is prior to the child thread, the child thread exits as an exception, then the blocking non-separation function pthread_join is definitely not used, in two cases:
A) the child thread has been separated from the parent thread, and if the thread separation function pthread_detach is called, the resource is automatically recovered and released.
B) the child thread is not separated from the parent thread, the resources cannot be released, causing the waste of resources and system bloated (in this case, I see some information on the said system can also automatically release the child thread resources, such as closing descriptors, release the memory space, and so on, but man did some testing, such as distribution of a lot of space in the child thread, etc., after the process, top for memory state when there is.
2) the child thread terminates before the parent thread
There are also two cases:
A) normal case: a child thread invokes the thread separation function ptread_detach(), or the parent thread invokes the wait thread termination function pthread_join().
  B) exception: if both of the above are calls, the resource allocated for the child thread cannot be released.

Second, the process
An existing process can create a new process by calling the fork function. The new process created by fork is called the child process. The fork function is called once but returns twice. The only difference between the two returns is that a 0 value is returned in the child and a child ID is returned in the parent.
1) the parent process terminates before the child process
When the parent exits first, the system makes the init process connect to the pipe process, and the child thread becomes Orphan process.
2) the child process terminates before the parent process
There are two cases:
A) normal: the parent process invokes the wait function (non-parent processes use the waitpid function), at which point the parent process waits for the child process to finish.
  B) the parent process does not call the wait function (the non-parent process does not call the waitpid function), in which case the child process enters The zombie state is the zombie process and will continue until the system restarts. When a child process is in a zombie state, the kernel stores only some of the necessary information about the process in case the parent needs it. At this point, the child process is always occupying the resource, which also reduces the maximum number of processes that the system can create.
Zombie state: a process that has terminated but has not yet been cleaned up by its parent (getting information about the terminated child and releasing the resources it still possesses) is called a zombie. The ps command prints the state of the dead process as Z.

 


Related articles: