Linux编程入门-进程

进程号

系统允许的最大进程号为“/proc/sys/kernel/pid_max”文件数值-1。查看“/proc/PID/status”文件的PPid字段可获知每个进程的父进程。若某个子进程的父进程终止,则init进程收养该进程,后去getppid返回进程号1。

查看“/proc/PID/cmdline”文件可读取任意进程命令行参数,程序也可用“/proc/self/cmdline”访问自己的命令行参数。GNU C语言库也有提供全局变量:

1
2
3
4
#define _GNU_SOURCE
#include <errno.h>
extern char *program_invocation_name; //调用该程序的完整路径名
extern char *program_invocation_short_name; //不含目录的程序部分

getpid

返回调用进程的进程号:

1
2
#include <unistd.h>
pid_t getpid(void);

getppid

返回父进程进程号:

1
pid_t getppid(void);

环境

查看“/proc/PID/environ”文件可访问任意进程环境列表,每条后面以空字符结尾。main函数中的argv和environ指针数组指向的字符串都驻留在进程栈上一个单一连续的内存区域,该区域可存储的字节数上限通过sysconf的_SC_ARG_MAX获取。

getenv

从进程环境中检索单个值:

1
2
3
char *getenv(
const char *name
); //不存在返回NULL

putenv

向调用进程的环境中添加一个新变量,或修改一个已存在的变量值。该函数将environ变量中某一元素的指向与string参数指向位置相同,如果随后修改string参数所指内容,将影响该进程环境,所以string不应为自动变量(在栈中分配的字符数组),以防定义此变量的函数返回后可能重写这块内存区域。当string不包含一个“=”则从环境列表中移除string命名的环境变量。

1
2
3
int putenv(
char *string //形式如“name=value”
); //成功0 失败非0

setenv

向环境中添加一个变量,可代替putenv。该函数为“name=value”字符串分配一块内存缓冲区并将name和value所指字符串复制到这里,创建一个新的环境变量,不需要手动添加“=”。若name变量已存在且overwrite为0,则不改变环境,overwrite为非0则总是改变环境。

1
2
3
4
5
int setenv(
const char *name, //name=value
const char *value,
int overwrite
);

unsetenv

从环境中移除name标识的变量,不含“=”。

1
2
3
int unsetenv(
const char *name
); //成功0 失败-1

clearenv

清除环境。

1
2
#include <stdlib.h>
int clearenv(void);

非局部跳转

非局部跳转指跳转目标为当前执行函数之外的某个位置。该小节使用时建议不要启用编译优化,必要局部变量建议使用volatile关键字,用-Wextra选项可提示本小节相关部分警告信息。

setjmp/longjmp

setjmp调用为后续longjmp调用确立了跳转目标。setjmp将当前进程环境各种信息保存到env参数中,调用longjmp时将发起longjmp调用的函数与之前调用setjmp函数之间的函数栈帧从栈上剥离,称为解栈。

1
2
3
4
5
#include <setjmp.h>
void longjmp(
jmp_buf env,
int val
);

例子如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <setjmp.h>
//...
static jmp_buf env;
static void f2(void){
longjmp(env,2);
};
static void f1(int argc){
if(argc==1)
longjmp(env,1);
f2();
};
int main(int argc,char* argv[]){
switch(setjmp(env)){
case 0:
printf("calling f1()\n");
f1(argc);
break;
case 1:
printf("back from f1()\n");
break;
case 2:
printf("back from f2()\n");
break;
};
exit(EXIT_SUCCESS);
};

结果大概为:

1
2
3
4
5
6
$ ./longjmp 
calling f1()
back from f1()
$ ./longjmp x
calling f1()
back from f2()

/proc文件系统

