popen 에러가 나는경우는 어떤경우 일까요?
pid를 가지고 cpu점유율을 구하고 있습니다.
그런데 일정시간이되면 popen 에러가 납니다.
원인이 뭔지 모르겠습니다.. 도움주시면 감사하겠습니다.
수고하세요..
int Monitor::getpinfo(pid_t pid)
{
int ret =0;
FILE *fp = NULL;
char buff[256];
char cpu[3];
char null[4];
pid_t rpid=0;
char getpsbuf[128]={0,};
snprintf(getpsbuf,sizeof(getpsbuf),"ps -eo \"pid pcpu\" | grep -v grep | grep %d ",pid);
int i=0;
if ((fp = popen(getpsbuf, "r")) == NULL)
{
perror("popen error ");
WRITE_LOG("ERROR getpinfo() popen error",LOGLEVEL_INFO);
return -1;
}
if(fgets(buff, 255, fp)!=NULL)
{
sscanf(buff, " %d %s",
&rpid,cpu);
ret = atoi(cpu);
#ifdef _DEBUG
printf("==>>>rpid:%d,cpu(str):%s,cpu(int):%d\n",rpid,cpu,ret);
#endif
}
else
{
WRITE_LOG("ERROR getpinfo() fgets() return is NULL",LOGLEVEL_INFO);
ret = 0;
}
pclose(fp);
fflush(NULL);
return ret;
}
댓글 달기