2014-10-07 42 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 &");