Chapter XI process and planned tasks

Posted by elbowgrease on Fri, 19 Nov 2021 12:25:25 +0100

1. Process and memory management

Kernel functions: process management, memory management, file system, network function, driver, security function, etc

1.1 what is a process

Process: a copy of a running program, a collection of instructions loaded into memory, and a unit of resource allocation

  • The Process ID (PID) number is used to mark each process
  • UID, GID, and SELinux contexts determine access and access rights to the file system
  • It is usually inherited from the user executing the process
  • Existence life cycle

Process creation:

  • init: the first process, systemd from CentOS7 onwards
  • Process: created by its parent process, fork(), parent-child relationship, CoW: Copy On Write

Processes, threads, and coroutines

To view threads in a process:

cat /proc/PID/status |grep -i threads

[root@rocky8 ~]# pstree -p
-bash: pstree: command not found
[root@rocky8 ~]# dnf provides pstree
psmisc-23.1-5.el8.x86_64 : Utilities for managing processes on your system
Repo        : BaseOS
Matched from:
Filename    : /usr/bin/pstree

[root@rocky8 ~]# dnf -y install psmisc
[root@rocky8 ~]# pstree -p
systemd(1)─┬─NetworkManager(728)─┬─{NetworkManager}(756)
           │                     └─{NetworkManager}(759)
           ├─VGAuthService(718)
           ├─agetty(855)
           ├─auditd(685)───{auditd}(686)
           ├─crond(843)
           ├─dbus-daemon(726)
           ├─polkitd(723)─┬─{polkitd}(751)
           │              ├─{polkitd}(768)
           │              ├─{polkitd}(776)
           │              ├─{polkitd}(777)
           │              └─{polkitd}(823)
           ├─rsyslogd(837)─┬─{rsyslogd}(850)
           │               └─{rsyslogd}(852)
           ├─sshd(765)───sshd(1299)───sshd(1311)───bash(1312)───pstree(9435)
           ├─sssd(721)─┬─sssd_be(773)
           │           └─sssd_nss(809)
           ├─systemd(1302)───(sd-pam)(1305)
           ├─systemd-journal(601)
           ├─systemd-logind(838)
           ├─systemd-udevd(635)
           ├─tuned(767)─┬─{tuned}(1143)
           │            ├─{tuned}(1158)
           │            └─{tuned}(1163)
           └─vmtoolsd(719)─┬─{vmtoolsd}(757)
                           └─{vmtoolsd}(778)
                           
[root@rocky8 ~]# ls /proc/723
attr        comm                exe      loginuid   mountstats  oom_score_adj  sched         stack    timens_offsets
autogroup   coredump_filter     fd       map_files  net         pagemap        schedstat     stat     timers
auxv        cpu_resctrl_groups  fdinfo   maps       ns          patch_state    sessionid     statm    timerslack_ns
cgroup      cpuset              gid_map  mem        numa_maps   personality    setgroups     status   uid_map
clear_refs  cwd                 io       mountinfo  oom_adj     projid_map     smaps         syscall  wchan
cmdline     environ             limits   mounts     oom_score   root           smaps_rollup  task

[root@rocky8 ~]# ls /proc/723/status
/proc/723/status
[root@rocky8 ~]# cat /proc/723/status
Name:	polkitd
Umask:	0022
State:	S (sleeping)
Tgid:	723
Ngid:	0
Pid:	723
PPid:	1
TracerPid:	0
Uid:	998	998	998	998
Gid:	996	996	996	996
FDSize:	64
Groups:	996 
NStgid:	723
NSpid:	723
NSpgid:	723
NSsid:	723
VmPeak:	 1630348 kB
VmSize:	 1630348 kB
VmLck:	       0 kB
VmPin:	       0 kB
VmHWM:	   24208 kB
VmRSS:	   24108 kB
RssAnon:	    7032 kB
RssFile:	   17076 kB
RssShmem:	       0 kB
VmData:	   54736 kB
VmStk:	     132 kB
VmExe:	     128 kB
VmLib:	   31412 kB
VmPTE:	     348 kB
VmSwap:	       0 kB
HugetlbPages:	       0 kB
CoreDumping:	0
THP_enabled:	1
Threads:	6
SigQ:	0/2989
SigPnd:	0000000000000000
ShdPnd:	0000000000000000
SigBlk:	0000000000000000
SigIgn:	0000000000001000
SigCgt:	000000018200040a
CapInh:	0000000000000000
CapPrm:	0000000000000000
CapEff:	0000000000000000
CapBnd:	000000ffffffffff
CapAmb:	0000000000000000
NoNewPrivs:	0
Seccomp:	0
Speculation_Store_Bypass:	thread vulnerable
Cpus_allowed:	ffffffff,ffffffff,ffffffff,ffffffff
Cpus_allowed_list:	0-127
Mems_allowed:	00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000001
Mems_allowed_list:	0
voluntary_ctxt_switches:	143
nonvoluntary_ctxt_switches:	133

[root@rocky8 ~]# cat /proc/723/status |grep -i threads
Threads:	6 # Threads thread

