2014-11-18 11 views
7

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:

  1. creare un processo con un thread principale
  2. Creare un nuovo thread indipendente
  3. pthread_exit thread principale
  4. pthread_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 
+1

forse si vuole 'ptherad_join()'? –

+0

Voglio un nuovo thread, che vivrà più a lungo del thread principale originale. E quel thread principale è distrutto correttamente, senza essere zombi. – likern

+0

quindi vuoi staccare il thread dal thread medio usando http://linux.die.net/man/3/pthread_detach :) – MimiEAM

risposta

4

Questo è un problema noto. A un tempo fa, il numero fix proposed di Kaz non è stato accettato da Ulrich Drepper. Il suo commento su questo è stato:

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. 

Leggi la catena posta elettronica per più discussione su questo qui:

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

3

Il sistema operativo pensa che il vostro processo è uno zombie, perché il filo conduttore, che è stato avviato da parte del sistema operativo, è tornato (vale a dire, uscito). Se non vuoi questo comportamento, allora non hai l'uscita principale del thread.

+1

Vorrei aggiungere che si tratta di uno zombi nominale (un "nombo"?). La chiusura del thread principale tramite 'pthread_exit' quando altri thread sono in esecuzione non genera un SIGCHLD, né interrompe alcuna chiamata di tipo" wait'-like nel genitore. Il processo è "defunto" più o meno solo dalla prospettiva di _ps (1) _. – pilcrow

+1

Bene, tutti i thread sono avviati dal sistema operativo in questo senso. 'pthread_exit()', per definizione, dovrebbe uscire solo dal thread corrente, proprio come quando viene chiamato da qualsiasi altro thread. Quindi, è perfettamente ragionevole aspettarsi che il thread principale si comporti come qualsiasi altro thread nel processo. Ma certo, non è così. –

+0

Ovviamente, il thread principale * non è * come nessun altro thread: non * deve essere creato da 'pthread_create()' e il sistema operativo può trattare la sua terminazione come speciale. –