Process management

Process concept 

Programs are generally placed on disk and triggered by user execution. After starting, it will be loaded into memory to become an individual, that is, a process.
The function of the kernel: process management, file system, network functions, memory management, drivers, security functions, etc.
Process: A copy of the running program is an instruction set that is loaded into the memory Process ID (Process ID, PID) number It is used to mark each process
UID, GID, and SELinux context to determine access and access rights to the file system, usually inherited from the user who executes the process
Existing life cycle
task struct: The Linux kernel stores process information in the data structure format
task list : Linked list composed of task structs of multiple tasks
Process creation: init: parent-child relationship of the first process
Process : all created by its parent process, CoW fork(), clone()
process : a program running on a data set Process. It is the basic unit of operating system resource allocation.
Thread : It is an entity in the process and the basic unit that is independently scheduled and executed by the operating system. A process contains one or more threads.
A job is a task entity that submits tasks to a computer,
while a process is an execution entity and the basic unit of resource allocation, and a
thread is the basic unit of processor scheduling.
Process characteristics  

         Dynamic: The essence of the process is the one-time execution of the program in the multi-program system. The process is dynamically generated and dynamically dies.
   Concurrency: any process can execute concurrently with other processes
   Independence: a process is a basic unit that can run independently, and it is also an independent unit for the system to allocate resources and schedule;
   asynchrony: Due to the mutual constraints between processes, a process has Intermittent execution, that is, the process advances at an independent and unpredictable speed.
   Structural characteristics: The process consists of three parts: program, data and process control block.
  Multiple different processes can contain the same program: a program constitutes different processes in different data sets and can get different results; but the program cannot be changed during execution.
The state and transition
of the process The discontinuity of the execution of the process determines that the process may have multiple states. In fact, a running process may have the following three basic states. 1) Ready state (Ready) :   The process has obtained the required resources except the processor and is waiting to allocate processor resources; the process can be executed as long as the processor is allocated. Ready processes can be queued by multiple priorities. For example, when a process enters the ready state due to the expiration of the time slice, it is enqueued into the low-priority queue; when the process enters the ready state due to the completion of the I/O operation, it is enqueued into the high-priority queue. 2) Running state (Running):  The process occupies processor resources; the number of processes in this state is less than or equal to the number of processors. When there are no other processes to execute (such as all processes are blocked), the system's idle process is usually executed automatically.  3) Blocked state (Blocked):  Because the process waits for a certain condition (such as I/O operation or process synchronization), it cannot continue execution until the condition is met. Even if the handler is assigned to the process before the event occurs, it cannot run.
  

  

 

 

 

Process state five-state model:

运行状态(Running):当一个进程正在处理机上运行时。
就绪状态(Ready):一个进程获得了除处理机之外的一切所需资源,一旦得到处理机即可运行.
等待状态又称阻塞状态(Blocked):一个进程正在等待某一事件而暂停运行时。如等待某资源,等待输入/输出完成。
创建状态(NEW):一个进程正在被创建,还没被转到就绪状态之前的状态。
结束状态(Exit):一个进程正在从系统中消失时的状态,这是因为进程结束或由于其他原因所导致。

 

状态之间转换六种情况

运行——>就绪:1,主要是进程占用CPU的时间过长,而系统分配给该进程占用CPU的时间是有限的;2,在采用抢先式优先级调度算法的系统中,当有更高优先级的进程要运行时,该进程就被迫让出CPU,该进程便由执行状态转变为就绪状态。
就绪——>运行:运行的进程的时间片用完,调度就转到就绪队列中选择合适的进程分配CPU
运行——>阻塞:正在执行的进程因发生某等待事件而无法执行,则进程由执行状态变为阻塞状态,如发生了I/O请求
阻塞——>就绪:进程所等待的事件已经发生,就进入就绪队列
以下两种状态是不可能发生的:
阻塞——>运行:即使给阻塞进程分配CPU,也无法执行,操作系统在进行调度时不会从阻塞队列进行挑选,而是从就绪队列中选取
就绪——>阻塞:就绪态根本就没有执行,谈不上进入阻塞态

查看进程ps [OPTION]...

