简体   繁体   English

如何使用 C 从 Linux 上的 /proc 文件的内容中提取信息?

[英]How to extract information from the content of /proc files on Linux using C?

I have been working on this for over 7 hours a day for 5 days.我一直在为此工作 5 天,每天超过 7 小时。 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.我需要知道如何在 Linux 上使用 C 程序从 /proc 获取信息。 The info has to be printed out and include the following:信息必须打印出来,并包括以下内容:

  • The complete command line for the process.进程的完整命令行。
  • State of the process.进程状态。
  • The PID of the parent.父进程的PID。
  • Priority.优先事项。
  • The nice value.不错的价值。
  • Realtime scheduling priority.实时调度优先。
  • CPU number last executed on.上次执行的 CPU 编号。
  • 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.驻留集大小 (RSS)(以字节为单位)。
  • Resident Set Size (RSS): number of pages the process has in real memory in pages.驻留集大小 (RSS):进程在实际内存中的页数(以页为单位)。
  • Text (code) size in pages.页中的文本(代码)大小。
  • Data + stack size in pages.数据 + 堆栈大小(以页为单位)。
  • Page table entries size in KB.页表条目大小(以 KB 为单位)。
  • Size of data in KB.以 KB 为单位的数据大小。
  • Size of stack in KB.以 KB 为单位的堆栈大小。
  • Size of text segment KB.文本段 KB 的大小。

It sounds like you don't know where to start.听起来你不知道从哪里开始。 Let me try to explain the information in /proc :让我尝试解释/proc的信息:

If we cat /proc/29519/stat , we get this info:如果我们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

What do all those numbers represent?所有这些数字代表什么? The answer is in man proc , in the section called /proc/[pid]/stat .答案在man proc 中,在名为/proc/[pid]/stat From this we see the first four things are:由此我们看到前四件事是:

pid %d pid %d

(1) The process ID. (1) 进程标识。

comm %s通讯 %s

(2) The filename of the executable, in parentheses. (2) 可执行文件的文件名,在括号中。 This is visible whether or not the executable is swapped out.无论可执行文件是否被换出,这都是可见的。

state %c状态 %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. (3) 字符串“RSDZTW”中的一个字符,其中 R 正在运行,S 在可中断等待中休眠,D 在磁盘不可中断休眠中等待,Z 是僵尸,T 被跟踪或停止(在信号上),W 是分页。

ppid %d ppid %d

(4) The PID of the parent. (4) 父母的PID。

With this knowledge we can parse it out with fscanf(f, "%d %s %c %d", ...) :有了这些知识,我们可以用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);
}

Now if I compile that file and run ./a.out 29519 , I get现在,如果我编译该文件并运行./a.out 29519 ,我会得到

pid = 29519
comm = (vim)
state = S
parent pid = 5997

Does that give you enough information to get started?这是否为您提供了足够的信息以开始使用?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM