Process Profile
what is a process
- A process is a running instance of an executable program that has been started and consists of:
• an address space of allocated memory;
• security attributes, including ownership credentials and privileges;
• one or more threads of execution of program code;
• a process state . - Program: Binary files, static, such as: /usr/bin/passwd, /usr/sbin/useradd
- Process: It is the process of program running, dynamic, with life cycle and running status.
process life cycle
- The parent process copies its own address space (fork) to create a new (child) process structure. Each new process is assigned a unique process ID (PID) for tracking security.
- Any process can create child processes.
- All processes are descendants of the first system process:
- Centos5/6 system process:
init
- Centos7 system process:
systemd
process state
The cause of the process state
- In a multitasking operating system, each CPU (or core) can only handle one process at a time. While a process is running, its requirements for CPU time and resource allocation are constantly changing, thereby assigning the process a state that changes with the demands of the environment.
process management process
Static view process ps
- PS, is process status, called process status,
such as the task manager of windows
A process as an example
[root@localhost ~]# ps aux | head -2
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.6 128096 6708 ? Ss 16:20 0:01 /usr/lib/systemd/systemd
-
Command parameter description
- ps a displays all programs under the current terminal
- ps u Displays program status in a user-oriented format.
- ps x does not distinguish between terminals.
-
Field meaning of ps aux output
-
USER: the user running the process
-
PID: Process ID
-
%CPU: CPU usage
-
%MEM: memory usage
-
VSZ: takes up virtual memory
-
RSS: takes real memory
-
TTY: The terminal the process is running on. terminal type, such
pts/0
pts/1
as -
STAT: process status
- [常见] - R 运行 - S 睡眠 Sleep - T 停止的进程 - Z 僵尸进程 - X 死掉的进程
-
START: The start time of the process
-
TIME: The total CPU time occupied by the process, format:
分钟:秒
-
COMMAND: process file, process name
-
process ordering
grammar
ps aux --sort %cpu
- Example
Sort in descending order by CPU percentage (the minus sign is in descending order)
[root@localhost ~]# ps aux --sort -%cpu
[root@localhost ~]# ps aux --sort %cpu
Process parent-child relationship
grammar
ps -ef
- Example
View the parent-child relationship of a process. Please observe PID and PPID, PPID is the ID of the parent process
[root@localhost ~]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 1月22 ? 00:00:07 /usr/lib/systemd/systemd
root 2 0 0 1月22 ? 00:00:00 [kthreadd]
root 3 2 0 1月22 ? 00:00:06 [ksoftirqd/0]
Custom display fields (understand)
grammar
ps axo
- example
[root@localhost ~]# ps axo user,pid,ppid,%mem,command |head -3
root 8310 1 0.1 /usr/sbin/httpd
apache 8311 8310 0.0 /usr/sbin/httpd
apache 8312 8310 0.0 /usr/sbin/httpd
Dynamic view process top
Use the command top
the top half
example
top - 11:45:08 up 18:54, 4 users, load average: 0.05, 0.05, 0.05
Tasks: 176 total, 1 running, 175 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.0 us, 0.3 sy, 0.0 ni, 99.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 3865520 total, 1100000 free, 580268 used, 2185252 buff/cache
KiB Swap: 4063228 total, 4063228 free, 0 used. 2917828 avail Mem
-
line 1
-
line 2
-
line 3
- line 4
- line 5
the second half
Field introduction (understand)
PID,USER,%CPU,%MEM略
VIRT:virtual memory usage 虚拟内存 # 需要这些内存,但并没有占满。
RES:resident memory usage 常驻内存 # 用了多少内存
SHR:shared memory 共享内存
- 1、除了自身进程的共享内存,也包括其他进程的共享内存
- 2、共享内存大小公式:RES – SHR
Top commonly used internal commands
h 或 ? 帮助
M 按内存的使用排序
P 按CPU使用排序
N 以PID的大小排序
< 向前翻页
> 向后翻页
> z 彩色,Z设置彩色,使用数字调整
top skills
- Dynamically view process top, like Windows task manager
[root@localhost ~]# top //回车,立刻刷新。按z彩色显示,按F,通过光标设置列的顺序。
[root@localhost ~]# top -d 1 //每1秒刷新。
[root@localhost ~]# top -d 1 -p 10126 查看指定进程的动态信息
[root@localhost ~]# top -d 1 -p 10126,1 查看10126和1号进程
Use signal to control process kill
Signal type
Send a signal to a process (kill -l lists all supported signals)
[root@localhost ~]# kill -l
编号 信号名
1) SIGHUP 重新加载配置
2) SIGINT 键盘中断Ctrl+C,按后发送信号
3) SIGQUIT 键盘退出Ctrl+\,类似SIGINT
9) SIGKILL 强制终止,无条件
15) SIGTERM 终止(正常结束),缺省信号
18) SIGCONT 继续
19) SIGSTOP 暂停
20) SIGTSTP 键盘暂停Ctrl+Z
Signal 9,15
1 Create 2 files and check the terminal number.
[root@localhost ~]# touch file1 file2
2 Through a terminal, open a vim
[root@localhost ~]# vim file1
3 Through another terminal, open a vim
[root@localhost ~]# vim file2
4 Through another terminal, query the two processes
[root@localhost ~]# 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
5 Send signal 15 and signal 9 to observe the status of the two terminal programs
[root@localhost ~]# kill -15 4362
[root@localhost ~]# kill -9 4363
15是走正常退出流程
9是直接杀死,会遗留下来.file2.swp文件,下次打开时会提示存在该文件,需要手动删除
process priority nice
Introduction
Linux process scheduling and multitasking, each CPU can only handle one process at a time point, and use time slice technology to run multiple programs at the same time. There is priority among multiple programs
Priority Ranges and Features
-
Priority icon
on the way Nice and PR correspond to the two priorities below -
Two Priorities in the System
- There are two priorities displayed in top, PR value and nice value
NI: Actual nice value
PR: primary (+20): The nice level is displayed as being mapped to a larger priority queue, -20 is mapped to 0, +19 Maps to 39
- There are two priorities displayed in top, PR value and nice value
-
priority feature
-
The larger the nice value: the lower the priority, such as +19
-
The smaller the nice value: the higher the priority, for example -20
-
View the nice level of the process
···
[root@localhost ~]# ps axo pid,command,nice --sort=-nice
···
Start processes with different nice levels
- default
When starting a process, it usually inherits the nice level of the parent process, which is 0 by default.
- Manually start different nice
[root@localhost ~]# nice -n -5 sleep 6000 & # 指定启动进程的nice为-5
[1] 2220
[root@localhost ~]# nice -n -10 sleep 7000 &
[2] 2229
[root@localhost ~]# ps axo command,pid,nice | grep sleep
sleep 6000 2220 -5
sleep 7000 2229 -10
grep --color=auto sleep 2233 0
Change the nice level of an existing process
example
Use the shell to change the nice level
1 Create a sleep example program
[root@localhost ~]# sleep 7000 &
[2] 2669
2 Modify his nice value.
[root@localhost ~]# renice -20 2669
2669 (process ID) The old priority is 0, the new priority is -20, observe the old nice value.
Job control jobs (understand)
Introduction
- Job control is a command-line function, also known as running in the background.
- Keyword introduction
fg: foreground # 前台进程:是在终端中运行的命令,占领终端。
bg:background # 后台进程:没有控制终端,它不需要终端的交互。看不见,但是在运行。
- Background program control example
1. Observe the phenomenon of occupying the foreground
[root@localhost ~]# sleep 2000
Run a program, the current terminal cannot input. Observe the phenomena that occupy the foreground. Most command line input is no longer valid.
ctrl + c
terminate process
2. Run the background program &
[root@localhost ~]# sleep 3000 &
3.ps query all programs.
[root@localhost ~]# ps aux |grep sleep
root 8895 0.0 0.0 100900 556 pts/0 S 12:13 0:00 sleep 3000
4.jobs View the background process.
[root@localhost ~]# jobs
[1]+ Running sleep 3000 &
+,-
Represents, fg
when used, the process that is transferred to the foreground by default. + first, then -
5. Transfer the background program to the foreground.
[root@localhost ~]# fg 1 //将作业1调回到前台
6. The foreground program is immediately transferred to the background ctrl+z
, the keyboard is suspended, but the status changes to Stopped
[root@localhost ~]# fg 2
sleep 555
^Z
[2]+ Stopped sleep 555
7. Run the stop program
bg 2
[2]+ sleep 555 &
[root@localhost ~]# jobs
[1]- Running nice -n -5 sleep 6000 &
[2]+ Running sleep 555 &
8. Eliminate background processes
[root@localhost ~]# kill %1
Note that "kill 1" is different from "kill %1". The former terminates the process with PID 1, while the latter kills the background program with job number 1.
- Summarize
& 后台运行程序
jobs 查询后台
kill %1 停止后台进程
Virtual file system proc (understand)
Introduction
Virtual file system: collect the status information of the server's own kernel and process operation
# CPU
/proc/cpuinfo
[root@localhost ~]# cat /proc/cpuinfo
# 内存
/proc/meminfo
[root@localhost ~]# less /proc/meminfo
# 内核
/proc/cmdline
[root@localhost ~]# cat /proc/cmdline