支持三种选项:
UNIX选项 如-A -e
BSD选项 如a
GNU选项 如--help
• 选项:默认显示当前终端中的进程
• a 选项包括所有终端中的进程
• x 选项包括不链接终端的进程
• u 选项显示进程所有者的信息
• f 选项显示进程树,相当于 --forest
• k|--sort 属性 对属性排序,属性前加- 表示倒序
• o 属性… 选项显示定制的信息 pid、cmd、%cpu、%mem
• L 显示支持的属性列表
ps常见选项
-C cmdlist 指定命令,多个命令用,分隔
-L 显示线程
-e: 显示所有进程,相当于-A
-f: 显示完整格式程序信息 -F: 显示更完整格式的进程信息
-H: 以进程层级格式显示进程相关信息
-u userlist 指定有效的用户ID或名称
-U userlist 指定真正的用户ID或名称
-g gid或groupname 指定有效的gid或组名称
-G gid或groupname 指定真正的gid或组名称
-p pid 显示指pid的进程
--ppid pid 显示属于pid的子进程
-M 显示SELinux信息,相当于Z
ps输出属性
STAT:进程状态
R:running
S: interruptable sleeping
D: uninterruptable sleeping
T: stopped
Z: zombie
+: 前台进程
l: 多线程进程
L:内存分页并带锁
N:低优先级进程
<: 高优先级进程
s: session leader,会话(子进程)发起者
ps常用组合:
aux
-ef
-eFH
-eo pid,tid,class,rtprio,ni,pri,psr,pcpu,stat,comm
axo stat,euid,ruid,tty,tpgid,sess,pgrp,ppid,pid,pcpu,comm
ps示例

查询你拥有的所有进程: ps -x
显示指定用户名(RUID)或用户ID的进程: ps -fU apache ps -fu 4
显示指定用户名(EUID)或用户ID的进程: ps -fu wang ps -fu 1000 
查看以root用户权限(实际和有效ID)运行的每个进程: ps -U root -u root
列出某个组拥有的所有进程(实际组ID:RGID或名称): ps -fG nginx
列出有效组名称(或会话)所拥有的所有进程: ps -fg mysql ps -fG 27
通过进程ID来显示所属的进程: ps -fp 1234
以父进程ID来显示其下所有的进程,如显示父进程为1154的所有进程: ps -f --ppid 1234 
显示指定PID的多个进程: ps -fp 1204,1239,1263
要按tty显示所属进程: ps -ft pst/0 ps示例
以进程树显示系统中的进程如何相互链接: ps -e --forest
以进程树显示指定的进程 ps -f --forest -C sshd ps -ef --forest | grep -v grep | grep sshd
要显示一个进程的所有线程,将显示LWP(轻量级进程)以及NLWP(轻量级进 程数)列: 
ps -fL -C nginx
要列出所有格式说明符: ps L
查看进程的PID,PPID,用户名和命令: ps -eo pid,ppid,user,cmd
使用其PID查找进程名称: ps -p 1244 -o comm= 
要以其名称选择特定进程,显示其所有子进程: ps -C sshd,bash

 

pstree:显示进程树

pidof:查询指定进程的PID

pgrep:搜索指定的进程

  • -u uid: effective user,生效者
  • -U uid: real user,真正发起运行命令者
  • -t terminal: 与指定终端相关的进程
  • -l: 显示进程名
  • -a: 显示完整格式的进程名
  • -P pid: 显示指定进程的子进程

uptime:查看系统的基本负载状态

top:实时监控系统状态工具

交互按键:

  • M:按内存使用排序
  • P:按CPU使用排序
  • l:显示、隐藏top第一行
  • t:改变/隐藏CPU的显示方式
  • m:改变/隐藏内存的显示方式
  • 1:单CPU和总CPU
  • s:改变刷新间隔,默认3s
  • k:杀死进程
  • PID -- signal(15 关闭进程)
  • q:退出
  • W:将状态保存到~/.roprc

选项:

  • -d #:指定时间间隔,秒
  • -b:显示所有进程的状态
  • -n #:刷新#次自动退出
复制代码
[root@rhel6 ~]# top -n1 -b
top - 03:06:25 up 10:41,  1 user,  load average: 0.00, 0.01, 0.05
top - 系统时间 up 启动了多长时间 用户 负载:(1,5,10)分钟的平均负载 Tasks: 132 total, 1 running, 131 sleeping, 0 stopped, 0 zombie
总进程数 正在运行进程数 睡眠进程数 停止态进程数 僵尸进程数 Cpu(s): 0.2%us, 0.5%sy, 0.0%ni, 99.2%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu(s):用户空间使用率 系统空间使用率 nice值 空闲率 等待率 硬中断率 软中断率 被偷走的时间(分配给虚拟机中的进程的时间片) Mem: 1004136k total, 452340k used, 551796k free, 40868k buffers Swap: 2097148k total, 0k used, 2097148k free, 263940k cached
总内存大小 已使用 空闲内存 缓存使用 PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 1 root 20 0 19356 1552 1232 S 0.0 0.2 0:00.89 init 2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd 3 root RT 0 0 0 0 S 0.0 0.0 0:00.00 migration/0
复制代码

htop(epel):top的增强版,界面更友好,支持鼠标点击

iotop:用来监视磁盘I/O使用状况

选项:

  • -o:只看产生IO的进程
  • -b:非交互式
  • -n #:检测#次,默认为一直检测
  • -d #:检测间隔#秒
  • -p PID:检测指定PID的进程
  • -u:检测指定用户产生IO的情况
  • -P:仅显示进程,默认显示线程
  • -a:显示累计的IO量
  • -k:使用KB为单位
  • -t:显示时间戳
  • -q 只在第一次监测时显示列名
  • -qq 永远不显示列名
  • -qqq 永远不显示I/O汇总