例如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
$ cat /proc/1/status
Name: systemd 该进程的运行命令
Umask: 0000
State: S (sleeping) 状态
Tgid: 1 线程组ID
Ngid: 0
Pid: 1 进程ID
PPid: 0 父进程ID
TracerPid: 0 跟踪进程ID 0为未被跟踪
Uid: 0 0 0 0 真实/有效/保留/文件系统用户ID
Gid: 0 0 0 0 真实/有效/保留/文件系统组ID
FDSize: 512 分配的文件描述符槽大小
Groups: 附加组ID
NStgid: 1
NSpid: 1
NSpgid: 1
NSsid: 1
VmPeak: 23576 kB 虚拟内存大小峰值
VmSize: 23540 kB 当前虚拟内存大小
VmLck: 0 kB 已锁定内存
VmPin: 0 kB
VmHWM: 14180 kB 保留集峰值大小
VmRSS: 14180 kB 当前保留集大小
RssAnon: 4992 kB
RssFile: 9188 kB
RssShmem: 0 kB
VmData: 4516 kB 数据段大小
VmStk: 132 kB 堆栈大小
VmExe: 44 kB 代码段大小
VmLib: 12188 kB 共享库大小
VmPTE: 84 kB 页表大小
VmSwap: 0 kB
HugetlbPages: 0 kB
CoreDumping: 0
THP_enabled: 1
Threads: 1 该线程的线程组成员数量
SigQ: 1/15164 当前/最大排队信号
SigPnd: 0000000000000000 线程挂起信号
ShdPnd: 0000000000000000 进程挂起信号
SigBlk: 7fefc1fe28014a03 阻塞的信号
SigIgn: 0000000000001000 忽略的信号
SigCgt: 00000000000004ec 捕获的信号
CapInh: 0000000000000000 可继承的能力
CapPrm: 000001ffffffffff 允许的能力
CapEff: 000001ffffffffff 有效的能力
CapBnd: 000001ffffffffff 能力边界集
CapAmb: 0000000000000000
NoNewPrivs: 0
Seccomp: 0
Seccomp_filters: 0
Speculation_Store_Bypass: thread vulnerable
SpeculationIndirectBranch: conditional enabled
Cpus_allowed: ffffffff,ffffffff,ffffffff,ffffffff 允许的CPU掩码
Cpus_allowed_list: 0-127 同上,列表格式
Mems_allowed: 00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000001 允许的内存节点
Mems_allowed_list: 0 同上,列表格式
voluntary_ctxt_switches: 8909 自愿上下文切换
nonvoluntary_ctxt_switches: 4770 非自愿上下文切换

