[英]is possible to get the exit status from the child of a child process in the parent
I try to get the exit code from a child of a child process in the parent process.我尝试从父进程中的子进程的子进程中获取退出代码。 If the process goes in the while loop to fork again i don't get the exit code.
如果进程进入 while 循环以再次分叉,我不会得到退出代码。 I tried some options for waitpid like WNOHANG but then the program hangs.
我尝试了一些诸如 WNOHANG 之类的 waitpid 选项,但随后程序挂起。 Maybe what i wan't is not possible because it's some like a zombie child?
也许我不想要的东西是不可能的,因为它有点像僵尸孩子?
This is my code.这是我的代码。
void parrent_process(t_token *token, t_info *info)
{
pid_t pid;
int wstatus;
pid = fork();
if (pid == -1)
return (print_error_msg(FORK_FAIL, NULL, NULL));
if (pid == 0)
{
child_process(token);
}
if (info->in)
close(info->in);
waitpid(pid, &wstatus, 0);
if (WIFEXITED(wstatus))
info->exit_code = WEXITSTATUS(wstatus);
}
void child_process(t_token *token)
{
t_token *cmd_token;
pid_t pid;
pid = 0;
cmd_token = token;
while (token->next && ((token->next)->type == GREAT))
{
pid = fork();
if (pid == -1)
return (print_error_msg(FORK_FAIL, NULL, NULL));
if (pid == 0)
{
redirect_output(token);
break;
}
token = token->next->next;
}
if (execve(cmd_token->path, cmd_token->args, cmd_token->envp) == -1)
{
print_error_msg(CMD_NOT_FOUND, "minishell", cmd_token->args[0]);
exit(127);
}
}
In most POSIX-like systems, the answer is "No — a process can only wait on its own children, those it created directly with fork()
1 ".在大多数类似 POSIX 的系统中,答案是“不,一个进程只能等待它自己的子进程,即它直接使用
fork()
1创建的子进程”。
However, on Linux, there is the prctl(2)
system call.但是,在 Linux 上,存在
prctl(2)
系统调用。 The option PR_SET_CHILD_SUBREAPER
allows a process to wait for more distant descendants (grandchildren, great-grandchildren, …) too.选项
PR_SET_CHILD_SUBREAPER
允许进程等待更远的后代(孙子、曾孙……)。 However, if the direct parent of a process waits for it, the ancestral process will not get the exit status information.但是,如果一个进程的直接父进程等待它,则祖先进程将不会获得退出状态信息。
1 Or posix_spawn()
1或
posix_spawn()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.