Process management of linux system

Posted by GooberDLX on Sun, 19 Jan 2020 13:19:44 +0100

Preface

We have an operating system to run user programs. The running of programs needs to transfer processes, so process management is the heart of all operating systems.
In fact, a process is the result of current events in the executing program code.

Process understanding

  • Process:
    A process is a static file, a copy of a running program, and has a lifecycle.
  • Process status:
state Definition
R(TASK_RUNNING) Running, ready
S(TASK_INTERRUPTIBLE) Wake up sleep state
D(TASK_UNINTERRUPTIBLE) Non wake up sleep state
T(TASK_STOPPED) Pause state
Z(EXIT_ZOMBIE) Dead state

The difference between running and ready state is whether it is taking up the cpu time slice

The difference between ready state and wake-up state is whether it is in the queue where the cpu is running

The difference between wake-up and non wake-up is whether it has the system resources needed for the program to run.

ctrl+z can make the process enter the background pause state, which is generally operated by human.

Dead state means that the process has no parent process to recover its system resources, which will cause system resources waste.

Process view

  • Process tree

    [root@workstation Desktop]# pstree
    systemd─┬─ModemManager───2*[{ModemManager}]
      	  ├─NetworkManager───2*[{NetworkManager}]
     	  ├─accounts-daemon───2*[{accounts-daemon}]
      ├─agetty
      ├─atd
      ├─auditd─┬─sedispatch
      │        └─2*[{auditd}]
      ├─avahi-daemon───avahi-daemon
      ├─boltd───2*[{boltd}]
      ├─chronyd
      ├─colord───2*[{colord}]
      ├─crond
      ├─cupsd
      ├─dbus-daemon───{dbus-daemon}
      ├─dnsmasq───dnsmasq
      ├─firewalld───{firewalld}
      ...
    

    Process: is the minimum unit of provisioning.
    Thread: the object scheduled by the kernel, with independent program counter, process stack and a set of process registers. For linux, threads are special processes. It is also the smallest unit of process operation.
    systemd is the daemons.

  • ps command (display the process status when the current command is executed)
    a display terminal related processes (BSD)
    x shows all terminal independent processes (BSD)
    u display process status information (BSD) by user
    f hierarchical display of process information

    [root@workstation Desktop]# ps
        PID TTY          TIME CMD
       2499 pts/0    00:00:00 bash
       3273 pts/0    00:00:00 ps
    [root@workstation Desktop]# bash
    [root@workstation Desktop]# ps
        PID TTY          TIME CMD
       2499 pts/0    00:00:00 bash
       3288 pts/0    00:00:00 bash
       3326 pts/0    00:00:00 ps
    [root@workstation Desktop]# ps f
        PID TTY      STAT   TIME COMMAND
       2499 pts/0    Ss     0:00 bash
       3288 pts/0    S      0:00  \_ bash
    

    o specify view options, pid,comm,nice,pri,pcpu,stat,ppid
    -e show all processes (uinx)
    -f display full format information (unix)
    -The H hierarchy displays information about the process
    -o specify view options, pid,comm,nice,%cpu,%mem,nice

    [root@workstation Desktop]# ps axo pid,comm,nice,stat | less
        PID COMMAND          NI STAT
          1 systemd           0 Ss
          2 kthreadd          0 S
          3 rcu_gp          -20 I<
          4 rcu_par_gp      -20 I<
          6 kworker/0:0H-kb -20 I<
          8 mm_percpu_wq    -20 I<
          9 ksoftirqd/0       0 S
         10 rcu_sched         0 I
         11 migration/0       - S
         12 watchdog/0        - S
         13 cpuhp/0           0 S
         14 cpuhp/1           0 S
         15 watchdog/1        - S
    

    Sort by a parameter:

    [root@workstation Desktop]# PS axo PID, comm,% CPU -- sort =% CPU (positive sequence, reverse sequence plus sign)
        PID COMMAND         %CPU
          1 systemd          0.0
          2 kthreadd         0.0
          3 rcu_gp           0.0
          4 rcu_par_gp       0.0
          6 kworker/0:0H-kb  0.0
          8 mm_percpu_wq     0.0
          9 ksoftirqd/0      0.0
         10 rcu_sched        0.0
         ...
    
  • ps command display information

     [root@workstation Desktop]# ps aux|less
     USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
     root         1  0.1  0.7 244444 13744 ?        Ss   00:27   0:03 /usr/lib/systemd/systemd --switched-root --system --deserialize 17
     root         2  0.0  0.0      0     0 ?        S    00:27   0:00 [kthreadd]
     root         3  0.0  0.0      0     0 ?        I<   00:27   0:00 [rcu_gp]
     root         4  0.0  0.0      0     0 ?        I<   00:27   0:00 [rcu_par_gp]
     root         6  0.0  0.0      0     0 ?        I<   00:27   0:00 [kworker/0:0H-kblockd]
     root         8  0.0  0.0      0     0 ?        I<   00:27   0:00 [mm_percpu_wq]
     root         9  0.0  0.0      0     0 ?        S    00:27   0:00 [ksoftirqd/0]
     root        10  0.0  0.0      0     0 ?        R    00:27   0:00 [rcu_sched]
     root        11  0.0  0.0      0     0 ?        S    00:27   0:00 [migration/0]
     root        12  0.0  0.0      0     0 ?        S    00:27   0:00 [watchdog/0]
     root        13  0.0  0.0      0     0 ?        S    00:27   0:00 [cpuhp/0]
     root        14  0.0  0.0      0     0 ?        S    00:27   0:00 [cpuhp/1]
     ...
    

