Process Niceness

From Free Knowledge Base- The DUCK Project: information for everyone
Jump to: navigation, search

NICE and RENICE

nice - set process scheduling priority

renice - change process priority real time.

A kernel scheduler is a unit of the kernel that determines the most suitable process out of all runnable processes to execute next.

There are a total of 140 priorities and two distinct priority ranges implemented in Linux. The first one is a nice value (niceness) which ranges from -20 (highest priority value) to 19 (lowest priority value) and the default is 0.

  • niceness of -20 gives the process the most priority
  • niceness of 19 gives the process the least priority

Lowest level of niceness (lower means more favorable) you can define is determined by limits.conf

Total number of priorities = 140
Real time priority range(PR or PRI):  0 to 99 
User space priority range: 100 to 139

check nice level setting

One way

ps -eo pid,ppid,ni,comm

This will list the process ID, the nice level, and the actual command.

ps ax -o pid,ni,cmd

Using the htop command will show nice level. This requires installation. For Ubuntu/Mint:

sudo apt install htop

changing the program priority

nice runs commands at increased priority, renice can raise or lower but works for processes that are already running.

  • If no value is provided, nice sets a priority of 10 by default.
  • A command or program run without nice defaults to a priority of zero.
  • Only root can run a command or program with increased or high priority.
  • Normal users can only run a command or program with low priority.

Example 1: As a non-privileged user we shall launch Discord client at the least important priority (the nicest)

nice -n20 /usr/share/discord/Discord

Now about everything else will have priority to the CPU than does Discord.

you can use the ionice command to start the process with low io priority:

nice -n18 ionice -c3 /path/to/mydaemon

How to renice all threads (and children) of one process

The kernel only handles "runnable entities", that is, something which can be run and scheduled. A thread, is just a kind of process that shares (at least) memory space and signal handlers with another one.

While Running

You can change the priority of the process already running. Example:

sudo renice -n -10 14566

This process has increased process priority. The process was originally executed without root. The process is running as a non privileged user, however, it is necessary to use root permissions to elevate the priority higher than 0. The process itself remains non privileged.