0

I am writing a C program that runs in the background of the Linux shell and if the total memory consumption is over 85% of the total memory, then it will print out a warning. What would be the best way to check if a new process was created (I want to check the values for the memory every time a process is created)?

riv94
  • 111
  • 12
  • what's _best_ according to your needs? i.e. would you mind not noticing some process creation? – Diego May 02 '15 at 05:12
  • I mean that all I need is for the C program to recognize when a new process has been created and act accordingly. – riv94 May 02 '15 at 05:15

1 Answers1

0

The most effective way of determining when processes are created (and exit) will be to use the proc connector. It's somewhat complex to use, but will notify your process immediately when events occur.

However, keep in mind that the memory usage of processes can change dramatically while they are running. Monitoring for processes being created is almost certainly not going to be sufficient for your needs; you will need to poll memory usage periodically. (There is no general way to get notifications for system memory usage, short of running your processes in a cgroup with a memory controller and registering an OOM handler. You don't want to do this.)

Community
  • 1
  • 1