RTS 관련 질문입니다.

bongtil의 이미지

joinc의 강좌란에서 가져온 자료를 가지고 간단하게 구현한것인데요.
보시면 아시겠지만 간단히 쓰레드 하나 생성하고 그 쓰레드에서 listen
하고 있다가 연결 요청 들어오면 sigwaitinfo 에서 이벤트 처리를
하게끔 만들었습니다.

그런데 다음 이부분에서 에러가 납니다.

void * test (void *data) 함수내 ret = sigwaitinfo(&set, &si);
에러 실-시각 시그날

이러면서 종료가 되더군요.
그래서 main() 함수에서(주석처리부분) 쓰레드 생성하기전에 sigset_t 을
설정하고 해주니깐 에러가 발생하지 않더군요.

다른 질문이나 강좌란등을 찾아보면 모두 쓰레드내에서
설정해줘도 에러가 났다는 내용은 없더군요.

왜 그럴까요? 조언 부탁드립니다.
즐거운 하루 되세요~~

#include <signal.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <string.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <arpa/inet.h>

//#include <map>
#include <string>

#ifndef __USE_GNU
#define __USE_GNU
#endif
#include <fcntl.h>
#define SOCK_MAX_NUM 1024

//using namespace std;

// 듣기 소켓 저장용
static int listen_sockfd;

/*
* 인자로 주어진 파일지정자 fd에 대해서
* RTS대응하도록 만든다.
*/
int setup_sigio(int fd)
{
if (fcntl(fd, F_SETFL, O_RDWR|O_NONBLOCK|O_ASYNC) < 0)
{
printf("Nonblocking error\n");
return -1;
}
if (fcntl(fd, F_SETSIG, SIGRTMIN) < 0)
{
printf("Couldn't set signal %d on %d\n", SIGRTMIN, fd);
return -1;
}
if (fcntl(fd, F_SETOWN, getpid()) < 0)
{
printf("Could'nt set owner %d on %d\n", getpid(), fd);
return -1;
}

return 0;
}

/*
* 듣기 소켓(endpoint socket)를 생성하고
* 만들어진 듣기 소켓에 대해서 RTS대응 하도록 한다.
* 일반적인 socket -> bind -> listen 과정을 거친다.
*/
int get_listener_fd()
{
int clilen;
int state;

struct sockaddr_in serveraddr;
clilen = sizeof(serveraddr) ;
listen_sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (listen_sockfd < 0)
{
printf("Socket create error\n");
return -1;
}

bzero(&serveraddr, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
serveraddr.sin_port = htons(7777);

state = bind (listen_sockfd, (struct sockaddr *)&serveraddr,
sizeof(serveraddr));
if (state < 0)
{
printf("Bind error\n");
return -1;
}
state = listen(listen_sockfd, 5);
if (state < 0)
{
printf("Listen error\n");
return -1;
}
printf("Listen Socket Create %d\n", listen_sockfd);
return setup_sigio(listen_sockfd);
}

void * test (void *data)
{
struct siginfo si;

int clilen;
int ret;
sigset_t set;

sigemptyset(&set);
sigaddset(&set, SIGRTMIN+1);
pthread_sigmask(SIG_BLOCK, &set, NULL);

// 듣기 소켓을 생성한다.
if(get_listener_fd() < 0)
{
printf("Socket Create error\n");
exit(0);
}

while(1)
{
printf("before sigwaitinfo \n");
// 소켓으로 부터 이벤트를 기다린다.

// Error 발생 ..
ret = sigwaitinfo(&set, &si);
printf("after sigwaitinfo \n");
if (ret == SIGRTMIN+1)
{
// 만약 듣기 소켓에 발생한 이벤트라면
// accept(2)를 호출해서 연결 소켓을 생성한다.
if (si.si_fd == listen_sockfd)
{
printf("ok signal %d %d\n", si.si_fd, si.si_code);
}
else
{
printf("socket event \n");
}
}
}
}

int main()
{
/*static sigset_t set;

sigemptyset(&set);
sigaddset(&set, SIGRTMIN+1);
pthread_sigmask(SIG_BLOCK, &set, NULL);*/

pthread_t *blah;

if( !(blah=calloc(1,sizeof(pthread_t)))) return NULL;

if(pthread_create(blah,NULL,test,NULL)) {
free(blah);
return NULL;
}

pthread_join(blah, NULL);

free(blah);

return 0;

}

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.