Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

/proc/PID files' format

Tags:

linux

process

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?

like image 265
Nellie Danielyan Avatar asked Nov 24 '12 10:11

Nellie Danielyan


People also ask

What types of files are located in the proc directory?

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.

What does proc PID maps contain?

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.

Where are proc files stored?

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.


1 Answers

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:

  1. The 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.
  2. I am considering Linux here only (based on tags). Different systems may have a bit different /proc format.
  3. If you are writing a C program (the shell tag was added by an editor), you should consider using the 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.
like image 117
Michał Górny Avatar answered Oct 13 '22 00:10

Michał Górny