请问popen怎么防止僵死进程
低调的猪猪 2011-11-01 01:32:52 程序功能是电脑网卡断开后自动关机。通过popen调用ethtool实现判断网卡状态。现在是程序运行几天后。系统会产生很多ethtool的进程。请问pclose关闭怎么没用呢?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<unistd.h>
#include<signal.h>
int checkethn(char *netcard)
{
FILE *fp;
char buf[200]={0};
if((fp = popen(netcard,"r"))!=NULL)
while(fgets(buf, 200, fp)!= NULL)
{
if (strstr(buf,"Link detected: yes")!=NULL)
{
return(1);
break;
}
}
pclose(fp);
return(0);
}
int main()
{
int errno;
printf("UPS Stop,Auto ShutDown\n");
daemon(0,0);
while(1)
{
errno=checkethn("ethtool eth0");
if(errno==0)
{
sleep(30);
errno=checkethn("ethtool eth0");
if(errno==0)
system("poweroff");//printf("poweroff\n");
}
sleep(90);
}
}