[Make it clear, to say it! Process Management]

Contents:
(a) understand the concept of process
(b) how to view the process && kill the process
(c) placing the program running in the background
(four) understand the process priority
(e) adjustment process priority


(A) understand the process of concept
(1.1) have more background concept in our system, the first "program" refers to the use of a language developed out of a piece of code, it is still stored on our computer's hard drive program, it is neither consume CPU resources, not consume memory resources. The second is the "process", when a program running in memory and CPU will be reflected in, for example, QQ, Thunder and other applications running on our computer, called process. The third is the "thread", the thread belonging to a unit in the process, in a process can contain multiple threads that share memory space of the process.
(1.2) we know that the system each user has its own ID number, referred to as UID, the same process each page has its own ID number, process id referred to PID. In the process of running the process may also produce a child process, such as A process running into over-run after a period of time will produce a B process, then we call the process A parent process, called process B for the child, the parent process may generate a child process, it may kill a child process, so will the communication between parent and child processes, we call this communication called "signals"
[Make it clear, to say it!  Process Management]


(B) how to view the process && kill the process
(2.1) pstree command
(2.1.1) We begin to see the parent-child relationship between processes, we can use pstree command, host it on vms002 can check out the system in all processes parent-child relationship, where the system starts parent of all processes is systemd, its PID is 1, and the parent rhel6 previous versions and, all systems are init.
[Make it clear, to say it!  Process Management]
(2.1.2) pstree process to display a tree diagram to show only the name of the process, and the same process of consolidation shows that we can progress to show the tree view, and displays the process PID
# pstree -p --- a tree diagram display process and display process PID
# pstree -p | More
[Make it clear, to say it!  Process Management]
(2.2) of the parent process will periodically send a signal to the child process, then the signal system, what does? We can use the "# kill -l" command to list all of the signal list system, in which 1) SIGHUP representing pending, that is to restart, if we want to restart a service, but do not want to change the ID number, you can use 1 no signal; 2) SIGINT representation generally, we use the "Ctrl + C" to terminate the process; and 15) SIGTERM indication conventional manner terminate the process, for example, we use the kill command without any parameters to terminate a process when the default is to use the No. 15 signal operation, but the power is not strong enough, the process for the abnormal state is not terminated; this time we use 9) SIGKILL signal that the forced termination of a process, power is also relatively strong.
# Kill -l --- list all the signal list
# kill -2 29626 --- use "Ctrl + C" way to terminate 29626 process
[Make it clear, to say it!  Process Management]
mode (2.3) to kill the process
(2.3.1) if we want to start the process, such as running a firefox, how should we view the PID of it? First, we can use the pgrep command, and then directly add the name of the program you can check out the PID of the (Figure 2-3), then use the kill command to kill abnormal processes the query on it. Of course, we may be combined into one step of two steps (FIG. 2-4).
# Pgrep firefox --- query firefox process PID number
# kill -9 $ (pgrep firefox) --- forced to kill the firefox process
# kill -9 pgrep firefox--- forced to kill the firefox process
[Make it clear, to say it!  Process Management]
[Make it clear, to say it!  Process Management]
(2.3.2) except, of course kill command we can also use killall command, directly to keep up with the program name.
# Killall firefox --- kill the firefox process
[Make it clear, to say it!  Process Management]
(2.3.3) We can also use pkill, which pkill can be implemented in a user kicked out of the system functionality. First, we use jerry vms002 users log on to the host (Figure 2-5-1), then we find that the system is the presence of jerry user logged in, we use the pkill command to the user kicked out of jerry (Figure 2-5-2), this when we use jerry administrator delete users and their home directories when they can smooth operation of the (Figure 2-5-3).
Pkill -kill -t PTS # / 1
# userdel -r Jerry
[Make it clear, to say it!  Process Management]
[Make it clear, to say it!  Process Management]
[Make it clear, to say it!  Process Management]
(2.4) to see the process we can also use the command pidof, but we need to find pidof command with the specific name of the process, there are inconsistencies if the process name queries are can not check out, compared to pgrep command, you can use pgrep fuzzy query of queries, and therefore more efficient.
# Pidof firefox --- query firefox process ID
[Make it clear, to say it!  Process Management]
(2.5) ps options and significance of the column
(2.5.1) If we want to query the system of all the process, you can use the ps command, if we just use the ps command is only a query when the terminal under its own in the current shell process several processes only, and you can not see the other end in the process, can not see the background process system. So this time we want to query the system to all processes, you can use the following query in two ways:
# PS -ef --- use Linux-style fashion show system daemons
# ps aux --- way to use Unix-style display system background processes
[Make it clear, to say it!  Process Management]
[Make it clear, to say it!  Process Management]
(2.5.2) then we have to look at the "# pa aux" command on behalf of the meaning of each column.
# Ps aux | information 10 lines of inquiry head head ---
USER: process owner
PID: ID process
PPID: parent
% CPU: the process takes a percentage of the CPU
% MEM: memory-intensive percentage
NI: NICE process value , great value, represents less CPU time
VSZ: virtual memory set (KB) used by the process
RSS: a fixed amount of memory consumed by this process (KB) (in number of resident pages)
TTY: the process on that terminal run (Lander end position), if independent of the terminal, is a question mark, if it is pts / 0, etc., is represented by a network connected host process (?)
WCHAN: whether the current process is under way, if it is "-" means being were
sTART: the start time of the process is triggered
tIME: the process actually using CPU time to run
cOMMAND: command name and parameters of
(2.5.3) we take a look at the meaning of the relevant state STAT column.
D: uninterruptible sleep (usually IO process)
R: Running in the queue executable state
S: lies dormant
T: Stop or tracking
W: enter Memory exchange (invalid from the beginning of a kernel 2.6)
the X-: dead process (basic rarely)
the Z-: zombie
<: higher-priority process
N: lower-priority processes
L: some pages are locked into memory
s: process leader (under it child process)
L: (use CLONE_THREAD, similar NPTL pthreads) multi-process
+: in the background of the process group
[Make it clear, to say it!  Process Management]
(2.6) ps command other uses
(2.6.1) we can use to query the current system lscpu cpu usage (Figure 3 -6), our system now runs an application firefox, we want to query this particular program is run on which CPU the current system, we can use the ps command to do the following query (Figure 3-7).
Lscpu #
# ps Mo pid, COMM, PSR $ (pgrep Firefox) --- Firefox processes the query and its threads run on which CPU, psr indicate which pieces to run in the CPU
[Make it clear, to say it!  Process Management]
[Make it clear, to say it!  Process Management]
(2.6.2) if we want to specify a specific query ps command which columns of information, you can use the following query mode:
# ps -o pid -ex, the CPU%,% MEM | head --- ps command to query information on specific columns
[Make it clear, to say it!  Process Management]


