【转】在嵌入式Linux和PC机Linux下使用popen函数时,程序运行结果有差异。

时间:2023-03-08 18:04:15
【转】在嵌入式Linux和PC机Linux下使用popen函数时,程序运行结果有差异。

下面程序演示了在嵌入式Linux和PC机Linux下使用popen函数时,程序的运行结果是有差异的。

两个程序 atest.c 和 btest.c,atest 检查是否有 btest 进程运行,如果没有就执行 btest 然后退出,如果有就直接退出。atest在检查时输出 btest 进程数,PC机是buf 值,但嵌入式是buf值减1,为什么?后面说明。

atest.c 源代码:

#include <stdio.h>
#include <sys/time.h> static int IsExistent(const char * name)
{
int ret = ; FILE *strm;
char buf[]; sprintf(buf,"ps | grep -c %s", name); if((strm=popen(buf, "r")) != NULL)
{
if(fgets(buf, sizeof(buf), strm) != NULL)
{
ret = atoi(buf) > ? : ;
printf("IsExistent, buf = %s, ret = %d\n", buf, ret);
}
pclose(strm);
} return ret;
} static void Execute(const char * name)
{
char buf[];
sprintf(buf, "./%s &", name);
printf("Execute, buf = %s\n", buf);
system(buf);
return;
} int main()
{
if( == IsExistent("btest"))
{
printf("no btest process.\n");
Execute("btest");
}else
{
printf("btest process exists.\n");
} return ;
}

btest.c 源代码:

#include<stdio.h>

int  main()
{
while()
{
printf("running...\n");
sleep();
} return ;
}

PC机Linux上的运行结果:

【转】在嵌入式Linux和PC机Linux下使用popen函数时,程序运行结果有差异。

【转】在嵌入式Linux和PC机Linux下使用popen函数时,程序运行结果有差异。

嵌入式Linux上的运行结果:

【转】在嵌入式Linux和PC机Linux下使用popen函数时,程序运行结果有差异。

【转】在嵌入式Linux和PC机Linux下使用popen函数时,程序运行结果有差异。

为什么在嵌入式系统上出现buf等于3的情况?先说说前面提到的“嵌入式是buf值减1”的原因。

如果 atest 中的 IsExistent 函数这样实现:

static int IsExistent(const char * name)  
{  
        char buf[128];  
        sprintf(buf,"ps | grep \"%s\"", name);  
        system(buf); 
        return 0;   
}

修改后的atest代码:

#include <stdio.h>
#include <string.h>
#include <sys/time.h> static int IsExistent(const char * name)
{
int ret = ; FILE *strm;
char buf[]; //[]sprintf(buf,"ps | grep \"%s\" | grep -cv \"grep\"", name);
sprintf(buf,"ps | grep \"%s\"", name); //[]
system(buf); //[]
return ; //[] if((strm=popen(buf, "r")) != NULL)
{
if(fgets(buf, sizeof(buf), strm) != NULL)
{
ret = atoi(buf) > ? : ;
printf("IsExistent, buf = %s, ret = %d\n", buf, ret);
}
pclose(strm);
} return ret;
} static void Execute(const char * name)
{
char buf[];
sprintf(buf, "./%s &", name);
printf("Execute, buf = %s\n", buf);
system(buf);
return;
} int main()
{
if( == IsExistent("btest"))
{
//[] printf("no btest process.\n");
//[] Execute("btest");
}else
{
//[] printf("btest process exists.\n");
} return ;
}

在嵌入式系统中运行,结果显示连 “ ps | grep "btest" ”命令也算入其中了,甚至还出现两条的情况,怎么回事?也许这是个BUG。截图:

【转】在嵌入式Linux和PC机Linux下使用popen函数时,程序运行结果有差异。

把 IsExistent 函数中的命令 buf 如下赋值:

sprintf(buf,"ps | grep \"%s\" | grep -cv \"grep\"", name);

即再加一个 grep 命令,把含有“grep”单词的行去掉,结果就正常了。

修改后的atest代码:

#include <stdio.h>
#include <string.h>
#include <sys/time.h> static int IsExistent(const char * name)
{
int ret = ; FILE *strm;
char buf[]; sprintf(buf,"ps | grep \"%s\" | grep -cv \"grep\"", name); if((strm=popen(buf, "r")) != NULL)
{
if(fgets(buf, sizeof(buf), strm) != NULL)
{
ret = atoi(buf) > ? : ;
printf("IsExistent, buf = %s, ret = %d\n", buf, ret);
}
pclose(strm);
} return ret;
} static void Execute(const char * name)
{
char buf[];
sprintf(buf, "./%s &", name);
printf("Execute, buf = %s\n", buf);
system(buf);
return;
} int main()
{
if( == IsExistent("btest"))
{
printf("no btest process.\n");
Execute("btest");
}else
{
printf("btest process exists.\n");
} return ;
}

是嵌入式Linux的BUG呢,还是有意这么设计的? 请知道的在下面留言说一说,谢谢~~~

原链接:https://blog.csdn.net/iw1210/article/details/47778247