pthread_atfork, mutex 를 써도 __pthread_handles() 에서 죽네
글쓴이: jai / 작성시간: 화, 2004/01/13 - 10:51오전
코어파일 스택
Program received signal SIGSEGV, Segmentation fault.
#0 0x42002022 in __pthread_handles () from /lib/i686/libc.so.6
#1 0x4003a642 in fork () from /lib/i686/libpthread.so.0
#2 0x0805d365 in my_system (
command=0x807b750 "insmod /tmp/test.o") at config.c:1088
프로그램 입니다. 내용이 길어서 에러처리하는 부분은 생략하고,
thread 처리에 대한 내용만 올립니다.
int main()
{
....
status = pthread_atfork(fork_prepare, fork_parent, fork_child);
pthread_attr_init(&thread_attr);
pthread_mutex_lock(&fork_mutex);
status = pthread_create(&thread_id, &thread_attr,
thread_routine, buf);
pthread_mutex_unlock(&fork_mutex);
...
}
void thread_routine(void *buf)
{...
my_system("insmod /tmp/test.o");
}
int my_system(char *command)
{...
if ((pid = fork()) < 0)
return (-1);
if (pid == 0) {
char *argv[4];
argv[0] = "/bin/sh";
argv[1] = "-c";
argv[2] = command;
argv[3] = NULL;
status = execv(argv[0], argv);
return (-1);
}
status = pthread_mutex_lock(&fork_mutex);
status = pthread_mutex_unlock(&fork_mutex);
if (waitpid(pid, &status, 0) < 0)
return (-1);
....
}
void fork_prepare(void)
{...
status = pthread_mutex_lock(&fork_mutex);
}
void fork_parent(void)
{ ....
status = pthread_mutex_unlock(&fork_mutex);
}
void fork_child(void)
{....
status = pthread_mutex_unlock(&fork_mutex);
}
수행순서는
1. 데몬으로 동작하는 중, 쓰레드를 생성합니다.
2. 그 쓰레드에서 my_system() 을 호출합니다.
3. my_system() 은 fork() 한 뒤 바로 execv() 를 수행합니다.
* -D_REENTRANT -g -Wall -lpthread 로 컴파일 옵션을 주었습니다
이 때 로깅한 바로는 fork() 호출하고, fork_prepare() 까지 수행하고 죽습니다. pthread_atfork() 를 등록하면, fork() 가 mt_safe 된다고 하던데,
왜 쓰레드가 죽는지 잘 모르겠습니다.
2번 중에 한번은 죽지 않구요, 연달아 my_system() 을 호출하면 항상 죽습니다.
Forums:


댓글 달기