【发布时间】:2017-04-02 07:29:53
【问题描述】:
我正在探索 c 中的流程。我想以某种方式更新以下程序,孩子以退出状态(例如 10)终止。从父进程中获取这个退出状态并打印在父进程中。我尝试了以下方式,但是从父母那里得到的状态是错误的。我哪里出错了?请帮我解决这个问题。提前致谢。
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
int main()
{
int pid;
int ppid;
int status;
pid=vfork();
pid_t return_pid = waitpid(pid, &status, WNOHANG);
//printf("%d\n", pid); --> value = 0
if (pid < 0) {
printf("\n Error ");
exit(1);
} else if (pid==0) {
printf("\n Hello I am the child process\n");
printf("\n My pid is: %d\n", getpid());
printf("\n My parent pid is: %d\n", getppid());
printf("\n My return_pid is: %d\n", return_pid);
printf("_______________________________________\n");
int es = WEXITSTATUS(status);
printf ("\n Child exit status: %d\n", es);
status = es;
exit(0);
} else {
printf("The child has terminated!\n");
printf("_______________________________________\n");
printf("\n Hello I am the parent process\n");
printf("\n My actual pid is %d\n" , getpid());
printf("\n My parent pid is: %d\n", getppid());
printf("\n My return_pid is: %d\n", return_pid);
printf("_______________________________________\n");
printf ("\n Child's Exit staus from parent: %d\n", status);
exit(1);
}
}
【问题讨论】:
-
您应该只在 parent 进程中等待子进程。并且记得检查
waitpid和WNOHANG实际返回的状态。 -
另外,来自the POSIX secification of
vfork:“如果vfork()创建的进程修改任何数据而不是变量,则行为未定义pid_t类型的用于存储来自vfork()、... 的返回值,或在成功调用_exit()或其中一个 exec 之前调用 任何其他函数函数族”。由于您在子进程中修改变量并调用“其他函数”,因此您有未定义的行为。 -
请注意
vfork()不是POSIX 2008 的一部分。它是在 POSIX 的早期版本中定义的,用于向后兼容 SUS(单一 Unix 规范)和 SVID(系统 V 接口定义)。由于对其使用的限制,通常最简单的方法是假装它不存在而不使用它。有人说它使 fork/exec 过程更加有效。您还可以查看posix_spawn()了解另一种控制子进程的方法。 -
哦,好的。那么我如何才能在父母中获得孩子的退出状态?
-
你的孩子的退出状态将为0,除非它被信号杀死,因为你的孩子无条件地
exit(0);。您可以使用wait()或waitpid()找到它——忽略WNOHANG选项;你想挂断电话,直到孩子死去。你的代码结构是……不寻常的,容我们说。只有父母需要等待。