Is there a defined structure for getting each field of this file for a particular process instead of parsing the file?
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.
Yes the value contained in /proc/[PID]/stat allows to determine the amount of CPU time used by a process and its children.
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.
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!!
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)
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