setrlimit isn't reliable?

泪湿孤枕 提交于 2019-12-04 17:44:38

The CPU limit is a limit on CPU seconds rather than elapsed time. CPU seconds is basically how many seconds the CPU has been in use and does not necessarily directly relate to the elapsed time.

When you do the fib call, you hammer the CPU so that elapsed and CPU time are close (most of the process time is spent using the CPU). That's not the case when printing since most time there is spent in I/O.

So what's happening in your particular case is that the rlimit is set but you're just not using your three seconds of CPU time before the process finishes.

Changing the main as follows causes the signal to be delivered on my system:

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);

    while (1) {                      // Run "forever".
        for(i=0; i<100000; i++) {
            printf("%d\n",i);
        }
        fib(30);                     // some CPU-intensive work.
    }

    return 0;
}

When you time that under Linux, you see:

: (much looping).
52670
52671
52672
52673
52674
Killed

real   0m18.719s
user   0m0.944s
sys    0m2.416s

In that case, it took almost 20 seconds of elapsed time but the CPU was in use for only 3.36 seconds (user + sys).

The rlimit is placed on CPU time, not wall time. Depending on where your output is going, your program might be spending most of its time waiting on the output device. While it's doing that, it doesn't consume CPU time. So the program may run longer than 3 seconds, but if you check its CPU usage (ps up $PID and look under TIME) it will show less than 3 seconds used.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!