title: setrlimit isn't reliable?
body: <p>I'm trying to use <a href="http://linux.die.net/man/2/setrlimit" rel="nofollow"><code>setrlimit()</code></a> to cap the amount of time a process takes. However it doesn't seem to work when I do certain operations like <code>printf()</code>.</p>
  
  <p>Here is a test program illustrating the problem:</p>
  
  <pre><code>#include &lt;sys/resource.h&gt;
  #include &lt;stdio.h&gt;
  
  int main(void) {
      int i;
      struct rlimit limit;
      limit.rlim_cur = 3;
      limit.rlim_max = 3; // send SIGKILL after 3 seconds 
      setrlimit(RLIMIT_CPU, &amp;limit);
  
      // doesn't get killed
      for(i=0; i&lt;1000000; i++)
          printf("%d",i);
  
      return 0;
  }
  </code></pre>
  
  <p>However if I replace the for loop with a different routine like naive fibonacci:</p>
  
  <pre><code>int fib(int n) {
      if(n&lt;=1) return 1;
      return fib(n-1)+fib(n-2);
  }
  int main(void) {
      ...
      fib(100);
      ...
  }
  </code></pre>
  
  <p>It works perfectly. What's going on here? Is <code>setrlimit()</code> simply unreliable?</p>