Why does Linux consider a process whose main thread has terminated to be a zombie process, and is there any way to avoid this?
In the code below I:
pthread_exit
the main threadpthread_exit
the detached threadBefore #3, ps(1)
shows my process as a normal process. After #3, however, ps(1)
shows my process as a zombie (e.g., 2491 pts/0 00:00:00 thread-app <defunct>
) even though it still has running threads.
Is it possible to quit the main thread but avoid going into a zombie state?
Code:
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
void *thread_function(void *args)
{
printf("The is new thread! Sleep 20 seconds...\n");
sleep(20);
printf("Exit from thread\n");
pthread_exit(0);
}
int main(int argc, char **argv)
{
pthread_t thrd;
pthread_attr_t attr;
int res = 0;
res = pthread_attr_init(&attr);
res = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
res = pthread_create(&thrd, &attr, thread_function, NULL);
res = pthread_attr_destroy(&attr);
printf("Main thread. Sleep 5 seconds\n");
sleep(5);
printf("Exit from main process\n");
pthread_exit(0);
}
# ./thread-app
A zombie thread is a thread that have terminated its execution but didn't terminated cleanly. It deallocates the resources used by the thread but keeps an entry in the thread/process table. Theoretically, the zombie thread exits from this status by executing a _join (POSIX).
A strace command stores all system calls and signals made by a process. Additionally, you can also kill the zombie process by sending the SIGCHLD signal to the parent process to make the parent process exit cleanly with its zombie process.
A process in Unix or Unix-like operating systems becomes a zombie process when it has completed execution but one or some of its entries are still in the process table. If a process is ended by an "exit" call, all memory associated with it is reallocated to a new process; in this way, the system saves memory.
Dangers of Zombie Processes Zombie processes also indicate an operating system bug if their parent processes are not running anymore. This is not a serious problem if there are a few zombie processes, but this can create issues for the system under heavier loads.
This is a known issue. A fix proposed a while ago by Kaz wasn't accepted by Ulrich Drepper. His comment on this was:
I haven't looked at the patch nor tried it.
If the patch changes the behavior that the main thread, after calling
sys_exit, still react to signals sent to this thread or to the process
as a whole, then the patch is wrong. The userlevel context of the
thread is not usable anymore. It will have run all kinds of
destructors. The current behavior is AFAIK that the main thread won't
react to any signal anymore. That is absolutely required.
Read the mail chain for more discussion on this here:
http://lkml.iu.edu/hypermail/linux/kernel/0902.0/00153.html
The operating-system thinks your process is a zombie because the main thread, which was started by the operating-system, returned (i.e., exited). If you don't want this behavior, then don't have the main thread exit.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With