我想使用 fork 创建一个新进程,然后使用 excl 启动一个带有 url 的网络浏览器。我对 fork 和 excel 不太熟悉,因此将不胜感激。谢谢
编辑:这是我的代码,但我认为它不正确
if(fork() == 0) {
execl (url,0);
printf("Route opened in brwoser\n");
} else {
printf("Route cannot be opened.\n");
}
首先阅读这些调用的手册页:
man 2 fork
man 3 execl
系统调用fork()
复制进程并在两者中返回,在父进程中返回子进程 ID,在子进程中返回零。如果返回负数,则表示失败。
pid_t pid = fork();
if (pid < 0)
printf("Fork failed\n");
else if (pid > 0) /* Here comes the parent process */
printf("Fork successful\n");
else /* Here comes the child process */
...
另一方面execl()
根本不回来。它会丢弃您的程序,并在同一进程中用其参数中指定的图像替换它。
如果execl()
返回,则为错误。它可能没有找到您指定的程序。
它的参数是被调用的程序(一个 URL 不是一个程序)和它的参数。
...
else { /* Here comes the child process */
execl("/usr/bin/firefox", "/usr/bin/firefox", "example.com", (char*)NULL);
printf("Could not execute Firefox\n");
}