linux system administration - Process Management

linux system administration - Process Management

First, the basic overview of the process

1. What is the process?

For example: the installation of the windows QQ, we'll procedures called QQ, QQ then when running in Task Manager, we can see QQ program running at this time, we call: QQ process.

Concise summary: When we run a program, the program we call process

Note:
1. When the program runs as a process, the system will allocate memory, as well as the identity and permissions for the running process.
2. In the process of running process, there are a variety of state on the server back to an index of information about the current process.

The process is running instance of an executable program has been started, the process has the following components:

局部和全局变量
当前的调度上下文
分配给进程使用的系统资源,例如文件描述符、网络端口等
给进程分配对应的pid,ppid

2. The difference between the programs and processes?

1. The program is to develop written code, is permanent. A set of data and instructions, is a static concept, such as / bin / ls, / bin / cp and other binary files.

2. The process is the process of running a program, it will terminate the child with the program of destruction, it does not always exist in the system. It is a dynamic concept, the process is the existence of life cycle concepts.

3. The process life cycle

The relationship between the state of the process when the program is running:

1. When the parent process receives task scheduling, will be handled by fork spawn child process, then the child will integrate the mantle of the parent process.
2. The child process when dealing with task code, the parent process will enter a state of waiting ...
3. If the child process in the course of processing tasks, withdrew from the parent process, no child process exits, then the parent process the child process will not be management, it becomes a zombie process.
4. Each process will have its own PID number, (process id) subprocss PPID

Second, the state monitoring process

img

1. Use the pscommand to view the current process status (static view)

Common combination: ps auxview the process

[root@zls ~]# ps aux
a:显示所有与终端相关的进程,由终端发起的
u:显示用户导向的用户列表
x:显示所有与终端无关的进程

In the multi-tasking operating system, each CPU (or core) at one point in time can only handle a process.
In the process runs, its allocation of CPU time and resource requirements will continue to change in order for the process to allocate a state, which along with environmental requirements change.

USER:       //启动程序的用户
PID:        //进程ID
%CPU:       //占用CPU的百分比
%MEM:       //占用内存的百分比
VSZ:        //虚拟内存集(进程占用虚拟内存的空间)
RSS:        //物理内存集(进程占用物理内存的空间)

TTY:        //运行的终端
        ?:      #内核运行的终端
        tty1:   #机器运行的终端
        pts/0:  #远程连接的终端

STAT:       //进程状态
        D:      #无法中断的休眠状态(通IO的进程)
        R:      #正在运行的状态
        S:      #处于休眠的状态
        T:      #暂停或被追踪的状态
        W:      #进入内存交换(从内核2.6开始无效)
        X:      #死掉的进程(少见)
        Z:      #僵尸进程
        <:      #优先级高的进程
        N:      #优先级较低的进程
        L:      #有些页被锁进内存
        s:      #父进程(在它之下有子进程开启着)
        l:      #以线程的方式运行
        |:      #多进程的
        +:      #该进程运行在前台
START:      //进程被触发开启的时间
TIME:       //该进程实际使用CPU的运行时间
COMMAND:    //命令的名称和参数
        []:     #内核态的进程
        没[]:    #用户态的进程

案例一:PS命令查看前台进程转换到停止

#在终端上运行vim
[root@zls ~]# vim zls.txt

#查看vim运行的状态,S:睡眠状态 +:在前台运行
[root@zls ~]# ps aux|grep [v]im
root       1306  0.0  0.2 151664  5180 pts/0    S+   13:00   0:00 vim zls.txt

#执行ctrl + z,将进程放置后台
[1]+  已停止               vim zls.txt

#进程状态变成了T,暂停或被追踪的状态
[root@zls ~]# ps aux|grep [v]im
root       1306  0.0  0.2 151664  5180 pts/0    T    13:00   0:00 vim zls.txt

案例二:PS命令查看不可中断状态

#在终端上运行tar命令
[root@zls ~]# tar zcf zls.tar.gz /etc/ /usr/ /var/ /usr/

