search

Home  >  Q&A  >  body text

c++ - ndk开发 popen函数替代

ndk开发中需要用到 命令获取一些参数信息,目前知道的是有两种方法

  1. popen

  2. system

popen发现在小米商店上不了架,但是system在Android下无法写入文件。
比如'ps > /sdcard/.log' 运行后可以发现'.log'文件已经创建 但是文件内并没有任务信息。

具体代码:

    FILE *fp;
    char cmd[MAX];
    char dir_path[MAX];
    sprintf(dir_path, "/sdcard/.log");
    sprintf(cmd, "ps > %s", dir_path);
    int status;
    sighandler_t old_handler;
    old_handler = signal(SIGCHLD, SIG_DFL);
    status = system(cmd);
    signal(SIGCHLD, old_handler);
    if(status < 0){
        DEBUG_PRINT("cmd: %s\t error: %s", cmd, strerror(errno));
    }
    fp = fopen(dir_path, "r");
    if (fp) {
        while (fgets(line, MAX, fp)) {
            process_count ++;
            DEBUG_PRINT("msg:%s, len:%d", line, strlen(line));
        }
    }
    fclose(fp);
ringa_leeringa_lee2773 days ago417

reply all(2)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-04-17 14:21:12

    Whether it is popen or system, it can be both fork and exec....

    reply
    0
  • 黄舟

    黄舟2017-04-17 14:21:12

    I think it’s not because it uses popen, a function in <stdio.h>, that it can’t be put on the shelves.
    Android apps like KSWeb that integrate Nginx/PHP/MySQL can’t be put on the Google Store.
    Java's Runtime.getRuntime().exec() is essentially a fork process.
    The zygote process zygote run by Android's root user is also essentially a fork.
    Could it be that the process cannot be launched because it is forked? I think it’s impossible.
    Isn’t there any feedback when the Xiaomi Mi Store app is removed?

    The bottom layer of popen in GNU libc is execve in Unix Standard Header (unistd.h).

    reply
    0
  • Cancelreply