In Linux, controlling resource usage is crucial for maintaining system performance, especially when running multiple processes simultaneously. Limiting the CPU usage of specific processes helps prevent any single process from monopolizing system resources. This is particularly useful in servers and multi-user environments, where resource allocation must be carefully managed.

To limit CPU usage for a specific process, Linux provides several tools and methods. Two of the most effective methods are using cpulimit and cgroups. These tools allow for precise control over how much CPU time a process can consume, ensuring that no single process overwhelms the system.

This article provides detailed instructions on how to use cpulimit and cgroups to limit CPU usage for specific processes on a Linux system. Each method is explained with a step-by-step guide, including a simple test to verify the limits.

Steps to limit CPU usage using cpulimit:

Cpulimit is a straightforward tool that limits the CPU usage of a specific process by setting a maximum percentage of CPU time that the process can use. It operates by continuously monitoring the process and adjusting its priority to keep the CPU usage within the specified limit. Cpulimit is easy to use and does not require any changes to the system's configuration, making it a convenient option for users who need to limit CPU usage for individual processes on an ad-hoc basis.

One of the key advantages of cpulimit is its simplicity. Users can quickly install and use it without needing to modify system files or create complex configurations. It is ideal for scenarios where a single process needs to be constrained temporarily. The following steps will guide you through the process of installing and using cpulimit to limit the CPU usage of a specific process on your Linux system.

  1. Install the cpulimit package from your distribution's package manager.
    $ sudo apt update && sudo apt install cpulimit  //Ubuntu and derivatives//
    $ sudo zypper refresh && sudo zypper install cpulimit  //openSUSE//
    $ sudo dnf install cpulimit  //Fedora and RHEL//
  2. Start a high CPU usage process for testing purposes.
    $ yes > /dev/null &

    The yes > /dev/null command runs in the background and consumes 100% of one CPU core, which is useful for testing CPU limits.

  3. Identify the process ID (PID) of the test process using the ps command.
    $ ps aux | grep yes
  4. Use the cpulimit command to limit the CPU usage of the test process.
    $ sudo cpulimit -p PID -l 20

    Replace PID with the actual process ID. The -l 20 option limits the process to 20% of one CPU core.

  5. Verify the CPU usage limit by monitoring the process with the top command.
    $ top -p PID

    The process should now show reduced CPU usage, reflecting the limit set by cpulimit.

  6. Stop the test process once you have verified the limit.
    $ kill PID

Steps to limit CPU usage using cgroups:

Cgroups (control groups) is a more advanced tool in Linux that provides fine-grained control over resource allocation, including CPU usage. It allows administrators to group processes and allocate resources such as CPU, memory, and I/O bandwidth to those groups. Unlike cpulimit, which only affects individual processes, cgroups can manage multiple processes and apply resource limits collectively, making it ideal for complex systems and environments where resource management is critical.

One of the key benefits of cgroups is its flexibility and precision. Users can create cgroups to control resource usage for a single process or an entire set of processes. This makes cgroups suitable for managing resources on servers, multi-user systems, and other environments where multiple processes need to be carefully managed to ensure optimal performance. The following steps will guide you through the process of creating and managing cgroups to limit the CPU usage of a process.

  1. Install the cgroup-tools package, if not already installed.
    $ sudo apt update && sudo apt install cgroup-tools  //Ubuntu and derivatives//
    $ sudo zypper refresh && sudo zypper install cgroup-tools  //openSUSE//
    $ sudo dnf install cgroup-tools  //Fedora and RHEL//
  2. Create a new cgroup for the process.
    $ sudo cgcreate -g cpu:/cpulimitedgroup

    This command creates a new cgroup named cpulimitedgroup under the cpu subsystem.

  3. Start a high CPU usage process for testing purposes.
    $ yes > /dev/null &

    The yes > /dev/null command runs in the background and consumes 100% of one CPU core, which is useful for testing CPU limits.

  4. Identify the process ID (PID) of the test process using the ps command.
    $ ps aux | grep yes
  5. Set a CPU usage limit for the cgroup.
    $ sudo cgset -r cpu.shares=512 cpulimitedgroup

    The value 512 limits the cgroup to half of the available CPU time. Adjust this value as needed.

  6. Add the test process to the cgroup.
    $ sudo cgclassify -g cpu:/cpulimitedgroup PID

    Replace PID with the process ID of the process you want to limit.

  7. Monitor the CPU usage of the process to ensure the limits are applied.
    $ top -p PID

    Using cgroups allows for more precise control over CPU and other resources, which is beneficial for managing multiple processes.

  8. Stop the test process once you have verified the limit.
    $ kill PID
Discuss the article:

Comment anonymously. Login not required.