2

I am creating a shell which can spawn programs in the background. When a program starts to use a certain amount of memory, like 100MB, I want an interrupt to be triggered that will cause a function to run. The alternative is to have a process keep running and checking the status of background processes. Using an interrupt seems to use less CPU resources. Is that correct? Is it possible to do this?

I'm trying to do this in Linux.

node ninja
  • 31,796
  • 59
  • 166
  • 254

3 Answers3

1

In Linux, you can set a resource limit for a process with setrlimit. In your case, RLIMIT_AS or RLIMIT_DATA can be used. malloc, which uses brk, will fail on reaching the limit. You can monitor the value returned by malloc, and raise a signal yourself.

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
ousoo
  • 4,584
  • 1
  • 15
  • 4
  • How is the monitoring done? If it's not an interrupt then how is it any better? – node ninja Apr 28 '11 at 04:08
  • You can check whether malloc return NULL every time when you call malloc , and send the process a user- defined signal, and then a signal handling function will be called – ousoo Apr 29 '11 at 01:30
1

One way to do this on Linux would be to use ptrace. You'd then use PTRACE_PEEKUSER to look at the values the child is supplying to the kernel when it makes system calls. You'd be looking for things like mmap and/or brk. The function being called is defined by the value in EAX when int 80 is executed (sorry, offhand I don't know the numbers for each mmap or brk).

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
1

When a program starts to use a certain amount of memory, like 100MB, I want an interrupt to be triggered that will cause a function to run.

Depending on requirements. If capturing malloc calls is enought you might be interested:

If, you'd like to capture brk(2) as well - (related article) , you might like to follow in some of tracing solution

or binary instrumentation

Community
  • 1
  • 1
Grzegorz Wierzowiecki
  • 10,545
  • 9
  • 50
  • 88