交互按键:

  • left和right方向键:改变排序
  • r:反向排序
  • o:切换至选项-o
  • p:切换至-p选项
  • a:切换至-a选项
  • q:退出
  • i:改变线程的优先级
Total DISK READ: 673.43 M/s | Total DISK WRITE: 0.00 B/s
  TID  PRIO  USER     DISK READ  DISK WRITE  SWAPIN     IO>    COMMAND         
线程ID(按p可切换PID) 优先级 用户 读速度 写速度 swap所占率 IO等待率 进程名 15330 be/4 root 673.43 M/s 0.00 B/s 0.00 % 2.07 % dd if=/dev/sda of=/dev/null 1 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % init

nice -n nice值 CMD:指定运行时的nice值

renice -n nice值 CMD:调整运行中进程的nice值

vmstat:查看虚拟内存状态

  • -s: 显示内存的统计数据
[root@centos7 ~]# vmstat
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 2  0      0 574860   2080 260516    0    0   105    37  165  179  0  1 99  0  0
  • procs
    • r:可运行(正运行或等待运行)进程的个数
    • b:处于不可中断睡眠态的进程个数(被阻塞的队列的长度)
  • memory
    • swpd: 交换内存的使用总量
    • free:空闲物理内存总量
    • buffer:用于buffer的内存总量
    • cache:用于cache的内存总量
  • swap
    • si:从磁盘交换进内存的数据速率(kb/s)
    • so:从内存交换至磁盘的数据速率(kb/s)
  • io
    • bi:从块设备读入数据到系统的速率(kb/s)
    • bo: 保存数据至块设备的速率
  • system
    • in: interrupts 中断速率,包括时钟
    • cs: context switch 进程切换速率
  • cpu
    • us:花在运行非内核代码上的时间
    • sy:花在运行内核代码上的时间
    • id:闲置的时间
    • wa:等待IO的时间
    • st:被虚拟机盗取的时间

iostat:统计CPU和设备IO信息

dstat:系统资源统计工具

  • -c: 显示cpu相关信息
  • -d: 显示disk相关信息
  • -g:显示page相关统计数据
  • -m: 显示memory相关统计数据
  • -n: 显示network相关统计数据
  • -p: 显示process相关统计数据
  • -r: 显示io请求相关的统计数据
  • -s: 显示swapped相关的统计数据
  • --tcp:显示tcp相关统计数据
  • --udp:显示udp相关统计数据 
  • --unix:显示unix-sockets相关统计数据 
  • --raw:显示raw相关统计数据
  • --socket:显示socket相关统计数据
  • --ipc:显示ipc相关统计数据
  • --top-cpu:显示最占用CPU的进程
  • --top-io: 显示最占用io的进程
  • --top-mem: 显示最占用内存的进程
  • --top-latency: 显示延迟最大的进程

pmap PID:查看进程对应的内存映射  ==  cat /proc/PID/maps

  • -x:显示详细信息

glances(epel):调用远程主机上的系统运行状态信息

[root@centos7 ~]# yum -q -y install glances #安装glances软件包
[root@centos7 ~]# glances -s -B 192.168.0.7 & #启动服务并监听端口
[root@centos7 ~]# glances -c 192.168.0.7 #客户端连接

释放buff/cache空间

echo 1 > /proc/sys/vm/drop_caches 

kill:向进程发送控制信号

  • -n # PID:向进程发信号
  • %作业号:结束作业
  • -l:查看所有信号

常用信号:man 7 signal

  • 1:SIGHUP 让进程重读配置文件
  • 2:SIGINT 终止信号
  • 3:SIGQUIT ctrl+\,退出信号
  • 9:SIGKILL 强制退出
  • 15:SIGTERM 正常退出,kill默认发送15信号
  • 18:SIGCONT 继续运行
  • 19:SIGSTOP 停止信号

pkill -n #:向进程发送信号,支持模式匹配,用法和pgrep相似

  • -u uid: effective user,生效者
  • -U uid: real user,真正发起运行命令者
  • -t terminal: 与指定终端相关的进程
  • -P pid: 显示指定进程的子进程

killall -n # CMD:向进程发送指定信号

jobs:显示当前终端所有的作业

g [[%]JOB_NUM]:把指定的后台作业调回前台

bg [[%]JOB_NUM]:让送往后台的作业在后台继续运行

当我们在执行一个时间比较长的前台任务(如:压缩大文件,编译内核等),如果途中网络中断会导致我们的任务执行中断,此时为了避免这种情况建议采取以下操作:

  1. nohup CMD &>/dev/null 剥离与终端的关系,如果终端关闭,进程由init(systemd)接管
  2. screen:使用Ctrl + a,d剥离会话,-r恢复会话

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325486480&siteId=291194637