In this exercise, you adjust the scheduling priority of processes with the nice and renice commands, and observe the effects on process execution.
Outcomes
Adjust scheduling priorities for processes.
As the student user on the workstation machine, use the lab command to prepare your system for this exercise.
This command ensures that all required resources are available.
[student@workstation ~]$ lab start tuning-procscheduling
This exercise uses commands that perform an endless checksum on a device file and intentionally use significant CPU resources.
Instructions
Use the ssh command to log in to the servera machine as the student user.
[student@workstation ~]$ ssh student@servera
...output omitted...
[student@servera ~]$Determine the number of CPU cores on the servera machine, and then start two instances of the sha1sum /dev/zero & command for each core.
Use the grep command to parse the number of existing virtual processors (CPU cores) from the /proc/cpuinfo file.
[student@servera ~]$ grep -c '^processor' /proc/cpuinfo
2Use a looping command to start multiple instances of the sha1sum /dev/zero & command.
Start two instances for each virtual processor that was indicated in the previous step.
In this example, a for loop creates four instances.
The PID values in your output might vary from the example.
[student@servera ~]$ for i in {1..4}; do sha1sum /dev/zero & done
[1] 1132
[2] 1133
[3] 1134
[4] 1135Verify that the background jobs are running for each of the sha1sum processes.
[student@servera ~]$jobs[1]Runningsha1sum /dev/zero & [2]Runningsha1sum /dev/zero & [3]-Runningsha1sum /dev/zero & [4]+Runningsha1sum /dev/zero &
Use the ps and pgrep commands to display the percentage of CPU usage for each sha1sum process.
[student@servera ~]$ ps u $(pgrep sha1sum)
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
student 1132 49.6 0.1 225336 2288 pts/0 R 11:40 2:40 sha1sum /dev/zero
student 1133 49.6 0.1 225336 2296 pts/0 R 11:40 2:40 sha1sum /dev/zero
student 1134 49.6 0.1 225336 2264 pts/0 R 11:40 2:40 sha1sum /dev/zero
student 1135 49.6 0.1 225336 2280 pts/0 R 11:40 2:40 sha1sum /dev/zeroTerminate all sha1sum processes, and then verify that no jobs are running.
Use the pkill command to terminate all running processes with the sha1sum name pattern.
[student@servera ~]$pkill sha1sum[2]Terminatedsha1sum /dev/zero [4]+Terminatedsha1sum /dev/zero [1]-Terminatedsha1sum /dev/zero [3]+Terminatedsha1sum /dev/zero
Verify that no jobs are running.
[student@servera ~]$ jobs
[student@servera ~]$Start multiple instances of the sha1sum /dev/zero & command, and then start one additional instance of the sha1sum /dev/zero & command with a nice level of 10.
Start at least as many instances as the number of system virtual processors.
In this example, three regular instances are started, plus another with a higher nice level.
Use looping to start three instances of the sha1sum /dev/zero & command.
[student@servera ~]$ for i in {1..3}; do sha1sum /dev/zero & done
[1] 1207
[2] 1208
[3] 1209Use the nice command to start the fourth instance with a nice level of 10.
[student@servera ~]$ nice -n 10 sha1sum /dev/zero &
[4] 1210Use the ps and pgrep commands to display the PID, percentage of CPU usage, nice value, and executable name for each process.
The instance with the nice value of 10 displays a lower percentage of CPU usage than the other instances.
[student@servera ~]$ps -o pid,pcpu,nice,comm $(pgrep sha1sum)PID %CPU NI COMMAND 1207 64.2 0 sha1sum 1208 65.0 0 sha1sum 1209 63.9 0 sha1sum12108.2 10 sha1sum
Use the sudo renice command to lower the nice level of a process from the previous step.
Use the PID value of the process instance with the nice level of 10 to lower its nice level to 5.
[student@servera ~]$sudo renice -n 5[sudo] password for student:1210student(process ID) old priority 10,1210new priority 5
Repeat the ps and pgrep commands to display the CPU percentage and nice level.
[student@servera ~]$ps -o pid,pcpu,nice,comm $(pgrep sha1sum)PID %CPU NI COMMAND 1207 62.9 0 sha1sum 1208 63.2 0 sha1sum 1209 63.2 0 sha1sum1210 10.9 5 sha1sum
Use the pkill command to terminate all running processes with the sha1sum name pattern.
[student@servera ~]$ pkill sha1sum
...output omitted...Return to the workstation machine as the student user.
[student@servera ~]$ exit
logout
Connection to servera closed.
[student@workstation ~]$Verify that you have terminated all exercise processes before leaving this exercise.
This concludes the section.