我通常使用fork+exec组合:
int sockets [2];
socketpair (AF_LOCAL, SOCK_STREAM, 0, sockets);
int pid = fork ();
if (pid == 0) {
// child
dup2 (sockets[0], STDIN_FILENO);
dup2 (sockets[0], STDOUT_FILENO);
execvp (argv[0], argv);
_exit (123);
}
// parent
close (sockets[0]);
// TODO wait and see if child crashes
是否可以等到子进程崩溃或开始等待读取(…)?
最佳答案
(AA)
pid_t waitpid(pid_t pid, int *stat_loc, int options);
From
#include <sys/wait.h>