Zombie process, although threads are still running

Why is Linux considering a process whose main thread has completed the zombie process, and is there a way to avoid this?

In code I below:

  • Create a process with one main thread
  • Create a new disconnected stream
  • pthread_exit main thread
  • pthread_exit dedicated thread

Prior to # 3, ps(1) shows my process as a regular process. However, after # 3, ps(1) shows my process as a zombie (e.g. 2491 pts/0 00:00:00 thread-app <defunct> ), although it still has threads.

Is it possible to exit the main stream, but not go into a zombie state?

the 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 
+7
c multithreading linux pthreads posix
source share
2 answers

This is a known issue. A correction, proposed some time ago by Kaz, was not accepted by Ulrich Drapper. 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 chain of letters for a more detailed discussion here:

http://lkml.iu.edu/hypermail/linux/kernel/0902.0/00153.html

+4
source share

The operating system considers your process a zombie, because the main thread that was launched by the operating system has returned (i.e., exited). If you do not want this behavior, then you have no output from the main thread.

+3
source share

All Articles