我一直在使用fork()
和的组合exec()
在 linux 上执行一些外部命令,但是,每当我尝试执行/usr/bin/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;
}
编辑:我更新了代码以包含答案并更改了主题的标题,因为问题似乎根本不是由符号链接引起的。谢谢大家。