Operations 10 min read

Master Linux CPU Scheduling: Using nice, cpulimit, and cgroups

This guide explains how Linux manages CPU scheduling fairness and demonstrates three practical methods—nice, cpulimit, and control groups—to prioritize, limit, or allocate CPU resources for processes, complete with compilation steps, command examples, and performance observations using top.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Linux CPU Scheduling: Using nice, cpulimit, and cgroups

Linux kernel schedules processes fairly, but you may need to prioritize or limit CPU for specific tasks.

Simulating high CPU usage

We compile and run the Mathomatic prime generator to generate CPU load.

/usr/local/bin/matho-primes 0 9999999999 > /dev/null &

Running top shows the process using all available CPU.

nice

The nice command adjusts a process's niceness (priority) from -20 (highest) to 19 (lowest). By default priority is 0; nice without arguments starts a process with niceness 10, giving it lower CPU share.

nice matho-primes 0 9999999999 > /dev/null &
matho-primes 0 9999999999 > /dev/null &

Observing top shows the non‑nice process receiving more CPU time.

You can change the priority of a running process with renice +10 1234, where 1234 is the PID.

cpulimit

cpulimit

limits a process's CPU usage by sending SIGSTOP/SIGCONT, without changing its priority.

Install it on CentOS:

wget -O cpulimit.zip https://github.com/opsengine/cpulimit/archive/master.zip
unzip cpulimit.zip
cd cpulimit-master
make
sudo cp src/cpulimit /usr/bin

Run a process with a 50 % CPU cap:

cpulimit -l 50 matho-primes 0 9999999999 > /dev/null &

Or limit an existing process by PID:

cpulimit -l 50 -p 1234

cgroups

Control groups (cgroups) let the kernel allocate specific resources to a group of processes, including CPU, memory, and I/O.

Create two groups with different CPU shares:

sudo cgcreate -g cpu:/cpulimited
sudo cgcreate -g cpu:/lesscpulimited
sudo cgset -r cpu.shares=512 cpulimited   # 2:1 ratio with default 1024

Start a task in each group:

sudo cgexec -g cpu:cpulimited /usr/local/bin/matho-primes 0 9999999999 > /dev/null &
sudo cgexec -g cpu:lesscpulimited /usr/local/bin/matho-primes 0 9999999999 > /dev/null &
top

shows the group with higher cpu.shares receiving more CPU time. Adding another process to the limited group still respects the 2:1 distribution.

TL;DR

nice : quick way to lower a process's priority.

cpulimit : enforce a hard CPU usage ceiling.

cgroups : flexible resource control for groups of processes.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Resource ManagementLinuxcgroupsCPU schedulingnicecpulimit
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.