请问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);
}
}
...全文
211 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
七擒关羽 2011-11-02
  • 打赏
  • 举报
回复
LZ的问题是,建立管道后会跳出了,执行不到pclose ,速度结贴 我已经帮你改好了
低调的猪猪 2011-11-02
  • 打赏
  • 举报
回复
不好意思。修改了一样的效果。。还是会产生很多进程。。根本没有结束。
qq120848369 2011-11-01
  • 打赏
  • 举报
回复
pclose里面就是wait+close。
七擒关羽 2011-11-01
  • 打赏
  • 举报
回复

int checkethn(char *netcard)
{
FILE *fp;
int mode = 0;
char buf[200]={0};
if((fp = popen(netcard,"r"))!=NULL){
if(fgets(buf, 200, fp)!= NULL)
if (strstr(buf,"Link detected: yes")!=NULL)
mode = 1;
pclose(fp);//只有建立管道成功才需要关闭
}
return mode ;
}


69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