[root@rocky8 ~]# cat /proc/1312/status 
Name:	bash
Umask:	0022
State:	S (sleeping)
Tgid:	1312
Ngid:	0
Pid:	1312
PPid:	1311
TracerPid:	0
Uid:	0	0	0	0
Gid:	0	0	0	0
FDSize:	256
Groups:	0 
NStgid:	1312
NSpid:	1312
NSpgid:	1312
NSsid:	1312
VmPeak:	   26244 kB
VmSize:	   26244 kB
VmLck:	       0 kB
VmPin:	       0 kB
VmHWM:	    4000 kB
VmRSS:	    4000 kB
RssAnon:	     588 kB
RssFile:	    3412 kB
RssShmem:	       0 kB
VmData:	     564 kB
VmStk:	     132 kB
VmExe:	    1056 kB
VmLib:	    2220 kB
VmPTE:	      80 kB
VmSwap:	       0 kB
HugetlbPages:	       0 kB
CoreDumping:	0
THP_enabled:	1
Threads:	1
SigQ:	2/2989
SigPnd:	0000000000000000
ShdPnd:	0000000000000000
SigBlk:	0000000000010000
SigIgn:	0000000000380004
SigCgt:	000000004b817efb
CapInh:	0000000000000000
CapPrm:	000000ffffffffff
CapEff:	000000ffffffffff
CapBnd:	000000ffffffffff
CapAmb:	0000000000000000
NoNewPrivs:	0
Seccomp:	0
Speculation_Store_Bypass:	thread vulnerable
Cpus_allowed:	ffffffff,ffffffff,ffffffff,ffffffff
Cpus_allowed_list:	0-127
Mems_allowed:	00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000001
Mems_allowed_list:	0
voluntary_ctxt_switches:	701
nonvoluntary_ctxt_switches:	3

1.2 process structure


The kernel stores processes in a two-way circular linked list called task list

Each item in the linked list is of type task_struct, which is called Processing Control Block. The PCB contains all the information of a specific process

The process control block PCB contains the following information:

  • Process id, user id, and group id
  • Program counter
  • Status of the process (ready, running, blocked)
  • The value of the CPU register that needs to be saved and restored during process switching
  • Information describing the virtual address space
  • Information describing the control terminal
  • Current working directory
  • The file descriptor table contains many pointers to the file structure
  • The upper limit of resources that can be used by the process (which can be viewed by ulimit – a command)
  • I/O status: configure the process to use I/O devices

1.3 process related concepts

Page Frame: a Page Frame used to store page data and Page 4k

[root@rocky8 ~]# getconf -a |grep -i size
PAGESIZE                           4096
PAGE_SIZE                          4096
SSIZE_MAX                          32767
_POSIX_SSIZE_MAX                   32767
_POSIX_THREAD_ATTR_STACKSIZE       200809
FILESIZEBITS                       64
POSIX_ALLOC_SIZE_MIN               4096
POSIX_REC_INCR_XFER_SIZE           
POSIX_REC_MAX_XFER_SIZE            
POSIX_REC_MIN_XFER_SIZE            4096
LEVEL1_ICACHE_SIZE                 32768
LEVEL1_ICACHE_LINESIZE             64
LEVEL1_DCACHE_SIZE                 32768
LEVEL1_DCACHE_LINESIZE             64
LEVEL2_CACHE_SIZE                  524288
LEVEL2_CACHE_LINESIZE              64
LEVEL3_CACHE_SIZE                  8388608
LEVEL3_CACHE_LINESIZE              64
LEVEL4_CACHE_SIZE                  0
LEVEL4_CACHE_LINESIZE              0

1.3.1 physical address space and virtual address space

MMU: the Memory Management Unit is responsible for converting virtual addresses to physical addresses

When a program accesses the memory pointed to by a memory address, the CPU does not directly send the address to the memory bus, but is sent to the MMU (Memory Management Unit), then maps the memory address to the actual physical memory address, and then accesses the memory through the bus. The address of the program operation is called the virtual memory address

TLB: Translation Lookaside Buffer, which is used to store the cache of the mapping relationship between virtual address and physical address

1.3.2 user and kernel space

1.3.3 correspondence between C code and memory layout


Each process includes five different data segments

  • Code segment: the operation instruction used to store the executable file, that is, it is the image of the executable program in memory. The code segment needs to be prevented from being illegally modified at runtime, so only read operation is allowed, not write (modify) operation - it is not writable
  • Data segment: used to store the initialized global variables in the executable file, in other words, to store the statically allocated variables and global variables of the program
  • bss segment: the abbreviation of "Block Started by Symbol", which means a block starting with a symbol. The bss segment contains uninitialized global variables in the program, and all bss segments are set to zero in memory
  • Heap: store arrays and objects. Heap is used to store memory segments dynamically allocated during process operation. Its size is not fixed and can be dynamically expanded or reduced. When a process calls malloc and other functions to allocate memory, the newly allocated memory is dynamically added to the heap (heap is expanded); when free and other functions are used to release memory, the released memory is removed from the heap (heap reduced)
  • Stack: stack is a local variable temporarily created by the user storage program, that is, the variable defined in the function bracket "{}" (but excluding the variable declared by static, which means storing the variable in the data segment) In addition, when a function is called, its parameters will also be pushed into the process stack that initiates the call, and the return value of the function will also be stored back on the stack after the call is completed. Due to the last in first out feature of the stack, the stack is particularly convenient for saving / recovering the call field. The stack can be regarded as a memory area for registering and exchanging temporary data. It is similar to drinking too much and returning to the stack The column is first in first out, which is similar to eating too much

1.3.4 process memory usage

1.3.4.1 Memory Leak: Memory Leak

It refers to that a piece of memory is applied for with malloc or new in the program, but the memory is not released with free or delete, resulting in the memory being occupied all the time

1.3.4.2 Memory Overflow

It means that the program has applied for 10M memory space, but writing more than 10M bytes of data in this space is overflow, which is similar to red apricot coming out of the wall

1.3.4.3 insufficient memory: OOM


OOM is Out Of Memory, which is common in java programs. The system will select a process to kill it, and you will see a prompt similar to the following in the log messages

Jul 10 10:20:30 kernel: Out of memory: Kill process 9527 (java) score 88 or sacrifice child

