I have written a C program which needs to get its own CPU and memory usage. So I have written something like this:
system("prs_pid=`ps | grep prs-m1 | awk '{print $1}'` \n top -n1 | grep -m1 $prs_pid | \
awk '{print \"prs_cpu:\" $7 \"\\nprs_mem:\" $6}' >> /stats");
My application name is prs and I do a PS and get the pid of my process and then want to get CPU usage from running TOP. The program reports it is using 2% memory and 0% CPU. But, running the same command manually on the cmd returns the same memry usage but a valid non-zero CPU that I can verify by running top. What I don't understand is why is the cpu usage always 0% when tried from inside the system?