1

I want to know, how can we find user's process statistics about resource utilization( like CPU, Memory) using c program and without using any user command tool. Currently I am running ubuntu 10.10. Thanks

Sushant Jain
  • 328
  • 2
  • 6
  • 12
  • [This](http://stackoverflow.com/questions/4450961/computation-of-cpu-percentage-by-a-single-process-in-unix-by-the-top-command) and [this](http://stackoverflow.com/questions/1420426/calculating-cpu-usage-of-a-process-in-linux) may be of some assistance to you. – mdec Apr 27 '11 at 06:15
  • @mdec They are process specific whereas I need it for user specific. I think, I have to iterate through all process and calculating user's statistics by UID of a process. But if there is any more efficient way then it will be highly appreciated. – Sushant Jain Apr 27 '11 at 06:39

1 Answers1

0

The canonical way these days is to parse the information in the /proc virtual filesystem procfs. It contains textual information on nearly all aspects of the system, including detailed per-process statistics. The information is structured, and is intended for ease of parsing and programmatic access. (This is how tools such as ps work.)

For example, to query the I/O metrics of a given process, you would read the file under /proc/<pid>/io. This contains a series of name: value pairs, like so:

rchar: 14823550
wchar: 138670414
syscr: 11549
syscw: 3013
read_bytes: 483328
write_bytes: 8192
cancelled_write_bytes: 0

For detailed information, see:

gavinb
  • 19,278
  • 3
  • 45
  • 60