- 论坛徽章:
- 0
|
本帖最后由 linuxkiller7967 于 2013-01-09 11:18 编辑
问题描述:当使用ls -l命令,也就是用fpout = popen("ls -l /Customer","r")这个语句的时候,程序顺利运行。ping.txt里面也存储了所有完成的流信息。
但是当fpout = popen("ls -l /Customer","r")换成fpout = popen("ping -c3 192.168.0.1","r")时,用ps查看这个进程始终不消失,卡死,整个程序也运行不完,ping.txt里面也没有数据。
跪求指点啊。为什么同样的程序,只是采用了两个不同的命令而已,为什么会出现差别巨大的结果呢?
BOOL Network_Detect(void)
{
printf("\n FUNC=%s,LINE=%d\n",__func__,__LINE__);
BOOL lanStatus = FALSE;
FILE* fpout = NULL;
FILE* fpin = NULL;
FILE* pFile = NULL;
int nCount_fread;
int nCount_write;
char buf[20],temp[1024];
memset(buf,'\0',sizeof(buf));
memset(temp,'\0',sizeof(temp));
if(TRUE == Network_Detect_Eth0_NetworkCable_State()) //check wheather is connected with NetworkCable
{
if( (fpout = popen("ls -l /Customer","r")) == NULL)
printf("pclose error!\n");
if((fpin= fopen("/Customer/ping.txt","w+"))== NULL) //check wheather creat new file success
{
printf(" fopen creat file failed!\n");
}
nCount_fread = fread(temp,sizeof(char),sizeof(temp),fpout);
printf("temp is equal to =%s\n",temp);
if(nCount_fread != sizeof(temp))
{
printf(" fread %d data from stream\n",nCount_fread);
printf(" fread file failed/not complete!\n");
}
nCount_write = fwrite(temp,1,sizeof(temp),fpin);
if( nCount_write != sizeof(temp))
{
printf(" fwrite %d data to file\n",nCount_write);
printf("fwrite data to file failed or not completed!\n");
}
if((pFile = popen("cat /Customer/ping.txt","r")) == NULL)
{
printf(" Popen cat data failed!\n");
}
if(pclose(fpout) == -1)
printf("pclose error!\n");
fclose(fpin);
while(fgets(buf,20,pFile))
{
printf(" fgets do!\n");
if(strstr(buf,"Ping"))
{
printf(" All packets Loss \n");
lanStatus = 0;
printf(" lanStatus is = %d\n",lanStatus);
break;
}
else if(strstr(buf,"boot.ini"))
{
printf(" No packets Loss\n");
lanStatus = 1;
printf(" lanStatus is = %d\n",lanStatus);
break;
}
}
if(pclose(pFile) == -1)
printf("pclose error too!\n");
}
else
{
printf(" not connected with NetworkCable!\n");
printf("LanStatus is = %d\n",lanStatus);
lanStatus = FALSE;
}
return TRUE;
}
|
|