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 threadpthread_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
c multithreading linux pthreads posix
likern
source share