title: How to deal with poll() without slowing server or wasting cpu?
body: <p>I ran into an interesting problem with my homemade server in C.</p>
  
  <p>The mainline part is summed up as follows:</p>
  
  <pre><code>while(donotend==1){
      if (somethingdetected==1){
      //do critical code
      }
      if (poll(structs,numberoffields,x) &lt; 1){continue;}
      //do functions based on poll fields that were filled in
  }
  </code></pre>
  
  <p>What I noticed greatly is that <code>poll()</code> determines the speed of output as well as CPU usage. </p>
  
  <p>If I set the last parameter x (the timeout value) to zero, then CPU usage for the program alone is above 95%, but each request to the server is processed instantly.</p>
  
  <p>If however, I set x to 500, then cpu usage is still above 95% but it takes 2 seconds for a request to be processed.</p>
  
  <p>If I use <code>usleep(500)</code> just before <code>poll()</code> then cpu usage goes down to almost nothing and it takes no time for a request to processed.</p>
  
  <p>This is what I don't understand:</p>
  
  <p>Why wouldn't poll have up to the number of milliseconds specified in the last parameter to catch an event and return when at least one is found instead of always waiting the full millisecond timeout?</p>
  
  <p>and What would be the effective usleep value? I feel specifying too high of a usleep value will cause the normal program execution to lock up while specifying too low of one won't have an effect.</p>