进程的基本概念
目标
解释进程在系统中的概念。
名词定义
进程的定义
进程是已启动的可执行程序的运行中实例。进程有以下组成部分:1.已分配内存的地址空间。
2.安全属性,包括所有权凭据和特权。
3.程序代码的一个或多个执行线程。
4.进程状态。进程的环境包括:
1.本地和全局变量。
2.当前调度上下文
3.分配的系统资源,如文件描述符和网络端口
现有的进程复制自己的地址空间(fork)来创建一个新的进程结构。每个新进程分配有一个唯一进程ID(pid),满足跟踪和安全性之需。PID和父进程ID(PPID)是新进程环境的元素。任何进程可创建子进程。所有进程都是第一个系统进程的后代,linux中第一个系统进程是systemd。
实操演示
- 查看进程信息
命令:ps
常用选项:
Usage:
ps [options]
Basic options:
-A, -e all processes
-a all with tty, except session leaders
x processes without controlling ttys
Selection by list:
-p, p, –pidprocess id
-t, t, –ttyterminal
-u, U, –usereffective user id or name
Output formats:
-f full-format, including command lines
-o, o, –formatuser-defined format
u user-oriented format
Show threads:
k, –sort specify sort order as: [+|-]key[,[+|-]key[,…]]
n display numeric uid and wchan
[root@jenkins ~]# ps -aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 192092 3132 ? Ss 2021 25:08 /usr/lib/systemd/systemd --switched-root --system --deserial
root 2 0.0 0.0 0 0 ? S 2021 0:00 [kthreadd]
root 4 0.0 0.0 0 0 ? S< 2021 0:00 [kworker/0:0H]
root 6 0.0 0.0 0 0 ? S 2021 4:59 [ksoftirqd/0]
root 7 0.0 0.0 0 0 ? S 2021 1:45 [migration/0]
root 8 0.0 0.0 0 0 ? S 2021 0:00 [rcu_bh]
root 9 0.0 0.0 0 0 ? R 2021 30:47 [rcu_sched]
root 10 0.0 0.0 0 0 ? S< 2021 0:00 [lru-add-drain]
root 11 0.0 0.0 0 0 ? S 2021 0:33 [watchdog/0]命令:top
[root@jenkins ~]# top
top - 15:23:04 up 68 days, 21:50, 1 user, load average: 0.08
Tasks: 254 total, 1 running, 253 sleeping, 0 stopped, 0
%Cpu(s): 1.7 us, 1.2 sy, 0.0 ni, 97.0 id, 0.0 wa, 0.0 hi,
PID USER PR NI VIRT RES SHR S %CPU %MEM
42988 jenkins 20 0 3688964 901280 12572 S 0.0 23.3
43481 nginx 20 0 264372 15956 5872 S 3.0 0.4
60549 nginx 20 0 260276 12172 5872 S 0.0 0.3
92803 nginx 20 0 260276 12172 5868 S 0.0 0.3
36014 root 20 0 163748 6224 4728 S 0.0 0.2
##按?键可以查看命令选项参数。