I want to retrieve some process info from /proc
directory and my question is the following: is there a standart 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 whitespace instead of \t
or smth like that?
Most users are familiar with the two primary types of files: text and binary. But the /proc/ directory contains another type of file called a virtual file. It is for this reason that /proc/ is often referred to as a virtual file system. These virtual files have unique qualities.
The /proc/[pid]/mem file and Virtual Memory In short, the /proc/[pid]/maps file provides us the address; the /proc/[pid]/mem provides an interface into the memory space holding our data.
The Linux /proc File System is a virtual filesystem that exists in RAM (i.e., it is not stored on the hard drive). That means that it exists only when the computer is turned on and running.
First of all, the documentation on /proc
in Linux is provided in the Linux sources, in Documentation/filesystems/proc.txt
. That should be the first place you look into if going to work with procfs. Sadly, AFAICS it doesn't mention the exact record format.
The second place to look is procps
sources (that is, the package which provides ps
tool). There you can find:
colon = strchr(S, ':');
if(unlikely(!colon)) break;
if(unlikely(colon[1]!='\t')) break;
which means that ps
relies on the :\t
being there. Therefore, you can assume that all current Linux kernels use this format. Moreover, I doubt that minor changes (like replacing the \t
with something else) would be considered important enough to break compatibility with the old versions of ps
tool.
That said, you can usually be more liberal in what you accept. Considering the specific contents of that file, you can assume the colon being field separator, and strip any whitespace following it. If you are using shell script, the regular field separation should suffice.
Lastly, I'd like to make a few points:
status
file is supposed to be human-readable. Therefore, programs are usually better reading the stat
file instead which is designed to be machine-oriented./proc
format.libprocps
library which comes with procps
instead of reading the files by hand. That way, you avoid re-inventing the wheel and relying on a specific format directly.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With