Reputation: 420
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.
Upvotes: 7
Views: 2849
Reputation: 54
Write your own memory manager can solve this problem.
For many applications written for morden OS, libc's malloc/free is OK, but the applications need large memories are not, it cannot tell how many memories our program have used. We can write a tree structured memory context class which is wrapper of glibc's malloc/free, when we allocate some memory , book the memory used on this memory context, and when we free some memory, minus the value from the booked value. So we can tell the memory size we actually used.
Upvotes: 1
Reputation: 4188
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.
Upvotes: 1
Reputation: 3126
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.
Upvotes: 2