关于hogs的显示进程号为1的进程的System时间。

我想在程序里面取出cpu当前空闲率,然后打印出来,下面是程序代码部分,不知道 运行是,红指示灯是一直亮着的,结果不能及时输出,我试过fflush(stdout)和setvbuf为_IONBF,还是没有解决,请各位指点一下,谢谢!

char *cmd = “hogs | awk ‘{if($1==1)print$4}’”;
// char *cmd = “hogs | awk ‘{print$4}’”;
char buf [10];
FILE *ptr;
short idlecpu;
if ((ptr = popen(cmd, “r”)) != NULL)
if(fgets(buf,10, ptr) != NULL)
{
if(fgets(buf,10, ptr) != NULL)
idlecpu=atoi(buf);
printf(“idlecpu=%d \n”,idlecpu);
printf("%s ",buf);

}
pclose(ptr);
printf(“exit!”);

如果最后使用pclose(ptr); 如上面程序所示,则没有结果输出,并且提示管道被破坏,不知道可有办法解决,我只想得到一次hogs的输出然后关闭之, 谢谢

是不是因为hogs是一直循环输出的呀.
你指定一个比较长的时间
-s sec
Sleep this long between updates (default: 3 seconds).
然后在拿到结果后,kill掉它就是了

我也估计是这个原因,但我不知道具体应该怎么写

具体?

system(“hogs -n -%1 -s300>123.txt”);
//do sth.
system(“slay hogs”);

这样可好?