zoukankan      html  css  js  c++  java
  • 【转】在嵌入式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 = 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
    ", buf, ret);
                }
                pclose(strm); 
            }
            
            return ret;  
    } 
     
    static void Execute(const char *  name)
    {
        char    buf[128];
        sprintf(buf, "./%s &", name);
        printf("Execute, buf = %s
    ", buf);
        system(buf);
        return;
    }
     
     
    int main()   
    {       
        if(0 == IsExistent("btest"))
        {
            printf("no btest process.
    ");
            Execute("btest");
        }else
        {
            printf("btest process exists.
    ");
        }
        
        return 0;  
    }

    btest.c 源代码:

    #include<stdio.h>
     
    int  main()
    {
        while(1)
        {
            printf("running...
    ");
            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
    ", buf, ret);
                }
                pclose(strm); 
            }
            
            return ret;  
    } 
     
    static void Execute(const char *  name)
    {
        char    buf[128];
        sprintf(buf, "./%s &", name);
        printf("Execute, buf = %s
    ", buf);
        system(buf);
        return;
    }
     
     
    int main()   
    {       
        if(0 == IsExistent("btest"))
        {
        //[]    printf("no btest process.
    ");
        //[]    Execute("btest");
        }else
        {
        //[]    printf("btest process exists.
    ");
        }
        
        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
    ", buf, ret);
                }
                pclose(strm); 
            }
            
            return ret;  
    } 
     
    static void Execute(const char *  name)
    {
        char    buf[128];
        sprintf(buf, "./%s &", name);
        printf("Execute, buf = %s
    ", buf);
        system(buf);
        return;
    }
     
     
    int main()   
    {       
        if(0 == IsExistent("btest"))
        {
            printf("no btest process.
    ");
            Execute("btest");
        }else
        {
            printf("btest process exists.
    ");
        }
        
        return 0;  
    }

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

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

  • 相关阅读:
    bzoj5157: [Tjoi2014]上升子序列(树状数组LIS)
    2435: [Noi2011]道路修建(树上操作)
    bzoj1019: [SHOI2008]汉诺塔(动态规划)
    bzoj1103: [POI2007]大都市meg(树链剖分)
    bzoj2190: [SDOI2008]仪仗队(欧拉)
    bzoj4519: [Cqoi2016]不同的最小割(分治最小割)
    bzoj2229: [Zjoi2011]最小割(分治最小割+最小割树思想)
    bzoj1816: [Cqoi2010]扑克牌(二分答案判断)
    [HEOI2015]兔子与樱花
    [POI2009]KAM-Pebbles
  • 原文地址:https://www.cnblogs.com/cslunatic/p/9639200.html
Copyright © 2011-2022 走看看