Linux process management systems

Foreword

We have an operating system is to run user programs, run the program requires the mobilization process, so the process management is the heart of any operating system.
In fact, the process is the result of current events program code being executed.

Understand the process

  • Process:
    the process is a static document, a copy of the running program, and there is a life cycle.
  • Process Status:
status definition
R(TASK_RUNNING) Executable state (RUNNING, READY)
S(TASK_INTERRUPTIBLE) You can wake to sleep
D(TASK_UNINTERRUPTIBLE) Can not wake up from sleep state
T(TASK_STOPPED) Suspended state
Z(EXIT_ZOMBIE) Dead state

Running andReady stateThe difference is whether it is occupied cpu time slice

Ready state andBe awakeThe difference is whether the run queue in the cpu

You can wake up withNot wake upThe difference is whether the system has the resources needed to run the program.

ctrl + z can process into the backgroundSuspended stateGenerally suspended state is manipulated by man.

Dead stateThis refers to the process of non-parent process to recover their system resources, will result in waste of system resources.

Process Viewer

  • Process Tree pstree

    [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 smallest unit of resource allocation.
    Thread: The object of the kernel scheduler, an independent program counter, process stack, and a set of processes register. For linux, thread is a special process. It is the smallest unit of a running process.
    systemdIt is daemon.

  • ps command (command displays the current process status)
    A display terminal associated with the process (the BSD)
    X Show all terminals independent of the process (the BSD)
    U user to display the progress status information to classify (the BSD)
    F hierarchy display 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 viewing options pid, COMM, Nice, PRI, pcpu, STAT, ppid
    -e show all processes (UINX)
    -f Show full format information (UNIX)
    -H hierarchy display information about processes
    -o designated viewing 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 according to a parameter:

    [root@workstation Desktop]# ps axo pid,comm,%cpu --sort=%cpu(正序,倒序加-号)
        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 displays 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]
     ...
    

Interpretation of the fields shown in the following table:

Field Explanation
USER user name
PID Process id
%CPU cpu Amount Percent
%MEM Memory Amount Percent
VSZ The virtual memory size
RSS Memory resident set size
TTY Character terminal
STAT Process State
START operation hours
TIME Occupancy cpu time
COMMAND Process Name
  • pgrep
    Displays the information process
    -u uid displays the specified user process
    -U name displays the specified user process
    -t tty terminal displays the specified process
    -l displays the process name
    -a display the full format process name
    -P pid show the child process specified processes
    such as:

    [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 (pid acquired according to the 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(当前系统时间) 
      up  1:19(当前系统运行时间),  
      1 user(当前系统有多少用户正在登录),  
      load average(系统负载): 0.10(一分钟内队列等待数), 0.04(五分钟), 0.01(十五分钟)
      Tasks(任务): 249 total(总数),   2 running(正在运行数), 247 sleeping,   0 stopped(暂停),   0 zombie
      %Cpu(s)(cpu使用量):  4.6 us(用户),  1.0 sy(内核),  0.0 ni(调整优先级), 94.1 id(空闲),  0.0 wa(等待输入输出时间),  0.2 hi(硬件中断时间),  0.0 si(软件中断时间),  0.2 st(被偷走的cpu量如虚拟机)
      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 avail Mem 
      PID(进程号)
      USER(用户)
      PR(优先级)
      NI(优先级代表的nice值)
      VIRT(vsz虚拟内存)
      RES(常驻内存地址)
      SHR(共享资源)
      S(状态)
      %CPU(cpu用量)
      %MEM(内存用量)
      TIME+ COMMAND(cpu时间即命令名称) 
    

    top internal parameters:

    P cpu by percentage
    M memory percentage of
    T occupied cpu time accumulated
    l turn on / off the uptime information
    t on / off task & cpu information
    s adjustment of the refresh frequency
    k terminate the process
    u view specified user process

    == top command parameters: ==

    -d specified refresh interval
    -b displayed on a batch
    -n shows the number of batch

Process priority

Process the order for possession of resources

  • Resource consumption divided according to the type of process
Resource usage Process Type
cpu intensive use CPU-Bound
I / O-intensive use I/O-Bound
  • Priority Range
    for Linux priority is divided into a fixed number (0-139)
    1-99 have real-time priority, the larger the number the higher the priority, the processing system for personal use without having to manually
    100-139 static priority, The smaller the number the higher the priority

  • NICE value
    NICE value: -20 ~ 19 100 ~ 139 static priority
    ordinary users can only lower priority, superuser be adjusted
    on command priority
    • PS AX -o Nice, PID, COMM
    • -n priority Nice program
    • reince-n pid priority programs
    such as:

    [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
    

    Increased value and nice to look at:

    [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
    

    Reduce 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 into the background and do not take up 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
    

    Assign priorities and into 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
    

Taiwan before and after the process of calling

Related command:

instruction meaning
jobs View background tasks
ctrl+z The foreground task into the background
bg Activation daemon
fg Repatriation process
& Runs in the background

E.g:

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

Graphics and turn into 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

Background can activate the wake-up 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 &

Active background can not be the wake-up process:

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

[1]+  Stopped                 vim
bg在此不可使用	
[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 See all signal information
Controllable signal meaning
1 Refresh
2 Keyboard interrupt input
3 Exit Keyboard
9 Forced End
15 Normally closed
18 Activation process
19 Forced pause
20 Normal pause
  • Command signal
    kill signal pid

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

    killall 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)

    与pgrep很相似,例如:
    [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

  • 守护进程的定义
    守护进程就是通常讲Daemon进程,是linux后台执行的服务进程。它独立于控制终端、周期性地执行某种任务或等待处理某些发生事件,不会随终端关闭而停止,直到接受停止信息才会结束。

后记

需要配置网络,才可以做守护进程的实验,所以暂时无法做。

发布了9 篇原创文章 · 获赞 10 · 访问量 3960

Guess you like

Origin blog.csdn.net/Thorne_lu/article/details/104039265