Dernière mise-à-jour : 2020/01/30 03:28

103.6 - Modifier les priorités des processus (2/60)

Rappelez-vous que dans la sortie de la commande ps, nous avons pu constater la présence des colonnes PRI et NI :

[root@centos7 ~]# ps -l
F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
4 S     0  4070  3760  0  80   0 - 50611 wait   pts/0    00:00:00 su
4 S     0  4077  4070  0  80   0 - 29027 wait   pts/0    00:00:00 bash
0 R     0  4309  4077  0  80   0 - 30319 -      pts/0    00:00:00 ps

et que dans la sortie de la commande top, nous avons pu constater la présence des colonnes PR et NI :

[root@centos7 ~]# top

top - 16:28:28 up 21 min,  2 users,  load average: 1.50, 1.21, 0.86
Tasks: 160 total,   3 running, 157 sleeping,   0 stopped,   0 zombie
%Cpu(s):  8.3 us,  2.7 sy,  0.0 ni, 89.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  1791624 total,   114728 free,   697212 used,   979684 buff/cache
KiB Swap:  3071996 total,  3071996 free,        0 used.   921100 avail Mem 

  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND                                   
 3249 trainee   20   0 1595324 235224  40572 S 13.6 13.1   3:43.96 gnome-shell                               
 1410 root      20   0  336640  50172   8980 R  4.4  2.8   1:15.09 Xorg                                      
 3756 trainee   20   0  626148  19044  12284 S  0.9  1.1   0:06.53 gnome-terminal-                           
 3883 trainee   20   0 1049588 208252  49496 S  0.9 11.6   0:20.22 firefox                                   
 4904 root      20   0  130024   1780   1240 R  0.6  0.1   0:00.09 top                                       
    3 root      20   0       0      0      0 S  0.3  0.0   0:01.18 ksoftirqd/0                               
  525 dbus      20   0   38480   3104   1444 S  0.3  0.2   0:01.53 dbus-daemon                               
  596 root      20   0  338392   1072    756 S  0.3  0.1   0:01.03 VBoxService                               
    1 root      20   0   59600   7028   3968 S  0.0  0.4   0:03.35 systemd                                   
    2 root      20   0       0      0      0 S  0.0  0.0   0:00.00 kthreadd                                  
    5 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kworker/0:0H                              
    6 root      20   0       0      0      0 S  0.0  0.0   0:00.17 kworker/u2:0                              
    7 root      rt   0       0      0      0 S  0.0  0.0   0:00.00 migration/0                               
    8 root      20   0       0      0      0 S  0.0  0.0   0:00.00 rcu_bh                                    
    9 root      20   0       0      0      0 S  0.0  0.0   0:00.00 rcuob/0                                   
   10 root      20   0       0      0      0 S  0.0  0.0   0:01.38 rcu_sched                                 
   11 root      20   0       0      0      0 R  0.0  0.0   0:02.55 rcuos/0                                   
...                                                                         

Pour modifier ces valeurs, il convient d'utiliser la commande nice ou renice.

La commande nice

Cette commande affiche ou modifie la priorité d’un processus. La priorité par défaut de nice est 10. La valeur de nice la plus prioritaire est -20. La valeur la moins prioritaire est 19 :

[root@centos7 ~]# nice -n -20 sleep 1234
^Z
[2]+  Stopped                 nice -n -20 sleep 1234

[root@centos7 ~]# ps lx | grep sleep
0     0  9870  4077  20   0 107892   616 hrtime S    pts/0      0:00 sleep 9999
0     0 10282   552  20   0 107892   612 hrtime S    ?          0:00 sleep 60
4     0 10283  4077   0 -20 107892   612 signal T<   pts/0      0:00 sleep 1234
0     0 10394  4077  20   0 112640   960 pipe_w S+   pts/0      0:00 grep --color=auto sleep

[root@centos7 ~]# nice -n 19 sleep 5678
^Z
[3]+  Stopped                 nice -n 19 sleep 5678

