[질문] pthread 메모리 반환 관련 ~ 이상하게 안되네요...ㅠ.ㅠ
스레드 생성, 종료간 메모리 해제가 되지 않습니다.
위키에 있는 예제를 돌려봤습니다.
http://www.joinc.co.kr/modules/moniwiki/wiki.php/article/Pthread_API_Reference#AEN25
--------- 예제 내용 ----------
예제 : pthread_detach.c #include
#include
#include
#include
void *t_function(void *data)
{
char a[100000];
int num = *((int *)data);
printf("Thread Start\n");
sleep(5);
printf("Thread end\n");
return NULL;
}
int main()
{
pthread_t p_thread;
int thr_id;
int status;
int a = 100;
printf("Before Thread\n");
sleep(5);
thr_id = pthread_create(&p_thread, NULL, t_function, (void *)&a);
if (thr_id < 0)
{
perror("thread create error : ");
exit(0);
}
// p_thread detach
// .
pthread_detach(p_thread);
pause();
return 0;
}
위의 쏘쓰 코드에서 detach 시켰을때와 그렇지 않았을때의 메모리 상황을 비교해보기 바란다. detatach 를 했을경우 프로세스의 메모리 사용율과 detache 를 주석 처리했을경우의 메모리 사용율의 변화를 서로 비교해보면 되는데, detach 를 사용하지 않았을경우 t_function 이 종료가 되더라도 자원이 해제되지 않음을 볼수 있을것이다. 테스트는 간단한 스크립트를 이용하도록 한다. [root@localhost test]# while [ 1 ]; do ps -aux | grep pthread | grep -v grep | grep -v vim; sleep 1; done
root 2668 0.0 0.1 1436 292 pts/8 S 18:37 0:00 ./pthread_detach
root 2668 0.0 0.1 1436 292 pts/8 S 18:37 0:00 ./pthread_detach
위의 ps 내용에서 5번째 필드의 변화를 확인하면 된다.
-----------------------------------------------------------------------
todo 18532 0.0 0.0 3048 388 pts/4 S+ 10:11 0:00 ./pthread_detach
todo 18532 0.0 0.0 3048 388 pts/4 S+ 10:11 0:00 ./pthread_detach
todo 18532 0.0 0.0 3048 388 pts/4 S+ 10:11 0:00 ./pthread_detach
todo 18532 0.0 0.0 3048 388 pts/4 S+ 10:11 0:00 ./pthread_detach
todo 18532 0.0 0.0 3048 388 pts/4 S+ 10:11 0:00 ./pthread_detach
todo 18532 0.0 0.0 13424 444 pts/4 Sl+ 10:11 0:00 ./pthread_detach
todo 18532 0.0 0.0 13424 444 pts/4 Sl+ 10:11 0:00 ./pthread_detach
todo 18532 0.0 0.0 13424 444 pts/4 Sl+ 10:11 0:00 ./pthread_detach
todo 18532 0.0 0.0 13424 444 pts/4 Sl+ 10:11 0:00 ./pthread_detach
todo 18532 0.0 0.0 13424 444 pts/4 Sl+ 10:11 0:00 ./pthread_detach
todo 18532 0.0 0.0 13424 448 pts/4 S+ 10:11 0:00 ./pthread_detach
todo 18532 0.0 0.0 13424 448 pts/4 S+ 10:11 0:00 ./pthread_detach
todo 18532 0.0 0.0 13424 448 pts/4 S+ 10:11 0:00 ./pthread_detach
todo 18532 0.0 0.0 13424 448 pts/4 S+ 10:11 0:00 ./pthread_detach
todo 18532 0.0 0.0 13424 448 pts/4 S+ 10:11 0:00 ./pthread_detach
todo 18532 0.0 0.0 13424 448 pts/4 S+ 10:11 0:00 ./pthread_detach
todo 18532 0.0 0.0 13424 448 pts/4 S+ 10:11 0:00 ./pthread_detach
todo 18532 0.0 0.0 13424 448 pts/4 S+ 10:11 0:00 ./pthread_detach
todo 18532 0.0 0.0 13424 448 pts/4 S+ 10:11 0:00 ./pthread_detach
todo 18532 0.0 0.0 13424 448 pts/4 S+ 10:11 0:00 ./pthread_detach
todo 18532 0.0 0.0 13424 448 pts/4 S+ 10:11 0:00 ./pthread_detach
todo 18532 0.0 0.0 13424 448 pts/4 S+ 10:11 0:00 ./pthread_detach
---------------------------------------------------------------------------------
저의 경우는 위와 같이 메모리 반환이 안되네요.
그리고 예제에 있듯이 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);를 사용하여 해봐도 위와 동일하게 메모리 반환이 안되네요...
무엇이 잘못인지 알려주시면 감사하겠습니다. 꾸벅
참고로 지금 작업하고 있는 시스템의 OS는 레드햇 엔터프라이즈 4.0 입니다.
Linux 2.6.9-42.ELsmp #1 SMP Wed Jul 12 23:27:17 EDT 2006 i686 i686 i386 GNU/Linux
detach하지 않은
detach하지 않은 경우는 pthread_join 해주시면 됩니다.
그리고 attr을 사용하는 경우는 pthread_create를 호출할때 속성인수 부분에 넣어주시면 됩니다.
댓글 달기