简体   繁体   English

子进程出错并等待`C`

[英]Error with child process & wait `C`

In the below code, if there is a problem creating a child process or something happens to the child process what happens to wait(&status) ? 在下面的代码中,如果在创建子进程时遇到问题,或者子进程发生问题,则wait(&status)会发生什么?

pid_t pid;
int status;
if(pid=fork()){
   printf("Parent Process\n");
   wait(&status);
} else... child process here

If there is a problem creating a child process, fork will return -1 , so this code will never wait . 如果创建子进程有问题,fork将返回-1 ,因此该代码将永不wait

If there's something happend to the child process, wait will return, and you can observe status . 如果子进程发生问题,则等待将返回,您可以观察status

If the child cannot be created, fork() will return with -1, you should look at errno after that. 如果无法创建子级,则fork()将返回-1,之后应查看errno。 No error process is created here. 此处未创建错误过程。 Your code does not check this case. 您的代码不会检查这种情况。

If the child is created and dies, wait() will return the PID of the terminated process, the reason for the child's death is given in status. 如果孩子被创建并死亡,wait()将返回终止进程的PID,状态中给出了孩子死亡的原因。 See the man page for wait on how to extract meaning from status. 请参见手册页,以等待如何从状态中提取含义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM