开发者

Limiting processes to 0.5s in Bash

How can I limit processes to less than 1s CPU time in Bash? I am using version 4.1.5(1) and if I type ulimit -t 0.5 command, I get:

-bash: ulimit: 0.5: invalid number

I am using ulimit -t 1 command as an imperfect substitute, but for technical reasons I don't need to bother you with, I would very much like command to be stopped after it has had CPU for a half-second or maybe a bit less.

A Linux-specific开发者_StackOverflow中文版 answer would be fine.


Perhaps you can't. The underlying system call (setrlimit) provides only whole-second resolution.

 RLIMIT_CPU
          CPU time limit in seconds.  When the process  reaches  the  soft
          limit, it is sent a SIGXCPU signal.  The default action for this
          signal is to terminate the process.  However, the signal can  be
          caught,  and the handler can return control to the main program.
          If the process continues to consume CPU time, it  will  be  sent
          SIGXCPU  once  per  second  until  the hard limit is reached, at
          which time it is sent SIGKILL.


As other people said, you can't do it with the standard built-in Bash commands.

It is possible to create your own command to limit the elapsed time for a command (distinct from the actual CPU time used by the command; see also the comments from Rob, for which, thanks!). I've previously published on SO code to do timeouts after an integer number of elapsed seconds (so it is not directly an answer to your problem). However, you could (fairly) easily adapt the code to use sub-second resolution on the elapsed time and the POSIX facilities. The core interface is:

timeout -t time [-s signal] cmd [args ...]

To handle your requirements, it would simply need to recognize sub-second times and call the appropriate functions; it affects the internals far more than the externals.

Theoretically (according to POSIX 2008), you should use the timer_create(), timer_settime() and timer_gettime() functions. These provide detailed control over which signal should be generated when the timer expires, etc. However, not all systems provide them (for example, MacOS X 10.6.7 does not document them). These use the headers <time.h> and <signal.h>, and struct itimerspec (and struct sigevent).

However, in the short term, you may need to use the functions getitimer() and setitimer() instead, which POSIX 2008 marks as obsolescent (but which are available on MacOS X 10.6.7). These use the header <sys/time.h> and struct itimerval.

To be portable, you may need to autoconfigure your program to deal with both these; you might also need to deal with Windows (which will undoubtedly provide an alternative interface). You probably fall back on alarm() and whole-second granularity timing if neither of the fine resolution signalling timer systems is available. Choose the timer_* functions over the *itimer functions if both are present - or simply test for timer_* first and use them if available.

Be aware that if you set a timeout of 0.5 seconds with these functions, they guarantee a minimum elapsed time of 0.5 seconds - but it may be arbitrarily longer than the time because of scheduling issues. Note that there is a timer_getoverrun() to help establish how long the overrun was.


You cannot. The underlying setrlimit call has a granularity of seconds.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