Perché Linux considera un processo il cui thread principale è terminato per essere un processo di zombie, e c'è un modo per evitarlo?Processo Zombie anche se i thread sono ancora in esecuzione
Nel seguente codice I:
- creare un processo con un thread principale
- Creare un nuovo thread indipendente
pthread_exit
thread principalepthread_exit
il filo staccato
Prima del n. 3, ps(1)
mostra il mio processo come un normale pro cess. Dopo # 3, tuttavia, ps(1)
mostra il mio processo come zombi (ad esempio 2491 pts/0 00:00:00 thread-app <defunct>
) anche se ha ancora thread in esecuzione.
È possibile uscire dal thread principale ma evitare di entrare in uno stato di zombie?
Codice:
#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
forse si vuole 'ptherad_join()'? –
Voglio un nuovo thread, che vivrà più a lungo del thread principale originale. E quel thread principale è distrutto correttamente, senza essere zombi. – likern
quindi vuoi staccare il thread dal thread medio usando http://linux.die.net/man/3/pthread_detach :) – MimiEAM