[완료] 시그널과 select와의 관계에 대한 질문입니다.
글쓴이: fastwater / 작성시간: 화, 2010/11/23 - 9:10오후
자꾸 도배처럼 질문을 올려 죄송합니다만... 시스템 프로그래밍의 세계는 참으로 오묘하네요.
일단 문제의 원인은 정확하게 파악했습니다.
클라이언트가 꺼질 때 소켓 디스크립터가 뭔가 동작을 해서 select가 소켓 디스크립터 쪽 비트를 마스킹한줄 알았더니 그게 아니라 시그날을 받으면 무조건 모든 비트를 마스킹하는 것 같네요.
딱히 특정한 시그널을 가리는 것 같지는 않습니다. 처음에는 시그널 핸들러가 SIGCHLD 였는데 SIGINT 로 바꾼 뒤 Ctrl+C 눌러도 똑같이 오작동을 일으키더군요.
select 대기중에 시그널을 받으면 오작동을 일으키는 것 같은데 어떻게 해결 할 수 있을까요?
Forums:
질문에 대한 정확한 답인지는 모르겠지만, select
질문에 대한 정확한 답인지는 모르겠지만, select man page에 이런 내용이 있습니다.
the following pselect()
call:
ready = pselect(nfds, &readfds, &writefds, &exceptfds,
timeout, &sigmask);
is equivalent to atomically executing the following calls:
sigset_t origmask;
sigprocmask(SIG_SETMASK, &sigmask, &origmask);
ready = select(nfds, &readfds, &writefds, &exceptfds, timeout);
sigprocmask(SIG_SETMASK, &origmask, NULL);
The reason that pselect() is needed is that if one wants to wait for either a signal or for
a file descriptor to become ready, then an atomic test is needed to prevent race conditions.
(Suppose the signal handler sets a global flag and returns. Then a test of this global flag
followed by a call of select() could hang indefinitely if the signal arrived just after the
test but just before the call. By contrast, pselect() allows one to first block signals,
handle the signals that have come in, then call pselect() with the desired sigmask, avoiding
the race.)
정확한 답이네요.
답변 감사합니다. 집에 오는 길에 버스에서 교재를 보는데 시그날 발생해서 인터럽트가 발생하면 당연히 오작동 할 수 밖에 없겠더군요.
음 ..
select 써도 signal 때문에 문제가 생겼던 기억은... 거의 없는것 같네요..
select 에서 block 된 상태에서, signal 이 발생하면 signal handler 로 점프하는데..
이때 select 는 -1 을 return 하고, errno 가 EINTR 로 세팅되면서 select 다음 루틴이 진행됩니다.
그래서 이 경우에는 다시 select 로 복귀하도록 하면 됩니다.
되면 한다! / feel no sorrow, feel no pain, feel no hurt, there's nothing gained.. only love will then remain.. 『 Mizz 』
네 맞습니다.
제가 바보라 인터럽트 발생시 추가 처리를 해줘야 한다는 사실을 전혀 생각도 못하고 있었습니다. 질문 올리고 집에 가는 길에 시그널 ppt 보는데 바보 질문 올렸다는 사실을 알겠더군요. -_-;;
사실 해결한 것도 pselect로 해결한게 아니라 select로 해결했습니다.
댓글 달기