Bookmark this page

Guided Exercise: Discovering Process Priorities

In this exercise, you will experience the influence that nice levels have on relative process priorities.

Resources
Machines: desktopX

Outcomes

An interactive tour of the effects of nice levels.

None

  1. Log in as student to your desktopX system.

  2. Using the special file /proc/cpuinfo, determine the number of CPU cores in your desktopX system, then start two instances of the command sha1sum /dev/zero & for each core.

    1. To determine the number of cores using /proc/cpuinfo:

      [student@desktopX ~]$ NCORES=$( grep -c '^processor' /proc/cpuinfo )
    2. Either manually or with a script, start two sha1sum /dev/zero & commands for every core in your system.

      Note

      The seq command prints a list of numbers.

      [student@desktopX ~]$ for I in $( seq $((NCORES*2)) )
      > do
      >    sha1sum /dev/zero &
      > done
  3. Verify that you have all the background jobs running that you expected (two for every core in your system).

    1. [student@desktopX ~]$ jobs
      [1]-  Running                 sha1sum /dev/zero &
      [2]+  Running                 sha1sum /dev/zero &
      ...
  4. Inspect the CPU usage (as a percentage) of all your sha1sum processes, using the ps and pgrep commands. What do you notice?

    1. [student@desktopX ~]$ ps u $(pgrep sha1sum)
    2. The CPU percentage for all sha1sum processes is about equal.

  5. Use the killall command to terminate all your sha1sum processes.

    1. [student@desktopX ~]$ killall sha1sum
  6. Start two sha1sum /dev/zero & commands for each of your cores, but give exactly one of them a nice level of 10.

    1. [student@desktopX ~]$ for I in $( seq $((NCORES*2-1)) )
      > do
      >    sha1sum /dev/zero &
      > done
      [student@desktopX ~]$ nice -n10 sha1sum /dev/zero &
  7. Using the ps command, inspect the CPU usage of your sha1sum commands. Make sure you include the nice level in your output, as well as the PID and the CPU usage. What do you notice?

    1. [student@desktopX ~]$ ps -o pid,pcpu,nice,comm $(pgrep sha1sum)
    2. The instance of sha1sum with the nice level of 10 gets significantly less CPU than the other instance(s).

  8. Use the renice command to set the nice level of the sha1sum with a nice level of 10 down to 5. The PID should still be visible in the output of the previous step.

    Did this work? Why not?

    1. [student@desktopX ~]$ renice -n 5 <PID>
      renice: failed to set priority for <PID> (process ID): Permission denied
    2. Unprivileged users are not allowed to set negative nice values or lower the nice value on an existing process.

  9. Using the sudo and renice commands, set the nice level for the process you identified in the previous step to -10.

    1. [student@desktopX ~]$ sudo renice -n -10 <PID>
  10. 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?

    1. [student@desktopX ~]$ sudo top
    2. Identify the sha1sum process using the most CPU. It will be near the top of the screen.

    3. Press r to enter renice mode, then enter the PID you identified, or press Enter if the offered default PID is the one you want.

    4. Enter 0, then press Enter.

    5. All sha1sum commands are again using an (almost) equal amount of CPU.

  11. Important: Clean up by exiting top and killing all your sha1sum processes.

    1. Press q to exit top.

    2. [student@desktopX ~]$ killall sha1sum
Revision: rh199-7-d0984a3