signal 처리가 잘안돼요.. 도와주세요..

signal 처리가 잘안되요..
제가 한 test 는 메인프로그램에서 1개의 서브 스레드를 생성하고 각자 돌다
가 SIGHUP signal 을 받으면 restart 하는 프로그램입니다. 실행시키면 3개의
스레드가 프로세스로 구동되는데, 제가 그 쓰레드에 kill -HUP 1234(PID 가정)
라고 수행하면 제대로 restart 합니다. 하지만 두번째로 SIGHUP 을 주면
restart가 되지 않습니다. 특히 첫번째 실행후에 최상위 프로세스(아마
initial thread인듯한데...???)는 왜 PID가 바뀌지 않는지도 궁금합니다....
제발 도움좀 주세요..
[실행결과]
[root@/root]# ps -ef | grep th
root 13727 13503 0 1555 pts/0 000000 ./thmain
root 13728 13727 0 1555 pts/0 000000 ./thmain
root 13729 13728 0 1555 pts/0 000000 ./thmain
[root@/root]# kill -HUP 13727
[root@/root]# ps -ef | grep th
root 13727 13503 0 1555 pts/0 000000 ./thmain
root 13732 13727 0 1555 pts/0 000000 ./thmain
root 13733 13732 0 1555 pts/0 000000 ./thmain
[root@/root]# kill -HUP 13727
[root@/root]# ps -ef | grep th
root 13727 13503 0 1555 pts/0 000000 ./thmain
root 13732 13727 0 1555 pts/0 000000 ./thmain
root 13733 13732 0 1555 pts/0 000000 ./thmain
[Makefile]
CC=gcc
LD=gcc
CFLAGS=-g -I../inc -D_REENTRANT
UTILOBJ = thmain.o thdcli1.o
PROGS = thmain
all $(PROGS)
thmain $(UTILOBJ)
$(LD) $(LDFLAGS) -o $@ $(UTILOBJ) -lpthread
clean
-/bin/rm -f *.o $(PROGS)
[thmain.c]
#include
#include
#include
#include
void cli1_thread_cancel();
int cli1_thread();
pthread_t cli1_thd;
pthread_t cli2_thd;
pthread_mutex_t s_lock;
char **gargv;
void xsleep (int no)
{
struct timeval tt;
tt.tv_sec = no / 1000;
tt.tv_usec = no % 1000;
select ( 0, NULL, NULL, NULL, &tt );
}
void aaa_restart(n)
int n;
{
printf("signal = %d\n",n);
printf("thmain restart\n",n);
xsleep(1000);
cli1_thread_cancel();
printf("cli1 thread cancel \n");
execv(gargv[0],gargv);
}
int main(int argc, char **argv)
{
int ret;
signal(SIGHUP,aaa_restart);
pthread_mutex_init(&s_lock,NULL);
gargv = argv;
ret = cli1_thread();
if (ret != 0) return 0;
printf("[ INIT ] cli1 Thread Start...\n");
while (1) {
xsleep(100);
printf("Main Thread Running...\n");
}
}
[thdcli1.c]
#include
#include
extern pthread_t cli1_thd;
void cli1_thread_cancel()
{
if ( pthread_kill ( cli1_thd, 0 ) == 0 )
pthread_kill ( cli1_thd, 31 );
}
void cli_sig31(int sn)
{
pthread_exit(NULL);
}
void* cli1_main()
{
int state;
signal(31,cli_sig31);
xsleep( 1502 );
while (1) {
xsleep(500);
printf("cli1 thread running ...\n");
}
}
int cli1_thread()
{
if (pthread_create(&cli1_thd, NULL, cli1_main, NULL) != 0 ) return -
1;
return 0;
}
PID가 같은 이유만...
exev()계열 함수는 PID가 변화하지 않습니다.
즉, 현재 실행된 상태에서 그 PID를 유지하면서 지정된 프로그램에게
그 PID를 넘겨주게 되며 자신은 종료가 되는겁니다.
SIGHUP은 조금더 봐야할 것 같네요...
Re: signal 처리가 잘안돼요.. 도와주세요..
signal 핸들러 함수 안에서는 signal 을 처리할수 없습니다..
watchdog 프로세스를 만들고 그를 통해 재시작 시키는 것이
해답인듯 합니다...
Re: signal 처리가 잘안돼요.. 도와주세요..
참고지만, POSIX thread에서는 thread간에 signal handler는 공유됩니다.
물론, signal과 pthread간의 관계에 있어서 LinuxThread가 완벽하게 표준
을 따르고 있지는 않지만 아무튼 signal handler는 공유됩니다.
댓글 달기