pthread_detach - 해당되는 글 1건

위의 쏘쓰 코드에서 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번째 필드의 변화를 확인하면 된다.


#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

// 쓰레드 함수
// 1초를 기다린후 아규먼트^2 을 리턴한다.
void *
t_function(void *data)
{
    char a[100000];
    int num = *((int *)data);
printf("Thread Start\n");
   
sleep(5);
printf("Thread end\n");
}

int
main()
{
    pthread_t p_thread;
    int thr_id;
    int status;
    int a = 100;

printf("Before Thread\n");
    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;
}
      c 언어  |  2007. 9. 19. 11:17



sokoban's Blog is powered by Daum & Tattertools