#持续查看tar进程的状态
[root@zls ~]# ps aux|grep [t]ar
root       1348 13.3  0.0 124008  1700 pts/0    R+   13:06   0:00 tar zcf zls.tar.gz /etc/ /usr/ /var/ /usr/
[root@zls ~]# ps aux|grep [t]ar
root       1348 13.2  0.0 124008  1716 pts/0    R+   13:06   0:00 tar zcf zls.tar.gz /etc/ /usr/ /var/ /usr/
[root@zls ~]# ps aux|grep [t]ar
root       1348 15.7  0.0 124008  1716 pts/0    S+   13:06   0:00 tar zcf zls.tar.gz /etc/ /usr/ /var/ /usr/
[root@zls ~]# ps aux|grep [t]ar
root       1348 13.6  0.0 124008  1736 pts/0    S+   13:06   0:00 tar zcf zls.tar.gz /etc/ /usr/ /var/ /usr/
[root@zls ~]# ps aux|grep [t]ar
root       1348 15.1  0.0 124008  1736 pts/0    R+   13:06   0:00 tar zcf zls.tar.gz /etc/ /usr/ /var/ /usr/
[root@zls ~]# ps aux|grep [t]ar
root       1348 14.1  0.0 124008  1736 pts/0    D+   13:06   0:00 tar zcf zls.tar.gz /etc/ /usr/ /var/ /usr/
[root@zls ~]# ps aux|grep [t]ar
root       1348 13.2  0.0 124008  1756 pts/0    R+   13:06   0:01 tar zcf zls.tar.gz /etc/ /usr/ /var/ /usr/
[root@zls ~]# ps aux|grep [t]ar
root       1348 15.2  0.0 124140  1756 pts/0    R+   13:06   0:01 tar zcf zls.tar.gz /etc/ /usr/ /var/ /usr/
[root@zls ~]# ps aux|grep [t]ar
root       1348 14.7  0.0 124240  1952 pts/0    S+   13:06   0:01 tar zcf zls.tar.gz /etc/ /usr/ /var/ /usr/

案例三:PS命令查看进程Ss+状态

#过滤bash进程,再开启终端
[root@zls ~]# ps aux|grep [b]ash
root       1127  0.0  0.1 115436  2084 tty1     Ss   11:49   0:00 -bash
root       1180  0.0  0.0 115436  1900 tty1     S+   11:49   0:00 bash
root       1198  0.0  0.1 115564  2152 pts/0    Ss   11:50   0:00 -bash
root       1324  0.0  0.1 115440  2064 pts/1    Ss+  13:01   0:00 -bash

了解进程如下选项:

PID,PPID
当前的进程状态
内存的分配情况
CPU 和已花费的时间
用户UID决定进程的特权

ps命令使用方法

#对进程的CPU进行排序展示
[root@zls ~]# ps aux --sort %cpu |less

#对进程的占用物理内存排序
[root@zls ~]# ps aux --sort rss |less

#排序,实在记不住,那就自己排序
[root@zls ~]# ps aux|sort -k3 -n

#自定义显示字段,指定想看的列
[root@zls ~]# ps axo user,pid,ppid,%mem,command |grep sshd
root        869      1  0.2 /usr/sbin/sshd -D
root       1194    869  0.2 sshd: root@pts/0
root       1307    869  0.2 sshd: root@pts/1
root       1574    869  0.2 sshd: root@pts/2

#显示进程的子进程
[root@zls ~]# yum install nginx -y
[root@zls ~]# systemctl start nginx
[root@zls ~]# ps auxf|grep [n]ginx
root       2033  0.0  0.1 125096  2112 ?        Ss   13:29   0:00 nginx: master process /usr/sbin/nginx
nginx      2034  0.0  0.1 125484  3148 ?        S    13:29   0:00  \_ nginx: worker process

#默认不加选项是查看指定进程PID
[root@zls ~]# ps aux|grep sshd
root       1157  0.0  0.1 105996  3604 ?        Ss   Feb27   0:00 /usr/sbin/sshd -D
[root@zls ~]# cat /run/sshd.pid
1157

