You could try this:
There are probably better ways to get CPU usage than top though - top is quite heavy itself. One good measure of system load is the
load average. See the uptime manual page.
I guess you're trying to write a system monitor eh? There are a lot already out there - maybe you can find some tips on how best to gather system info from their code. I like the name of
this one.