2014-10-07 43 views
0

我需要在execlp calls()中使用多个显示器。我想这样的:将指示路径前的显示设置为(execlp)

#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 有什么办法与显示器的工作?

+2

你可以做两个'execlp ()总是像这样,如果第一个成功,你的原始程序将会消失。你可以使用'setenv()'来设置环境变量。 – 2014-10-07 02:15:39

回答

0

尝试system()取而代之,它将启动一个新的子进程并从那里调用exec()

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

此外,学会从这样的函数来检查返回代码,并做一些理智的行动(:另外,它通过调用壳交给你的shell构建到外壳,这样的处理shell命令行构建如果你想这两个指令并行(不是一个接一个地运行

#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 &");