Using C program to output the memory occupation information of a process in Linux system
Preface
We should all have encountered a situation, in the actual work sometimes need the program to print out the memory occupation of a process for reference, here is a program implementation method to calculate the memory occupation of a process through the pseudo file system / proc under Linux. There is no more to say below, let's take a look at the detailed introduction.
Implementation analysis
First of all, why are there so-called fake files? The file types of Linux system can be divided into three categories: ordinary files, directory files and pseudo files. Pseudo files are not used to store data, so they do not take up disk space, they just exist in memory. / proc allows you to interact with kernel data to get useful information about the process.
Here are four files under / proc: / proc/stat, / proc/meminfo, / proc//stat, / proc//status.
/ proc/stat stores the cpu time of the system, which contains information about all cpu activities.
Cpu 72389 2891 16811 1148664 31374 0 67 00 0cpu0 17608 452 3786 288899 6210 0 30 00 0cpu1 18724 926 4598 285844 8911 0 15 00 0cpu2 16803 658 3726 288710 7220 0 700 0cpu3 19254 855 4700 285209 9032 0 13 00 0.
/ proc/meminfo stores the memory information of the system, and the information represented by each variable in the file can be known by the name of each variable.
MemTotal: 4046236 kBMemFree: 1054440 kBMemAvailable: 2460060 kBBuffers: 359688 kBCached: 1158056 kBSwapCached: 0 kBActive: 2020096 kBInactive: 677948 kBActive (anon): 1181376 kB.
/ proc//stat stores the cpu information of a process
2476 (firefox) S 1773 1910 1910 0-1 4210688 3413511 1712 757 1,45466 4629 720 057 020381 1774743552 150565 18446744073709551615 9484469301280948693126372 14073296184784 140732961858304 13947170914269 0409633572079 001720 011788094469526592 94844695228536 948447553287296186764140732961867668 140732961867668 140732961867668 140732961867668
/ proc//status stores the cpu information of a process and some comprehensive information
Name: firefoxState: s (sleeping) Tgid: 2476Ngid: 0Pid: 2476PPid: 1773TracerPid: 0Uid: 1000 1000 1000 1000Gid: 1000 1000 1000 1000FDSize: 256Groups: 4 24 27 30 46 108 124 1000 NStgid: 2476NSpid: 2476NSpgid: 1910NSsid: 1910VmPeak: 1722812 kBVmSize: 1690920 kBVmLck: 0 kBVmPin: 0 kBVmHWM: 684048 kBVmRSS: 600324 kBVmData: 993040 kBVmStk: 192 kB.
The above data can be obtained by reading files. According to the needs of your own experiment, you can calculate the corresponding data, such as pmem = VmRSS/MemTotal*100 and so on.
Sample code
The following is just a simple example of a c code implementation that gets the actual memory occupied by a process at the current time.
/ / get_mem.h#include # define VMRSS_LINE 21//VMRSS. Note: the location may vary depending on the system. # define pid_t intint get_phy_mem (const pid_t p) {char file [64] = {0}; / / filename FILE * fd; / / definition file pointer fd char line_buff [256] = {0} / / buffer sprintf (file, "/ proc/%d/status", p) for reading rows; fprintf (stderr, "current pid:%d\ n", p); fd = fopen (file, "r"); / / Open the file as R read and assign it to the pointer fd / / get vmrss: actual physical memory occupies int i; char name [32]; / / stores the project name int vmrss;// stores memory / / reads the data for of the VmRSS line (item0) I