setrlimit

POSIX rlimit: What exactly can we assume about RLIMIT_DATA?

旧街凉风 提交于 2020-01-12 14:11:19
问题 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,

How do I use `setrlimit` to limit memory usage? RLIMIT_AS kills too soon; RLIMIT_DATA, RLIMIT_RSS, RLIMIT_STACK kill not at all

[亡魂溺海] 提交于 2019-12-29 01:35:29
问题 I'm trying to use setrlimit to limit my memory usage on a Linux system, in order to stop my process from crashing the machine (my code was crashing nodes on a high performance cluster, because a bug led to memory consumption in excess of 100 GiB). I can't seem to find the correct resource to pass to setrlimit ; I think it should be resident, which cannot be limited with setrlimit, but I am confused by resident, heap, stack. In the code below; if I uncomment only RLIMIT_AS , the code fails

How do I use `setrlimit` to limit memory usage? RLIMIT_AS kills too soon; RLIMIT_DATA, RLIMIT_RSS, RLIMIT_STACK kill not at all

99封情书 提交于 2019-12-29 01:35:15
问题 I'm trying to use setrlimit to limit my memory usage on a Linux system, in order to stop my process from crashing the machine (my code was crashing nodes on a high performance cluster, because a bug led to memory consumption in excess of 100 GiB). I can't seem to find the correct resource to pass to setrlimit ; I think it should be resident, which cannot be limited with setrlimit, but I am confused by resident, heap, stack. In the code below; if I uncomment only RLIMIT_AS , the code fails

setting low rlimit_nproc value doesn't allow even a single fork

◇◆丶佛笑我妖孽 提交于 2019-12-24 14:34:24
问题 I am trying to use setrlimit to limit the number of processes a program can create. Here is my code: struct rlimit limiter; getrlimit( RLIMIT_NPROC, &limiter ); limiter.rlim_max = limiter.rlim_cur = 10; setrlimit( RLIMIT_NPROC, &limiter ); int val = fork(); printf( "Error number %d\n", errno ); //gives 11 if( val == -1 ) { printf( "Fork failed\n" ); } else if( val ) { printf( "parent\n" ); } else { printf("child\n" ); } return 0; Since the value of rlim_max and rlim_cur is 10, my program

Set stack size with setrlimit() and provoke a stack overflow/segfault

こ雲淡風輕ζ 提交于 2019-12-20 08:47:00
问题 In the given example below I try to set the stacksize to 1kb. Why is it now possible to allocate an array of ints on the stack with size 8kb in foo() ? #include <stdio.h> #include <sys/resource.h> void foo(void); int main() { struct rlimit lim = {1024, 1024}; if (setrlimit(RLIMIT_STACK, &lim) == -1) return 1; foo(); return 0; } void foo() { unsigned ints[2048]; printf("foo: %u\n", ints[2047]=42); } 回答1: The limit is set immediately but only checked when trying to allocate a new stack or

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

人走茶凉 提交于 2019-12-18 19:10:23
问题 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

java.lang.OutOfMemoryError: requested 16 bytes for CHeapObj-new. Out of swap space?

杀马特。学长 韩版系。学妹 提交于 2019-12-11 23:39:13
问题 I got this error on trying to get the Java search process UP(start a java process). I am setting the address space using the RLIMIT_AS . Please help me to get past this error. I have doubts about the VM Arguements. (See below). Is there any way to get past this issue without changing the configurations. (VM Arguements) A fatal error has been detected by the Java Runtime Environment: java.lang.OutOfMemoryError: requested 16 bytes for CHeapObj-new. Out of swap space? Internal Error (allocation

Processes resources not limited by setrlimit

元气小坏坏 提交于 2019-12-10 03:31:57
问题 I wrote a simple program to restrict it's data size to 65Kb and to verify the same i am allocating a dummy memory of more than 65Kb and logically if i am doing all correct (as below) malloc call should fail, isn't it? #include <sys/resource.h> #include <stdio.h> #include <stdlib.h> #include <errno.h> int main (int argc, char *argv[]) { struct rlimit limit; /* Get max data size . */ if (getrlimit(RLIMIT_DATA, &limit) != 0) { printf("getrlimit() failed with errno=%d\n", errno); return 1; }

Find current number of open filehandle ( NOT lsof )

二次信任 提交于 2019-12-07 02:49:10
问题 On *NIX systems, is there a way to find out how many open filehandles are there in the current running process? I am looking for an API or a formula for use in C, from within the running process in question. 回答1: On certain systems (see below) you can count them in /proc/[pid]/fd. If not on one of those, see below for: wallyk's answer. In c, you can list the dir and count the total, or list the dir contents: #include <stdio.h> #include <sys/types.h> #include <dirent.h> int main (void) { DIR

setrlimit isn't reliable?

Deadly 提交于 2019-12-06 09:30:19
问题 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