title: How to sleep for a few microseconds
body: <p>Consider the following code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;time.h&gt;
  #include &lt;math.h&gt;
  
  // Compile with gcc -lrt -lm -o test_clock test_clock.c
  
  #define CLOCK CLOCK_MONOTONIC
  
  int main(int argc, char** argv) {
      double temp, elapsed;
      int j;
      struct timespec requestStart, requestEnd, req;
  
      // Pseudo-sleep
      clock_gettime(CLOCK, &amp;requestStart);
      temp = 0;
      for(j=0; j &lt; 40; j++)
          temp += sin(j);
      clock_gettime(CLOCK, &amp;requestEnd);
      elapsed = ( requestEnd.tv_sec - requestStart.tv_sec ) / 1e-6
                   + ( requestEnd.tv_nsec - requestStart.tv_nsec ) / 1e3;
      printf("Elapsed: %lf us\n", elapsed);
  
      // Nanosleep
      clock_gettime(CLOCK, &amp;requestStart);
      req.tv_nsec = 5000;
      req.tv_sec = 0;
      clock_nanosleep(CLOCK, 0, &amp;req, NULL);
      clock_gettime(CLOCK, &amp;requestEnd);
      elapsed = ( requestEnd.tv_sec - requestStart.tv_sec ) / 1e-6
                   + ( requestEnd.tv_nsec - requestStart.tv_nsec ) / 1e3;
  
      printf("Elapsed: %lf us\n", elapsed);
  
  }
  </code></pre>
  
  <p>On my 2.6.32 system, the result is</p>
  
  <pre><code>Elapsed: 5.308000 us
  Elapsed: 69.142000 us
  </code></pre>
  
  <p>I agree that this is most likely because nanosleep() asks the kernel to reschedule the process. How can I avoid this? I want to keep ownership of the CPU and just idle around for a precise amount of time.</p>