The fields are explained as follows:

field explain
USER User name
PID Process id
%CPU cpu usage percentage
%MEM Memory usage percentage
VSZ Occupied virtual memory size
RSS Resident memory set size
TTY Character terminal
STAT Process state
START Running time
TIME cpu time occupied
COMMAND Process name
  • pgrep
    Display the specified information process
    -u uid displays the specified user process
    -U name displays the specified user process
    -t tty displays the specified terminal process
    -l display process name
    -a displays the full format process name
    -P pid shows the sub processes of the specified process
    For example:

    [root@workstation Desktop]# id student
      uid=1000(student) gid=1000(student) groups=1000(student),10(wheel)
    [root@workstation Desktop]# pgrep -u 1000
      3831
    [root@workstation Desktop]# pgrep -l -u 1000
      3831 bash
    [root@workstation Desktop]# pgrep -l -U student
      3831 bash
    [root@workstation Desktop]# pgrep -l -t pts/1
      3791 bash
      3830 su
      3831 bash
    [root@workstation Desktop]# pgrep -la -t pts/1
      3791 bash
      3830 su - student
      3831 -bash
    [root@workstation Desktop]# pgrep -P 1
      621
      656
      799
      811
      863
      864
      868
      869
      871
      872
      875
      ...
      [root@workstation Desktop]# pgrep -aP 1
      621 /usr/lib/systemd/systemd-journald
      656 /usr/lib/systemd/systemd-udevd
      799 /usr/bin/rpcbind -w -f
      811 /sbin/auditd
      863 /usr/lib/systemd/systemd-machined
      864 /usr/sbin/mcelog --ignorenodev --daemon --foreground
      868 avahi-daemon: running [workstation.local]
      869 /usr/libexec/udisks2/udisksd
      871 /sbin/rngd -f
      872 /usr/libexec/rtkit-daemon
      875 /usr/sbin/smartd -n -q never
      ...
    
  • Pidof (get PID according to process name)
    For example:

    [root@workstation Desktop]# watch -n 1 date
    [root@workstation Desktop]# pidof watch
      4166
    [root@workstation Desktop]# ps aux|grep watch
      root        12  0.0  0.0      0     0 ?        S    00:27   0:00 [watchdog/0]
      root        15  0.0  0.0      0     0 ?        S    00:27   0:00 [watchdog/1]
      root        36  0.0  0.0      0     0 ?        S    00:27   0:00 [watchdogd]
      root      4166  0.0  0.1 223888  2844 pts/0    S+   01:42   0:00 watch -n 1 date
      root      4375  0.0  0.0 221864   972 pts/2    S+   01:42   0:00 grep --color=auto watch
    
  • top

      Top - 01:46:39 (current system time) 
      Up 1:19 (current system running time),  
      1 user (how many users of the current system are logging in),  
      Load average: 0.10 (queue waiting in one minute), 0.04 (five minutes), 0.01 (fifteen minutes)
      Tasks: 249 total, 2 running, 247 sleeping, 0 stopped, 0 zombie
      %cpu (s): 4.6 us (user), 1.0 sy (kernel), 0.0 Ni (adjust priority), 94.1 ID (idle), 0.0 wa (wait for I / O time), 0.2 hi (hardware interrupt time), 0.0 Si (software interrupt time), 0.2 st (stolen cpu such as virtual machine)
      MIB MEM: 1829.1 total, 211.9 free, 1003.3 used, 613.9 buff / cache
      MIB swap: 0.0 total, 0.0 free, 0.0 used. 640.1 available mem 
      PID (process number)
      USER (USER)
      PR (priority)
      NI (nice value represented by priority)
      VIRT (vsz virtual memory)
      RES (resident memory address)
      SHR (shared resources)
      S (status)
      %cpu (cpu usage)
      %MEM (memory usage)
      TIME+ COMMAND (cpu time is the command name) 
    

    top internal parameters:

    P cpu percentage sort
    M memory percentage
    T total cpu time
    l turn uptime information on / off
    t turn task & CPU information on / off
    s adjust refresh rate
    k terminate process
    u view the specified user process

    ==top command parameters:==

    -d specify refresh interval
    -b display in batch mode
    -n displays the number of batches

Process priority

The sequence of process's possession of resources

  • Process types by resource usage
