pthread를 이용한 매트릭스 효과 프로그램이 자꾸 죽습니다
유키
질문 제목 :pthread를 이용한 매트릭스 효과 프로그램이 자꾸 죽습니다
pthread를 이용한 프로그램에서 메모리 누수가 발생하는 듯한데,
디버깅을 어떤 식으로 해야하는지
아니면 해당 프로그램의 문제점은 무엇인지 알려주시면 감사하겠습니다.
질문 내용 :
아래는 매트릭스 효과...말하자면, 초록색 메세지들이 비처럼 주룩주룩 내리는 효과를 보여주는 프로그램입니다.pthread 와 ncurses 라이브러리를 사용하였습니다.리눅스에서 gcc로 컴파일해서 테스트 했는데원하는 동작은 정상적으로 수행하지만 일정시간, 짧게는 10분 길게는 몇 시간동안 돌려놓으면 혼자 죽습니다.http://codepad.org/cjb3fni3#include pthread.h#include ncurses.h#include stdio.h#include stdlib.h#include unistd.h#include sys/time.h#include string.h// 밀리세컨드 단위로 현재 시간을 반환int mygettimeofday() { struct timeval mytime; gettimeofday(&mytime, null); return (int) mytime.tv_usec;}// 비 내리는 듯한 효과로 문자열을 세로로 한 줄 출력// 함수 내부에서 스스로 스레드를 detach한다void *printcharline(void *arg) { pthread_detach(pthread_self()); char *str = whoknowwhat; // 이 문자열이 비처럼 주룩 주룩 내린다 srand( (int) mygettimeofday() ); int y = rand()%10 + 1; int x = rand()%40 + 1; int y_backup = y; int i; for( i = 0; i strlen(str); i++ ) { if( i 0 ) { attron(color_pair(1)); mvprintw(y-1, x, %c, str[i - 1]); attroff(color_pair(1)); } mvprintw(y++, x, %c, str[i]); refresh(); usleep(50000); } attron(color_pair(1)); mvprintw(y-1, x, %c, str[i - 1]); attroff(color_pair(1)); y = y_backup; sleep(2); for( i = 0; i strlen(str); i++ ) { mvprintw(y++, x, ); refresh(); usleep(50000); } pthread_exit((void *) 0);}int main() { pthread_t th; initscr(); start_color(); curs_set(0); init_pair(1, color_green, color_black); mvprintw(0, 0, matrix effect test); refresh(); while(1) { if( pthread_create(&th, null, printcharline, null) ) { perror(thread create error :); exit(0); } usleep(100000); } getch(); endwin(); return 0;}다음은 쉘 스크립트를 이용하여 프로세스의 상태를 모니터링한 결과입니다.whoknowwhat 6097 0.0 0.0 256424 1188 pts/1 sl+ 16:17 0:00 ./introwhoknowwhat 6097 0.0 0.0 256424 1188 pts/1 sl+ 16:17 0:00 ./introwhoknowwhat 6097 0.0 0.0 305612 1188 pts/1 sl+ 16:17 0:00 ./introwhoknowwhat 6097 0.0 0.0 305612 1188 pts/1 sl+ 16:17 0:00 ./introwhoknowwhat 6097 0.0 0.0 305612 1188 pts/1 sl+ 16:17 0:00 ./intro쉘스크립트를 이용하여 1초 간격으로 주기적으로 메모리 사용량을 계속 모니터링 하도록 하였는데계속 256424로 일정하다가죽고나서 로그를 보니죽기 한 30초~1분전쯤 사용하고 있는 메모리의 양이 위 처럼 증가합니다.제대로 detach가 되지 않아서 문제가 되나 싶어 detach를 주석처리 했더니,메모리 사용량이 초마다 미친 듯이 증가하는 것이 확인되었기에, detach에서 문제가 발생하는 것 같지는 않았습니다.뭣보다 일단 기본적으로 프로그램이 하는 동작에 비해 메모리 사용량이 지나치게 큰것을 봤을때문제가 분명 있긴 있는데 어디서 발생하는지 도저히 알 수가 없네요 ㅠㅠ이런 경우에 디버깅을 하려면 어떤 식으로 어디에 초점을 맞춰서 해야하는 지 조언이나문제점 자체에 대한 의견을 알려주시면 감사하겠습니다.
(사실 이미 지식in에도 올라가있는데, 좋은 답변 주시는 분께는 링크 알려드리고 채택해드릴게요)