/ proc / PID files

I want to get process information from the /proc directory, and my question is: is there a standard format for files in /proc/PID ?
For example, there is this proc/PID/status file with Name:'\t'ProcName in its first line. Can I meet this file elsewhere with a space instead of \t or smth, like this?

+4
source share
1 answer

First of all, documentation on /proc on Linux is provided in Linux sources, at Documentation/filesystems/proc.txt . This should be the first thing you look at if you intend to work with procfs. Unfortunately, AFAICS does not mention the exact recording format.

The second place to look is procps sources (i.e. the package that the ps tool provides). There you can find:

 colon = strchr(S, ':'); if(unlikely(!colon)) break; if(unlikely(colon[1]!='\t')) break; 

which means ps relies on :\t . Therefore, you can assume that all current Linux kernels use this format. Moreover, I doubt that minor changes (for example, replacing \t with something else) will be considered important enough to break compatibility with older versions of the ps tool.

However, you can usually be more liberal in what you accept. Given the specific contents of this file, you can assume that the colon is a field delimiter and remove spaces after it. If you are using a shell script, regular field separation should be sufficient.

Finally, I would like to make a few points:

  • The status file must be human readable. Therefore, programs usually read the stat file better, which is for machine orientation.
  • I only consider Linux here (tag based). Different systems may have a different /proc format bit.
  • If you are writing a C program (the shell tag was added by the editor), you should use the libprocps library, which comes with procps instead of reading files manually. This way you avoid reinventing the wheel and relying on a specific format directly.
+5
source

Source: https://habr.com/ru/post/1448082/


All Articles