This error is thrown when the JVM does not have enough memory to allocate space for the object and the garbage collector has no space to recycle, because the problem is too serious to be handled by the application).

reason:

  • Too little memory is allocated to the application: for example, the memory available to the virtual machine itself (usually specified through the VM parameter at startup) is too small.
  • The application uses too much memory, and it is not released when it is used up, which is wasted. At this time, it will cause memory leakage or memory overflow.

example:

[root@rocky8 ~]# sysctl -a |grep vm.panic_on_oom
vm.panic_on_oom = 0

1.4 process status


Basic state of the process

  • Creation status: when a process is created, it needs to apply for a blank PCB(process control block) to fill in the process control and management information to complete the resource allocation. If the creation cannot be completed, such as the resources cannot be met, it cannot be scheduled for operation. The state of the process at this time is called creation status
  • Ready status: the process is ready and allocated the required resources. As long as it is allocated to the CPU, it can run immediately
  • Execution status: the process is in the ready state. After being scheduled, the process enters the execution state
  • Blocking status: the executing process is temporarily unable to run due to some events (I/O request, failed to apply for cache), and the process is blocked. When the request is satisfied, it enters the ready state and waits for the system call
  • Termination status: the process ends, or an error occurs, or is terminated by the system. It enters the termination status. It can no longer be executed

There are six cases of transition between states

Run - > ready: 1. The main reason is that the process takes up too much CPU, and the time allocated to the process by the system is limited; 2. In the system using preemptive priority scheduling algorithm, when a process with higher priority wants to run, the process is forced to give up the CPU, and the process changes from execution state to ready state

Ready - > run: when the time slice of the running process runs out, the scheduling will go to the ready queue and select the appropriate process to allocate CPU

Running - > blocking: if the executing process cannot be executed due to a waiting event, the process changes from execution state to blocking state, such as I/O request

Blocking - > ready: when the event that the process is waiting for has occurred, it enters the ready queue

The following two states are impossible:

Blocking - > running: even if the CPU is allocated to the blocking process, it cannot be executed. When scheduling, the operating system will not select from the blocking queue, but from the ready queue

Ready - > blocking: the ready state is not executed at all, so it is impossible to enter the blocking state

Process status:

  • Running status: running
  • Ready status: ready
  • Sleep state: there are two types: interruptible and non interruptible
  • Stopped status: stopped, suspended in memory, but will not be scheduled unless manually started
  • Dead state: Zombie, zombie state, end the process. Before the parent process ends, the child process does not close. Killing the parent process can close the dead state

example:

