如何在Linux上使用C从/proc文件的内容中提取信息? [英] How to extract information from the content of /proc files on Linux using C?
问题描述
我每天工作7个小时以上,共5天.我并不是最好的编码器,所以我需要一些帮助.我需要知道如何在Linux上使用C程序从/proc获取信息. 该信息必须打印出来,并包括以下内容:
I have been working on this for over 7 hours a day for 5 days. I am not exactly the best coder, so I need some help. I need to know how should I get the info from /proc using a C program on Linux. The info has to be printed out and include the following:
- 过程的完整命令行.
- 进程状态.
- 父母的PID.
- 优先.
- 很好的值.
- 实时调度优先级.
- 最后一次执行的CPU数量.
- 已在用户模式下计划了此过程的时间.
- 已在内核模式下计划了此过程的时间量.
- 虚拟内存大小(以字节为单位).
- 程序总大小(以页面为单位).
- 居民设置大小(RSS)以字节为单位.
- 常驻集大小(RSS):进程的页面数已存在于真实内存中 页面.
- 页面中的文本(代码)大小.
- 页面中的数据+堆栈大小.
- 页面表条目的大小(KB)
- 数据大小(KB).
- 堆栈大小(KB).
- 文本段大小KB.
- The complete command line for the process.
- State of the process.
- The PID of the parent.
- Priority.
- The nice value.
- Realtime scheduling priority.
- CPU number last executed on.
- Amount of time that this process has been scheduled in user mode.
- Amount of time that this process has been scheduled in kernel mode.
- Virtual memory size in bytes.
- Total program size in pages.
- Resident Set Size (RSS) in bytes.
- Resident Set Size (RSS): number of pages the process has in real memory in pages.
- Text (code) size in pages.
- Data + stack size in pages.
- Page table entries size in KB.
- Size of data in KB.
- Size of stack in KB.
- Size of text segment KB.
推荐答案
听起来您不知道从哪里开始.让我尝试解释/proc
中的信息:
It sounds like you don't know where to start. Let me try to explain the information in /proc
:
如果我们cat /proc/29519/stat
,我们会获得以下信息:
If we cat /proc/29519/stat
, we get this info:
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
所有这些数字代表什么?答案在/proc/[pid]/stat
部分的 man proc 中.由此可见,前四件事是:
What do all those numbers represent? The answer is in man proc, in the section called /proc/[pid]/stat
. From this we see the first four things are:
pid%d
pid %d
(1)进程ID.
通讯%s
(2)可执行文件的文件名,用括号括起来.这是可见的 可执行文件是否被换出.
(2) The filename of the executable, in parentheses. This is visible whether or not the executable is swapped out.
状态%c
(3)字符串"RSDZTW"中的一个字符,其中R正在运行,S是 在不间断的等待中睡觉,D在不间断的等待中 磁盘睡眠,Z是僵尸,T被跟踪或停止(根据信号),W 正在分页.
(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.
点%d
(4)父级的PID.
有了这些知识,我们可以用fscanf(f, "%d %s %c %d", ...)
解析它:
With this knowledge we can parse it out with 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
,我会得到
Now if I compile that file and run ./a.out 29519
, I get
pid = 29519
comm = (vim)
state = S
parent pid = 5997
这是否给您足够的信息以开始使用?
Does that give you enough information to get started?
这篇关于如何在Linux上使用C从/proc文件的内容中提取信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!