스레드 + select 사용시

살자의 이미지

간단한 게임 서버를 만들고 있는데여..

스레드 3개를 먼저 실행 시켜놓고 그 스레드 안에는 select가 동작하고 있습니다.

fd_set waitRoomset; 이 변수는 전역변수 입니다..

사용자가 접속을 하였을대 waitRoomset에
FD_SET(sockfd[n], &waitRoomset); 이렇게 설정해 주고 select에 적용을
시키려고 select인자중에 맨마지가막 인자 값인 timeval 구조체 값을 0을주었습니다... NULL 값을 주면 blocking이 되어서여....

위 처럼 3개의 스레드 함수를 구현 하였더니 cpu 사용량이 99. 몇이 나오네여..
이 방법 말구 다른 방법으로 사용자 접속시 마다 waitRoomset에 설정하고 select에 적용 시키는 방법이 없을까여..?

struct timeval tim;
tim.tv_sec = 0;
tim.tv_usec = 0;

 while(1)
        {
                rset = waitRoomset;
                select(waitUser + 1, &rset, NULL, NULL, &tim);
                for(cur = waitlist_root; cur != NULL; cur = cur->next)
                {
                        if(FD_ISSET(cur->sockfd, &rset))
                        {
                                if((readbyte = recv(cur->sockfd, recvBuff, MAXSIZE, 0)) > 0)
                                {
                                        waitRoom_packet(recvBuff, cur);
                                }
                                else if(readbyte <= 0)
                                {
                                        close(cur->sockfd);
                                        FD_CLR(cur->sockfd , &waitRoomset);
                                        break;
                                }
                        }
                }
        }

ifyou의 이미지

Quote:

select인자중에 맨마지가막 인자 값인 timeval 구조체 값을 0을주었습니다... 다른 값을 주면 blocking이 되어서여....

block이 된다니.. 정확이 어떤 의미인지 모르겠습니다.


int select(int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);

timeout is an upper bound on the amount of time elapsed before select returns. It may be zero, causing select
to return immediately. (This is useful for polling.) If timeout is NULL (no timeout), select can block indefinitely.

timeout을 0으로 지정하였으니 데이터가 들어오지 않는한 끊임없이 while() 루프를
돌게되어 cpu를 다 잡아먹게되는 것입니다.

적절한 timeout 값을 사용하시고.. 리턴값으로 그 상태를 확인해야 합니다.

      ret=select(1, &rfds, NULL, NULL, &tv);

리턴값이 양수이면 성공한 것이고, 0이면 타임아웃, -1이면 error입니다.

그리고, linux에서는 select후 timeout 값이 0으로 변하므로 루프문안에서

타임아웃 값을 지정해야 합니다.

지금 좀 바빠서 자세히 설명을 못하겠습네요. 이해가 되지 않으시면, 다시

한번 자세히 설명드리지요. 아니면 다른 분께서 훌륭하게 답변을 올릴것입니다 :)

살자의 이미지

답변 정말 감사합니다...

제가 하고 싶은 작업은여...

client_sockfd = accept(server_sockfd, &client, &clientlen);

클라이언트 접속시 fd_set waitRoomset; 전역 변수에
FD_SET(client_sockfd, &waitRoomset); 설정을 하면
바로 select 에 적용되는 작업을 구현 하고 싶습니다..
지금은 select 의 맨 마지막 인자의 timeval 구조체에 0값을 넣어 ....
바로 적용은 되지만 cpu를 많이 잡아 먹어서여......

다른 방법을 알고 싶습니다...

kdoll의 이미지

리스닝 소켓의 accept도 read select에 반응합니다..

즉 server_sd도 select같이 해버리시면 될듯 싶은데여.

ifyou의 이미지

kdoll wrote:
리스닝 소켓의 accept도 read select에 반응합니다..

즉 server_sd도 select같이 해버리시면 될듯 싶은데여.

그런 방법이 있었네요. 어려운 사실도 아니고 한걸음 물러나서 보았으면

저도 생각했을텐데.. :)

사실, 왜 그런 구조여야 하는지 의문이 들기는 합니다만(전체 프로그램을 모르니까)..

현재 상태에서 해결책 찾으려고, 어제부터 생각해 봤는데 마땅한 답이 나오지 않았거든요.

tim.tv_sec = 0; 
tim.tv_usec = 500; 

ret=select(waitUser + 1, &rset, NULL, NULL, &tim); 
if(ret==0) continue;

혹시 kdoll님의 방법이 해결책이 아니라면 위와 같이 해 보는 것이 어떨까요?

바람직한 방법은 아니지만, timeout 시간을 극히 짧은 시간주어서 FD_SET을

빨리빨리 반영하면서 cpu점유율도 낮추고..

댓글 달기

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