[root@rocky8 ~]# ps aux
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root           1  0.0  1.4 183604 11372 ?        Ss   13:03   0:01 /usr/lib/systemd/systemd --switched-root --system --deserialize 17
root           2  0.0  0.0      0     0 ?        S    13:03   0:00 [kthreadd]
root           3  0.0  0.0      0     0 ?        I<   13:03   0:00 [rcu_gp]
root           4  0.0  0.0      0     0 ?        I<   13:03   0:00 [rcu_par_gp]
root           6  0.0  0.0      0     0 ?        I<   13:03   0:00 [kworker/0:0H-events_highpri]
root           9  0.0  0.0      0     0 ?        I<   13:03   0:00 [mm_percpu_wq]
root          10  0.0  0.0      0     0 ?        S    13:03   0:00 [ksoftirqd/0]
root          11  0.0  0.0      0     0 ?        R    13:03   0:00 [rcu_sched]
root          12  0.0  0.0      0     0 ?        S    13:03   0:00 [migration/0]
root          13  0.0  0.0      0     0 ?        S    13:03   0:00 [watchdog/0]
root          14  0.0  0.0      0     0 ?        S    13:03   0:00 [cpuhp/0]
root          16  0.0  0.0      0     0 ?        S    13:03   0:00 [kdevtmpfs]
root          17  0.0  0.0      0     0 ?        I<   13:03   0:00 [netns]
root          18  0.0  0.0      0     0 ?        S    13:03   0:00 [kauditd]
root          19  0.0  0.0      0     0 ?        S    13:03   0:00 [khungtaskd]
root          20  0.0  0.0      0     0 ?        S    13:03   0:00 [oom_reaper]
root          21  0.0  0.0      0     0 ?        I<   13:03   0:00 [writeback]
root          22  0.0  0.0      0     0 ?        S    13:03   0:00 [kcompactd0]
root          23  0.0  0.0      0     0 ?        SN   13:03   0:00 [ksmd]
root          24  0.0  0.0      0     0 ?        SN   13:03   0:00 [khugepaged]
root          25  0.0  0.0      0     0 ?        I<   13:03   0:00 [crypto]
root          26  0.0  0.0      0     0 ?        I<   13:03   0:00 [kintegrityd]
root          27  0.0  0.0      0     0 ?        I<   13:03   0:00 [kblockd]
root          28  0.0  0.0      0     0 ?        I<   13:03   0:00 [blkcg_punt_bio]
root          29  0.0  0.0      0     0 ?        I<   13:03   0:00 [tpm_dev_wq]
root          30  0.0  0.0      0     0 ?        I<   13:03   0:00 [md]
root          31  0.0  0.0      0     0 ?        I<   13:03   0:00 [edac-poller]
root          32  0.0  0.0      0     0 ?        S    13:03   0:00 [watchdogd]
root          33  0.0  0.0      0     0 ?        I<   13:03   0:00 [kworker/0:1H-kblockd]
root          35  0.0  0.0      0     0 ?        I    13:03   0:00 [kworker/u256:1-xfs-cil/sda2]
root          61  0.0  0.0      0     0 ?        S    13:03   0:00 [kswapd0]
root         154  0.0  0.0      0     0 ?        I<   13:03   0:00 [kthrotld]
root         155  0.0  0.0      0     0 ?        S    13:03   0:00 [irq/24-pciehp]
root         156  0.0  0.0      0     0 ?        S    13:03   0:00 [irq/25-pciehp]
root         157  0.0  0.0      0     0 ?        S    13:03   0:00 [irq/26-pciehp]
root         158  0.0  0.0      0     0 ?        S    13:03   0:00 [irq/27-pciehp]
root         159  0.0  0.0      0     0 ?        S    13:03   0:00 [irq/28-pciehp]
root         160  0.0  0.0      0     0 ?        S    13:03   0:00 [irq/29-pciehp]
root         161  0.0  0.0      0     0 ?        S    13:03   0:00 [irq/30-pciehp]
root         162  0.0  0.0      0     0 ?        S    13:03   0:00 [irq/31-pciehp]
root         163  0.0  0.0      0     0 ?        S    13:03   0:00 [irq/32-pciehp]
root         164  0.0  0.0      0     0 ?        S    13:03   0:00 [irq/33-pciehp]
root         165  0.0  0.0      0     0 ?        S    13:03   0:00 [irq/34-pciehp]
root         166  0.0  0.0      0     0 ?        S    13:03   0:00 [irq/35-pciehp]
root         167  0.0  0.0      0     0 ?        S    13:03   0:00 [irq/36-pciehp]
root         168  0.0  0.0      0     0 ?        S    13:03   0:00 [irq/37-pciehp]
root         169  0.0  0.0      0     0 ?        S    13:03   0:00 [irq/38-pciehp]
root         170  0.0  0.0      0     0 ?        S    13:03   0:00 [irq/39-pciehp]
root         171  0.0  0.0      0     0 ?        S    13:03   0:00 [irq/40-pciehp]
root         172  0.0  0.0      0     0 ?        S    13:03   0:00 [irq/41-pciehp]
root         173  0.0  0.0      0     0 ?        S    13:03   0:00 [irq/42-pciehp]
root         174  0.0  0.0      0     0 ?        S    13:03   0:00 [irq/43-pciehp]
root         175  0.0  0.0      0     0 ?        S    13:03   0:00 [irq/44-pciehp]
root         176  0.0  0.0      0     0 ?        S    13:03   0:00 [irq/45-pciehp]
root         177  0.0  0.0      0     0 ?        S    13:03   0:00 [irq/46-pciehp]
root         178  0.0  0.0      0     0 ?        S    13:03   0:00 [irq/47-pciehp]
root         179  0.0  0.0      0     0 ?        S    13:03   0:00 [irq/48-pciehp]
root         180  0.0  0.0      0     0 ?        S    13:03   0:00 [irq/49-pciehp]
root         181  0.0  0.0      0     0 ?        S    13:03   0:00 [irq/50-pciehp]
root         182  0.0  0.0      0     0 ?        S    13:03   0:00 [irq/51-pciehp]
root         183  0.0  0.0      0     0 ?        S    13:03   0:00 [irq/52-pciehp]
root         184  0.0  0.0      0     0 ?        S    13:03   0:00 [irq/53-pciehp]
root         185  0.0  0.0      0     0 ?        S    13:03   0:00 [irq/54-pciehp]
root         186  0.0  0.0      0     0 ?        S    13:03   0:00 [irq/55-pciehp]
root         187  0.0  0.0      0     0 ?        I<   13:03   0:00 [acpi_thermal_pm]
root         188  0.0  0.0      0     0 ?        I<   13:03   0:00 [kmpath_rdacd]
root         189  0.0  0.0      0     0 ?        I<   13:03   0:00 [kaluad]
root         190  0.0  0.0      0     0 ?        I<   13:03   0:00 [ipv6_addrconf]
root         191  0.0  0.0      0     0 ?        I<   13:03   0:00 [kstrp]
root         448  0.0  0.0      0     0 ?        I<   13:03   0:00 [mpt_poll_0]
root         449  0.0  0.0      0     0 ?        I<   13:03   0:00 [ata_sff]
root         450  0.0  0.0      0     0 ?        I<   13:03   0:00 [mpt/0]
root         451  0.0  0.0      0     0 ?        S    13:03   0:00 [scsi_eh_0]
root         452  0.0  0.0      0     0 ?        I<   13:03   0:00 [scsi_tmf_0]
root         453  0.0  0.0      0     0 ?        S    13:03   0:00 [scsi_eh_1]
root         454  0.0  0.0      0     0 ?        I<   13:03   0:00 [scsi_tmf_1]
root         459  0.0  0.0      0     0 ?        S    13:03   0:00 [scsi_eh_2]
root         460  0.0  0.0      0     0 ?        I<   13:03   0:00 [scsi_tmf_2]
root         468  0.0  0.0      0     0 ?        S    13:03   0:00 [irq/16-vmwgfx]
root         470  0.0  0.0      0     0 ?        I<   13:03   0:00 [ttm_swap]
root         471  0.0  0.0      0     0 ?        S    13:03   0:00 [card0-crtc0]
root         472  0.0  0.0      0     0 ?        S    13:03   0:00 [card0-crtc1]
root         473  0.0  0.0      0     0 ?        S    13:03   0:00 [card0-crtc2]
root         474  0.0  0.0      0     0 ?        S    13:03   0:00 [card0-crtc3]
root         475  0.0  0.0      0     0 ?        S    13:03   0:00 [card0-crtc4]
root         476  0.0  0.0      0     0 ?        S    13:03   0:00 [card0-crtc5]
root         477  0.0  0.0      0     0 ?        S    13:03   0:00 [card0-crtc6]
root         478  0.0  0.0      0     0 ?        S    13:03   0:00 [card0-crtc7]
root         503  0.0  0.0      0     0 ?        I<   13:03   0:00 [xfsalloc]
root         504  0.0  0.0      0     0 ?        I<   13:03   0:00 [xfs_mru_cache]
root         505  0.0  0.0      0     0 ?        I<   13:03   0:00 [xfs-buf/sda2]
root         506  0.0  0.0      0     0 ?        I<   13:03   0:00 [xfs-conv/sda2]
root         507  0.0  0.0      0     0 ?        I<   13:03   0:00 [xfs-cil/sda2]
root         508  0.0  0.0      0     0 ?        I<   13:03   0:00 [xfs-reclaim/sda]
root         509  0.0  0.0      0     0 ?        I<   13:03   0:00 [xfs-eofblocks/s]
root         510  0.0  0.0      0     0 ?        I<   13:03   0:00 [xfs-log/sda2]
root         511  0.0  0.0      0     0 ?        S    13:03   0:00 [xfsaild/sda2]
root         601  0.0  1.1  95692  9204 ?        Ss   13:03   0:00 /usr/lib/systemd/systemd-journald
root         635  0.0  1.2 124580 10004 ?        Ss   13:03   0:00 /usr/lib/systemd/systemd-udevd
root         639  0.0  0.0      0     0 ?        I<   13:03   0:00 [xfs-buf/sda3]
root         640  0.0  0.0      0     0 ?        I<   13:03   0:00 [xfs-conv/sda3]
root         641  0.0  0.0      0     0 ?        I<   13:03   0:00 [xfs-buf/sda1]
root         642  0.0  0.0      0     0 ?        I<   13:03   0:00 [xfs-cil/sda3]
root         643  0.0  0.0      0     0 ?        I<   13:03   0:00 [xfs-conv/sda1]
root         644  0.0  0.0      0     0 ?        I<   13:03   0:00 [xfs-reclaim/sda]
root         645  0.0  0.0      0     0 ?        I<   13:03   0:00 [xfs-cil/sda1]
root         646  0.0  0.0      0     0 ?        I<   13:03   0:00 [xfs-eofblocks/s]
root         647  0.0  0.0      0     0 ?        I<   13:03   0:00 [xfs-reclaim/sda]
root         648  0.0  0.0      0     0 ?        I<   13:03   0:00 [xfs-log/sda3]
root         649  0.0  0.0      0     0 ?        I<   13:03   0:00 [xfs-eofblocks/s]
root         650  0.0  0.0      0     0 ?        S    13:03   0:00 [xfsaild/sda3]
root         651  0.0  0.0      0     0 ?        I<   13:03   0:00 [xfs-log/sda1]
root         652  0.0  0.0      0     0 ?        S    13:03   0:00 [xfsaild/sda1]
root         685  0.0  0.2  77000  1948 ?        S<sl 13:03   0:00 /sbin/auditd
root         718  0.0  1.4  86200 11416 ?        Ss   13:03   0:00 /usr/bin/VGAuthService -s
root         719  0.1  1.2 278328 10044 ?        Ssl  13:03   0:03 /usr/bin/vmtoolsd
root         721  0.0  1.7 216924 14320 ?        Ss   13:03   0:00 /usr/sbin/sssd -i --logger=files
polkitd      723  0.0  3.0 1630348 24108 ?       Ssl  13:03   0:00 /usr/lib/polkit-1/polkitd --no-debug
dbus         726  0.0  0.6  76628  5504 ?        Ss   13:03   0:00 /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --syst
root         728  0.0  2.2 391536 18224 ?        Ssl  13:03   0:00 /usr/sbin/NetworkManager --no-daemon
root         765  0.0  0.9  94444  7412 ?        Ss   13:03   0:00 /usr/sbin/sshd -D -oCiphers=aes256-gcm@openssh.com,chacha20-poly1305@openssh
root         767  0.0  3.7 414624 30244 ?        Ssl  13:03   0:00 /usr/libexec/platform-python -Es /usr/sbin/tuned -l -P
root         773  0.0  1.8 225348 14724 ?        S    13:03   0:00 /usr/libexec/sssd/sssd_be --domain implicit_files --uid 0 --gid 0 --logger=f
root         779  0.0  0.0      0     0 ?        I<   13:03   0:00 [kworker/u257:0-hci0]
root         783  0.0  0.0      0     0 ?        I<   13:03   0:00 [kworker/u257:1-hci0]
root         809  0.0  5.1 226992 40940 ?        S    13:03   0:00 /usr/libexec/sssd/sssd_nss --uid 0 --gid 0 --logger=files
root         837  0.0  0.6 215336  5292 ?        Ssl  13:03   0:00 /usr/sbin/rsyslogd -n
root         838  0.0  1.0 103976  8328 ?        Ss   13:03   0:00 /usr/lib/systemd/systemd-logind
root         843  0.0  0.4  36956  3604 ?        Ss   13:03   0:00 /usr/sbin/crond -n
root         855  0.0  0.2  13656  1724 tty1     Ss+  13:03   0:00 /sbin/agetty -o -p -- \u --noclear tty1 linux
root        1299  0.0  1.0 139184  8792 ?        Ss   13:04   0:00 sshd: root [priv]
root        1302  0.0  1.2 100692  9904 ?        Ss   13:04   0:00 /usr/lib/systemd/systemd --user
root        1305  0.0  0.3 238892  2588 ?        S    13:04   0:00 (sd-pam)
root        1311  0.0  0.8 153404  6920 ?        S    13:04   0:00 sshd: root@pts/0
root        1312  0.0  0.4  26244  4000 pts/0    Ss   13:04   0:00 -bash
root        1357  0.0  0.0      0     0 ?        I    13:25   0:00 [kworker/0:1-ata_sff]
root        1409  0.0  0.0      0     0 ?        R    13:33   0:00 [kworker/u256:2-events_unbound]
root        9433  0.1  0.0      0     0 ?        I    13:33   0:01 [kworker/0:2-events_power_efficient]
root        9442  0.0  0.0      0     0 ?        I    13:38   0:00 [kworker/0:0-ata_sff]
root        9452  0.0  0.4  58728  3884 pts/0    R+   13:43   0:00 ps aux
#I idle threads

