【问题标题】:Set display before indicate path into (execlp)在指示路径进入(execlp)之前设置显示
【发布时间】:2014-10-07 02:01:06
【问题描述】:

我需要在execlp calls () 中使用多个显示器。我正在尝试这个:

#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
    printf("calling to execlp:\n\n");
    execlp("DISPLAY=:0 /usr/bin/qtdisplay","qtdisplay", "-r", NULL);
    execlp("DISPLAY=:1 /usr/bin/qtdisplay","qtdisplay", "-r", NULL);

    printf("fail!");
    exit(0);
}

但这失败了,并显示以下消息:execlp: No such file or directory 有什么方法可以使用显示器吗?

【问题讨论】:

  • 你不能像这样连续做两个execlp()s,如果第一个成功,你原来的程序就会消失。您可以使用setenv() 设置环境变量。

标签: c linux executable displayobject


【解决方案1】:

尝试system(),它会启动一个新的子进程并从那里调用exec()。此外,它通过调用 shell 并将您的 shell 构造交给该 shell 来处理 shell 命令行构造,如下所示:

system("DISPLAY=:0; /usr/bin/qtdisplay -r");

另外,学习检查此类函数的返回码,并做一些理智的操作(如打印错误消息):

#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int rc;

    rc = system("DISPLAY=:0; /usr/bin/qtdisplay -r");
    if (rc == -1) {
        perror("error starting qtdisplay on :0");
        exit(1);
    }
    rc = system("DISPLAY=:1; /usr/bin/qtdisplay -r");
    if (rc == -1) {
        perror("error starting qtdisplay on :1");
        exit(1);
    }
    exit(0);
}

如果您希望这两个命令并行运行(而不是一个接一个),您应该使用这样的命令行:

system("DISPLAY=:0; /usr/bin/qtdisplay -r &");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-19
    • 2012-09-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多