[root@centos7 ~]# ps lx | grep sleep
0     0  9870  4077  20   0 107892   616 hrtime S    pts/0      0:00 sleep 9999
4     0 10283  4077   0 -20 107892   612 signal T<   pts/0      0:00 sleep 1234
0     0 10402   552  20   0 107892   612 hrtime S    ?          0:00 sleep 60
0     0 10403  4077  39  19 107892   616 signal TN   pts/0      0:00 sleep 5678
0     0 10405  4077  20   0 112640   960 pipe_w S+   pts/0      0:00 grep --color=auto sleep

Comme vous pouvez constater la 6ième colonne contient la valeur de nice qui s'applique à la priorité dans la colonne 5.

Important - Notez que seul root peut lancer des processus avec une valeur négative.

Options de la commande

Les options de cette commande sont :

[root@centos7 ~]# nice --help
Usage: nice [OPTION] [COMMAND [ARG]...]
Run COMMAND with an adjusted niceness, which affects process scheduling.
With no COMMAND, print the current niceness.  Niceness values range from
-20 (most favorable to the process) to 19 (least favorable to the process).

Mandatory arguments to long options are mandatory for short options too.
  -n, --adjustment=N   add integer N to the niceness (default 10)
      --help     display this help and exit
      --version  output version information and exit

NOTE: your shell may have its own version of nice, which usually supersedes
the version described here.  Please refer to your shell's documentation
for details about the options it supports.

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
For complete documentation, run: info coreutils 'nice invocation'

La commande renice

Cette commande modifie la priorité d’un processus déjà en cours. La valeur de la priorité ne peut être modifiée que par le propriétaire du processus ou par root.

[root@centos7 ~]# jobs -l
[1]   9870 Running                 sleep 9999 &
[2]- 10283 Stopped                 nice -n -20 sleep 1234
[3]+ 10403 Stopped                 nice -n 19 sleep 5678
[root@centos7 ~]# bg %2
[2]- nice -n -20 sleep 1234 &
[root@centos7 ~]# bg %3
[3]+ nice -n 19 sleep 5678 &
[root@centos7 ~]# jobs -l
[1]   9870 Running                 sleep 9999 &
[2]- 10283 Running                 nice -n -20 sleep 1234 &
[3]+ 10403 Running                 nice -n 19 sleep 5678 &
[root@centos7 ~]# renice +5 10283
10283 (process ID) old priority -20, new priority 5
[root@centos7 ~]# renice -5 10403
10403 (process ID) old priority 19, new priority -5
[root@centos7 ~]# ps lx | grep sleep
0     0  9870  4077  20   0 107892   616 hrtime S    pts/0      0:00 sleep 9999
4     0 10283  4077  25   5 107892   612 restar SN   pts/0      0:00 sleep 1234
0     0 10403  4077  15  -5 107892   616 restar S<   pts/0      0:00 sleep 5678
0     0 10570   552  20   0 107892   616 hrtime S    ?          0:00 sleep 60
0     0 10648  4077  20   0 112640   960 pipe_w S+   pts/0      0:00 grep --color=auto sleep

Important -Notez que seul root peut décrémenter la valeur de priorité avec la commande renice.

Options de la commande

Les options de cette commande sont :

[r[root@centos7 ~]# renice --help

Usage:
 renice [-n] <priority> [-p|--pid] <pid>...
 renice [-n] <priority>  -g|--pgrp <pgid>...
 renice [-n] <priority>  -u|--user <user>...

Options:
 -g, --pgrp <id>        interpret argument as process group ID
 -n, --priority <num>   specify the nice increment value
 -p, --pid <id>         interpret argument as process ID (default)
 -u, --user <name|id>   interpret argument as username or user ID
 -h, --help             display help text and exit
 -V, --version          display version information and exit

For more information see renice(1).

<html>

Copyright © 2004-2017 Hugh Norris.<br><br> <a rel=“license” href=“http://creativecommons.org/licenses/by-nc-nd/3.0/fr/”><img alt=“Licence Creative Commons” style=“border-width:0” src=“http://i.creativecommons.org/l/by-nc-nd/3.0/fr/88x31.png” /></a><br />Ce(tte) oeuvre est mise à disposition selon les termes de la <a rel=“license” href=“http://creativecommons.org/licenses/by-nc-nd/3.0/fr/”>Licence Creative Commons Attribution - Pas d’Utilisation Commerciale - Pas de Modification 3.0 France</a>.

</html>


Menu