#pgrep常用参数, -l -a
[root@zls ~]# pgrep sshd
869
1194
1307
1574
[root@zls ~]# pgrep -l sshd
869 sshd
1194 sshd
1307 sshd
1574 sshd
[root@zls ~]# pgrep -l -a sshd
869 /usr/sbin/sshd -D
1194 sshd: root@pts/0
1307 sshd: root@pts/1
1574 sshd: root@pts/2

#查看进程的pid
[root@zls ~]# pidof sshd
1574 1307 1194 869

#查看进程树 
[root@zls ~]# pstree
systemd─┬─NetworkManager───2*[{NetworkManager}]
        ├─VGAuthService
        ├─abrt-watch-log
        ├─abrtd
        ├─agetty
        ├─auditd───{auditd}
        ├─crond
        ├─dbus-daemon───{dbus-daemon}
        ├─irqbalance
        ├─master─┬─pickup
        │        └─qmgr
        ├─nginx───4*[nginx]
        ├─polkitd───6*[{polkitd}]
        ├─rsyslogd───2*[{rsyslogd}]
        ├─sshd─┬─sshd───bash───pstree
        │      └─sshd───bash───bash───bash
        ├─systemd-journal
        ├─systemd-logind
        ├─systemd-udevd
        ├─tuned───4*[{tuned}]
        ├─vmtoolsd───{vmtoolsd}
        └─vsftpd

2. 动态监控进程--top 命令

img

img

[root@gong ~]# top
top - 22:58:05 up  4:37,  3 users,  load average: 0.00, 0.01, 0.05
Tasks:  98 total,   1 running,  97 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  2047488 total,  1566920 free,   116060 used,   364508 buff/cache
KiB Swap:        0 total,        0 free,        0 used.  1763644 avail Mem 


#当前系统的时间
22:58:05

#开启时间
up  4:37,  

#几个用户同时在线
3 users,  

#平均负载:1分钟,5分钟,15分钟
load average: 0.00, 0.01, 0.05

#总共工作数量98个
Tasks:98 total,   

#1个正在处理
1 running,  

#97个S状态
97 sleeping,   

#00个停止状态
2 stopped,   

#0个僵尸进程
0 zombie


%Cpu(s): 
#用户态:用户占用CPU的百分比
0.0 us,  

#内核态:系统程序占用CPU的百分比(通常内核与硬件进行交互)
0.0 sy,  

dd </dev/zero >/dev/null bs=200M count=1000

#优先级:优先被调度的程序占用CPU百分比
0.0 ni, 

#空闲:CPU空闲的百分比(windows也有)
99.7 id,  

#等待:CPU等待IO的完成时间
0.0 wa,  

#硬中断:占CPU的百分比
0.0 hi,
由与系统相连的外设(比如网卡、硬盘)自动产生的。主要是用来通知操作系统,系统外设状态的变化。比如当网卡收到数据包的时候,就会发出一个中断。我们通常所说的中断指的是硬中断(hardirq)。

#软中断:占CPU的百分比
0.0 si,
为了满足实时系统的要求,中断处理应该是越快越好。linux为了实现这个特点,当中断发生的时候,硬中断处理那些短时间就可以完成的工作,而将那些处理事件比较长的工作,放到中断之后来完成,也就是软中断(softirq)来完成。  

#虚拟机占用物理机的百分比
0.0 st

3.中断

硬中断是系统用来影响硬件设备请求的一种机制,它会打断进程的正常调度和执行,然后调用内核中的中断处理程序来影响设备的请求

举个例子:

比如你定了一份外卖,但是不确定外卖什么时候送到,也没有别的方法了解外卖的进度,但是配送人员送外卖是不等人的,到了你这,没人接取的话,直接走人了。所以你只能苦苦的等着,时不时的去门口看看外卖送到没有,而不能做其他的事情。

不过如果在订外卖的时候,你就跟配送员约定好了,让他送到给你打电话,那你就不用苦苦等着了,可以去忙别的事情了,直到电话一响,接到电话,就可以取外卖了。此时 ==打电话== 就是一个中断的操作。

