9

我正在运行 Ubuntu Linux(2.6.28-11-generic #42-Ubuntu SMP Fri Apr 17 01:57:59 UTC 2009 i686 GNU/Linux),似乎命令“ulimit -t”无法正常工作。我跑了:

ulimit -t 1;我的程序

其中“myprogram”是一个无限循环。我预计程序会在 1 秒后中断,但它并没有停止。我在 Linux Fedora 安装上尝试了同样的事情,它按预期工作。

是否必须设置一些配置才能使其正常工作?

-- tsf

4

1 回答 1

17

正如Tsf指出的那样,问题是由于内核 2.6.28 中的错误造成的。我留下了我原来的答案,因为我认为无论如何它都会有所帮助。

从 ulimit 联机帮助页

-t The maximum amount of cpu time in seconds.

就 ulimit 而言,重要的是CPU time。尝试像这样启动您的程序:

time myprogram

这将向您显示它实际使用了多少 CPU 时间。

我怀疑你的无限循环包含sleep() 并且睡眠时间不会影响进程的 CPU 时间。

这会在一秒钟后被杀死:

me@host:~$ bash
me@host:~$ ulimit -t 1; for (( i=1; 1; i++ )); do a=1; done
Killed

这似乎永远运行(但当然不会):

me@host:~$ bash
me@host:~$ ulimit -t 1; for (( i=1; 1; i++ )); do sleep 1; done

像这样测量CPU时间......

me@host:~$  time for (( i=1; i<5; i++ )); do sleep 1; done

……5秒后……

real        0m4.008s
user        0m0.000s
sys         0m0.012s

...仅使用了 12 毫秒的 CPU 时间。

我在 ubuntu Jaunty Jackalope (9.04) 上试过

Linux host 2.6.28-11-generic #42-Ubuntu SMP 
Fri Apr 17 01:57:59 UTC 2009 i686 GNU/Linux
于 2009-06-10T21:58:47.727 回答