如何通过c语言实现Linux下的top命令来获取cpu利用率?

如何通过c语言实现Linux下的top命令来获取cpu利用率?我下载了好几个版本的procps,但是在Ubuntu下编译里面的top.c时都出错,求各位大神帮忙解答。

 #include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
system("top -n 1 |grep Cpu | cut -d \",\" -f 1 | cut -d \":\" -f 2 >cpu.txt");
system("top -n 1 |grep Cpu | cut -d \",\" -f 2 >>cpu.txt");
system("top -n 1 |grep Mem | cut -d \",\" -f 1 | cut -d \":\" -f 2 >>cpu.txt");
system("top -n 1 |grep Mem | cut -d \",\" -f 2 >>cpu.txt");
return 0;
}

参考:http://www.it165.net/os/html/201410/9465.html