没接到电话之前,你可以做其他事情,当你接到电话之后(就发生了中断),你才要进行另一个动作**取外卖**

PS:中断是一个异步的事件处理机制,可以提高操作系统处理并发的能力。

由于中断处理程序会打断其他进程的运行,所以,为了减少对正常进程运行调度的影响,中弄断处理程序就需要尽可能快的运行,如果中弄断本身要做的事情不多,那么处理起来也不会有太大的问题,但是如果中断要处理的事情很多,中断服务程序就有可能要运行很长时间。

特别是,中断处理程序在影响中断时,还会临时关闭中断,这就会导致上一次中断处理完成之前,其他中断都能不能响应,也就是说中断有可能会丢失。

还是以外卖为例:加入你定了2份外卖

一份主食和一份饮料,由2个不同的配送员来配送。这次你不用时时等待着,两份外卖都约定了电话取外卖的方式。那么问题又来了。

当第一份外卖送到时,配送员给你打了个很长的电话,商量发票处理的方式,与此同时,第二个配送员也到了,也想给你打电话,但是会占线,因为电话占线(也就关闭了中断的响应),第二个配送给你打电话打不通,所以,那么很有可能在尝试几次还占线,就走了(丢失了一次中断)

刚才说了丢失一次中断,如果对于系统来说,每次都只能处理一次中断,那就很刺激了,天天都在丢失中断,用户的请求发过来,没响应,还做个P的运维,回家种地吧...

img

img

软中断:

事实上,为了解决中断处理程序执行过长的和丢失中断的问题,Linux将中断处理过程分成了两个阶段:
第一阶段:用来快速处理中断,它在中断禁止模式下运行,主要处理跟硬件紧密相关工作
第二阶段:用来延迟处理第一阶段未完成的工作,通常以内核线程的方式运行。

还是外卖的那个例子:

第一阶段:当你接到第一个配送员电话时,你可以跟他说,你已经知道了,其他事见面再细说,然后就可以挂断电话了。
第二阶段:才是取外卖,然后见面聊发票的处理动作。
如此一来,第一个配送员不会在电话里占用你很长时间,第二个配送员来的时候,照样可以打通电话。

当网卡在接收数据包的时候,会通过硬中断的方式通知内核,有新数据到了。这时,内核就应该调用中断处理程序来影响它。对第一阶段来说,既然是快速处理,其实就是把网卡接收到的数据包,先放置内存当中,然后更新一下硬件寄存器的状态(表示数据已经读好了),而第二阶段,被软中断信号唤醒后,需要从内存中找到网络数据,再按照网络协议栈,对数据进行逐层解析和处理,直到把它发送给应用程序。

Summary:
The first stage: direct request processing hardware, that is, we often say that the hardware interrupt, is characterized by rapid implementation.
Phase II: The request triggered by the kernel, that is, we often say that the soft interrupt, is characterized by delayed execution.

4.Linux hard and soft interrupt interrupted Summary:

1.Linux the interrupt handler is divided into upper and lower halves:
the upper half of the corresponding hardware interrupt for fast processing of
the lower half of the corresponding soft interrupt, to handle the asynchronous half unfinished work

The soft interrupt 2.Linux comprising: a network transceiver types, timing, scheduling, etc., can /proc/softirqsbe observed in the operation of the interrupt

In business, will often hear a question, is the large number of network packets can lead to performance problems, so why then?

因为大量的网络小包会导致频繁的硬中断和软中断,所以大量的网络小包传输速度很慢,但如果将所有的网络小包"打包","压缩"一次性传输,是不是会快很多。

就好比,你在某东自营买了100个快递,都是第二天到,如果分100个快递员,给你配送,你一天要接100个电话,~~~~~~~~ 但是如果,某东只让一个快递员,把你买的100个快递,打包成一个大包裹,派送给你,会不会快很多?

top command:

