Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get information of /proc/[pid]/status

Is there a defined structure for getting each field of this file for a particular process instead of parsing the file?

like image 297
Bionix1441 Avatar asked Sep 18 '15 15:09

Bionix1441


People also ask

How can I check my pid status?

A PID is automatically assigned to each process when it is created. A process is nothing but running instance of a program and each process has a unique PID on a Unix-like system. The easiest way to find out if process is running is run ps aux command and grep process name.

What does proc pid Stat show?

Yes the value contained in /proc/[PID]/stat allows to determine the amount of CPU time used by a process and its children.

How do you find pid in proc?

If you want a list of PIDs of currently running processes, you can use opendir() and readdir() to open /proc and iterate over the list of files/directories in there. Then you can check for directories having a filename that is a number.

How often is proc pid Stat updated?

In test environment, this file refreshed 1 ~ 2 sec, so I assume this file often updated by system at least 1 sec. But, In commercial servers environment, process run with high load (cpu and file IO), /proc/[pid]/stat file update period increasing even 20~60 sec!!


1 Answers

The /proc/pid pseudo-filesystem was created in order to make access to a ton of kernel data accessible to other programs without being tied to binary structures. while /proc/pid/status was designed to

Provides much of the information in /proc/[pid]/stat and /proc/[pid]/statm in a format that's easier for humans to parse. Here's an example:

$ cat /proc/$$/status
Name:   bash
State:  S (sleeping)
Tgid:   3515
Pid:    3515
PPid:   3452
...

This was in contrast to much older mechanisms like stat(2) which required binary structures like

struct stat {
    dev_t     st_dev;     /* ID of device containing file */
    ino_t     st_ino;     /* inode number */
    mode_t    st_mode;    /* protection */
    nlink_t   st_nlink;   /* number of hard links */
    uid_t     st_uid;     /* user ID of owner */
    gid_t     st_gid;     /* group ID of owner */
    dev_t     st_rdev;    /* device ID (if special file) */
    ...
};

If you want a more machine readable version of /proc/pid/status you can use the lexically simpler stat and statm as described in proc(5)

like image 136
msw Avatar answered Oct 04 '22 20:10

msw