I want to run a process with a memory limit set (ideally data segment, stack and heap as well) My code looks something like
child = fork();
if ( child == 0 )
{
...
execv( program, args );
}
wait( &status );
and this structure should be preserver, I do some stuff with it (redirecting stdin/out, measuring execution time etc)
My question: How can I set a memory limit for a program process and let parent know, if it was exceeded? The process should not be killed with sigsegv, I want to know, the process was killed just because of this memory limit. Or better, is there a way to get memory usage of this process when it's finished? After the process is finished, I can compare max used memory.
I can't use valgrind (or something similar), because I can't slow down the execution time.
Linux provides a variety of APIs for memory allocation. You can allocate small chunks using kmalloc or kmem_cache_alloc families, large virtually contiguous areas using vmalloc and its derivatives, or you can directly request pages from the page allocator with alloc_pages .
To ensure memory limitation is correctly applied, it's possible to look in directory /sys/fs/cgroup/memory/mygroup/ and precisely at file memory. limit_in_bytes . In my case, I use cgroups only to limit memory allocated but it's also possible to limit CPU. Then, let's see if it's works.
You can call setrlimit()
after your check for the child process and before the execv()
call. I don't know how to notify the parent, but perhaps this will point you in the right direction.
you can call ulimit
inside a system
(or setrlimit
, as written by Mike). When your programm will reach that limit, malloc will fail (i.e. return NULL), and you'll need to handle that situation (either they exit with an error, or die with SIGSEGV if they try to access a null pointer).
About signalling the parent... Can you change the child program? You could return a particular exit code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With