To find CPU Usage

I am programatically trying to find CPU usage for a particular process. I am writing a C program for this. I am not sure if my approach is good at all. I first of all find the PID using getpid() method. Then I call top -f <filename> and then parse to reach the PID row. I then try to read the 10th field namely %CPU. But this is very tedious and likely to have many bugs in parsing process.
I had to read from file since top command does not terminate automatically. Can anyone, please suggest some better way to do?
Thanks a lot in advance.

Finding cpu usage of another process is generally a hardware dependent kind of thing.
What hardware and OS are you using?

Plus, most of the tools available for system information usually work better in a shell script. C is normally not the best choice.

use unix shell,it is too diffcult to using c program

i give a shell script under aix,as follow
usage:nproc.sh -m -p
nproc.sh

#!/bin/ksh
ShowUsage ( )
{
echo "===================================="
echo "nproc.sh"
echo "\t[-h help]"
echo "\t[-m memory statics]"
echo "\t[-p cpu statics]"
echo "\t[-t internal seconds]"
echo "\t[-n display count]"
echo "===================================="
exit 0;
}

#define FILED sort
PID_FIELD=1
TTY_FIELD=2
STAT_FIELD=3
TIME_FIELD=4
SIZE_FIELD=5
RSS_FIELD=6
CPU_FIELD=7
MEM_FIELD=8
USER_FIELD=9
COMMAND_FIELD=10
TMPFILE=`date '+%Y%m%d%H%M%S'`
STATFILE="$TMPFILE"
TMPFILE="$TMPFILE.log"

#define input tocken
HELP_TOCKEN="-h"
MEMORY_TOCKEN="-m"
CPU_TOCKEN="-p"
INTERTIME_TOCKEN="-t"
COUNT_TOCKEN="-n"

#set init value
typeset -i I
typeset -i K
typeset -i SORT_FILED
typeset -i INTERTIME_SECOND
typeset -i CPU_FLAG
typeset -i MEM_FLAG

I=0
K=$#
DISPLAY_COUNT=10
SORT_FILED=$RSS_FIELD-1
INTERTIME_SECOND=2
CPU_FLAG=0
MEM_FLAG=0

cat /dev/null >/tmp/$STATFILE.mem
cat /dev/null >/tmp/$STATFILE.cpu

while [ $I -lt $K ]
do
#echo $1
if [ "$1" = "$HELP_TOCKEN" ]; then
ShowUsage
exit 1
fi

if [ "$1" = "$CPU_TOCKEN" ]; then
#SORT_FILED=$CPU_FIELD-1
CPU_FLAG=1
fi

if [ "$1" = "$MEMORY_TOCKEN" ]; then
#SORT_FILED=$CPU_FIELD-1
MEM_FLAG=1
fi

if [ "$1" = "$COUNT_TOCKEN" ]; then
DISPLAY_COUNT=$2
shift 1
I=$I+1
fi

if [ "$1" = "$INTERTIME_TOCKEN" ]; then
INTERTIME_SECOND=$2
shift 1
I=$I+1
fi

shift 1
I=$I+1
done

while [ 1 ]
do
ps -Ao pid,tty,stat,time,vsz=VSZ,rss=RSS,pcpu,pmem,comm,user|awk '{
PID=$1
TTY=$2
STAT=$3
if (length($4) > 8) {
TIME=substr( $4 ,length($4)-8+1,8 )
}
else{
TIME=$4
}

            SIZE=$5
            RSS=$6
            CPU=$7
            MEM=$8
            COMMAND=$9
            USER=$10


            TTY="-"
            \#printf "%-8ld %s   %s    %05s %-7ld %-07s  %-07s %-6s %-8.2f %-8ld%-6.2f%-5.2f %s\\n",PID,TTY,S

TAT,TIME,PGIN,SIZE/1024,RSS/1024,LIM,TSIZ/1024,TRS,CPU,MEM,COMMAND
#printf "%-8ld %s %s %-8s %-7ld %-7.2f %-7.2f %-6s %-8.2f %-8ld%-6.2f%-6.2f %s %s\n",PID,
TTY,STAT,TIME,PGIN,SIZE/1024,RSS/1024,LIM,TSIZ/1024,TRS,CPU,MEM,COMMAND,USER
printf "%-8ld %s %s %-10s %-7.2f %-7.2f %-6.2f %-6.2f %-10s %s\n",PID,TTY,STAT,TIME,SIZE
/1024,RSS/1024,CPU,MEM,USER,COMMAND
#print PID,"\t",STAT,TIME,PGIN,SIZE/1024,RSS/1024,LIM,TSIZ/1024,TRS,CPU,MEM,COMMAND

        \}' &gt;$TMPFILE

clear
echo ""
echo "=================================================================================================="
echo "SIZE:virtual size of the data section of the process"
echo "RSS:The real-memory (resident set) size of the process"
#echo "TSIZ:The size of text (shared-program) image"
#echo "LIM: The soft limit on memory used"
#echo "TRS:The size of resident-set (real memory) of text"
#echo "================================================================================================="
echo "PID\tTTY STAT TIME SIZE(MB) RSS(MB) %CPU %MEM USER COMMAND"

if [ $MEM_FLAG -eq 1 ]; then
SORT_FILED=$RSS_FIELD-1
sort -n -r +$SORT_FILED $TMPFILE >$STATFILE.mem
fi

if [ $CPU_FLAG -eq 1 ]; then
SORT_FILED=$CPU_FIELD-1
sort -n -r +$SORT_FILED $TMPFILE >$STATFILE.cpu
fi

if [ $MEM_FLAG -eq 1 ]; then
echo "=============================================MEMORY=STAT========================================="
SORT_FILED=$RSS_FIELD-1
head -n $DISPLAY_COUNT $STATFILE.mem
fi

if [ $CPU_FLAG -eq 1 ]; then
echo "=============================================CPU===STAT=========================================="
head -n $DISPLAY_COUNT $STATFILE.cpu
fi
echo "================================================================================================="
rm -f $TMPFILE
rm -f $STATFILE.cpu
rm -f $STATFILE.mem
sleep $INTERTIME_SECOND
done

Thanks a lot.