[root@zls ~]# top
#指定N秒变化时间
[root@zls ~]# top -d 1
#查看指定进程的动态信息
[root@zls ~]# top -d 1 -p 10126
[root@zls ~]# top -d 1 -p 10126,1
#查看指定用户的进程
[root@zls ~]# top -d 1 -u apache
#将 2 次 top 信息写入到文件
[root@zls ~]# top -d 1 -b -n 2 > top.txt

top 常见指令
h   查看帮出
z   高亮显示
1   显示所有CPU的负载
s   设置刷新时间
b   高亮现实处于R状态的进程
M   按内存使用百分比排序输出
P   按CPU使用百分比排序输出
R   对排序进行反转
f   自定义显示字段
k   kill掉指定PID进程
W   保存top环境设置 ~/.toprc
q   退出

#进程ID
PID
#用户
USER
#优先级,正常为20
PR  
#nice值,正常为0,负值表示高优先级,正值表示低优先级
NI    
#虚拟内存占用
VIRT
#真实内存占用
RES
#共享内存占用
SHR
#模式状态
S
#CPU占用百分比
%CPU
#内存占用百分比
%MEM
#运行时间
TIME+
#运行命令
COMMAND

5.kill signal management:

When the program runs as a process, if you want to forcibly stop can be sent to the process using the kill command 关闭信号, in addition to kill there pkill, killall

Role Definitions daemon
end user sessions and processes
kill,killall,pgrep,pkill

[root@zls ~]# kill -l //列出所有支持的信号

//常见信号列表:
数字信号        信号别名        作用
1       HUP     挂起信号,往往可以让进程重新配置
2       INT     中断信号,起到结束进程的作用,和ctrl + c 的作用一样
3       QUIT        让进程退出,结果是进程退出
9       KILL        直接结束进程,不能被进程捕获
15      TERM        进程终止,这是默认信号
18      CONT        被暂停的进程将继续恢复运行
19      STOP        暂停进程
20      TSTP        用户停止请求,作用类似于ctrl + z 把进程放到后台并暂停

6.kill command sends signal

// 给 vsftpd 进程发送信号 1,15
[root@zls ~]# yum -y install vsftpd
[root@zls ~]# systemctl start vsftpd
//发送重启信号,例如 vsftpd 的配置文件发生改变,希望重新加载
[root@zls ~]# kill -1 9160
//发送停止信号,vsftpd 服务有停止的脚本 systemctl stop vsftpd
[root@zls ~]# kill 9160


// 给vim进程发送信号 9,15
[root@zls ~]# touch file1 file2 
//使用远程终端1打开file1
[root@zls ~]# tty
/dev/pts/1
[root@zls ~]# vim file1
//使用远程终端2打开file2
[root@zls ~]# tty 
/dev/pts/2
[root@zls ~]# vim file2
//查看当前进程pid
[root@zls ~]# ps aux |grep vim
root 4362 0.0 0.2 11104 2888 pts/1 S+ 23:02 0:00 vim file1 
root 4363 0.1 0.2 11068 2948 pts/2 S+ 23:02 0:00 vim file2
//发送15信号
[root@zls ~]# kill 4362
//发送9信号
[root@zls ~]# kill -9 4363
//还可以同时给所有vim进程发送信号, 模糊匹配,同时给多个进程发送信号
[root@zls ~]# killall vim


//使用pkill踢出从远程登录到本机的用户, pkill  类似killall
[root@zls ~]# w
 20:50:17 up 95 days,  9:30,  1 user,  load average: 0.00, 0.00, 0.00
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
xuliangw pts/0    115.175.115.39   20:22    0.00s  0.01s  0.00s sshd: zls [priv]

//终止 pts/0上所有进程, 除了bash本身
[root@zls ~]# pkill -t pts/0

-t:指定终端
//终止pts/0上所有进程, 并且bash也结束(用户被强制退出)
[root@zls ~]# pkill -9 -t pts/0

//列出zls用户的所有进程,-l输出pid
[root@linux-zls ~]# pgrep -l -u zls
32206 sshd
32207 bash

Guess you like

Origin www.cnblogs.com/gongjingyun123--/p/11228308.html