Linux administration

Modify Process Execution Priorities

Nguyen Hai Chau
Vietnam National University

Concepts

  • Not all tasks require the same amount of execution time
  • Linux has the concept of execution priority to deal with this
  • Process priority is dynamically altered by the kernel
  • You can view the current priority by looking at top or ps -l and looking at the PRI column
  • The priority can be biased using nice
    • The current bias can be seen in the NI column in top

nice

  • Starts a program with a given priority bias
  • Peculiar name: ‘nicer' processes require fewer resources
  • Niceness ranges from +19 (very nice) to −20 (not very nice)
  • Non-root users can only specify values from 1 to 19; the root user can specify the full range of values
  • Default niceness when using nice is 10
  • To run a command at increased niceness (lower priority):
$ nice -10 long-running-command &
$ nice -n 10 long-running-command &
  • To run a command at decreased niceness (higher priority):
$ nice --15 important-command &
$ nice -n -15 important-command &

renice

  • renice changes the niceness of existing processes
  • Non-root users are only permitted to increase a process's niceness
  • To set the process with pid 2984 to the maximum niceness (lower priority):
$ renice 20 2984
  • The niceness is just a number: no extra - sign
    • To set the process with pid 3598 to a lower niceness (higher priority):
$ renice -15 3598
  • You can also change the niceness of all a user's processes:
$ renice 15 -u mikeb

Exercise

  • a. Create the following shell script, called forever, in your home directory:
#!/bin/sh
while [ 1 ]; do
echo hello... >/dev/null;
done

Make it executable and run it in the background as follows:

$ chmod a+rx forever
$ ./forever &
  • b. Use ps -l to check the script's nice level
  • c. Run the script with nice and give it a niceness of 15. Try running it alongside a less nice version, and see what the difference is in top
  • d. Try using nice or renice to make a process' niceness less than 0