Search code examples
c++linuxmemoryresource-managementcgroups

Cgroup usage to limit resources


My Goal: To provide user a way to limit resources like CPU, memory for the given process (C++).

So someone suggested me to utilize Cgroups which looks like an ideal utility.

After doing some research I have a concern:

When we utilize memory.limit_in_bytes to limit the memory usage for the given process, is there way to handle the out of memory exception in the process? I see control groups provide a parameter called "memory.oom_control" which when enabled, it kills the process that is requesting more memory than allowed. When disabled it just pauses the process.

I want a way to let the process know that it is requesting more memory than expected and should throw out of memory exception. This is so that the process gracefully exits.

Does cgroups provide such kind of behaviour?

Also is cgroup available in all flavour of linux? I am mainly interested in RHEL 5+, CENTOS 6+ and ubuntu 12+ machines.

Any help is appreciated.

Thanks


Solution

  • I want a way to let the process know that it is requesting more memory than expected and should throw out of memory exception. This is so that the process gracefully exits.

    Does cgroups provide such kind of behaviour?

    All processes in recent releases already run inside a cgroup, the default one. If you create a new cgroup and then migrate the process into the new cgroup, everything works as before but using the constraints from the new cgroup. If your process allocates more memory than permitted, it gets an ENOSPC or a malloc failure just as it presently does.