(三)把程序放置后台运行
(3.1)使用nohup
(3.1.1)当父进程关闭的时候,它所对应的子进程全部跟着关闭。我们在vms002主机上首先打开一个firefox进程,使用pstree命令发现,firefox是运行在第三个终端bash之上的。
# pstree | grep -C2 firefox
# ps aux | grep -v grep | grep firefox
[Make it clear, to say it!  Process Management]
[Make it clear, to say it!  Process Management]
(3.1.2)当我们将第三个终端关闭之后,会发现在终端中创建的firefox应用程序也自动关闭了。
# ps aux | grep -v grep | grep firefox
[Make it clear, to say it!  Process Management]
(3.1.3)但是很多时候我们会通过远程连接的方式执行脚本,如果执行一个脚本需要10个小时,是否是说我们需要将终端保持连接10个小时不关闭?因此为了解决这个问题,我们需要将运行的进程放置到后台运行(图3-4),此时我们发现关闭了所有的终端后firefox仍然能够正常的运行。
# nohup firefox &> /dev/null &---我们创建一个firefox并放置到后台运行
# ps aux | grep -v grep | grep firefox---查询当前firefox的运行状态
# pstree | grep -C2 firefox---使用pstree查询firefox的运行状态
注意:grep -v grep表示的是反向过滤,不去搜索grep命令后包含的字段
[Make it clear, to say it!  Process Management]
[Make it clear, to say it!  Process Management]
(3.2)使用jobs
(3.2.1)当我们在vms002的终端直接运行一个firefox进程后,发现此时终端就无法再运行其他的命令了,此时我们可以使用键盘“Ctrl+Z”将firefox程序停止并放到后台。
[Make it clear, to say it!  Process Management]
(3.2.2)如果我们希望查询在后台的程序状态信息,可以使用jobs命令查看。如果我们想将停止的firefox程序放在后台激活运行,可以使用“# bg 1”命令。
# jobs---查询进程的状态
# bg 1---将1号jobs激活并放在后台运行
[Make it clear, to say it!  Process Management]
(3.2.3)如果我们希望将后台运行程序调到前台来,则可以使用“# fg 1”命令,如果我们想终止运行的程序,则可以使用“Ctrl+C”命令。如果我们在运行程序的时候想直接运行在后台,则应该使用“# firefox &”这种方式即可,如果我们想结束进程则可以使用kill命令,其中1号jobs使用“%1”表示。
注意:虽然我们将firefox放置到后台运行了,但是此时firefox仍然是在当前的终端上运行的,即如果当前终端关闭了那么此进程也会被关闭,这个要和nohup区分(图3-12)。
# fg 1---希望将后台运行程序调到前台来运行
# firefox &---运行程序的时候想直接运行在后台
# kill -9 %1---结束进程
# ps aux | grep -v grep | grep firefox
[Make it clear, to say it!  Process Management]
[Make it clear, to say it!  Process Management]


