上传文件至 /
This commit is contained in:
parent
1bdebbd0c8
commit
67be13968b
385
进程管理.md
Normal file
385
进程管理.md
Normal file
@ -0,0 +1,385 @@
|
||||
<h2><center>进程管理</center></h2>
|
||||
|
||||
------
|
||||
|
||||
## 一:进程介绍
|
||||
|
||||
### 1. 进程的基本属性
|
||||
|
||||
**PID(Process ID)**
|
||||
|
||||
- 每个进程的唯一标识符,由操作系统分配,范围从 `1`(`init/systemd` 进程)开始递增。
|
||||
- **PPID(Parent Process ID)**:父进程的 PID,表示创建该进程的进程。
|
||||
|
||||
**用户和组**
|
||||
|
||||
- **UID/GID**:进程所属的用户和组,决定进程的权限(如文件访问权限)。
|
||||
|
||||
**进程状态**
|
||||
|
||||
通过`ps`或`top`查看进程状态,常见的状态包括:
|
||||
|
||||
- **R(Running)**:正在运行或可运行(在运行队列中)。
|
||||
- **S(Sleeping)**:可中断的睡眠状态(等待资源或事件)。
|
||||
- **D(Uninterruptible Sleep)**:不可中断的睡眠(通常等待磁盘 I/O)。
|
||||
- **T(Stopped)**:进程被暂停(如通过 `Ctrl+Z` 或 `SIGSTOP` 信号)。
|
||||
- **Z(Zombie)**:僵尸进程(已终止但未被父进程回收资源)。
|
||||
|
||||
**资源占用**
|
||||
|
||||
- **CPU 占用率**:进程使用的 CPU 时间比例。
|
||||
- **内存占用**:包括虚拟内存(VSZ)和物理内存(RSS)。
|
||||
- **打开的文件和端口**:通过 `lsof` 或 `netstat` 查看。
|
||||
|
||||
### 2. 进程的生命周期
|
||||
|
||||
**创建**
|
||||
|
||||
- 通过 `fork()` 系统调用创建子进程(复制父进程的内存空间)。
|
||||
- 通过 `exec()` 系统调用加载新程序到子进程的内存空间。
|
||||
|
||||
**运行**
|
||||
|
||||
- 进程在 CPU 时间片内运行,可能处于用户态或内核态。
|
||||
|
||||
**终止**
|
||||
|
||||
- **正常终止**:进程主动调用 `exit()` 或返回 `main()`。
|
||||
- **强制终止**:通过 `kill -9` 发送 `SIGKILL` 信号。
|
||||
- **僵尸进程**:子进程终止后,父进程未调用 `wait()` 回收其资源。
|
||||
|
||||
## 二:进程管理
|
||||
|
||||
### 1. 查看进程
|
||||
|
||||
**`ps`命令**
|
||||
|
||||
查看当前进程状态,常用组合:
|
||||
|
||||
```bash
|
||||
ps aux # 查看所有进程的详细信息(用户、CPU、内存等)
|
||||
ps aux --sort -%cpu # 以CPU的降序排列查看进程
|
||||
ps aux --sort %cpu # 以CPU的升序排列查看进程
|
||||
ps -ef # 查看完整格式的进程列表
|
||||
ps -p <PID> # 查看指定 PID 的进程
|
||||
ps aox 属性,[属性],... # 查看进程的指定属性
|
||||
|
||||
示例:
|
||||
[user03@wxin ~]$ ps aux --sort %cpu | head -3
|
||||
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
|
||||
root 1 0.0 0.2 193820 4676 ? Ss 01:38 0:12 /usr/lib/systemd/systemd --switched-root --system --deserialize 22
|
||||
root 2 0.0 0.0 0 0 ? S 01:38 0:00 [kthreadd]
|
||||
|
||||
[user03@wxin ~]$ ps -ef | head -5
|
||||
UID PID PPID C STIME TTY TIME CMD
|
||||
root 1 0 0 01:38 ? 00:00:12 /usr/lib/systemd/systemd --switched-root --system --deserialize 22
|
||||
root 2 0 0 01:38 ? 00:00:00 [kthreadd]
|
||||
root 4 2 0 01:38 ? 00:00:00 [kworker/0:0H]
|
||||
root 6 2 0 01:38 ? 00:00:00 [ksoftirqd/0]
|
||||
|
||||
[user03@wxin ~]$ ps -p 1
|
||||
PID TTY TIME CMD
|
||||
1 ? 00:00:12 systemd
|
||||
|
||||
[user03@wxin ~]$ ps -axo uid,pid,%cpu,vsz | head -5
|
||||
UID PID %CPU VSZ
|
||||
0 1 0.0 193820
|
||||
0 2 0.0 0
|
||||
0 4 0.0 0
|
||||
0 6 0.0 0
|
||||
```
|
||||
|
||||
**`top`命令**
|
||||
|
||||
实时监控进程及系统资源占用
|
||||
|
||||
```bash
|
||||
top
|
||||
|
||||
# 快捷键:
|
||||
# - P:按 CPU 使用率排序
|
||||
# - M:按内存使用率排序
|
||||
# - N:按PID大小率排序
|
||||
# - k:终止指定 PID 的进程
|
||||
# - q:退出
|
||||
# - <: 向前
|
||||
# - >: 向后
|
||||
# - z: 彩色
|
||||
# - Z: 设置彩色
|
||||
```
|
||||
|
||||
### 2. 启动进程
|
||||
|
||||
**直接运行程序(前台)**
|
||||
|
||||
```shell
|
||||
/path/to/command
|
||||
```
|
||||
|
||||
**后台运行程序**
|
||||
|
||||
```bash
|
||||
/path/to/command &
|
||||
```
|
||||
|
||||
### 3. 终止进程
|
||||
|
||||
通过 `kill` 发送信号
|
||||
|
||||
```bash
|
||||
kill <PID> # 默认发送 SIGTERM(15),正常终止
|
||||
kill -9 <PID> # 强制终止(SIGKILL,9)
|
||||
```
|
||||
|
||||
通过进程名终止
|
||||
|
||||
```bash
|
||||
pkill <进程名> # 根据名称终止进程
|
||||
killall <进程名> # 终止所有同名进程
|
||||
```
|
||||
|
||||
### 4. 恢复/暂停进程
|
||||
|
||||
- 暂停进程:`Ctrl + Z`(发送 SIGTSTP 信号)
|
||||
- 恢复进程到前台:`fg`
|
||||
- 恢复进程到后台:`bg`
|
||||
|
||||
### 5. 后台进程管理
|
||||
|
||||
**`jobs`命令**
|
||||
|
||||
查看当前 Shell 的后台任务:
|
||||
|
||||
```bash
|
||||
jobs -l # 列出任务及其 PID
|
||||
```
|
||||
|
||||
**`nohup`命令**
|
||||
|
||||
让进程在终端关闭后继续运行:
|
||||
|
||||
```bash
|
||||
nohup /path/to/command > output.log 2>&1 &
|
||||
```
|
||||
|
||||
**`disown`命令**
|
||||
|
||||
将已启动的进程从当前 Shell 分离
|
||||
|
||||
```bash
|
||||
disown -h <PID> # 防止进程因 Shell 退出而终止
|
||||
```
|
||||
|
||||
### 6. 进程优先级调整
|
||||
|
||||
**`nice`命令**
|
||||
|
||||
启动进程时指定优先级(范围:-20 到 19,值越小优先级越高):
|
||||
|
||||
```bash
|
||||
nice -n 10 /path/to/command # 以优先级 10 启动
|
||||
```
|
||||
|
||||
**`renice`命令**
|
||||
|
||||
调整运行中进程的优先级
|
||||
|
||||
```bash
|
||||
renice -n 5 -p <PID> # 将 PID 的优先级调整为 5
|
||||
```
|
||||
|
||||
### 7. 查找进程
|
||||
|
||||
**按名称查找**
|
||||
|
||||
```bash
|
||||
pgrep <进程名> # 返回匹配的 PID
|
||||
pgrep -l <关键字> # 显示进程名和 PID
|
||||
```
|
||||
|
||||
**按端口查找**
|
||||
|
||||
```bash
|
||||
netstat -tunlp | grep <端口号>
|
||||
# 或
|
||||
ss -tunlp | grep <端口号>
|
||||
```
|
||||
|
||||
**按资源占用查找**
|
||||
|
||||
```bash
|
||||
lsof -i :<端口号> # 查看占用端口的进程
|
||||
lsof /path/to/file # 查看占用文件的进程
|
||||
```
|
||||
|
||||
## 三:虚拟文件系统
|
||||
|
||||
### 1.cpu
|
||||
|
||||
```bash
|
||||
[root@wxin ~]# cat /proc/cpuinfo
|
||||
processor : 0
|
||||
vendor_id : GenuineIntel
|
||||
cpu family : 6
|
||||
model : 183
|
||||
model name : Intel(R) Core(TM) i7-14700HX
|
||||
stepping : 1
|
||||
microcode : 0x125
|
||||
cpu MHz : 2304.002
|
||||
cache size : 33792 KB
|
||||
physical id : 0
|
||||
siblings : 2
|
||||
core id : 0
|
||||
cpu cores : 2
|
||||
apicid : 0
|
||||
initial apicid : 0
|
||||
fpu : yes
|
||||
fpu_exception : yes
|
||||
cpuid level : 32
|
||||
wp : yes
|
||||
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon rep_good nopl xtopology tsc_reliable nonstop_tsc eagerfpu pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch invpcid_single ssbd ibrs ibpb stibp ibrs_enhanced fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid rdseed adx smap clflushopt clwb sha_ni xsaveopt xsavec xgetbv1 arat umip pku ospke gfni vaes vpclmulqdq movdiri movdir64b md_clear spec_ctrl intel_stibp flush_l1d arch_capabilities
|
||||
bogomips : 4608.00
|
||||
clflush size : 64
|
||||
cache_alignment : 64
|
||||
address sizes : 45 bits physical, 48 bits virtual
|
||||
power management:
|
||||
|
||||
processor : 1
|
||||
vendor_id : GenuineIntel
|
||||
cpu family : 6
|
||||
model : 183
|
||||
model name : Intel(R) Core(TM) i7-14700HX
|
||||
stepping : 1
|
||||
microcode : 0x125
|
||||
cpu MHz : 2304.002
|
||||
cache size : 33792 KB
|
||||
physical id : 0
|
||||
siblings : 2
|
||||
core id : 1
|
||||
cpu cores : 2
|
||||
apicid : 1
|
||||
initial apicid : 1
|
||||
fpu : yes
|
||||
fpu_exception : yes
|
||||
cpuid level : 32
|
||||
wp : yes
|
||||
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon rep_good nopl xtopology tsc_reliable nonstop_tsc eagerfpu pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch invpcid_single ssbd ibrs ibpb stibp ibrs_enhanced fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid rdseed adx smap clflushopt clwb sha_ni xsaveopt xsavec xgetbv1 arat umip pku ospke gfni vaes vpclmulqdq movdiri movdir64b md_clear spec_ctrl intel_stibp flush_l1d arch_capabilities
|
||||
bogomips : 4608.00
|
||||
clflush size : 64
|
||||
cache_alignment : 64
|
||||
address sizes : 45 bits physical, 48 bits virtual
|
||||
power management:
|
||||
|
||||
processor : 2
|
||||
vendor_id : GenuineIntel
|
||||
cpu family : 6
|
||||
model : 183
|
||||
model name : Intel(R) Core(TM) i7-14700HX
|
||||
stepping : 1
|
||||
microcode : 0x125
|
||||
cpu MHz : 2304.002
|
||||
cache size : 33792 KB
|
||||
physical id : 1
|
||||
siblings : 2
|
||||
core id : 0
|
||||
cpu cores : 2
|
||||
apicid : 2
|
||||
initial apicid : 2
|
||||
fpu : yes
|
||||
fpu_exception : yes
|
||||
cpuid level : 32
|
||||
wp : yes
|
||||
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon rep_good nopl xtopology tsc_reliable nonstop_tsc eagerfpu pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch invpcid_single ssbd ibrs ibpb stibp ibrs_enhanced fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid rdseed adx smap clflushopt clwb sha_ni xsaveopt xsavec xgetbv1 arat umip pku ospke gfni vaes vpclmulqdq movdiri movdir64b md_clear spec_ctrl intel_stibp flush_l1d arch_capabilities
|
||||
bogomips : 4608.00
|
||||
clflush size : 64
|
||||
cache_alignment : 64
|
||||
address sizes : 45 bits physical, 48 bits virtual
|
||||
power management:
|
||||
|
||||
processor : 3
|
||||
vendor_id : GenuineIntel
|
||||
cpu family : 6
|
||||
model : 183
|
||||
model name : Intel(R) Core(TM) i7-14700HX
|
||||
stepping : 1
|
||||
microcode : 0x125
|
||||
cpu MHz : 2304.002
|
||||
cache size : 33792 KB
|
||||
physical id : 1
|
||||
siblings : 2
|
||||
core id : 1
|
||||
cpu cores : 2
|
||||
apicid : 3
|
||||
initial apicid : 3
|
||||
fpu : yes
|
||||
fpu_exception : yes
|
||||
cpuid level : 32
|
||||
wp : yes
|
||||
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon rep_good nopl xtopology tsc_reliable nonstop_tsc eagerfpu pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch invpcid_single ssbd ibrs ibpb stibp ibrs_enhanced fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid rdseed adx smap clflushopt clwb sha_ni xsaveopt xsavec xgetbv1 arat umip pku ospke gfni vaes vpclmulqdq movdiri movdir64b md_clear spec_ctrl intel_stibp flush_l1d arch_capabilities
|
||||
bogomips : 4608.00
|
||||
clflush size : 64
|
||||
cache_alignment : 64
|
||||
address sizes : 45 bits physical, 48 bits virtual
|
||||
power management:
|
||||
```
|
||||
|
||||
### 2.内存
|
||||
|
||||
```bash
|
||||
[root@wxin ~]# cat /proc/meminfo
|
||||
MemTotal: 2027892 kB
|
||||
MemFree: 225776 kB
|
||||
MemAvailable: 847368 kB
|
||||
Buffers: 36 kB
|
||||
Cached: 730156 kB
|
||||
SwapCached: 496 kB
|
||||
Active: 506068 kB
|
||||
Inactive: 845292 kB
|
||||
Active(anon): 201568 kB
|
||||
Inactive(anon): 473196 kB
|
||||
Active(file): 304500 kB
|
||||
Inactive(file): 372096 kB
|
||||
Unevictable: 0 kB
|
||||
Mlocked: 0 kB
|
||||
SwapTotal: 2097148 kB
|
||||
SwapFree: 2082548 kB
|
||||
Dirty: 4 kB
|
||||
Writeback: 0 kB
|
||||
AnonPages: 620724 kB
|
||||
Mapped: 95716 kB
|
||||
Shmem: 53596 kB
|
||||
Slab: 221480 kB
|
||||
SReclaimable: 133040 kB
|
||||
SUnreclaim: 88440 kB
|
||||
KernelStack: 10208 kB
|
||||
PageTables: 35596 kB
|
||||
NFS_Unstable: 0 kB
|
||||
Bounce: 0 kB
|
||||
WritebackTmp: 0 kB
|
||||
CommitLimit: 3111092 kB
|
||||
Committed_AS: 4185336 kB
|
||||
VmallocTotal: 34359738367 kB
|
||||
VmallocUsed: 179396 kB
|
||||
VmallocChunk: 34359310332 kB
|
||||
Percpu: 37376 kB
|
||||
HardwareCorrupted: 0 kB
|
||||
AnonHugePages: 247808 kB
|
||||
CmaTotal: 0 kB
|
||||
CmaFree: 0 kB
|
||||
HugePages_Total: 0
|
||||
HugePages_Free: 0
|
||||
HugePages_Rsvd: 0
|
||||
HugePages_Surp: 0
|
||||
Hugepagesize: 2048 kB
|
||||
DirectMap4k: 118656 kB
|
||||
DirectMap2M: 1978368 kB
|
||||
DirectMap1G: 0 kB
|
||||
```
|
||||
|
||||
### 3.内核
|
||||
|
||||
```bash
|
||||
[root@wxin ~]# cat /proc/cmdline
|
||||
BOOT_IMAGE=/vmlinuz-3.10.0-1160.el7.x86_64 root=/dev/mapper/centos-root ro rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb quiet LANG=zh_CN.UTF-8
|
||||
```
|
||||
|
Loading…
x
Reference in New Issue
Block a user