[완료] 시그널과 select와의 관계에 대한 질문입니다.

fastwater의 이미지

자꾸 도배처럼 질문을 올려 죄송합니다만... 시스템 프로그래밍의 세계는 참으로 오묘하네요.

일단 문제의 원인은 정확하게 파악했습니다.

클라이언트가 꺼질 때 소켓 디스크립터가 뭔가 동작을 해서 select가 소켓 디스크립터 쪽 비트를 마스킹한줄 알았더니 그게 아니라 시그날을 받으면 무조건 모든 비트를 마스킹하는 것 같네요.

딱히 특정한 시그널을 가리는 것 같지는 않습니다. 처음에는 시그널 핸들러가 SIGCHLD 였는데 SIGINT 로 바꾼 뒤 Ctrl+C 눌러도 똑같이 오작동을 일으키더군요.

select 대기중에 시그널을 받으면 오작동을 일으키는 것 같은데 어떻게 해결 할 수 있을까요?

SoulreaveR의 이미지

질문에 대한 정확한 답인지는 모르겠지만, 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.)

fastwater의 이미지

답변 감사합니다. 집에 오는 길에 버스에서 교재를 보는데 시그날 발생해서 인터럽트가 발생하면 당연히 오작동 할 수 밖에 없겠더군요.

ymir의 이미지

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 』

fastwater의 이미지

제가 바보라 인터럽트 발생시 추가 처리를 해줘야 한다는 사실을 전혀 생각도 못하고 있었습니다. 질문 올리고 집에 가는 길에 시그널 ppt 보는데 바보 질문 올렸다는 사실을 알겠더군요. -_-;;
사실 해결한 것도 pselect로 해결한게 아니라 select로 해결했습니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.