/proc/PID/fd目录中为每个打开的文件描述符包含了一个符号链接,任何进程可用“/proc/self/xxx”访问自己的/proc目录。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
$ sudo ls /proc/1/fd -Sail
总计 0
19272 dr-x------ 2 root root 249 6月 21 10:03 .
24040 lrwx------ 1 root root 64 6月 21 10:03 0 -> /dev/null
24041 lrwx------ 1 root root 64 6月 21 10:03 1 -> /dev/null
22761 lrwx------ 1 root root 64 6月 21 10:03 10 -> 'anon_inode:[pidfd]'
117943 lrwx------ 1 root root 64 6月 21 20:37 100 -> 'anon_inode:[pidfd]'
30799 lrwx------ 1 root root 64 6月 21 10:03 101 -> 'socket:[48060]'
59034 lrwx------ 1 root root 64 6月 21 10:09 102 -> 'socket:[48648]'
30800 lrwx------ 1 root root 64 6月 21 10:03 103 -> 'socket:[47207]'
117907 lr-x------ 1 root root 64 6月 21 20:37 11 -> anon_inode:inotify
62473 lrwx------ 1 root root 64 6月 21 10:09 111 -> 'socket:[47167]'
117944 lrwx------ 1 root root 64 6月 21 20:37 115 -> 'socket:[48310]'
117945 lrwx------ 1 root root 64 6月 21 20:37 116 -> 'socket:[48137]'
24470 lrwx------ 1 root root 64 6月 21 10:03 12 -> 'anon_inode:[pidfd]'
50280 lrwx------ 1 root root 64 6月 21 10:04 120 -> 'socket:[47108]'
117946 lrwx------ 1 root root 64 6月 21 20:37 121 -> 'socket:[47107]'
50195 lrwx------ 1 root root 64 6月 21 10:04 128 -> 'socket:[45056]'
117947 lrwx------ 1 root root 64 6月 21 20:37 129 -> 'socket:[45987]'
24471 lrwx------ 1 root root 64 6月 21 10:03 13 -> 'anon_inode:[eventpoll]'
41366 lrwx------ 1 root root 64 6月 21 10:03 131 -> 'socket:[45986]'
117948 lrwx------ 1 root root 64 6月 21 20:37 134 -> 'socket:[46356]'
36325 lrwx------ 1 root root 64 6月 21 10:03 136 -> 'socket:[46330]'
117949 lrwx------ 1 root root 64 6月 21 20:37 138 -> 'socket:[46237]'
117950 lrwx------ 1 root root 64 6月 21 20:37 139 -> 'socket:[46211]'
117908 lr-x------ 1 root root 64 6月 21 20:37 14 -> /proc/1/mountinfo
117951 lrwx------ 1 root root 64 6月 21 20:37 140 -> 'socket:[46200]'
36999 lrwx------ 1 root root 64 6月 21 10:03 141 -> 'socket:[46199]'
117952 lrwx------ 1 root root 64 6月 21 20:37 142 -> 'socket:[46189]'
36998 lrwx------ 1 root root 64 6月 21 10:03 143 -> 'socket:[46188]'
117953 lrwx------ 1 root root 64 6月 21 20:37 149 -> 'socket:[35318]'
117909 lr-x------ 1 root root 64 6月 21 20:37 15 -> anon_inode:inotify
117954 lrwx------ 1 root root 64 6月 21 20:37 152 -> 'socket:[46187]'
117955 lrwx------ 1 root root 64 6月 21 20:37 153 -> 'socket:[46182]'
117956 lrwx------ 1 root root 64 6月 21 20:37 158 -> 'socket:[46176]'
117910 lr-x------ 1 root root 64 6月 21 20:37 16 -> /proc/swaps
50196 lrwx------ 1 root root 64 6月 21 10:04 162 -> 'socket:[46156]'
117957 lrwx------ 1 root root 64 6月 21 20:37 163 -> 'socket:[46155]'
117958 lrwx------ 1 root root 64 6月 21 20:37 164 -> 'socket:[46152]'
117959 lrwx------ 1 root root 64 6月 21 20:37 165 -> 'socket:[46150]'
117960 lrwx------ 1 root root 64 6月 21 20:37 166 -> 'socket:[46135]'
117961 lrwx------ 1 root root 64 6月 21 20:37 167 -> 'socket:[46134]'
117962 lrwx------ 1 root root 64 6月 21 20:37 169 -> 'socket:[46091]'
24495 lrwx------ 1 root root 64 6月 21 10:03 17 -> 'socket:[24496]'
117963 lrwx------ 1 root root 64 6月 21 20:37 170 -> 'socket:[44022]'
117964 lrwx------ 1 root root 64 6月 21 20:37 174 -> 'socket:[44012]'
117965 lrwx------ 1 root root 64 6月 21 20:37 177 -> 'socket:[44011]'
58674 lrwx------ 1 root root 64 6月 21 10:09 18 -> 'anon_inode:[pidfd]'
117966 lrwx------ 1 root root 64 6月 21 20:37 183 -> 'socket:[44010]'
58672 lrwx------ 1 root root 64 6月 21 10:09 19 -> 'anon_inode:[pidfd]'
117967 lrwx------ 1 root root 64 6月 21 20:37 190 -> 'socket:[44009]'
117968 lrwx------ 1 root root 64 6月 21 20:37 195 -> 'socket:[44005]'
24042 lrwx------ 1 root root 64 6月 21 10:03 2 -> /dev/null
58673 lrwx------ 1 root root 64 6月 21 10:09 20 -> 'anon_inode:[pidfd]'
117969 lrwx------ 1 root root 64 6月 21 20:37 201 -> 'socket:[44003]'
117970 lrwx------ 1 root root 64 6月 21 20:37 202 -> 'socket:[44001]'
117971 lrwx------ 1 root root 64 6月 21 20:37 205 -> 'socket:[43997]'
117911 lrwx------ 1 root root 64 6月 21 20:37 21 -> 'socket:[12082]'
12083 lrwx------ 1 root root 64 6月 21 10:03 22 -> 'socket:[12084]'
117912 lrwx------ 1 root root 64 6月 21 20:37 23 -> 'socket:[12085]'
117972 lrwx------ 1 root root 64 6月 21 20:37 230 -> 'socket:[43996]'
12087 lrwx------ 1 root root 64 6月 21 10:03 24 -> 'anon_inode:[pidfd]'
117973 lrwx------ 1 root root 64 6月 21 20:37 244 -> 'socket:[43992]'
117974 lrwx------ 1 root root 64 6月 21 20:37 245 -> 'socket:[43985]'
117975 lrwx------ 1 root root 64 6月 21 20:37 246 -> 'socket:[43979]'
117976 lrwx------ 1 root root 64 6月 21 20:37 247 -> 'socket:[43974]'
117977 lrwx------ 1 root root 64 6月 21 20:37 248 -> 'socket:[43959]'
117978 lrwx------ 1 root root 64 6月 21 20:37 249 -> 'socket:[43958]'
117913 lrwx------ 1 root root 64 6月 21 20:37 25 -> 'anon_inode:[pidfd]'
117979 lrwx------ 1 root root 64 6月 21 20:37 250 -> 'socket:[43948]'
117980 lrwx------ 1 root root 64 6月 21 20:37 251 -> 'socket:[43933]'
117981 lrwx------ 1 root root 64 6月 21 20:37 252 -> 'socket:[43932]'
117982 lrwx------ 1 root root 64 6月 21 20:37 253 -> 'socket:[43931]'
117983 lrwx------ 1 root root 64 6月 21 20:37 254 -> 'socket:[43909]'
117984 lrwx------ 1 root root 64 6月 21 20:37 255 -> 'socket:[43907]'
117985 lrwx------ 1 root root 64 6月 21 20:37 256 -> 'socket:[45563]'
117986 lrwx------ 1 root root 64 6月 21 20:37 259 -> 'socket:[45529]'
117914 lrwx------ 1 root root 64 6月 21 20:37 26 -> 'anon_inode:[pidfd]'
117987 lrwx------ 1 root root 64 6月 21 20:37 260 -> 'socket:[45432]'
117988 lrwx------ 1 root root 64 6月 21 20:37 261 -> 'socket:[43774]'
117989 lrwx------ 1 root root 64 6月 21 20:37 266 -> 'socket:[45383]'
117990 lrwx------ 1 root root 64 6月 21 20:37 267 -> 'socket:[45372]'
117915 lrwx------ 1 root root 64 6月 21 20:37 27 -> 'anon_inode:[pidfd]'
117991 lrwx------ 1 root root 64 6月 21 20:37 270 -> 'socket:[43738]'
117992 lrwx------ 1 root root 64 6月 21 20:37 271 -> 'socket:[45326]'
117993 lrwx------ 1 root root 64 6月 21 20:37 272 -> 'socket:[45325]'
117994 lrwx------ 1 root root 64 6月 21 20:37 275 -> 'socket:[43700]'
117995 lrwx------ 1 root root 64 6月 21 20:37 276 -> 'socket:[43698]'
117996 lrwx------ 1 root root 64 6月 21 20:37 277 -> 'socket:[45226]'
117916 lrwx------ 1 root root 64 6月 21 20:37 28 -> 'socket:[12090]'
117997 lrwx------ 1 root root 64 6月 21 20:37 280 -> 'socket:[45189]'
117998 lrwx------ 1 root root 64 6月 21 20:37 281 -> 'socket:[45178]'
117999 lrwx------ 1 root root 64 6月 21 20:37 282 -> 'socket:[43661]'
118000 lrwx------ 1 root root 64 6月 21 20:37 286 -> 'socket:[43659]'
46179 lrwx------ 1 root root 64 6月 21 10:03 287 -> 'socket:[43658]'
118001 lrwx------ 1 root root 64 6月 21 20:37 288 -> 'socket:[45115]'
118002 lrwx------ 1 root root 64 6月 21 20:37 289 -> 'socket:[45094]'
36368 lrwx------ 1 root root 64 6月 21 10:03 29 -> 'anon_inode:[pidfd]'
46180 lrwx------ 1 root root 64 6月 21 10:03 293 -> 'socket:[45093]'
118003 lrwx------ 1 root root 64 6月 21 20:37 294 -> 'socket:[44037]'
118004 lrwx------ 1 root root 64 6月 21 20:37 295 -> 'socket:[42149]'
118005 lrwx------ 1 root root 64 6月 21 20:37 296 -> 'socket:[37519]'
118006 lrwx------ 1 root root 64 6月 21 20:37 297 -> 'socket:[39499]'
46215 lrwx------ 1 root root 64 6月 21 10:03 298 -> 'socket:[37253]'
118007 lrwx------ 1 root root 64 6月 21 20:37 299 -> 'socket:[37166]'
24043 l-wx------ 1 root root 64 6月 21 10:03 3 -> /dev/kmsg
12092 lr-x------ 1 root root 64 6月 21 10:03 30 -> /dev/autofs
46216 lrwx------ 1 root root 64 6月 21 10:03 300 -> 'socket:[37145]'
118008 lrwx------ 1 root root 64 6月 21 20:37 303 -> 'socket:[24756]'
118009 lrwx------ 1 root root 64 6月 21 20:37 304 -> 'socket:[32467]'
118010 lrwx------ 1 root root 64 6月 21 20:37 306 -> 'socket:[32468]'
118011 lrwx------ 1 root root 64 6月 21 20:37 307 -> 'socket:[32588]'
118012 lrwx------ 1 root root 64 6月 21 20:37 308 -> 'socket:[34066]'
118013 lrwx------ 1 root root 64 6月 21 20:37 309 -> 'socket:[35126]'
36337 lrwx------ 1 root root 64 6月 21 10:03 31 -> 'anon_inode:[pidfd]'
118014 lrwx------ 1 root root 64 6月 21 20:37 310 -> 'socket:[35164]'
118015 lrwx------ 1 root root 64 6月 21 20:37 311 -> 'socket:[35206]'
118016 lrwx------ 1 root root 64 6月 21 20:37 313 -> 'socket:[35243]'
118017 lrwx------ 1 root root 64 6月 21 20:37 314 -> 'socket:[35257]'
118018 lrwx------ 1 root root 64 6月 21 20:37 315 -> 'socket:[35277]'
118019 lrwx------ 1 root root 64 6月 21 20:37 316 -> 'socket:[35289]'
118020 lrwx------ 1 root root 64 6月 21 20:37 318 -> 'socket:[35309]'
118021 lrwx------ 1 root root 64 6月 21 20:37 319 -> 'socket:[35356]'
36338 lrwx------ 1 root root 64 6月 21 10:03 32 -> 'anon_inode:[pidfd]'
118022 lrwx------ 1 root root 64 6月 21 20:37 320 -> 'socket:[35357]'
118023 lrwx------ 1 root root 64 6月 21 20:37 321 -> 'socket:[34612]'
118024 lrwx------ 1 root root 64 6月 21 20:37 322 -> 'socket:[39061]'
118025 lrwx------ 1 root root 64 6月 21 20:37 323 -> 'socket:[39098]'
118026 lrwx------ 1 root root 64 6月 21 20:37 324 -> 'socket:[58419]'
118027 lrwx------ 1 root root 64 6月 21 20:37 327 -> 'socket:[71717]'
12095 lrwx------ 1 root root 64 6月 21 10:03 33 -> 'anon_inode:[pidfd]'
118028 lrwx------ 1 root root 64 6月 21 20:37 332 -> /run/initctl
118029 lrwx------ 1 root root 64 6月 21 20:37 333 -> 'anon_inode:[pidfd]'
118030 lrwx------ 1 root root 64 6月 21 20:37 335 -> 'socket:[38495]'
118031 lrwx------ 1 root root 64 6月 21 20:37 336 -> 'anon_inode:[pidfd]'
118032 lrwx------ 1 root root 64 6月 21 20:37 337 -> 'anon_inode:[pidfd]'
118033 lrwx------ 1 root root 64 6月 21 20:37 338 -> 'socket:[12108]'
12099 lrwx------ 1 root root 64 6月 21 10:03 34 -> 'anon_inode:[pidfd]'
118034 lr-x------ 1 root root 64 6月 21 20:37 342 -> 'pipe:[12094]'
118035 lrwx------ 1 root root 64 6月 21 20:37 343 -> 'socket:[35846]'
118036 lrwx------ 1 root root 64 6月 21 20:37 345 -> 'anon_inode:[pidfd]'
118037 lrwx------ 1 root root 64 6月 21 20:37 346 -> 'anon_inode:[pidfd]'
118038 lrwx------ 1 root root 64 6月 21 20:37 349 -> 'anon_inode:[pidfd]'
117917 lrwx------ 1 root root 64 6月 21 20:37 35 -> 'anon_inode:[pidfd]'
118039 lrwx------ 1 root root 64 6月 21 20:37 350 -> /dev/input/event5
118040 lrwx------ 1 root root 64 6月 21 20:37 351 -> /dev/input/event2
118041 lrwx------ 1 root root 64 6月 21 20:37 352 -> /dev/input/event3
118042 lrwx------ 1 root root 64 6月 21 20:37 356 -> /dev/input/event1
118043 lrwx------ 1 root root 64 6月 21 20:37 357 -> /dev/input/event4
118044 lrwx------ 1 root root 64 6月 21 20:37 358 -> /dev/input/event0
12101 lrwx------ 1 root root 64 6月 21 10:03 36 -> 'anon_inode:[pidfd]'
118045 lrwx------ 1 root root 64 6月 21 20:37 360 -> /dev/dri/card0
118046 lrwx------ 1 root root 64 6月 21 20:37 366 -> 'socket:[12110]'
118047 lrwx------ 1 root root 64 6月 21 20:37 369 -> 'socket:[30862]'
12103 lrwx------ 1 root root 64 6月 21 10:03 37 -> 'anon_inode:[pidfd]'
118048 lrwx------ 1 root root 64 6月 21 20:37 370 -> 'anon_inode:[pidfd]'
118049 lrwx------ 1 root root 64 6月 21 20:37 372 -> 'anon_inode:[pidfd]'
118050 lrwx------ 1 root root 64 6月 21 20:37 373 -> 'anon_inode:[pidfd]'
118051 lrwx------ 1 root root 64 6月 21 20:37 374 -> 'socket:[12102]'
118052 lrwx------ 1 root root 64 6月 21 20:37 375 -> 'socket:[12104]'
118053 lrwx------ 1 root root 64 6月 21 20:37 376 -> 'anon_inode:[pidfd]'
118054 lrwx------ 1 root root 64 6月 21 20:37 377 -> 'anon_inode:[pidfd]'
12105 lr-x------ 1 root root 64 6月 21 10:03 38 -> anon_inode:inotify
118055 lrwx------ 1 root root 64 6月 21 20:37 380 -> /dev/rfkill
118056 lrwx------ 1 root root 64 6月 21 20:37 381 -> 'anon_inode:[pidfd]'
118057 lrwx------ 1 root root 64 6月 21 20:37 382 -> 'socket:[12106]'
118058 lrwx------ 1 root root 64 6月 21 20:37 384 -> 'anon_inode:[pidfd]'
118059 lrwx------ 1 root root 64 6月 21 20:37 385 -> 'anon_inode:[pidfd]'
118060 lrwx------ 1 root root 64 6月 21 20:37 386 -> 'anon_inode:[pidfd]'
118061 lrwx------ 1 root root 64 6月 21 20:37 388 -> 'anon_inode:[pidfd]'
12107 lrwx------ 1 root root 64 6月 21 10:03 39 -> 'anon_inode:[pidfd]'
118062 lrwx------ 1 root root 64 6月 21 20:37 395 -> 'anon_inode:[pidfd]'
118063 lrwx------ 1 root root 64 6月 21 20:37 396 -> 'anon_inode:[pidfd]'
118064 lrwx------ 1 root root 64 6月 21 20:37 399 -> 'socket:[12100]'
19273 lrwx------ 1 root root 64 6月 21 10:03 4 -> 'anon_inode:[eventpoll]'
12109 lrwx------ 1 root root 64 6月 21 10:03 40 -> 'anon_inode:[pidfd]'
118065 lrwx------ 1 root root 64 6月 21 20:37 404 -> 'anon_inode:[pidfd]'
118066 lrwx------ 1 root root 64 6月 21 20:37 407 -> 'anon_inode:[pidfd]'
117918 lrwx------ 1 root root 64 6月 21 20:37 41 -> 'anon_inode:[timerfd]'
118067 lrwx------ 1 root root 64 6月 21 20:37 414 -> 'anon_inode:[pidfd]'
118068 lrwx------ 1 root root 64 6月 21 20:37 415 -> 'socket:[35848]'
118069 lrwx------ 1 root root 64 6月 21 20:37 416 -> 'anon_inode:[pidfd]'
117919 lrwx------ 1 root root 64 6月 21 20:37 42 -> 'socket:[12195]'
118070 lrwx------ 1 root root 64 6月 21 20:37 421 -> 'anon_inode:[pidfd]'
118071 lrwx------ 1 root root 64 6月 21 20:37 423 -> 'anon_inode:[pidfd]'
118072 lrwx------ 1 root root 64 6月 21 20:37 427 -> 'anon_inode:[pidfd]'
118073 lrwx------ 1 root root 64 6月 21 20:37 428 -> 'anon_inode:[pidfd]'
117920 lrwx------ 1 root root 64 6月 21 20:37 43 -> 'socket:[22844]'
118074 lrwx------ 1 root root 64 6月 21 20:37 430 -> 'socket:[12097]'
118075 lrwx------ 1 root root 64 6月 21 20:37 431 -> 'anon_inode:[pidfd]'
118076 lrwx------ 1 root root 64 6月 21 20:37 432 -> 'socket:[25493]'
118077 lrwx------ 1 root root 64 6月 21 20:37 433 -> 'anon_inode:[pidfd]'
118078 lrwx------ 1 root root 64 6月 21 20:37 434 -> 'anon_inode:[pidfd]'
118079 lrwx------ 1 root root 64 6月 21 20:37 436 -> 'socket:[35849]'
118080 lrwx------ 1 root root 64 6月 21 20:37 437 -> 'socket:[35850]'
118081 lrwx------ 1 root root 64 6月 21 20:37 438 -> 'anon_inode:[pidfd]'
118082 lrwx------ 1 root root 64 6月 21 20:37 439 -> 'socket:[12098]'
24669 lrwx------ 1 root root 64 6月 21 10:03 44 -> 'anon_inode:[pidfd]'
118083 lrwx------ 1 root root 64 6月 21 20:37 440 -> 'anon_inode:[pidfd]'
118084 lrwx------ 1 root root 64 6月 21 20:37 441 -> 'anon_inode:[pidfd]'
118085 lrwx------ 1 root root 64 6月 21 20:37 442 -> 'socket:[35855]'
117921 lrwx------ 1 root root 64 6月 21 20:37 45 -> 'anon_inode:[timerfd]'
22908 lrwx------ 1 root root 64 6月 21 10:03 46 -> 'anon_inode:[pidfd]'
30863 lrwx------ 1 root root 64 6月 21 10:03 47 -> 'anon_inode:[pidfd]'
117922 lrwx------ 1 root root 64 6月 21 20:37 48 -> 'anon_inode:[pidfd]'
24632 lrwx------ 1 root root 64 6月 21 10:03 49 -> 'anon_inode:[timerfd]'
117904 lrwx------ 1 root root 64 6月 21 20:37 5 -> 'anon_inode:[signalfd]'
117923 lrwx------ 1 root root 64 6月 21 20:37 50 -> 'anon_inode:[pidfd]'
117924 lrwx------ 1 root root 64 6月 21 20:37 51 -> 'anon_inode:[pidfd]'
117925 lr-x------ 1 root root 64 6月 21 20:37 52 -> anon_inode:inotify
117926 lr-x------ 1 root root 64 6月 21 20:37 53 -> anon_inode:inotify
117927 lrwx------ 1 root root 64 6月 21 20:37 54 -> 'anon_inode:[pidfd]'
30865 lrwx------ 1 root root 64 6月 21 10:03 55 -> 'anon_inode:[pidfd]'
117928 lrwx------ 1 root root 64 6月 21 20:37 56 -> 'anon_inode:[pidfd]'
24609 lrwx------ 1 root root 64 6月 21 10:03 57 -> 'anon_inode:[pidfd]'
117929 lrwx------ 1 root root 64 6月 21 20:37 58 -> 'anon_inode:[pidfd]'
32062 lrwx------ 1 root root 64 6月 21 10:03 59 -> 'anon_inode:[pidfd]'
117905 lr-x------ 1 root root 64 6月 21 20:37 6 -> anon_inode:inotify
24824 lrwx------ 1 root root 64 6月 21 10:03 60 -> 'anon_inode:[pidfd]'
117930 lrwx------ 1 root root 64 6月 21 20:37 61 -> 'anon_inode:[pidfd]'
24825 lrwx------ 1 root root 64 6月 21 10:03 62 -> anon_inode:bpf-prog
117931 lrwx------ 1 root root 64 6月 21 20:37 63 -> /sys/fs/cgroup/init.scope/memory.pressure
31091 lrwx------ 1 root root 64 6月 21 10:03 64 -> anon_inode:bpf-prog
58694 lrwx------ 1 root root 64 6月 21 10:09 65 -> anon_inode:bpf-prog
34001 lrwx------ 1 root root 64 6月 21 10:03 66 -> anon_inode:bpf-prog
117932 lrwx------ 1 root root 64 6月 21 20:37 67 -> 'socket:[32587]'
117933 lrwx------ 1 root root 64 6月 21 20:37 68 -> anon_inode:bpf-prog
117934 lrwx------ 1 root root 64 6月 21 20:37 69 -> anon_inode:bpf-prog
24044 lr-x------ 1 root root 64 6月 21 10:03 7 -> /sys/fs/cgroup
117935 lrwx------ 1 root root 64 6月 21 20:37 70 -> anon_inode:bpf-prog
117936 lrwx------ 1 root root 64 6月 21 20:37 71 -> anon_inode:bpf-prog
25316 lrwx------ 1 root root 64 6月 21 10:03 72 -> anon_inode:bpf-prog
25214 lrwx------ 1 root root 64 6月 21 10:03 73 -> anon_inode:bpf-prog
25210 lrwx------ 1 root root 64 6月 21 10:03 74 -> anon_inode:bpf-prog
12158 lrwx------ 1 root root 64 6月 21 10:03 75 -> anon_inode:bpf-prog
25395 lrwx------ 1 root root 64 6月 21 10:03 76 -> anon_inode:bpf-prog
35851 lrwx------ 1 root root 64 6月 21 10:03 77 -> anon_inode:bpf-prog
71280 lrwx------ 1 root root 64 6月 21 11:53 79 -> anon_inode:bpf-prog
117906 lrwx------ 1 root root 64 6月 21 20:37 8 -> 'anon_inode:[pidfd]'
117937 lrwx------ 1 root root 64 6月 21 20:37 80 -> anon_inode:bpf-prog
25494 lrwx------ 1 root root 64 6月 21 10:03 81 -> anon_inode:bpf-prog
25498 lrwx------ 1 root root 64 6月 21 10:03 82 -> anon_inode:bpf-prog
25501 lrwx------ 1 root root 64 6月 21 10:03 84 -> 'socket:[12079]'
57198 lrwx------ 1 root root 64 6月 21 10:06 85 -> 'socket:[12080]'
117938 lrwx------ 1 root root 64 6月 21 20:37 86 -> 'socket:[12081]'
117939 lrwx------ 1 root root 64 6月 21 20:37 87 -> 'socket:[35995]'
117940 lrwx------ 1 root root 64 6月 21 20:37 89 -> 'socket:[35996]'
24469 l--------- 1 root root 64 6月 21 10:03 9 -> /usr/lib/systemd/systemd-executor
62486 lrwx------ 1 root root 64 6月 21 10:09 92 -> 'socket:[40863]'
117941 lrwx------ 1 root root 64 6月 21 20:37 93 -> 'socket:[40864]'
117942 lrwx------ 1 root root 64 6月 21 20:37 94 -> 'anon_inode:[pidfd]'
41150 lrwx------ 1 root root 64 6月 21 10:03 95 -> 'socket:[73511]'
11838 dr-xr-xr-x 9 root root 0 6月 21 10:03 ..

