title: How to count CPU Usage of multiprocess application in Linux
body: <p>I try to make a program with C/C++, to behave like top command in Linux.
  I've done some research and already known how to count CPU Usage of a process. We can get the CPU Usage by calculating stime + utime from /proc/[PID]/stat in current time and after several seconds. Then calculate the stime + utime differences and divide the result with uptime differences to, then we get the CPU Usage percentage. It will be so easy on single process/multithread process.</p>
  
  <p>The problem is in the case like httpd, where it works as multiprocess. When the webserver busy, httpd will fork child processes to serve bunch of requests. Then I count the number of total process, let's say 500. I want to calculate the CPU Usage of those processes, but summarize them so I only see 1 httpd CPU Usage. But if I do the algorithm like I've mentioned above, when the number of processes decrease into &lt; 500 after several seconds, I get the negative values, since the calculation will be like this (for example, I choose random number, just to give you brief description):</p>
  
  <pre><code>Uptime: 155123, No of processes : 500, Stime + Utime total of 500 processes : 3887481923874
  Uptime: 155545, No of processes : 390, Stime + Utime total of 390 processes : 2887123343874
  </code></pre>
  
  <p>If you look the the example above, the delta of Stime + Utime will results in negative value, since the number of process decreasing, and give the lower value after few miliseconds. I just want to know, is there any other way to calculate such process behave like this? Thank you.</p>