[root@rocky8 ~]# top
top - 16:00:38 up  2:56,  3 users,  load average: 1.00, 1.00, 1.00
Tasks: 158 total,   3 running, 155 sleeping,   0 stopped,   0 zombie
%Cpu(s):  9.4 us, 43.8 sy,  0.0 ni, 46.9 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
MiB Mem :    782.4 total,    139.4 free,    205.0 used,    438.0 buff/cache
MiB Swap:   2048.0 total,   2048.0 free,      0.0 used.    433.1 avail Mem 

    PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND                                                                
  10507 root      20   0    7352    980    912 R  94.1   0.1  78:15.82 dd                                                                     
      1 root      20   0  183736  11408   8964 S   0.0   1.4   0:01.73 systemd                                                                
      2 root      20   0       0      0      0 S   0.0   0.0   0:00.01 kthreadd                                                               
      3 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 rcu_gp                                                                 
      4 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 rcu_par_gp                                                             
      6 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 kworker/0:0H-events_highpri                                            
      9 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 mm_percpu_wq                                                           
     10 root      20   0       0      0      0 S   0.0   0.0   0:00.22 ksoftirqd/0                                                            
     11 root      20   0       0      0      0 R   0.0   0.0   0:01.09 rcu_sched                                                              
     12 root      rt   0       0      0      0 S   0.0   0.0   0:00.00 migration/0                                                            
     13 root      rt   0       0      0      0 S   0.0   0.0   0:00.00 watchdog/0                                                             
     14 root      20   0       0      0      0 S   0.0   0.0   0:00.00 cpuhp/0                                                                
     16 root      20   0       0      0      0 S   0.0   0.0   0:00.00 kdevtmpfs                                                              
     17 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 netns                                                                  
     18 root      20   0       0      0      0 S   0.0   0.0   0:00.00 kauditd                                                                
     19 root      20   0       0      0      0 S   0.0   0.0   0:00.00 khungtaskd                                                             
     20 root      20   0       0      0      0 S   0.0   0.0   0:00.00 oom_reaper                                                             
     21 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 writeback                                                              
     22 root      20   0       0      0      0 S   0.0   0.0   0:00.00 kcompactd0                                                             
     23 root      25   5       0      0      0 S   0.0   0.0   0:00.00 ksmd


