Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set memory limit of process in C (with fork and exec)

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.

like image 345
Michal Bukovy Avatar asked Jun 02 '13 20:06

Michal Bukovy


People also ask

How do I allocate more memory to a process in Linux?

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 .

How do I check my Cgroup memory limit?

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.


2 Answers

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.

like image 188
Mike Pelley Avatar answered Nov 14 '22 23:11

Mike Pelley


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.

like image 40
Ottavio Campana Avatar answered Nov 14 '22 23:11

Ottavio Campana