9.2 Process Priorities
We want to discover the amount of CPU cores within the server we are working on. And we want to fully use each CPU core of the server.
Check Number of Cores
To determine the number of cores using, we can use the file /proc/cpuinfo and the grep command:
[greater@rhcsa ~]$ grep -c processor /proc/cpuinfo
Now we want to use these CPU cores completely. We can use 1 CPU core completely with the sha1sum /dev/zero & command twice. So we would need to start this command double the amount of times to the amount of CPU cores in the server.
Go ahead and start two sha1sum /dev/zero & commands for each CPU core.
[greater@rhcsa ~]$ sha1sum /dev/zero &
Verify that you have all the background jobs running that you expected (two for every core in your system).
[greater@rhcsa ~]$ jobs
Inspect the CPU usage (as a percentage) of all your sha1sum processes, using the ps and grep commands. What do you notice?
[greater@rhcsa ~]$ ps u | grep sha1shum
-bash: psgrep: command not found
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
student+ 1322 0.0 0.1 115392 2068 pts/0 Ss 23:43 0:00 -bash
student+ 1349 100 0.0 116604 1128 pts/0 R 23:44 2:42 sha1sum /dev/zero
student+ 1350 99.6 0.0 116604 1128 pts/0 R 23:44 2:39 sha1sum /dev/zero
student+ 1353 0.0 0.0 151036 1728 pts/0 R+ 23:46 0:00 ps u
Check CPU
You should notice that the cpu usage on both process is almost the same.
Use the killall command to terminate all your sha1sum processes.
[greater@rhcsa ~]$ killall sha1sum
Start two sha1sum /dev/zero & commands for each of your cores, but give exactly one of them a nice level of 10.
[greater@rhcsa ~]$ sha1sum /dev/zero &
[greater@rhcsa ~]$ sha1sum /dev/zero &
[greater@rhcsa ~]$ nice -n10 sha1sum /dev/zero &
[greater@rhcsa ~]$ ps -o pid,pcpu,nice,comm $(pgrep sha1sum)
PID %CPU NI COMMAND
1359 93.3 0 sha1sum
1360 97.7 0 sha1sum
1361 6 10 sha1sum
Nice and Renice
Use the renice command to set the nice level of the sha1sum with a nice level of 10 down to 5. The PIO should still be visible in the output of the previous step. Did this work? Why not?
[greater@rhcsa ~]$ renice -n 5 1361
As a non-privileged user, you’re not allowed to do a renice.
Using the sudo and renice commands, set the nice level for the process you identified in the previous step to -10.
[greater@rhcsa ~]$ sudo renice -n 5 1361
Start the top command as root, then use top to lower the nice level for the sha1sum process using the most CPU back down to 0. What do you observe afterwards?
Press r to enter renice mode, then enter the PIO you identified, or press Enter if the offered default PIO is the one you want. Enter 0, then press Enter. All sha1sum commands are again using an (almost) equal amount of CPU. Important: clean up by exiting top and killing all your sha1sum processes.
Extras. One liner to start double the amount of commands to each core. Replace the “echo core” text with the command you want to start.
Neat way of one liner (used by most, understood by fewer):
COMMAND='echo core'; AMOUNT_PER_CORE="2"; CORES=$(grep -c processor /proc/cpuinfo);
CORES=$(($CORES*$AMOUNT_PER_CORE)) COUNT="0"; while [ $COUNT -lt $CORES ]; do $COMMAND;
COUNT=$(($COUNT+1)); done
Quick and dirty (one liner on the go):
COUNT="0"; while [ $COUNT -lt $(grep -c processor /proc/cpuinfo) ]; do echo core;
echo core; COUNT=$(($COUNT+1)); done