pthread에서 또다른 pthread생성시 좀비 스레드가 생기는 이유.

ningoi의 이미지

안녕하세요 이제 막 공부하려는 한 우민입니다.
운영체제는 리눅스이구요.
스레드에서 스레드를 생성시켰는데 좀비스레드가 생성되네요..
이상한건 좀비 스레드가 생성되었다가 잠시후에ps-aux로 보면 다시 원래 상태로 돌아가거든요.
(테스트는 이렇게 했습니다. 한 컴에서 막~~ 클라이언트로 요청 보내고...
모니터링을 ps -aux | grep 프로세스명 으로 한번씩 찍어줬습니다.)

소스는 대충 이렇습니다.(지면 압박때문에 스레드에 관련된것만 적습니다.)
int main()
{
pthread_t pthread1;
...
pthread_create(&p_thread1, NULL, processThread, NULL);
}

void * processThread(void * data)
{
pthread_t pthread2;
int pthread_id;
pthread_id = pthread_self();
pthread_detach(pthread_id);

pthread_create(&pthread2, NULL, processThread2, NULL);
...
return NULL;
}

void * processThread2(void * data)
{
int pthread_id1 = pthread_self();
pthread_detach(pthread_id1);
...
return NULL;
}
위와 같은데요...
첫번재 스레드는 두번째 스레드가 종료되기전에 종료가 되면 안됩니다.

MackTheKnife의 이미지

thread2가 종료되고나서 thread1이 종료될려면 thread2가 종료되기를
기다려야됩니다.
Pthread_join으로 기다리는부분을 추가면 됩니다.

void * processThread(void * data) 
{ 
pthread_t pthread2; 
int pthread_id; 
pthread_id = pthread_self(); 
//pthread_detach(pthread_id);  

pthread_create(&pthread2, NULL, processThread2, NULL); ... 

<--------- pthread_join() 을 추가함
return NULL; 
} 

void * processThread2(void * data) 
{ 
int pthread_id1 = pthread_self(); 
pthread_detach(pthread_id1); 
... 
return NULL; 
}