Get system memory and CPU usage from within C++11

2019-08-23 18:19发布

问题:

This question already has an answer here:

  • How to determine CPU and memory consumption from inside a process? 9 answers

How can I get the total percentage or volume of memory usage and CPU consumption from within C++11 code on ubuntu (or any other linux-based system)?

The usage I want to get is for the whole system, not just for current thread or process.

回答1:

Read about proc(5) and use /proc/meminfo. You can open it and read it as a sequential file (even if it often behaves as a pipe; e.g. it is not seekable).

Of course, this is Linux specific.

And you might scan the /proc/ directory (using opendir(3), readdir(3), closedir) for directories starting with a digit, and read each of their /proc/1234/maps or /proc/1234/status - these correspond to process 1234)

Read also linuxatemyram