(四)了解进程优先级
(4.1)在我们系统中所有的任务都是由CPU来完成的,在系统中如果一颗CPU有多颗核,那么我们认为就是有多颗CPU。在计算机中我们是可以一次性执行多个任务的,同时一颗CPU也是可以同时执行多个任务的。对于CPU来说,是分为时间片执行的,例如现在系统中有多个任务,那么CPU会对每个任务分片执行,假设每个时间片的时长是1ms,由CPU轮询的在每个任务之间循环执行,由于时间片循环的速度非常快,因此一般我们在系统中操作的时候是感觉不到有时间片在轮询的。有时候我们会感觉系统中某些任务是非常重要的,需要CPU优先执行,此时我们是可以在系统中调整进程的优先级的。优先级的组成如下(图4-1),一般来说,使用chrt命令可以修改优先系数,不过一般不建议修改(图4-2),而nice值的范围是“-20~19”之间,nice值越小优先级越高,nice值越大优先级越低。我们在图形化界面中可以调整进程的优先级的,我们选择“应用程序”--“系统工具”--“系统监视器”,然后便可以在图形化界面中修改进程的优先级了。
[Make it clear, to say it!  Process Management]
[Make it clear, to say it!  Process Management]
[Make it clear, to say it!  Process Management]
(4.2)如果我们的系统现在有两个进程,同时有两颗CPU,如果一个CPU上单独运行一个进程,那么对这两个进程设置进程优先级是没有意义的,但是我们希望能出现抢占资源这种情况,因此我们需要把两个进程放到一颗CPU上去运行。我们想要实现这样的效果,一般可以采用两种方式,第一种是关闭掉其中的一颗CPU,第二种方式是配置CPU的亲和性。


