2

I want to set limits for how long programs spawned by execv can use a certain amount of memory and a certain amount of CPU time. For example, I want to set limits like a program cannot exceed 100MB for 30 seconds, and a program cannot use 90% or more of the CPU for more than 20 seconds.

Andy Finkenstadt
  • 3,547
  • 1
  • 21
  • 25
node ninja
  • 31,796
  • 59
  • 166
  • 254

2 Answers2

2

You can use RLIMIT_CPU, RLIMIT_DATA and friends, but none of them has any concept of time duration or windowing. If those work for you as they are, great; if not, can you explain in more detail the problem you are trying to solve?

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • I want to be able to set limits like `MAXMEM=100M:30`, meaning a program can exceed 100M for only 30 seconds, and `MAXCPU=90:20` meaning a program can run at most 20 seconds when using 90% or more of the CPU. – node ninja Apr 26 '11 at 03:37
  • In that case you would need to implement it yourself. I'm still curious what problem you are trying to solve. – John Zwinck Apr 26 '11 at 12:21
  • I'm trying to make a shell that can set limitations on programs as described. – node ninja Apr 26 '11 at 16:02
  • Then you need to write some code for it, either in your shell's application code, or if you prefer, alongside the system's own rlimit stuff (I'm not familiar with its implementation, but I imagine it is possible to add new limits). Since your shell is going to exec the other programs, you may as well start by sampling the data in procfs (`/proc//...`), as Ignacio mentioned in his answer. – John Zwinck Apr 26 '11 at 17:13
1

No. You will need to run some sort of supervisor "around" the code, either as a program that will fork/exec, or as an injected library that will intercept libc/system calls.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358