Get system memory and CPU usage from within C++11 code [duplicate]

扶醉桌前 提交于 2019-12-25 01:48:28

问题


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



来源:https://stackoverflow.com/questions/50397395/get-system-memory-and-cpu-usage-from-within-c11-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!