(五)调整进程优先级
(5.1)当前系统我们有两颗CPU,首先我们通过关闭其中的一颗CPU的方式来实现进程对资源抢占的效果,我们在vms002主机上进入到/sys/devices/system/cpu/cpu1目录下,发现online的值为1,表示正在启用的状态,我们可以将online的值设置为0,此时表示1号CPU处于关闭的状态(图5-2),我们发现系统中确实此时只有0号CPU在运行(图5-3)。
[Make it clear, to say it!  Process Management]
[Make it clear, to say it!  Process Management]
[Make it clear, to say it!  Process Management]
(5.2)top命令每一行的含义
(5.2.1)接着我们在vms002主机上执行两个cat命令,实现不断消耗内存的效果(图5-4)。然后我们使用top命令查询当前系统所有进程的情况,再按键盘1可以显示所有CPU运行的信息,其中第一行“top - 18:01:29 up 11:52, 6 users, load average: 2.72, 2.11, 1.11”分别代表的是当前的时间是18:01:29,机器一共运行了11:52的时长,当前系统有6 users个用户登录,机器在一分钟、五分钟、十五分钟的负载分别是“2.72, 2.11, 1.11”。第二行“Tasks: 475 total, 7 running, 468 sleeping, 0 stopped, 0 zombie”表示的是系统总共有进程475 total,其中7 running在运行,有468 sleeping在睡眠状态,有0 stopped个是停止的,有0 zombie时僵尸的状态。由于我们将1号CPU关闭了,所以此时2个进程都是在0号CPU上抢占式运行(图5-5)。第三行“%Cpu(s): 20.4 us, 79.6 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st”表示的是系统用户进程使用CPU百分比为20.4 us,内核中的进程占用CPU百分比为79.6 sy,用户进程空间内改变过优先级的进程占用CPU百分比为0.0 ni,空闲CPU百分比为0.0 id,CPU等待I/O完成的时间总量为0.0 wa(如果wa占用较多CPU,原因是磁盘性能有问题,建议更换磁盘),硬件中断占用CPU百分比为0.0 hi,软件中断占用CPU百分比为0.0 si,被虚拟内存偷走占用CPU百分比为0.0 st。
注意:htop命令也可以实现top命令的相关显示,不过需要我们额外的安装相关的软件包
# cat /dev/zero > /dev/null &
# top -d 1---每1秒中更新一次所有进程的显示
[Make it clear, to say it!  Process Management]
[Make it clear, to say it!  Process Management]
(5.2.2)经验:
0.0 hi:硬中断,占的CPU百分比。硬中断是由硬件产生的,比如,像磁盘、网卡、键盘、时钟等。每个设备或设备集都有它自己的IRQ(中断请求)。基于IRQ,CPU可以将相应的请求分发到对应的硬件驱动上(注:硬件驱动通常是内核中的一个子程序,而不是一个独立的进程)hi->Hardware IRQ:The amount of time the CPU has been servicing hardware interrupts
0.0 si:软中断,占的CPU百分比。通常软中断是一些对I/O的请求。这些请求会调用内核中可以调度I/O发生的程序。对于某些设备,I/O请求需要被立即处理,而磁盘I/O请求通常可以排队并且可以稍后处理。根据I/O模型的不同,进程或许会被挂起直到I/O完成,此时内核调度器就会选择另一个进程去运行。I/O可以在进程之间产生并且调度过程通常和磁盘I/O的方式是相同的。Si->Software Interrupts:The amount of time the CPUhas been servicing software interrupts。
0.0 st:虚拟机偷取物理CPU的时间。比如:物理机已经运行了KVM,KVM虚拟机占用物理机的CPU时间。
(5.2.3)第四行中“Mem : 4033636 total”表示物理内存总量,“3265760 free”表示空闲内存总量,“380604 used”表示使用的物理内存总量,“387272 buff/cache”表示用作内核缓存的内存量和free -k是一个意思。第五行中“Swap: 6291448 total”表示交换区总量,“6291448 free”表示空闲交换分区总量,“0 used”表示使用的交换区总量,“3394360 avail Mem”表示总的可利用内存是多少。
[Make it clear, to say it!  Process Management]
(5.2.4)第六行中PID表示进程id,USER表示进程所有者的用户名,PR表示由内核动态调整的优先级、用户不能修改,NI表示进程优先级即nice值、负值表示高优先级、正值表示低优先级、用户可以自己调整,VIRT表示虚拟内存、是进程正在使用的所有内存,RES表示的是常驻内存集(resident memory usage),SHR表示的是共享内存大小(shared memory)单位kb,S表示进程状态、其中D(不可中断的睡眠状态)、R(运行中或可运行)、S(睡眠中)、T(已跟踪/已停止)、Z(僵死),%CPU表示上次更新到现在的CPU时间占用百分比,%MEM表示进程使用的物理内存百分比,TIME+表示使用的CPU时间总计、单位1/100秒,COMMAND表示命令行。
注意:对于SHR:1、除了M自身进程的共享内存,也包括其他进程的共享内存;2、虽然进程只使用了几个共享库的函数,但它包含了整个共享库的大小;3、计算某个进程所占的物理内存大小公式为RES-SHR;4、swap out后,它将会降下来
[Make it clear, to say it!  Process Management]
(5.2.5)对于top快捷键:
默认3s刷新一次,按s键刷新时间
按空格:立即刷新
q:退出
P:按CPU排序(CPU进行排序)
M:按内存排序
T:按时间排序
数字键1:显示每个内核的CPU使用率
u/U:指定显示的用户
h:帮助
# top -p 9465---使用top命令动态只查看某个或某些进程的信息
[Make it clear, to say it!  Process Management]
(5.3)此时我们在图5-5中发现两个cat进程的NICE值都是0是一致的,而我们希望将进程11239的进程优先级提高,我们可以使用renice命令。我们发现11239进程的nice值为-19,CPU的占用率为96.1%,而11232的nice值为0,CPU的占用率只有1.9%,因此我们通过设置nice值而实现了进程抢占资源的效果,最后我们使用killall命令终止cat命令的总线。
# renice -n -19 11239---将11239进程的nice值设置为-19
# killall -7 cat---终止cat命令的总线
[Make it clear, to say it!  Process Management]
[Make it clear, to say it!  Process Management]
(5.4)调整进程优先级
(5.4.1)接着我们将1号CPU开启,实现1号CPU和0号CPU都是启用的状态(图5-8),并重新运行两个cat命令,实现不断消耗内存的效果(图5-9),此时我们发现两个进程是分别在两个CPU上运行的(图5-10)。
# echo 1 > cpu1/online
# cat /dev/zero > /dev/null &
[Make it clear, to say it!  Process Management]
[Make it clear, to say it!  Process Management]
[Make it clear, to say it!  Process Management]
[Make it clear, to say it!  Process Management]
(5.4.2)当然现在我们想具体的知道每个进程究竟是在哪个CPU上运行的,我们可以使用ps命令,此时我们发现13884进程是在0号CPU上运行,13899是在1号CPU上运行的。
PS Mo pid #, COMM, PSR `pgrep cat`
[Make it clear, to say it!  Process Management]
(5.4.3) and now we want to state two CPU are enabled, still be able to specify all the processes running on the same processes are a CPU, we can use taskset command specified (Figure 5-12), then we find that both processes are already running on the same CPU, and accounting for 50% of the capacity of the CPU running 0 (Figure 5-13).
# Taskset -c 0 cat / dev / zero> / dev / null & --- specify a cat on the number of processes running the CPU 0
# PS Mo pid, COMM, PSR pgrep cat--- query process is run on what pieces of the CPU
# top -d 1 --- top command and updates every 1 second
[Make it clear, to say it!  Process Management]
[Make it clear, to say it!  Process Management]
(5.5) we adjust the priority of the time, are changing the priority of a process already running, then if we can run at a certain priority program it? In fact, this is possible, we use a nice specified process priority is -10, then began to run a cat command cycle (Figure 5-14). We use the top command discovery process nice value of -10 created has been running (Figure 5-15).
# Nice -n -10 taskset -c 1 cat / dev / zero> / dev / null & --- we use nice specified process priority is -10, then began to run a cat command cycle
# top -d 1
[Make it clear, to say it!  Process Management]
[Make it clear, to say it!  Process Management]

------ This concludes the article, thanks for reading ------

Guess you like

Origin blog.51cto.com/13613726/2461226