Example: zombie state

[root@rocky8 ~]# bash 
[root@rocky8 ~]# echo $BASHPID
9454
[root@rocky8 ~]# echo $PPID
1312
[root@rocky8 ~]# pstree -p |grep bash
           |-sshd(765)---sshd(1299)---sshd(1311)---bash(1312)---bash(9454)-+-grep(9472)

#Open a new terminal
[root@rocky8 ~]# pstree -p |grep bash
           |-sshd(765)-+-sshd(1299)---sshd(1311)---bash(1312)---bash(9454)
           |           `-sshd(9473)---sshd(9475)---bash(9476)-+-grep(9498)
[root@rocky8 ~]# ps aux |grep bash
root        1312  0.0  0.4  26244  4000 pts/0    Ss   13:04   0:00 -bash
root        9454  0.0  0.4  26216  3924 pts/0    S+   13:45   0:00 bash
root        9476  0.0  0.4  26244  3812 pts/1    Ss   13:47   0:00 -bash
root        9500  0.0  0.1  12136  1156 pts/1    R+   13:47   0:00 grep --color=auto bash

#Set the parent process to stop
[root@rocky8 ~]# kill -19 1312
[root@rocky8 ~]# ps aux |grep bash
root        1312  0.0  0.4  26244  4000 pts/0    Ts   13:04   0:00 -bash
root        9454  0.0  0.4  26216  3924 pts/0    S+   13:45   0:00 bash
root        9476  0.0  0.4  26244  3812 pts/1    Ss   13:47   0:00 -bash
root        9502  0.0  0.1  12136  1152 pts/1    R+   13:48   0:00 grep --color=auto bash

#Kill the child process and make it enter zombie state
[root@rocky8 ~]# kill 9454
[root@rocky8 ~]# ps aux |grep bash
root        1312  0.0  0.4  26244  4000 pts/0    Ts   13:04   0:00 -bash
root        9454  0.0  0.0      0     0 pts/0    Z+   13:45   0:00 [bash] <defunct> #It can be seen that the upper STAT is Z, indicating zombie state
root        9476  0.0  0.4  26244  3812 pts/1    Ss   13:47   0:00 -bash
root        9505  0.0  0.1  12136  1156 pts/1    R+   13:49   0:00 grep --color=auto bash

[root@rocky8 ~]# kill 1312
[root@rocky8 ~]# ps aux |grep bash
root        1312  0.0  0.4  26244  4000 pts/0    Ts   13:04   0:00 -bash
root        9454  0.0  0.0      0     0 pts/0    Z+   13:45   0:00 [bash] <defunct>
root        9476  0.0  0.4  26244  3812 pts/1    Ss   13:47   0:00 -bash
root        9507  0.0  0.1  12136  1036 pts/1    R+   13:50   0:00 grep --color=auto bash

#Method 1: restore the parent process
[root@rocky8 ~]# kill -18 1312
#Method 2: kill the parent process
[root@rocky8 ~]# kill -9 1312

#Again, we can see that the zombie process no longer exists
[root@rocky8 ~]# ps aux |grep bash
root        9476  0.0  0.4  26244  3812 pts/1    Ss   13:47   0:00 -bash
root        9512  0.0  0.4  26244  4000 pts/0    Ss+  13:50   0:00 -bash
root        9534  0.0  0.1  12136  1140 pts/1    R+   13:50   0:00 grep --color=auto bash

1.5 LRU algorithm

LRU: Least Recently Used algorithm (Chen Shimei algorithm that likes the new and hates the old) to free memory

example:

Assuming that the sequence is 4 3 4 2 3 1 4 2 and there are 3 physical blocks, then

The first round 4 calls into memory 4

Round 2 3 call in memory 3 4

Round 3 4 call in memory 4 3

Round 4 2 call in memory 2 4 3

The 5th round 3 calls into memory 3 2 4

Round 6 1 call in memory 1 3 2

Round 7 4 call in memory 4 1 3

Round 8 2 call in memory 2 4 1

1.6 IPC interprocess communication

IPC: Inter Process Communication

  • Same host:
pipe The Conduit,Unidirectional transmission
socket s socket ,Bidirectional transmission
Memory-maped file file map,A piece of data in the file is mapped to physical memory, which is shared by multiple processes
shm shared memory Shared memory
signal signal
Lock Lock the resource. If the resource has been locked by a process, other processes will be blocked if they want to modify or even read these resources until the lock is opened
semaphore Semaphore, a counter
  • Different hosts: socket=IP and port number (five elements)
RPC remote procedure call
MQ Message queues, producers and consumers, such as: Kafka,RabbitMQ,ActiveMQ

example:

[root@rocky8 ~]# ll /run/
total 20
-rw-------  1 root root    0 Oct 31 13:03 agetty.reload
-rw-r--r--  1 root root    4 Oct 31 13:03 auditd.pid
drwxr-xr-x  2 root root   40 Oct 31 13:03 console
----------  1 root root    0 Oct 31 13:03 cron.reboot
drwx------  2 root root   40 Oct 31 13:03 cryptsetup
drwxr-xr-x  2 root root   60 Oct 31 13:03 dbus
drwxr-xr-x  2 root root   40 Oct 31 13:03 faillock
drwxr-xr-x  2 root root   60 Oct 31 13:03 fsck
prw-------  1 root root    0 Oct 31 13:03 initctl
drwxr-xr-x  4 root root  100 Oct 31 13:03 initramfs
drwxr-xr-x  3 root root   80 Oct 31 13:03 lock
drwxr-xr-x  3 root root   60 Oct 31 13:03 log
drwxr-xr-x  2 root root   40 Oct 31 13:03 mount
drwxr-xr-x  6 root root  160 Oct 31 13:03 NetworkManager
drwxr-xr-x  2 root root   40 Oct 31 13:03 plymouth
-rw-------  1 root root    3 Oct 31 13:03 rsyslogd.pid
drwxr-xr-x  2 root root   40 Oct 31 13:03 sepermit
drwxr-xr-x  2 root root   40 Oct 31 13:03 setrans
-rw-r--r--  1 root root    4 Oct 31 13:03 sshd.pid
-rw-------  1 root root    4 Oct 31 13:03 sssd.pid
drwx--x--x  3 root root   60 Oct 31 13:03 sudo
drwxr-xr-x 16 root root  400 Oct 31 13:04 systemd
drwxr-xr-x  2 root root   60 Oct 31 13:03 tmpfiles.d
drwxr-xr-x  2 root root   60 Oct 31 13:03 tuned
drwxr-xr-x  7 root root  160 Oct 31 13:42 udev
drwxr-xr-x  3 root root   60 Oct 31 13:04 user
-rw-rw-r--  1 root utmp 1920 Oct 31 13:50 utmp
drwxr-xr-x  2 root root   60 Oct 31 13:03 vmware

Example: using pipeline file to implement IPC

[root@rocky8 ~]# mkfifo test.pipe
[root@rocky8 ~]# ll test.pipe
prw-r--r-- 1 root root 0 Oct 31 13:57 test.pipe

[root@rocky8 ~]# echo abc >> test.pipe
#Waiting to read

#At another terminal, data can be read from the file
[root@rocky8 ~]# cat test.pipe #Read pipeline data
abc

[root@rocky8 ~]# free -h
              total        used        free      shared  buff/cache   available
Mem:          782Mi       177Mi       254Mi       5.0Mi       350Mi       476Mi
Swap:         2.0Gi          0B       2.0Gi

[root@rocky8 ~]# vim anaconda-ks.cfg 
#version=RHEL8                                                                                                                                 
# Use graphical install
graphical

repo --name="AppStream" --baseurl=file:///run/install/sources/mount-0000-cdrom/AppStream

%packages
@^minimal-environment
kexec-tools

%end

# Keyboard layouts
keyboard --xlayouts='us'
# System language
lang en_US.UTF-8

# Network information
network  --bootproto=dhcp --device=ens33 --ipv6=auto --activate
network  --hostname=localhost.localdomain

# Use CDROM installation media
cdrom

# Run the Setup Agent on first boot
firstboot --enable
"anaconda-ks.cfg" 51L, 1318C                                                                                                 1,1           Top

[1]+  Stopped                 vim anaconda-ks.cfg
#ctrl+z does not exit vim normally, so that the program enters the stop state

[root@rocky8 ~]# vim anaconda-ks.cfg 

E325: ATTENTION
Found a swap file by the name ".anaconda-ks.cfg.swp"
          owned by: root   dated: Sun Oct 31 16:01:37 2021
         file name: ~root/anaconda-ks.cfg
          modified: no
         user name: root   host name: rocky8
        process ID: 10704 (still running)
While opening file "anaconda-ks.cfg"
             dated: Wed Oct  6 19:20:38 2021

(1) Another program may be editing the same file.  If this is the case,
    be careful not to end up with two different instances of the same
    file when making changes.  Quit, or continue with caution.
(2) An edit session for this file crashed.
    If this is the case, use ":recover" or "vim -r anaconda-ks.cfg"
    to recover the changes (see ":help recovery").
    If you did this already, delete the swap file ".anaconda-ks.cfg.swp"
    to avoid this message.

Swap file ".anaconda-ks.cfg.swp" already exists!
[O]pen Read-Only, (E)dit anyway, (R)ecover, (Q)uit, (A)bort:  
#When you open it again, you will be prompted that the file is in use

[root@rocky8 ~]# ps aux |grep vim
root       10704  0.0  1.0  46888  8220 pts/1    T    16:01   0:00 vim anaconda-ks.cfg #Entered the stop state
root       10712  0.0  0.1  12136  1148 pts/1    R+   16:05   0:00 grep --color=auto vim

[root@rocky8 ~]# ls -a
.   anaconda-ks.cfg       .bash_history  .bash_profile  .config  .tcshrc    .viminfo
..  .anaconda-ks.cfg.swp  .bash_logout   .bashrc        .cshrc   test.pipe  .vimrc
#Files that do not exit normally will be temporarily opened. swp file

[root@rocky8 ~]# rm -f .anaconda-ks.cfg.swp 
[root@rocky8 ~]# vim anaconda-ks.cfg 
#version=RHEL8                                                                                                                                 
# Use graphical install
graphical

repo --name="AppStream" --baseurl=file:///run/install/sources/mount-0000-cdrom/AppStream

%packages
@^minimal-environment
kexec-tools

%end

# Keyboard layouts
keyboard --xlayouts='us'
# System language
#Delete the. swp file and enter it again

Example: find socket file

[root@rocky8 ~]# find / -type s -ls
    25571      0 srw-rw-rw-   1  root     root            0 Oct 31 13:03 /run/vmware/guestServicePipe
    24284      0 srw-rw-rw-   1  root     root            0 Oct 31 13:03 /run/dbus/system_bus_socket
    24280      0 srw-rw-rw-   1  root     root            0 Oct 31 13:03 /run/.heim_org.h5l.kcm-socket
    28565      0 srw-rw-rw-   1  root     root            0 Oct 31 13:04 /run/user/0/bus
    28558      0 srwxr-xr-x   1  root     root            0 Oct 31 13:04 /run/user/0/systemd/private
    28554      0 srwxr-xr-x   1  root     root            0 Oct 31 13:04 /run/user/0/systemd/notify
    22384      0 srw-------   1  root     root            0 Oct 31 13:03 /run/udev/control
    22177      0 srw-------   1  root     root            0 Oct 31 13:03 /run/systemd/coredump
    22134      0 srwxrwxrwx   1  root     root            0 Oct 31 13:03 /run/systemd/private
    13169      0 srw-rw-rw-   1  root     root            0 Oct 31 13:03 /run/systemd/journal/socket
    13166      0 srw-rw-rw-   1  root     root            0 Oct 31 13:03 /run/systemd/journal/stdout
    13161      0 srw-rw-rw-   1  root     root            0 Oct 31 13:03 /run/systemd/journal/dev-log
    13150      0 srwx------   1  root     root            0 Oct 31 13:03 /run/systemd/cgroups-agent
    13148      0 srwxrwxrwx   1  root     root            0 Oct 31 13:03 /run/systemd/notify
    12887      0 s---------   1  root     root            0 Oct 31 13:03 /run/systemd/inaccessible/sock
 67632837      0 srw-------   1  root     root            0 Oct 31 13:03 /var/lib/sss/pipes/private/sbus-monitor
 67632840      0 srw-------   1  root     root            0 Oct 31 13:03 /var/lib/sss/pipes/private/sbus-dp_implicit_files.773
   635386      0 srw-rw-rw-   1  root     root            0 Oct 31 13:03 /var/lib/sss/pipes/nss

1.7 process priority


CentOS priority

System priority: 0-139, The smaller the number, the higher the priority. There are 140 running queues and 140 expired queues
 Real time priority: 99-0 Maximum priority
nice Value:-20 To 19, corresponding to system priority 100-139

1.8 process classification

Operating system classification:

  • Collaborative multitasking: used in early Windows systems, that is, a task gets CPU time. Unless it gives up using the CPU, it will completely occupy the CPU. Therefore, collaboration is required between tasks - use the CPU for a period of time and give up using it actively
  • Preemptive multitasking: in the Linux kernel, the total control of the CPU is in the hands of the operating system. The operating system will take turns to ask whether each task needs to use the CPU, and let it use if necessary. However, after a certain time, the operating system will deprive the CPU use right of the current task, put it at the end of the query queue, and then ask for the next task

Process type:

  • Daemon: daemon, a process started during system boot, and a process unrelated to the terminal
  • Foreground process: the process related to the terminal and started through the terminal

Note: the two can be transformed into each other

Classification by process resource usage:

  • CPU bound: CPU intensive, non interactive
  • IO bound: IO intensive, interactive

Topics: Linux Operation & Maintenance architecture DevOps Cloud Native