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

本文对比了在嵌入式Linux与PC机Linux环境下使用popen函数时的不同表现,具体分析了检查进程存在的函数在两种环境下的运行结果差异,并给出了修正建议。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

下面程序演示了在嵌入式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 = 0; 
		
		FILE *strm;  
		char buf[128];  
 
		sprintf(buf,"ps | grep -c %s", name);  
		
		if((strm=popen(buf, "r")) != NULL)  
		{  
			if(fgets(buf, sizeof(buf), strm) != NULL)   
			{
				ret = atoi(buf) > 1 ? 1 : 0;
				printf("IsExistent, buf = %s, ret = %d\n", buf, ret);
			}
			pclose(strm); 
		}
		
		return ret;  
} 

static void Execute(const char *  name)
{
	char    buf[128];
	sprintf(buf, "./%s &", name);
	printf("Execute, buf = %s\n", buf);
	system(buf);
	return;
}


int main()   
{   	
	if(0 == IsExistent("btest"))
	{
		printf("no btest process.\n");
		Execute("btest");
	}else
	{
		printf("btest process exists.\n");
	}
	
	return 0;  
}


btest.c 源代码:

#include<stdio.h>

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

 

PC机Linux上的运行结果:


 

嵌入式Linux上的运行结果:


 

为什么在嵌入式系统上出现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 = 0; 
		
		FILE *strm;  
		char buf[128];  
 
		//[]sprintf(buf,"ps | grep \"%s\" | grep -cv \"grep\"", name);  
		sprintf(buf,"ps | grep \"%s\"", name);  //[]
		system(buf); //[]
		return 0; //[] 

		if((strm=popen(buf, "r")) != NULL)  
		{  
			if(fgets(buf, sizeof(buf), strm) != NULL)   
			{
				ret = atoi(buf) > 0 ? 1 : 0;
				printf("IsExistent, buf = %s, ret = %d\n", buf, ret);
			}
			pclose(strm); 
		}
		
		return ret;  
} 

static void Execute(const char *  name)
{
	char    buf[128];
	sprintf(buf, "./%s &", name);
	printf("Execute, buf = %s\n", buf);
	system(buf);
	return;
}


int main()   
{   	
	if(0 == IsExistent("btest"))
	{
	//[]	printf("no btest process.\n");
	//[]	Execute("btest");
	}else
	{
	//[]	printf("btest process exists.\n");
	}
	
	return 0;  
}

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


把 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 = 0; 
		
		FILE *strm;  
		char buf[128];  
 
		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) > 0 ? 1 : 0;
				printf("IsExistent, buf = %s, ret = %d\n", buf, ret);
			}
			pclose(strm); 
		}
		
		return ret;  
} 

static void Execute(const char *  name)
{
	char    buf[128];
	sprintf(buf, "./%s &", name);
	printf("Execute, buf = %s\n", buf);
	system(buf);
	return;
}


int main()   
{   	
	if(0 == IsExistent("btest"))
	{
		printf("no btest process.\n");
		Execute("btest");
	}else
	{
		printf("btest process exists.\n");
	}
	
	return 0;  
}

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值