Bookmark this page

Chapter 5.  Managing Priority of Linux Processes

Abstract

Overview
Goal To influence the relative priorities at which Linux processes run.
Objectives

  • Describe nice levels.

  • Set nice levels on new and existing processes.

Sections
  • Process Priorities and "nice" Concepts (and Practice)

  • Using nice and renice to Influence Process Priority (and Practice)

Lab
  • Managing Priority of Linux Processes

Process Priority and "nice" Concepts

  • All processes on a Linux system have a relative priority.

  • The niceness of a process influences its priority.

Objectives

After completing this section, students should be able to describe nice levels and their effects.

Linux process scheduling and multitasking

Modern computer systems range from low-end processors that can only execute one single instruction at a time to high-performing supercomputers with hundreds of CPUs each and multiple cores on each CPU, performing hundreds of instructions in parallel. But all of these systems tend to have one thing in common: They always need to run more processes than they actually have cores.

The way Linux (and other operating systems) can actually run more processes (and threads) than there are actual processing units available is by employing a technique called time-slicing. The operating system process scheduler will rapidly switch between processes on a single core, giving a user the impression that there are more processes running at the same time.

The part of the Linux kernel that performs this switching is called the process scheduler.

Relative priorities

Since not every process is as important as another one, the scheduler can be told to use different scheduling policies for different processes. The scheduling policy used for most processes running on a regular system is called SCHED_OTHER (also called SCHED_NORMAL), but there are other policies available for different purposes.

Since not all processes are created equally, processes running with the SCHED_NORMAL policy can be given a relative priority. This priority is called the nice value of a process, and there are exactly 40 different levels of niceness a process can have.

These nice levels range from -20 to 19. By default, processes will inherit their nice level from their parent, which is usually 0. Higher nice levels indicate less priority (the process easily gives up its CPU usage for others), while lower nice levels indicate a higher priority (the process is less inclined to give up the CPU). If there is no contention for resources—for example, when there are fewer active processes than available CPU cores—even processes with a high nice level will still use all available CPU resources they can. But when there are more processes requesting CPU time than available cores, the processes with a higher nice level will receive less CPU time than those with a lower nice level.

Figure 5.1: Nice levels and how they are reported by top

Nice levels and permissions

Since setting a low nice level on a CPU-hungry process might negatively impact the performance of other processes running on the same system, only root (more detailed: users with the CAP_SYS_NICE capability) is allowed to set negative nice levels and lower the nice level on existing processes.

Regular, unprivileged users are only allowed to set positive nice levels. Furthermore, they are only allowed to raise the nice level on their existing process, but cannot lower them.

Important

There are more ways to influence process priority and resource usage than just nice levels. There are alternate scheduler policies and settings, control groups (cgroups), and more. Nice levels are, however, the easiest to use, and can be used by regular users as well as system administrators.

References

nice(1) and sched_setscheduler(2) man pages

Revision: rh134-7-c643331