[질문] select와 timer(setitimer) poll message queue등

pleasantman의 이미지

프로그램을 하고 있습니다.
select로는 네트워크와 serial에서 들어오는 데이터를 봐야 합니다.
그리고 메세지 큐에서 들어오는 메세지도 받아야 합니다.
또한 500ms마다 host로 status도 전송해야 합니다.

여러가지 문제가 있더군요..
문제 1)
message queue의 크기가 메세지 queue를 보내는 곳에서 다 보내질 못하고 queue가 full이 되어 버리는 군요.
일단 이 문제는 공유메모리를 쓰는 걸로 해결 해야 할 것 같습니다. 문제는 보낸는 넘이 delay가 있으면 안된다는 건데.. 패킷도 빠지질 않고 보내는 쪽에서 block도 걸리지 않는 방법이 없을까요?

문제2) 500ms마다 정확하게 status를 보내야 하는데... setitimer를 사용했더니 소켓과 serial에서 들어오는 것을 감시하는 select에서 SIGARM를 받으면
나와버리는 것 같더군요.. 그래서 select error가 발생합니다.
setitimer를 사용하지 않고 500ms마다 check를 하게 하려고 합니다.
지금은 setitimer없이 polling으로 돌려서 500ms가까이에 처리를 하고 있습니다만.. 시간이 민감함 문제라서. 반드시 수정해야 합니다.

대강의 코드 흐름은 다음과 같습니다.

while(1)
{
t.tv_sec = 0;
t.tv_usec = 0;

nfds = 0;
FD_ZERO(&read_fds);

if( nfds <= sockFd) nfds = sockFd + 1;
FD_SET(sockFd, &read_fds);

if( nfds <= sockFd) nfds = serialFd + 1;
FD_SET(serialFd, &read_fds);

for(i=0; i<Num_Cli; i++)
{
if( nfds <= client[i]) nfds = client[i] + 1;
FD_SET(client[i], &read_fds);
}

if(select(nfds, &read_fds, (fd_set *)0, (fd_set *)0, &t) < 0)
{
fprintf(stdout, "select error\n");
return -1;
}

if(FD_ISSET(sockFd, &read_fds))
{
cliLen = sizeof(cliAddr);
client_fd = accept(sockFd, (struct sockaddr *)&cliAddr, &cliLen);

if(client_fd != -1)
{
setnonblocking(client_fd);
client[Num_Cli] = client_fd;
Num_Cli++;
if (nfds <= client_fd)
nfds=client_fd+1;
}
else
{
fprintf(stderr, "accept failed \n");
exit(0);
}
fprintf(stdout,"New Client connected Client fdnum is %d\n",client_fd);
}

for(i = 0; i < Num_Cli; i++)
{
if(FD_ISSET(client_s[i], &read_fds))
{
if( (recvcount=recv(client[i], socketbuf, sizeof(socketbuf), rflag)) > 0 )
// do_something
}
else
{
fprintf(stderr, "Connection closed by client");
close(client[i]);
Num_Cli--;
}
}

k = read_message(qid, (_MSGBUF *)&qbuf, 1);
if( k != -1 )
{
//do_something
}

CurTime = get_time_msec();
time_interval = PreTime - CurTime;

if( ( time_interval > 450 ) && (time_interval <= 550 ))
{
PreTime = CurTime;
send_status();
//do_something
}
}

댓글 달기

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