2.9. Starting a Process in a Control Group
Some subsystems have mandatory parameters that must be set before you can move a task into a cgroup which uses any of those subsystems. For example, before you move a task into a cgroup which uses the
cpuset.memsparameters must be defined for that cgroup.
The examples in this section illustrate the correct syntax for the command, but only work on systems on which the relevant mandatory parameters have been set for any controllers used in the examples. If you have not already configured the relevant controllers, you cannot copy example commands directly from this section and expect them to work on your system.
Refer to Chapter 3, Subsystems and Tunable Parameters for a description of which parameters are mandatory for given subsystems.
Launch processes in a cgroup by running the
cgexeccommand. For example, this command launches the firefox web browser within the
group1cgroup, subject to the limitations imposed on that group by the
cgexec -g cpu:group1 firefox http://www.redhat.com
The syntax for
-gsubsystems:path_to_cgroup command arguments
- subsystems is a comma‑separated list of subsystems, or
*to launch the process in the hierarchies associated with all available subsystems. Note that, as with
cgsetdescribed in Section 2.7, “Setting Parameters”, if cgroups of the same name exist in multiple hierarchies, the
-goption creates processes in each of those groups. Ensure that the cgroup exists within each of the hierarchies whose subsystems you specify here.
- path_to_cgroup is the path to the cgroup relative to the hierarchy.
- command is the command to run.
- arguments are any arguments for the command.
You can also add the
--stickyoption before the command to keep any child processes in the same cgroup. If you do not set this option and the cgred service is running, child processes will be allocated to cgroups based on the settings found in
/etc/cgrules.conf. The process itself, however, will remain in the cgroup in which you started it.
When you start a new process, it inherits the group of its parent process. Therefore, an alternative method for starting a process in a particular cgroup is to move your shell process to that group (refer to Section 2.8, “Moving a Process to a Control Group”), and then launch the process from that shell. For example:
echo $$ > /cgroup/cpu_and_mem/group1/tasks~]#
Note that after exiting firefox, your existing shell is still in the
group1cgroup. Therefore, an even better way would be:
sh -c "echo \$$ > /cgroup/cpu_and_mem/group1/tasks && firefox"
2.9.1. Starting a Service in a Control Group
You can start certain services in a cgroup. Services that can be started in cgroups must:
- use a
- use the
/etc/init.d/functionsto start the service
To make an eligible service start in a cgroup, edit its file in the
/etc/sysconfigdirectory to include an entry in the form
CGROUP_DAEMON="subsystem:control_group"where subsystem is a subsystem associated with a particular hierarchy, and control_group is a cgroup in that hierarchy. For example:
cpusetis mounted to
/cgroup/cpu_and_mem/, the above configuration translates to