setrlimit isn't reliable?
===

I'm trying to use [`setrlimit()`][1] to cap the amount of time a process takes. However it doesn't seem to work when I do certain operations like `printf()`.

Here is a test program illustrating the problem:

    #include <sys/resource.h>
    #include <stdio.h>
    
    void spam(int n) {
        int i;
    }
    
    int main(void) {
        int i;
        struct rlimit limit;
        limit.rlim_cur = 3;
        limit.rlim_max = 3; // send SIGKILL after 3 seconds 
        setrlimit(RLIMIT_CPU, &limit);
    
        // doesn't get killed
        for(i=0; i<1000000; i++)
            printf("%d",n);
    
        return 0;
    }

However if I replace the for loop with a different routine like naive fibonacci:

    int fib(int n) {
        if(n<=1) return 1;
        return fib(n-1)+fib(n-2);
    }
    int main(void) {
        ...
        fib(100);
        ...
    }

It works perfectly. What's going on here? Is `setrlimit()` simply unreliable?

  [1]: http://linux.die.net/man/2/setrlimit