Reputation: 305
How can we know that how much load does our program is on CPU?
I tried to find it using htop. But htop wont give the cpu load. It actually gives the cpu utilization percentage of my program(using pid).
I am using C programming, Linux environment.
Upvotes: 2
Views: 446
Reputation: 6531
The function you are probably looking for is getrusage
. It fills struct rusage
. There are two members of the struct you are interested in:
ru_utime
- user CPU time usedru_stime
- system CPU time usedYou can call the function at regular intervals of time and based on the results you can estimate the cpu load (e.g. in percentage) of your own process.
If you want to get it at the system level, then you need to read (and parse) /proc/stat file (also at regular intervals), see here.
Upvotes: 2