Linux learning-unit 7-process management under Linux system

1. Introduction to the basic knowledge of the process

Simply put, a process is the state (dynamic) of the program while it is running. The process is a copy of the running program, and the program will be copied to the memory cache space during the running of the program.

进程存在的生命周期
准备期:程序代码直接缓存在系统硬件上
运行期:程序代码处于工作执行的状态
终止期:工作完成后系统把所占用的系统资源进行全部回收

Note: During the termination period, all occupied system resources will be fully recovered after the work is over. If this operation is not performed, there will be more and more garbage in the system until the system crashes.

Process & thread relationship
1. Process is the smallest unit of resource call
2. Thread is the smallest unit of process
3. Program execution flow runs from top to bottom, when the process is multitasking, the cpu is multi-core, and multitasking should be It is processed by each core at the same time, and the tasks processed by each core are called threads
. 4. Thread resources are shared. The process is the smallest unit of resource allocation. The thread is the smallest unit of the process. All resources in the process are shared by threads ==

  • Process status classification and interpretation
State of the process State interpretation
R(TAKS_RUNNING) Executable state (running, ready)
S(TASK_INTRRUPTABLE) Wake-up sleep
D(TASK_UNINTRRUPTABLE) Can not wake up sleep
T(TASK_STOP) Suspended state
Z(EXIT_ZOMBIE) Dead state

2. View of the process

2.1 Use graphical viewing tools to view the progress

gnome-system-monitor graphical process viewing tool to view the process

Insert picture description here

2.2 Use the ps command to view the process

Insert picture description here
The ps command displays the status of the current command execution

The detailed explanation of the ps command can be viewed with man ps

Insert picture description here

2.2.1 Common parameters and explanations of the ps command

1. BSD style (common command parameters)

ps command + parameters Parameter explanation
a Processes related to the terminal (the processes generated after the user logs into the system are all with a terminal)
x Processes not related to the terminal
u View method of user information classification
f Process hierarchy
O Specify viewing options, pid, comm, nice,%cpu,%mem

2.UNIX style (common command parameters)

ps command + parameters Parameter explanation
-O Specify viewing options, pid, comm, nice,%cpu,%mem,nice (unix)
-e Show all processes
-f Display the full format of the information
-H Display process hierarchy
- -sort=uid,-ppid,+pid Sort and display by a certain parameter

Difference: UNLIX parameters with (-) and BSD format parameters without (-)

2.2.2 ps command display information

ps aux displays the following information:
Insert picture description here

Information displayed by ps aux Explanation
USER Process owner
PID Process id
%CPU The percentage of CPU occupied by the process
%MEM The percentage of memory resources occupied by the process
VSZ The virtual memory size used by the process
RSS The size of the data in the permanent memory of the process
TTY Terminal used by the process
STAT Process status
START Process running time
TIME CPU time occupied by the process
COMMAND Process name
  • ps a-display and terminal related processes (BSD operation mode)

Insert picture description here


Insert picture description here


• ps x-display all processes that have nothing to do with the terminal (BSD operating mode)

Insert picture description here


• ps ax-display all processes, including terminal-related processes and terminal-independent processes (BSD operation mode)
Note: ps ax shows that the processes are classified by resources (PIDs are sorted from small to large)

Insert picture description here


• ps u-display process status information based on user classification (BSD operation mode)

Insert picture description here


• ps f-hierarchical structure display process information (BSD operation mode)

Insert picture description here


  • ps -e displays all processes (UNIX operating mode)

Insert picture description here


• ps -f displays complete format information (UNIX operating mode)

Insert picture description here


• ps -H hierarchical structure displays process related information (UNIX operating mode)

Insert picture description here


  • ps -o specifies viewing options, such as pid, comm,%cpu,%mem,nice

Insert picture description here


ps can sort some current resources

ps axo pid,comm,%mem --sort=%mem display in positive order

Insert picture description here


ps axo pid,comm,%mem --sort=-%mem display in reverse order
Insert picture description here
Insert picture description here


2.3 pgrep command-process filtering

pgrep command Instruction explanation
-u uid Display the specified user process
-U user Display the specified user process
-t tty Display the specified terminal process
-l Show process name
-a Display the full name of the process
-P pid Show the child processes of the specified process

Insert picture description here

2.4 pidof obtains pid based on process name

  • pidof command-view the ID of a command process

Insert picture description here
Insert picture description here

2.5 Usage of top command

top 命令 —— 动态进程查看
top中的内容显示信息:(ctrl +c结束查看)

top命令可以真实查看进程状况,具有实时显示功能,默认3s刷新一次。

