쓰레드 & sleep & 성능에 관한 질문.

air74의 이미지

조금 종합적인 질문을 드리겠습니다. ^^;;;
application 을 만들었는데. 생각보다 성능이 생각 보다 많이 안 나옵니다.

제 생각에는 main 함수 부분의 루핑시 sleep 때문인거 같습니다.
1. sleep을 안 주고 main 함수에서 looping 을 돌리자니 thread 와 main 함수 사이에 동기화 문제가 발생하여 main 함수의 루핑 부분이 CPU 를 다 잡는 현상 발생
2. 세마 포어를 이용하자니 main 함수의 while 루프 부분에 block 이생겨서 timer_routine 이 동작에 문제가 생깁니다.

application 성능을 위하여 할 수 있는 최선의 방법은 무엇일까요?
thread 가 얼추 균등하게 cpu 를 잡을 수 있도록 하고. 타이머 루틴도 어느정도 릴라이어블(최소 20ms 단위) 하면서 성능을 극대화 시킬 수 있는 방법이 몰까요?

/* 쓰레드는 소켓에서 데이터 수신 큐에 집어 넣는 역할만 */
thread_function(){ 
    while {
    recvfrom(소켓에서 데이터 수신할때까지 block);
    put_eventQ(수신한 데이터를 큐 넣음);
    }
}

main(){
    초기화 수행 
    Timer_register(타이머 루틴 register)
    pthread_create(thread_function); -> 쓰레드 생성
    while { /* 루핑을 돌며 큐 검사후 실행, 타이머 검사, thread 을 위한 sleep */
         if(get_eventQ(큐검사)){
            process_eventQ(큐에서 데이터를 꺼내 처리)
         }
         
         timer_checking_routine(시간 검사 하여 expire 돼었으면 timer 루틴 실행)
         
         delay.tv_sec = 0;  /* 쓰레드간 동기화를 위해 sleep */
         delay.tv_nsec = 1;
         nanosleep (&delay, NULL);
    }  
}

그럼 여러 고수분들의 현명한 지적 부탁 드립니다.

simpid의 이미지

제 생각으로는 get_eventQ() 에서 블록되는 형태로 하고..
nonosleep()은 빼버리는게 좋을 듯 합니다.
timer_checking_routine()은 get_eventQ()에 통합해야 겠죠.

put_eventQ(), get_eventQ()를 condition으로 연결해서
put이 됐을때 condition을 셋하고.. get_eventQ()에서는 condition이 셋 되었을때 큐에 lock을 걸고 데이터를 가져와야 겠죠.

get_eventQ()에서는 condition이 셋될때 까지 무조건 기다리는게 아니고 최소의 timeout값으로 기다리면서 timeout이 걸리면 timer_checking_routine() 을 사용하고 다시 wait에 들어가는 식으로...

어째됐던... 슬립은 사용할 필요가 없을것 같군요.

atomaths의 이미지

http://bbs.kldp.org/viewtopic.php?t=50170&highlight=sleep

세마포어때문에 timer_routine 이 영향을 받는다면
timer_routine 을 따로 떼어내시는게 낫지 않을까요?

익명 사용자의 이미지

pthread_cond_t 를 사용해 signal로 읽을 수 있음을 알려주는 방법이

좋을거 같습니다.

댓글 달기

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