next up previous contents
Next: 開機關流程與 loader Up: 程序與資源管理 Previous: 系統資源的觀察   Contents

程序的執行順序


		nice		調整或給予程序的 nice 值。

renice 改變正執行中的程序之 nice 值。

  1. 每個程序都有一個「優先執行序 (priority)」的屬性,以判斷那個程序要先處理。
  2. 以指令 ps 觀察優先執行序:
    [root@dywOffice ~]# ps -l
    F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
    4 S     0 29849 29844  0  76   0 -   629 wait   pts/2    00:00:00 su
    4 S     0 29949 29849  0  75   0 -   784 wait   pts/2    00:00:00 bash
    0 R     0 26424 29949  0  76   0 -   556 -      pts/2    00:00:00 ps
    
    1. UID 代表執行者的身份;
    2. PID 代表這個程序的代號;
    3. PPID 代表這個程序是由那個程序發展出來的,亦即是父程序;
    4. PRI 代表這個程序『可被執行的優先順序』越小越早被執行;
    5. NI 代表這個程序的 nice 值。
  3. nice 值為『系統可被執行的修正數值』:
    1. PRI(new) = PRI(old) + nice;
    2. PRI 是系統動態決定;
    3. 一般使用者的 nice 值為 0 ~ 19
    4. root 可用的 nice 值為 -20 ~ 19
    5. 一般使用者僅可將 nice 值調高;
    6. 一般使用者僅能調整屬於自己的程序之 nice 值。
  4. nice:調整或給予程序的 nice 值。
    1. 程序一開始就給予一個 nice 值;
      [root@dywOffice ~]# nice --help
      Usage: nice [OPTION] [COMMAND [ARG]...]
      Run COMMAND with an adjusted scheduling priority.
      With no COMMAND, print the current scheduling priority.  ADJUST is 10
      by default.  Range goes from -20 (highest priority) to 19 (lowest).
      
        -n, --adjustment=ADJUST   increment priority by ADJUST first
            --help     display this help and exit
            --version  output version information and exit
      
      Report bugs to <bug-coreutils@gnu.org>.
      [root@dywOffice ~]# nice
      0
      [root@dywOffice ~]# nice -n -5 su
      [root@dywOffice ~]# ps -l
      F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
      4 S     0 29849 29844  0  76   0 -   629 wait   pts/2    00:00:00 su
      4 S     0 29949 29849  0  75   0 -   784 wait   pts/2    00:00:00 bash
      4 S     0 26449 29949  0  72  -5 -   629 wait   pts/2    00:00:00 su
      4 S     0 26452 26449  0  70  -5 -   732 wait   pts/2    00:00:00 bash
      0 R     0 26483 26452  0  71  -5 -   556 -      pts/2    00:00:00 ps
      [root@dywOffice ~]# exit
      exit
      [root@dywOffice ~]# ps -l
      F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
      4 S     0 29849 29844  0  76   0 -   629 wait   pts/2    00:00:00 su
      4 S     0 29949 29849  0  75   0 -   784 wait   pts/2    00:00:00 bash
      0 R     0 26485 29949  0  76   0 -   555 -      pts/2    00:00:00 ps
      
    2. renice:改變正執行中的程序之 nice 值
      [root@dywOffice ~]# renice --help
      usage: renice priority [ [ -p ] pids ] [ [ -g ] pgrps ] [ [ -u ] users ]
      
      # -p :default
      
      # 一般使用者
      [dywang@dywHome2 ~]$ ps -l
      F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
      0 S   500  5975  5942  0  75   0 -   837 wait   pts/4    00:00:00 bash
      0 R   500  6569  5975  0  76   0 -   560 -      pts/4    00:00:00 ps
      [dywang@dywHome2 ~]$ renice 10 -u dywang
      500: old priority 0, new priority 10
      [dywang@dywHome2 ~]$ bash
      [dywang@dywHome2 ~]$ ps -l
      F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
      0 S   500  5975  5942  0  85  10 -   837 wait   pts/4    00:00:00 bash
      0 S   500  6574  5975  0  85  10 -   836 wait   pts/4    00:00:00 bash
      0 R   500  6637  6574  0  86  10 -   560 -      pts/4    00:00:00 ps
      [dywang@dywHome2 ~]$ renice 5 -u dywang
      500: old priority 10, new priority 5
      [dywang@dywHome2 ~]$ ps -l
      F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
      0 S   500  5975  5942  0  85   5 -   837 wait   pts/4    00:00:00 bash
      0 S   500  6574  5975  0  80   5 -   836 wait   pts/4    00:00:00 bash
      0 R   500  6649  6574  0  81   5 -   561 -      pts/4    00:00:00 ps
      [dywang@dywHome2 ~]$ renice -5 -u dywang
      renice: 500: setpriority: Permission denied
      [dywang@dywHome2 ~]$ renice 15 -p 6574
      6574: old priority 5, new priority 15
      [dywang@dywHome2 ~]$ ps -l
      F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
      0 S   500  5975  5942  0  85   5 -   837 wait   pts/4    00:00:00 bash
      0 S   500  6574  5975  0  90  15 -   836 wait   pts/4    00:00:00 bash
      0 R   500  6655  6574  0  91  15 -   561 -      pts/4    00:00:00 ps
      
      # root 管理
      [root@dywHome2 ~]# ps -l
      F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
      4 S     0  5966  5942  0  85   5 -   639 wait   pts/1    00:00:00 su
      4 S     0  6383  5966  0  76   0 -   752 wait   pts/1    00:00:00 bash
      0 R     0  6661  6383  0  77   0 -   561 -      pts/1    00:00:00 ps
      [root@dywHome2 ~]# renice 0 -u dywang
      500: old priority 5, new priority 0
      [root@dywHome2 ~]# ps -l
      F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
      4 S     0  5966  5942  0  80   0 -   639 wait   pts/1    00:00:00 su
      4 S     0  6383  5966  0  75   0 -   752 wait   pts/1    00:00:00 bash
      0 R     0  6734  6383  0  76   0 -   561 -      pts/1    00:00:00 ps
      [root@dywHome2 ~]# renice 10 6383
      6383: old priority 0, new priority 10
      [root@dywHome2 ~]# ps -l
      F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
      4 S     0  5966  5942  0  80   0 -   639 wait   pts/1    00:00:00 su
      4 S     0  6383  5966  0  85  10 -   752 wait   pts/1    00:00:00 bash
      0 R     0  6738  6383  0  86  10 -   560 -      pts/1    00:00:00 ps
      [root@dywHome2 ~]# renice -10 6383
      6383: old priority 10, new priority -10
      [root@dywHome2 ~]# ps -l
      F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
      4 S     0  5966  5942  0  80   0 -   639 wait   pts/1    00:00:00 su
      4 S     0  6383  5966  0  65 -10 -   752 wait   pts/1    00:00:00 bash
      0 R     0  6740  6383  0  66 -10 -   561 -      pts/1    00:00:00 ps
      

練習題

  1. 執行順序較優先的程序,以指令 ps -l 列出的 PRI 較大還是較小?
    Sol. 較小
  2. 執行指令 nice -n -5 su ,則程序 su 的 PRI 值如何改變?
    Sol. 新的 PRI = 舊的 PRI -5。
  3. 一般使用者執行指令 nice -n -5 kile,會產生什麼結果?
    Sol. 一般使用者只能增加程序的 PRI,沒有權限降低程序的 PRI。


2017-07-03