Linux priority and nice

Difference between nice value and priority in the top output

top, by default, lists both columns. I am curious as to what is the difference. I checked out the man pages and cannot figure it out: Priority:

 h: PR -- Priority The priority of the task. 
 i: NI -- Nice value The nice value of the task. A negative nice value means higher priority, whereas a positive nice value means lower priority. Zero in this field simply means priority will not be adjusted in determining a task’s dis- patchability. 

I understand that Nice value is related to the Kernel’s CPU scheduler queue; then what does Priority indicate? Something regarding I/O perhaps?

3 Answers 3

The nice value is a «global» mechanism, whereas priority is relevant for the task switcher right now.

The task switcher (properly called the «scheduler») is a little bit of code within the kernel that decides what task will run next.

The difference is that PR is a real priority of a process at the moment inside of the kernel and NI is just a hint for the kernel what the priority the process should have.

In most cases PR value can be computed by the following formula: PR = 20 + NI. Thus the process with niceness 3 has the priority 23 (20 + 3) and the process with niceness -7 has the priority 13 (20 — 7). You can check the first by running command nice -n 3 top . It will show that top process has NI 3 and PR 23. But for running nice -n -7 top in most Linux systems you need to have root privileges because actually the lower PR value is the higher actual priority is. Thus the process with PR 13 has higher priority than processes with standard priority PR 20. That’s why you need to be root. But minimum niceness value allowed for non-root process can be configured in /etc/security/limits.conf.

Theoretically the kernel can change PR value (but not NI) by itself. For example it may reduce the priority of a process if it consumes too much CPU, or it may increase the priority of a process if that process had no chance to run for a long time because of other higher priority processes. In these cases the PR value will be changed by kernel and NI will remain the same, thus the formula «PR = 20 + NI» will not be correct. So the NI value can be interpreted as hint for the kernel what the priority the process should have, but the kernel can choose real priority (PR value) on its own depending on the situation. But usually the formula «PR = 20 + NI» is correct.

Читайте также:  Linux btrfs file system

Exact rules how the kernel changes priority aren’t clear. setpriority (the function that change nice value) manual says:

The effect of changing the nice value may vary depending on the process-scheduling algorithm in effect.

The dynamic priority is based on the nice value (set by nice(2), setpriority(2), or sched_setattr(2)) and increased for each time quantum the thread is ready to run, but denied to run by the scheduler.

It seems that PR value corresponds to dynamic priority.

The range of the NI value is -20..19. Thus the PR value can have the values from 0 (20 — 20) to 39 (20 + 19). But it is correct only for the processes with default scheduling policy (SHED_OTHER). There may be also processes with so called «real time» scheduling policies. These policies are SCHED_RR and SCHED_FIFO. Such processes have a PR value less than 0. You can check this by running chrt -r 1 top command (need to be root). The top process will have PR -2. You even can run chrt -r 90 top in which case the top process will have PR -91.

It seems that for SCHED_RR processes the PR value can be calculated by the formula:

PR = — 1 — sched_rr_priority.

Thus a SCHED_RR process has at least PR -1 which means that any SCHED_RR process has higher priority than any SCHED_OTHER. This corresponds to pthread manual:

SCHED_FIFO can be used only with static priorities higher than 0, which means that when a SCHED_FIFO threads becomes runnable, it will always immediately preempt any currently running SCHED_OTHER, SCHED_BATCH, or SCHED_IDLE thread.

SCHED_RR is a simple enhancement of SCHED_FIFO. Everything described above for SCHED_FIFO also applies to SCHED_RR,

The priority of real time processes is referred as static priority that cannot be changed by the kernel. So positive PR values can be treated as dynamic priority for non-realtime (SCHED_OTHER, SCHED_BATCH) processes and negative PR value as static priority for realtime processes (SCHED_RR, SCHED_FIFO).

Читайте также:  Sqlmap kali linux использование

I also tried to run nice -n 10 chrt -r 50 top (and chrt -r 50 nice -n 10 top ). The NI value was 10, but the PR still was -51. So it seems that NI value doesn’t affect priority of SCHED_RR processes. This corresponds to setpriority manual:

Any processes or threads using SCHED_FIFO or SCHED_RR shall be unaffected by a call to setpriority(). This is not considered an error. A process which subsequently reverts to SCHED_OTHER need not have its priority affected by such a setpriority() call.

One funny note. If you run chrt -r 99 top , you will see RT value instead of a number in PR column.

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 28489 root RT 0 2852 1200 896 R 0 0.1 0:00.01 top

I don’t think that this means that the process is now special. I thinks that this means that top just don’t print -100 because it would take 4 character to print.

You can also use htop instead of top in all examples which can be more convenient. ps -l can be used too, but it base point that separates realtime and non-realtime priorities is not 0, but 60, so nice -n -20 ps -l will print

F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD 4 R 0 28983 28804 0 60 -20 - 1176 - pts/6 00:00:00 ps

Источник

Команда nice, или вежливые процессы

Когда на компьютере достаточно процессорного времени на исполнение всех процессов, нет необходимости в выставлении приоритетов. Но когда необходимо параллельно запустить несколько процессов, каждый из которых использует всё доступное процессорное время, может очень пригодиться утилита nice.

Читайте также:  Virtualbox подключить общую папку astra linux

Команда nice запускает программу с изменённым приоритетом для планироващика задач. Слово «nice» в английском языке обозначает, в частности, «вежливый». По этимологии этой команды процесс с большим значением nice — более вежлив к другим процессам, позволяя им использовать больше процессорного времени, поскольку он сам имеет меньший приоритет (и, следовательно, большее «значение вежливости» — niceness value).

Наибольший приоритет (и наименьшее значение nice) — −20. 19, либо 20 — наименьший приоритет, но это, с другой стороны, самые вежливые процессы.

Чтобы посмотреть идентификатор и значение nice нужного процесса в системе, можно воспользоваться командой ps axl.

Пример использования команды nice — запуск ресурсоёмкой задачи в фоновом режиме, так, чтобы она выполнялась, но освобождала часть процессорного времени для других процессов, как только оно становятся им нужно. Таким образом можно, скажем, запустить кодирование OGG/MP3 с большим значением nice, чтобы оно происходило в фоновом режиме, используя не всё процессорное время, а то, что не используется другими процессами (с меньшим значением nice — то есть с большим приоритетом).

Чтобы запустить команду с определённым приоритетом, слева нужно добавить «nice -n значение_nice», либо «nice —adjustment значение_nice» (adjustment — настройка, регулировка).

То есть: nice -n значение_nice команда.

Если нужно изменить приоритет уже запущенного процесса, можно воспользоваться командой renice.

renice -n значение_nice id_процесса

Команда renice выводит старый и новый приоритет процесса.

Теперь, зная эти команды, можно управлять процессами так, чтобы определённые задачи выполнялись быстрее в случае, когда сразу много процессов используют всё доступное процессорное время.

Источник

Оцените статью
Adblock
detector