setrlimit

setrlimit isn't reliable?

泪湿孤枕 提交于 2019-12-04 17:44:38
I'm trying to use setrlimit() to cap the amount of time a process takes. However it doesn't seem to work when I do certain operations like printf() . Here is a test program illustrating the problem: #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; } However if I replace the for loop with a different routine like naive fibonacci: int fib(int n) { if(n<=1) return 1; return fib(n-1)

POSIX rlimit: What exactly can we assume about RLIMIT_DATA?

荒凉一梦 提交于 2019-12-04 01:37:31
Prequisites POSIX.1 2008 specifies the setrlimit() and getrlimit() functions. Various constants are provided for the resource argument, some of which are reproduced below for easier understaning of my question. The following resources are defined: (...) RLIMIT_DATA This is the maximum size of a data segment of the process, in bytes. If this limit is exceeded, the malloc() function shall fail with errno set to [ENOMEM]. (...) RLIMIT_STACK This is the maximum size of the initial thread's stack, in bytes. The implementation does not automatically grow the stack beyond this limit. If this limit is

How can I limit memory acquired with `malloc()` without also limiting stack?

别说谁变了你拦得住时间么 提交于 2019-11-30 17:49:52
I'm trying to keep student code from running wild with allocations and dragging my test machine to a halt. I've tried setrlimit(RLIMIT_DATA, r); where r is a struct holding the limits. But unfortunately although this limit stops brk and sbrk from allocating, the C library just fails over to mmap and keeps right on allocating. I've also tried setrlimit(RLIMIT_AS, r) and this stops the process in its tracks, but this remedy is too severe—it is impossible for the process to recover from the ENOMEM error because there's no stack space for the calls the code makes on encountering a NULL value