【问题标题】:linux popen stuck on fgetslinux popen卡在fgets上
【发布时间】:2018-01-03 09:48:59
【问题描述】:

伙计们!我使用popen函数在linux(ubuntu16.04)上执行shell脚本。

char buffer[1024];
char cmd[1024];
snprintf(cmd, 1024, "./vm_manager.sh start %s %d %d", vm_name, vm_start_port, vm_start_core);

FILE *fp = popen(cmd, "r");

while(fgets(buffer, sizeof(buffer), fp) != NULL){
    printf("%s", buffer);
    fflush(stdout);
}   
printf("over=============");
pclose(fp);

在 vm_manager.sh 中,只需运行 android 模拟器 cmd 即可启动 avd,如下所示:

emulator -avd $VM_NAME -kernel $KERNEL -port $VM_START_PORT -no-window -writable-system -ranchu -cores 4 -gpu mesa &

实际上它会注销模拟器启动日志,但卡在 fgets 上并且永远不会打印出“over===”。

感谢您的任何建议!

【问题讨论】:

    标签: linux android-emulator popen fgets


    【解决方案1】:

    我通过在 shell 脚本中添加 end_flag 解决了这个问题:

    echo "START_SUCCESS"
    

    然后在 c++ 中检查 end_flag 是否出现。如果出现,只需打破 fgets 循环:

    while(fgets(buffer, sizeof(buffer), fp) != NULL){
        printf("%s", buffer);
        fflush(stdout);
        if(strcmp(buffer, "START_SUCCESS\n") == 0){ 
            break;
        }   
    }   
    

    无论如何谢谢。

    【讨论】:

      猜你喜欢
      • 2014-12-08
      • 2018-01-02
      • 2018-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-30
      • 1970-01-01
      • 2015-04-17
      相关资源
      最近更新 更多