我想通过按钮单击一个在linux中运行程序,因此我写了一个函数 execute
:
void execute(const char* program_call, const char* param )
{
pid_t child = vfork();
if(child == 0) // child process
{
int child_pid = getpid();
char *args[2]; // arguments for exec
args[0] = (char*)program_call; // first argument is program_call
args[1] = (char*)param;
// close all opened file descriptors:
const char* prefix = "/proc/";
const char* suffix = "/fd/";
char child_proc_dir[16];
sprintf(child_proc_dir,"%s%d%s",prefix,child_pid, suffix);
DIR *dir;
struct dirent *ent;
if ((dir = opendir (child_proc_dir)) != NULL) {
// get files and directories within directory
while ((ent = readdir (dir)) != NULL) {
// convert file name to int
char* end;
int fd = strtol(ent->d_name, &end, 32);
if (!*end) // valid file descriptor
{
close(fd); // close file descriptor
// or set the flag FD_CLOEXEC
//fcntl( fd, F_SETFD, FD_CLOEXEC );
}
}
closedir (dir);
}
else
{
cerr<< "can not open directory: " << child_proc_dir <<endl;
}
// replace the child process with exec*-function
execv(program_call,args);
_exit(2);
}
else if (child == -1) // fork error
{
if (errno == EAGAIN)
{
cerr<<“To much processes"<<endl;
}
else if (errno == ENOMEM)
{
cerr<<“Not enough space available."<<endl;
}
}
else // parent process
{
usleep(50); // give some time
if ( errno == EACCES)
{
cerr<<“Permission denied or process file not executable."<<endl;
}
else if ( errno == ENOENT)
{
cerr<<"\n Invalid path or file."<<endl;
}
int child_status;
if ( waitpid(child, &child_status, WNOHANG | WUNTRACED) < 0) // waitpid failed
{
cerr<<"Error - Execution failed"<<endl;
}
else if ( WIFEXITED( child_status ) && WEXITSTATUS( child_status ) != 0)
{
cerr<<“Child process error - Execution failed"<<endl;
}
}
}
有两个问题:
-
关闭文件描述符会导致一些问题,例如Thunderbird崩溃或VLC运行时没有声音 . 更确切地说:关闭
stdout(1)
和stderr(2)
会导致这些问题 . 据我所知,在exec之前关闭文件描述符只能防止它们被复制(不需要从子进程向父进程发送信息) . 为什么这会影响子进程?通过设置标志FD_CLOEXEC
来替换close()
不会改变任何内容 . 在fork之前设置FD_CLOEXEC
标志也无法解决问题 . 有没有更好的方法来阻止文件描述符的继承? -
waitpid的返回值通常为0,即使程序调用失败,我认为因为有两个(异步)进程 .
usleep(50)
为我的需求解决了这个问题,但我希望有更好的解决方案来解决这个问题 .
我正在使用vfork,但使用fork会出现同样的问题 .
2 回答
首先,在2014年,永远不要使用
vfork
,而只需fork(2) . (因为vfork(2)自POSIX 2001以来已经过时并在POSIX 2008中被删除) .然后,关闭大多数文件描述符的最简单方法就是
(提示:如果fd无效,close(fd)将失败并且我们忽略失败;并且你从3开始保持打开0 == stdin,1 == stdout,2 == stderr;所以原则上所有关闭以上会失败) .
然而,表现良好且编写良好的程序在关闭时不应该需要这样的循环(因此它是克服先前错误的粗略方法) .
当然,如果你知道stdin,stdout,stderr以外的某些文件描述符是有效的并且需要给孩子
program_call
(这不太可能),你需要明确地跳过它 .然后尽可能使用
FD_CLOEXEC
.如果你的程序不知道它们,你的程序就不太可能有很多文件描述符 .
也许你想要daemon(3)或(由vality评论)posix_spawn .
如果你需要明确地关闭
STDIN_FILENO
(即0),或STDOUT_FILENO
(即1),或STDERR_FILENO
(即2),那么在调用exec
之前你会更好open("/dev/null",
和dup2
,因为大多数程序都希望它们存在 .第一个问题:没有办法阻止文件描述符的继承,除非你自己关闭它们或设置
FD_CLOEXEC
,检查this第二个问题:你得到
The return value of waitpid is often 0
,因为你在waitpid
中选择了WNOHANG
.