如何使用 C 从 Linux 上的 /proc 文件的内容中提取信息?
How to extract information from the content of /proc files on Linux using C?
我已经为此工作了 5 天,每天工作 7 个多小时。我不是最好的编码员,所以我需要一些帮助。我需要知道如何使用 Linux 上的 C 程序从 /proc 获取信息。
信息必须打印出来并包括以下内容:
- 该过程的完整命令行。
- 过程状态。
- 父级的 PID。
- 优先。
- 物有所值。
- 实时调度优先级。
- CPU 上次执行日期。
- 在用户模式下安排此进程的时间。
- 此进程已在内核模式下安排的时间。
- 虚拟内存大小(以字节为单位)。
- 程序总大小(以页为单位)。
- 驻留集大小 (RSS),以字节为单位。
- 常驻集大小 (RSS):进程在实际内存中的页数
页数。
- 页面中的文本(代码)大小。
- 数据 + 页面堆栈大小。
- 页面 table 条目大小(以 KB 为单位)。
- 数据大小(以 KB 为单位)。
- 堆栈大小(以 KB 为单位)。
- 文本段的大小 KB。
听起来你不知道从哪里开始。让我试着解释一下 /proc
:
中的信息
如果我们cat /proc/29519/stat
,我们得到这个信息:
29519 (vim) S 5997 29519 5997 34835 29519 24576 1275 0 47 0 5 0 0 0 20 0 2 0 49083340 188043264 3718 18446744073709551615 4194304 6665820 140737488349264 140737488347024 140737280970147 0 0 12288 1837256447 18446744073709551615 0 0 17 3 0 0 21 0 0 8764120 8861948 8925184 140737488349925 140737488349929 140737488349929 140737488351211 0
所有这些数字代表什么?答案在 man proc 中,在名为 /proc/[pid]/stat
的部分中。从这里我们看到前四件事是:
pid %d
(1) The process ID.
comm %s
(2) The filename of the executable, in parentheses. This is visible
whether or not the executable is swapped out.
state %c
(3) One character from the string "RSDZTW" where R is running, S is
sleeping in an interruptible wait, D is waiting in uninterruptible
disk sleep, Z is zombie, T is traced or stopped (on a signal), and W
is paging.
ppid %d
(4) The PID of the parent.
有了这些知识,我们可以用 fscanf(f, "%d %s %c %d", ...)
:
解析它
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
void main(int argc, char **argv) {
int pid;
sscanf(argv[1], "%d", &pid);
printf("pid = %d\n", pid);
char filename[1000];
sprintf(filename, "/proc/%d/stat", pid);
FILE *f = fopen(filename, "r");
int unused;
char comm[1000];
char state;
int ppid;
fscanf(f, "%d %s %c %d", &unused, comm, &state, &ppid);
printf("comm = %s\n", comm);
printf("state = %c\n", state);
printf("parent pid = %d\n", ppid);
fclose(f);
}
现在,如果我编译该文件并运行 ./a.out 29519
,我得到
pid = 29519
comm = (vim)
state = S
parent pid = 5997
这是否为您提供了足够的入门信息?
我已经为此工作了 5 天,每天工作 7 个多小时。我不是最好的编码员,所以我需要一些帮助。我需要知道如何使用 Linux 上的 C 程序从 /proc 获取信息。 信息必须打印出来并包括以下内容:
- 该过程的完整命令行。
- 过程状态。
- 父级的 PID。
- 优先。
- 物有所值。
- 实时调度优先级。
- CPU 上次执行日期。
- 在用户模式下安排此进程的时间。
- 此进程已在内核模式下安排的时间。
- 虚拟内存大小(以字节为单位)。
- 程序总大小(以页为单位)。
- 驻留集大小 (RSS),以字节为单位。
- 常驻集大小 (RSS):进程在实际内存中的页数 页数。
- 页面中的文本(代码)大小。
- 数据 + 页面堆栈大小。
- 页面 table 条目大小(以 KB 为单位)。
- 数据大小(以 KB 为单位)。
- 堆栈大小(以 KB 为单位)。
- 文本段的大小 KB。
听起来你不知道从哪里开始。让我试着解释一下 /proc
:
如果我们cat /proc/29519/stat
,我们得到这个信息:
29519 (vim) S 5997 29519 5997 34835 29519 24576 1275 0 47 0 5 0 0 0 20 0 2 0 49083340 188043264 3718 18446744073709551615 4194304 6665820 140737488349264 140737488347024 140737280970147 0 0 12288 1837256447 18446744073709551615 0 0 17 3 0 0 21 0 0 8764120 8861948 8925184 140737488349925 140737488349929 140737488349929 140737488351211 0
所有这些数字代表什么?答案在 man proc 中,在名为 /proc/[pid]/stat
的部分中。从这里我们看到前四件事是:
pid %d
(1) The process ID.
comm %s
(2) The filename of the executable, in parentheses. This is visible whether or not the executable is swapped out.
state %c
(3) One character from the string "RSDZTW" where R is running, S is sleeping in an interruptible wait, D is waiting in uninterruptible disk sleep, Z is zombie, T is traced or stopped (on a signal), and W is paging.
ppid %d
(4) The PID of the parent.
有了这些知识,我们可以用 fscanf(f, "%d %s %c %d", ...)
:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
void main(int argc, char **argv) {
int pid;
sscanf(argv[1], "%d", &pid);
printf("pid = %d\n", pid);
char filename[1000];
sprintf(filename, "/proc/%d/stat", pid);
FILE *f = fopen(filename, "r");
int unused;
char comm[1000];
char state;
int ppid;
fscanf(f, "%d %s %c %d", &unused, comm, &state, &ppid);
printf("comm = %s\n", comm);
printf("state = %c\n", state);
printf("parent pid = %d\n", ppid);
fclose(f);
}
现在,如果我编译该文件并运行 ./a.out 29519
,我得到
pid = 29519
comm = (vim)
state = S
parent pid = 5997
这是否为您提供了足够的入门信息?