2

setrlimit()プロセスにかかる時間を制限するために使用しようとしています。ただし、のような特定の操作を行うと機能しないようですprintf()

問題を説明するテスト プログラムを次に示します。

#include <sys/resource.h>
#include <stdio.h>

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",i);

    return 0;
}

ただし、 for ループを単純なフィボナッチのような別のルーチンに置き換えると、次のようになります。

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

それは完全に機能します。何が起きてる?setrlimit()単に信頼できないのですか?

4

2 に答える 2

9

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

于 2012-07-26T01:36:40.543 に答える
2

rlimitは、壁時間ではなくCPU時間に設定されます。出力の送信先によっては、プログラムがほとんどの時間を出力デバイスで待機している場合があります。それをしている間、それはCPU時間を消費しません。そのため、プログラムは3秒より長く実行される可能性がありますが、CPU使用率を確認すると(ps up $PIDおよび下を見るとTIME)、3秒未満の使用率が表示されます。

于 2012-07-26T01:30:23.750 に答える