Bookmark this page

Guided Exercise: Influence Process Scheduling

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

Important

This exercise uses commands that perform an endless checksum on a device file and intentionally use significant CPU resources.

Instructions

  1. Use the ssh command to log in to the servera machine as the student user.

    [student@workstation ~]$ ssh student@servera
    ...output omitted...
    [student@servera ~]$
  2. Determine the number of CPU cores on the servera machine, and then start two instances of the sha1sum /dev/zero & command for each core.

    1. 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
      2
    2. Use 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] 1135
  3. Verify that the background jobs are running for each of the sha1sum processes.

    [student@servera ~]$ jobs
    [1]   Running                 sha1sum /dev/zero &
    [2]   Running                 sha1sum /dev/zero &
    [3]-  Running                 sha1sum /dev/zero &
    [4]+  Running                 sha1sum /dev/zero &
  4. 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/zero
  5. Terminate all sha1sum processes, and then verify that no jobs are running.

    1. Use the pkill command to terminate all running processes with the sha1sum name pattern.

      [student@servera ~]$ pkill sha1sum
      [2]   Terminated              sha1sum /dev/zero
      [4]+  Terminated              sha1sum /dev/zero
      [1]-  Terminated              sha1sum /dev/zero
      [3]+  Terminated              sha1sum /dev/zero
    2. Verify that no jobs are running.

      [student@servera ~]$ jobs
      [student@servera ~]$
  6. 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.

    1. 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] 1209
    2. Use the nice command to start the fourth instance with a nice level of 10.

      [student@servera ~]$ nice -n 10 sha1sum /dev/zero &
      [4] 1210
  7. Use 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 sha1sum
     1210  8.2  10 sha1sum
  8. 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 1210
    [sudo] password for student: student
    1210 (process ID) old priority 10, new priority 5
  9. 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 sha1sum
     1210 10.9   5 sha1sum
  10. Use the pkill command to terminate all running processes with the sha1sum name pattern.

    [student@servera ~]$ pkill sha1sum
    ...output omitted...
  11. Return to the workstation machine as the student user.

    [student@servera ~]$ exit
    logout
    Connection to servera closed.
    [student@workstation ~]$

    Important

    Verify that you have terminated all exercise processes before leaving this exercise.

Finish

On the workstation machine, change to the student user home directory and use the lab command to complete this exercise. This step is important to ensure that resources from previous exercises do not impact upcoming exercises.

[student@workstation ~]$ lab finish tuning-procscheduling

This concludes the section.

Revision: rh199-9.0-4fecb06