Resource usage Process type
cpu intensive CPU-Bound
I/O intensive I/O-Bound
  • Priority range
    For Linux, priority is divided into a fixed number (0-139)
    1-99 refers to real-time priority. The higher the number is, the higher the priority is. The system does not need to be manually processed for its own use
    100-139 is static priority, the smaller the number, the higher the priority

  • NICE value
    NICE value: - 20-19 corresponds to static priority 100-139
    Ordinary users can only lower the priority and super users can adjust it at will
    Commands about priority
    • ps ax –o nice,pid,comm
    • nice – n priority program
    • reince – n priority program pid
    For example:

    [root@workstation Desktop]# vim
      
      [1]+  Stopped                 vim
    [root@workstation Desktop]# ps -o stat,nice,comm,pid
      STAT  NI COMMAND           PID
      Ss     0 bash             2499
      S      0 bash             3288
      T      0 vim              5343
      R+     0 ps               5350
    

    Raise the nice value and check:

    [root@workstation Desktop]# renice -n -10 5343
      5343 (process ID) old priority 0, new priority -10
    [root@workstation Desktop]# ps -o stat,nice,comm,pid
      STAT  NI COMMAND           PID
      Ss     0 bash             2499
      S      0 bash             3288
      T<   -10 vim              5343
      R+     0 ps               5373
    

    Lower the nice value and view:

    [root@workstation Desktop]# renice -n 10 5343
      5343 (process ID) old priority -10, new priority 10
    [root@workstation Desktop]# ps -o stat,nice,comm,pid
      STAT  NI COMMAND           PID
      Ss     0 bash             2499
      S      0 bash             3288
      TN    10 vim              5343
      R+     0 ps               5395
    

    Open vim and put it into the background without occupying the terminal:

    [root@workstation Desktop]# vim &
      [2] 5474
    [root@workstation Desktop]# ps -o stat,nice,comm,pid
      STAT  NI COMMAND           PID
      Ss     0 bash             2499
      S      0 bash             3288
      TN    10 vim              5343
      T      0 vim              5474
      R+     0 ps               5481
      
      [2]+  Stopped                 vim
    

    Specify priority and put it in the background:

    [root@workstation Desktop]# nice -n 5 vim &
      [3] 5511
    [root@workstation Desktop]# ps -o stat,nice,comm,pid
      STAT  NI COMMAND           PID
      Ss     0 bash             2499
      S      0 bash             3288
      TN    10 vim              5343
      T      0 vim              5474
      TN     5 vim              5511
      R+     0 ps               5518
      
      [3]+  Stopped                 nice -n 5 vim
    

Call before and after the process

Related commands:

instructions Meaning
jobs View background tasks
ctrl+z Enter the foreground operation task into the background
bg Activate background process
fg Repatriation process
& Running in the background

For example:

[root@workstation Desktop]# jobs
[1]   Stopped                 vim
[2]-  Stopped                 vim
[3]+  Stopped                 nice -n 5 vim

Turn on graphic editing and put it in the background:

[root@workstation Desktop]# gedit
  ^Z
  [4]+  Stopped                 gedit
[root@workstation Desktop]# jobs
  [1]   Stopped                 vim
  [2]   Stopped                 vim
  [3]-  Stopped                 nice -n 5 vim
  [4]+  Stopped                 gedit

Activate background wake up pause process:

[root@workstation Desktop]# bg 4
  [4]+ gedit &
[root@workstation Desktop]# jobs
  [1]   Stopped                 vim
  [2]-  Stopped                 vim
  [3]+  Stopped                 nice -n 5 vim
  [4]   Running                 gedit &

Activate background non wake up pause process:

[root@workstation Desktop]# bg 1
[1] vim &

[1]+  Stopped                 vim
bg Not available here	
[root@workstation Desktop]# fg 1
vim
[root@workstation Desktop]# jobs
[2]-  Stopped                 vim
[3]+  Stopped                 nice -n 5 vim

signal

  • Controllable signal type
    man 7 signal view all signal information
Controllable signal Meaning
1 Refresh
2 Interrupt keyboard input
3 Quit keyboard
9 Mandatory termination
15 Normal closure
18 Activation process
19 Forced suspension
20 Normal suspension
  • Signal command
    kill signal pid

    [root@workstation Desktop]# gedit &
    [1] 9662
    [root@workstation Desktop]# kill -9 9662
    [1]+  Killed                  gedit
    

    Kill signal process name

    [root@workstation Desktop]# gedit &
    [1] 9628
    [root@workstation Desktop]# killall -9 gedit
    [1]+  Killed                  gedit
    

    pkill signal conditions (can help to view)

    And pgrep Very similar, for example:
    [root@workstation Desktop]# su - student
    Last login: Sun Jan 19 01:30:16 EST 2020 on pts/1
    

    Open a new shell:

    [root@workstation Desktop]# pkill -u 1000
    

    Original shell:

    [student@workstation ~]$ logout
    

System Daemons

  • Definition of Daemons
    Daemon s are usually called daemons, which are the service processes executed in the background of linux. It is independent of the control terminal, periodically executing certain tasks or waiting to handle certain events. It will not stop with the terminal shutdown until receiving the stop information.

Epilogue

You need to configure the network to do the experiment of the daemons, so you can't do it for the time being.

Published 9 original articles, won praise 9, visited 3934
Private letter follow

Topics: vim Linux DBus less