In this step, you will learn how to influence the scheduling priority of processes using the nice and renice commands. The nice value (also known as niceness) of a process indicates its priority to the Linux scheduler. A lower nice value (more negative) means higher priority, while a higher nice value (more positive) means lower priority. The range for nice values is typically from -20 (highest priority) to 19 (lowest priority), with 0 being the default.
-
Start multiple instances of sha1sum /dev/zero & and then start one additional instance with a nice level of 10.
We will start several sha1sum processes to simulate a busy system. Then, we'll start one with a deliberately lower priority (higher nice value) to observe the effect.
First, start three regular instances (adjust based on your CPU core count if desired, but at least as many as virtual processors to create contention):
for i in {1..3}; do sha1sum /dev/zero & done
Next, start the fourth instance with a nice level of 10. This process will have a lower priority compared to the others.
nice -n 10 sha1sum /dev/zero &
You will see output similar to this, indicating the PIDs of the background processes:
[1] 5443
[2] 5444
[3] 5445
[4] 5446
(Note: PID values in your output will vary.)
-
Use ps and pgrep commands to display the PID, percentage of CPU usage, nice value, and executable name for each process.
Observe the %CPU and NI columns. The instance with the nice value of 10 should display a lower percentage of CPU usage than the other instances, as the scheduler gives it less CPU time.
ps -o pid,pcpu,nice,comm $(pgrep sha1sum)
Look for the process with NI value 10. Its %CPU should be noticeably lower than the others.
PID %CPU NI COMMAND
5443 56.8 0 sha1sum
5444 58.0 0 sha1sum
5445 56.5 0 sha1sum
5446 6.7 10 sha1sum
(Note: The exact %CPU values will vary based on system load and core count, but the process with nice 10 should have a lower share.)
-
Use the sudo renice command to change the nice level of one of the regular processes to 5.
The renice command allows you to change the nice value of an already running process. We will demonstrate this by changing one of the regular processes (nice value 0) to a nice value of 5.
First, identify the PID of one of the sha1sum processes that has a nice value of 0 from the output of the previous ps command. Let's use the first one from the example above (PID 5443).
sudo renice -n 5 <PID_of_regular_process>
Replace <PID_of_regular_process> with the actual PID you identified. For example:
sudo renice -n 5 5443
You should see output confirming the priority change:
5443 (process ID) old priority 0, new priority 5
-
Repeat the ps and pgrep commands to display the CPU percentage and nice level.
Observe the change in CPU usage for the process whose nice value you modified. The process with nice value 5 should now have slightly lower CPU usage compared to the processes with nice value 0, but higher than the process with nice value 10.
ps -o pid,pcpu,nice,comm $(pgrep sha1sum)
You should see the NI value for the modified process is now 5, and its CPU usage reflects its new priority level.
PID %CPU NI COMMAND
5443 55.4 5 sha1sum
5444 67.2 0 sha1sum
5445 67.1 0 sha1sum
5446 7.5 10 sha1sum
(Note: The exact %CPU values will vary, but you should observe that processes with lower nice values (higher priority) get more CPU time.)