如何使用 C++ 程序调用路径 /usr/bin 中的可执行文件?

How to invoke an executable in the path /usr/bin using a C++ program?

我在 linux 机器的路径 /usr/bin 中有一个基于 GUI 的可执行文件 此可执行文件采用三个参数 - 两个整数值和一个字符

你能告诉我如何从用户 space C++ 程序

调用和 运行 这个可执行文件吗

不要无缘无故地置之不理

pid_t runprocess(int arg1, int arg2, char arg3)
{
    static const char program[] = "/usr/bin/...";
    char arg1c[12];
    char arg2c[12];
    char arg3c[2];
    sprintf(arg1c, "%d", arg1);
    sprintf(arg2c, "%d", arg2);
    arg3c[0] = arg3;
    arg3c[1] = 0;
    pid_t pid = vfork();
    if (pid == 0) {
        signal(SIGHUP, SIG_IGN); /* since it's a GUI program, detach from console HUP */
        close(0); /* and detach from stdin */
        if (open("/dev/null", O_RDWR)) _exit(137); /* assertion failure */
        execl(program, program, arg1c, arg2c, arg3c, NULL);
        _exit(errno);
    }
    return pid;
}

将参数构建为字符串,派生并执行它。真的很琐碎。别忘了 wait().

由于子进程是一个 GUI 进程,我们将 HUP 从我们可能会或可能不会 运行 的终端分离,并将标准输入替换为 /dev/null