Linux CPU usage
I work on UNIX.
I want to know the current cpu usage of the process.
I understand that ps gives the average of CPU used before the process starts – this is not the current usage.
Is there a way to print only the cpu from the top command without more than 10 arguments and
Title? I know how to use awk to do it – it’s not the way I want it to.
top -p 20705 -bc -n 1 | tail -n 2 | awk '{ print $9}' | head -n 1
If there is another easy way to do it instead of reading /proc/stat….
If there is an easy way to implement it from C++, that is fine.
Solution
Most likely, you need to read /proc/stat
, however, here is an interesting article with C code This might help you. To understand and use the output of the program, you should execute man 5 proc
. This is source code
Most importantly, you need to read /proc/stat
to do what you want.