简体   繁体   中英

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. 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:

  • 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.

It sounds like you don't know where to start. Let me try to explain the information in /proc :

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

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

(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.

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);
}

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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