/proc/PID/task/TID可访问进程中的各个线程信息。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
$ ls /proc/1/task -Sail
总计 0
19275 dr-xr-xr-x 3 root root 0 6月 21 10:03 .
11838 dr-xr-xr-x 9 root root 0 6月 21 10:03 ..
19276 dr-xr-xr-x 7 root root 0 6月 21 10:03 1
cat /proc/1/task/1/status
Name: systemd
Umask: 0000
State: S (sleeping)
Tgid: 1
Ngid: 0
Pid: 1
PPid: 0
TracerPid: 0
Uid: 0 0 0 0
Gid: 0 0 0 0
FDSize: 512
Groups:
NStgid: 1
NSpid: 1
NSpgid: 1
NSsid: 1
VmPeak: 23576 kB
VmSize: 23540 kB
VmLck: 0 kB
VmPin: 0 kB
VmHWM: 14180 kB
VmRSS: 14180 kB
RssAnon: 4992 kB
RssFile: 9188 kB
RssShmem: 0 kB
VmData: 4516 kB
VmStk: 132 kB
VmExe: 44 kB
VmLib: 12188 kB
VmPTE: 84 kB
VmSwap: 0 kB
HugetlbPages: 0 kB
CoreDumping: 0
THP_enabled: 1
Threads: 1
SigQ: 1/15164
SigPnd: 0000000000000000
ShdPnd: 0000000000000000
SigBlk: 7fefc1fe28014a03
SigIgn: 0000000000001000
SigCgt: 00000000000004ec
CapInh: 0000000000000000
CapPrm: 000001ffffffffff
CapEff: 000001ffffffffff
CapBnd: 000001ffffffffff
CapAmb: 0000000000000000
NoNewPrivs: 0
Seccomp: 0
Seccomp_filters: 0
Speculation_Store_Bypass: thread vulnerable
SpeculationIndirectBranch: conditional enabled
Cpus_allowed: ffffffff,ffffffff,ffffffff,ffffffff
Cpus_allowed_list: 0-127
Mems_allowed: 00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000001
Mems_allowed_list: 0
voluntary_ctxt_switches: 8981
nonvoluntary_ctxt_switches: 4778

其他/proc文件系统每个进程目录下的常见文件或目录:

目录/文件 描述
/proc/PID/cmdline 命令行参数,以“\0”分隔
/proc/PID/cwd 当前工作目录的符号链接
/proc/PID/environ 环境列表,以“\0”分隔
/proc/PID/exe 指向正在执行文件符号链接
/proc/PID/maps 内存映射
/proc/PID/mem 进程虚拟内存
/proc/PID/mounts 进程安装点
/proc/PID/root 指向根目录的符号链接
/proc/net 网络和套接字状态信息
/proc/sys/fs 文件系统相关
/proc/sys/kernel 常规内核设置
/proc/sys/net 网络和套接字设置
/proc/sys/vm 内存管理设置
/proc/sysvipc System V IPC对象相关
/proc/version 系统版本信息

uname

获取主机系统标识信息。

1
2
3
4
5
6
7
8
9
10
11
12
#include <sys/utsname.h>
struct utsname {
char sysname[]; /* Operating system name (e.g., "Linux") */
char nodename[]; /* Name within communications network to which the node is attached, if any */
char release[]; /* Operating system release (e.g., "2.6.28") */
char version[]; /* Operating system version */
char machine[]; /* Hardware type identifier */
char domainname[]; /* NIS or YP domain name */
};
int uname(
struct utsname *buf
); //成功0 错误-1

utsname的sysname、release和version字段分别从下面文件中获取:

1
2
3
4
5
6
$ cat /proc/sys/kernel/ostype
Linux
$ cat /proc/sys/kernel/osrelease
6.2.0-39-generic
$ cat /proc/sys/kernel/version
#40-Ubuntu SMP PREEMPT_DYNAMIC Tue Nov 14 14:18:00 UTC 2023

nodename和domainname字段分别可由sethostnamesetdomainname设置。可分别用gethostnamegetdomainname获取系统主机名和NIS域名,也可分别查看文件:

1
2
3
4
$ cat /proc/sys/kernel/hostname
monoceros406-VMware-Virtual-Platform
$ cat /proc/sys/kernel/domainname
(none)