All about Linux Process Scheduling And Multitasking

Posted on January 5th, 2017

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.  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 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 SHED_OTHER or SHED_NORMAL, but there are other policies available for different purposes. Since not all processes are created equally, processes running with the SHED_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, while lower nice levels indicate a higher priority. Of there is no contention of 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. 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.

 

Nice levels and permissions

Since getting a low nice level on a CPU-hungry process might negatively impact the performance of other processes running on the same system, only root 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. There are more ways to influence process priority and resource usage than just nice levels. There are alternate scheduler policies and settings, control groups, and more. Nice levels are however, the easiest to use, and can be used by regular users as well as system administrators.

 

Reporting on nice levels

The nice levels force existing processes can be viewed in a number of different ways. Most process management tools already display the nice level by default, or can be configure to display the nice level.

 

Displaying nice levels with top command

The ‘top’ command can be used to interactively view process. In a default configuration, top will display two columns of interest to the nice level: ‘NI’ with the actual nice level, and ‘PR’, which displays the nice level as mapped to a larger priority queue, with a nice level of -20 mapping to a priority of 0 and a nice level of +19 mapping to a priority of 39.

 

Displaying nice levels with ps command

The ‘ps’ command can also display nice levels for processes, although it does not do so in most of it’s default output formats. Users can request exactly the columns they want from ps, however, and the name for the nice field is nice

The following example requests a list of all processes, with their pid, name and nice level, sorted in descending order by nice level.

kubuntu@vm:~$ ps axo pid,comm,nice –sort=-nice

PID COMMAND                        NI

27 khugepaged                        19

2058 baloo_file                       19

2138 akonadi_baloo_i            19

2219 cat                                    19

2221 cat                                    19

2225 cat                                    19

2226 cat                                    19

2227 cat                                    19

26 ksmd                                    5

2115 rtkit-daemon                  1

 

Whenever a process is started, it will normally inherit the nice level from it’s parent. This means that when a process is started from the command line, it will get the same nice level as the shell process that it was started from. In most cases, this will result in new processes running with a nice level of 0. Unprivileged users are only allowed to set a positive nice level (0 to 19). Only root can set a negative nice level (-20 to -1).

 

If you need any further assistance please contact our support department.

 

 

Leave a Reply