我一直在使用fork()
和exec()
的組合來在linux上執行一些外部命令,但是,當我試圖執行/usr/bin/firefox
這個代碼似乎失敗了,這是一個符號鏈接到一個真實二進制文件。exec()不支持firefox
有誰知道如何解決這個問題?我測試過其他程序(實際上是可執行的二進制文件,而不是符號鏈接),它的工作原理。
下面是程序代碼:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
#include <errno.h>
int main(int argc, char **argv) {
pid_t pid;
// this was the old line:
// char *parmList[] = {"", "index.html", NULL};
// and this is the one that solves the problem:
char *parmList[] = {"firefox", "index.html", NULL};
int a;
if ((pid = fork()) == -1)
perror("fork failed");
if (pid == 0) {
a = execvp("/usr/bin/firefox", parmList);
fprintf(stdout, "execvp() returned %d\n", a);
fprintf(stdout, "errno: %s (%d).\n", strerror(errno), errno);
}
else {
waitpid(pid, 0, 0);
}
return 0;
}
編輯:我更新的代碼包含的答案,換了個話題的標題,因爲確實似乎問題不是是由於符號鏈接的。感謝大家。
這似乎爲我工作都當你粘貼它時,Firefox已經運行並且Firefox已經關閉。如果Firefox已經打開,它會打開一個新標籤頁。你有沒有其他的錯誤細節? – 2010-12-19 00:36:01
「你好像失敗了」是什麼意思? – bk1e 2010-12-19 00:47:06
我根本無法得到它的工作,我只是真的不知道什麼是錯的:\ – pwseo 2010-12-19 00:53:25