top 显 示 进 程 信 息
top–系统当前时间
up:系统运行时长
user:登录当前系统的用户数量
oad average:系统的负载,分别代表平均负载1分钟 5分钟 15 分钟的队列长度
task–任务总量
running:运行进程数量
sleeping:休眠进程数量
stopped:暂停进程数量
zombie:僵死进程数量
%cpu(s)–cpu占用百分比/使用量
us:用户空间,用户占用CPU量
sy:内核空间,内核占用CPU量
ni :nice,进程优先级调整占用的CPU量调整时间
id:空闲时间
wa :等待I/O输入输出时间
hi:处理硬件中断时间
si:处理软件中断时间
st :被虚拟化程序占用的百分比
buff/cache:缓存=buff等待写入磁盘的+cache读取的,都可以被系统回收利用(内存的
Swap:虚拟内存使用量,交换分区占用
PR–优先级
NI :优先级代表的nice值
VIRT:虚拟CPU
RES:常驻内存
SHR:贡献资源
S:状态

top命令的参数

top命令的参数 命令解释
top -d 指定刷新频率
top -b 以批次方式显示
top -n 指定显示的批次数量

top -d 1 指定刷新频率为1s

Insert picture description here
Insert picture description here

top -b 以批次方式显示
top -b -d1 以批次的方式,指定刷新频率为1s进行显示

Insert picture description here

Insert picture description here
top -n 以批次方式显示

top -b -d1 -n 2 以批次的方式,指定刷新频率为1s,批次数量为2进行显示

Insert picture description here
Insert picture description here


top的相关参数解释说明

top内部参数 功能
P cpu百分比排序
M 内存百分比
T 累计占用cpu时间
l 开启/关掉uptime信息
t 开启/关闭task&cpu信息
s 调整刷新频率
k 终止进程
u 查看指定用户进程

3.进程优先级

内核优先级的范围

  • 对Linux而言优先级被划分成固定个数 (0-139),共140个优先级(内核把程序划分为140个队列)
  • 1-99为实时有优先级,数字越大优先级越高,不能人为干预,是内核对于程序本身的控制,系统自用
  • 100-139为静态优先级(共40个),数字越小优先级越高,是人为能干预的

NICE值
NICE值:-20~19对用静态优先级,数字越小优先级越高
普通用户只可以调低优先级,超级用户任意调整

关于优先级的命令 功能
ps   ax  –o nice 查看进程优先级
reince  –n  优先级的值  pid 更改进程优先级
nice   –n  优先级  程序 调整开启进程时的指定优先级

reince  –n  优先级的值  pid :更改进程优先级

Insert picture description here
Insert picture description here
Insert picture description here
nice   –n  优先级  程序:调整开启进程时的指定优先级

Insert picture description here
Insert picture description here

注意:状态的一些说明

stat(状态) 解释说明
S sleeping
< 优先级高
s 顶级进程
T stop
N 优先级低
R running
+ 运行在前台

4.程序的前后台的调用

进程前后台调用相关命令

指令 功能
ctrl+z 把占用shell的进程打入后台挂起
bg 把后台挂起的进程运行起来
fg 把后台进程调回前台
& 运行进程在后台
jobs 查看当前shell中后台所有工作

ctrl+z :把占用shell的进程打入后台挂起

Insert picture description here


jobs : 查看当前shell中后台所有工作|

Insert picture description here


bg+后台运行job编号:把后台挂起的进程运行起来

Insert picture description here

Insert picture description here


fg +后台运行job编号:把后台进程调回前台

Insert picture description here


&:开启时直接在后台进行执行

Insert picture description here


5.进程信息号

进程信息号可以用man 7 signal指令进行查看

Insert picture description here

5.1 进程信号中可控信号类型

可控进程信号 信号含义
1 重新加载系统配置文件(source)
2 中断键盘输入
3 清空内存中的鼠标(ctrl+ \ )
9 强行结束进程(不能被阻塞)
15 正常关闭进程(会被阻塞)
18 运行被暂停的进程
19 暂停进程(不会被系统阻塞)
20 暂停进程(会被系统阻塞的)

1 —重新加载系统配置文件


2 —中断键盘输入

Insert picture description here


3 —清空内存中的鼠标(ctrl+ \ )


9— 强行结束进程(不能被阻塞)

Insert picture description here


15 — shut down the process normally (will be blocked)

Insert picture description here


18— Run the suspended process

Insert picture description here


19—Forcibly pause the process (will not be blocked by the system)
Insert picture description here


20—Pause the process normally (it will be blocked by the system)

Insert picture description here

5.2 Process signal control tool

1.kill process signal pid

Insert picture description here

2.killall process signal process name

Insert picture description here

3.pkill signal process name

Insert picture description here


Guess you like

Origin blog.csdn.net/qq_41537102/article/details/109208257