C 子进程中的OS:wait()

C 子进程中的OS:wait(),c,operating-system,wait,child-process,C,Operating System,Wait,Child Process,在学习操作系统时,在任何教科书中都可以看到,在父进程中使用fork()创建子进程,有时在父进程中调用wait()以等待子进程的完成 但是,如果我在子对象中使用wait(),会发生什么 #include <stdlib.h> #include <stdio.h> #include <unistd.h> #include <sys/wait.h> int main(int argc, char** argv){ int x; int w1,

在学习操作系统时,在任何教科书中都可以看到,在父进程中使用fork()创建子进程,有时在父进程中调用wait()以等待子进程的完成

但是,如果我在子对象中使用wait(),会发生什么

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>

int
main(int argc, char** argv){
  int x;
  int w1, w2;
  x = 100;
  printf("the initialized x is: %d\n",x);
  int rc = fork();
  if(rc < 0){
    fprintf(stderr, "fork failed.\n");
    exit(1);
  }
  else if(rc == 0){
    //x = 200;                                                                  
    w1 = wait(NULL);
    printf("the x in child process (pid:%d) is: %d\n", (int) getpid(), x);
  } else {
    x = 300;
    w2 = wait(NULL);
    printf("the x in parent process (pid:%d) is: %d\n",(int) getpid(), x);
  }
  printf("the x in the end is: %d\n",x);
  printf("the returns of waits: %d, %d\n",w1,w2);
  return 0;
}
怎么解释呢

但是,如果我在子对象中使用wait(),会发生什么

你看过它的文档了吗?特别是关于返回值和错误条件的部分?我看到你做了一个实验,很好;在这和文档之间,应该已经很清楚了,如果调用
wait()
的进程没有子进程要等待,那么它会立即返回
-1
,指示错误。在这种情况下,
errno
将设置为
ECHILD


立即返回错误是完全有意义的,因为如果调用进程没有子进程,唯一的替代方法是等待,可能是无限期地等待,等待一个只要等待就不能发生的事件。

您已经说过在示例输出中会发生什么,但要获得更多详细信息,您必须在从
wait
系统调用收到故障代码后检查
errno
值。在检查
是否(errno==ECHILD)
有帮助并可用于执行某些可能处理此错误的操作时,您还可以使用以下代码获取带有错误消息的字符串(字符数组):

#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>

...

char err_msg[4096]; // a buffer big enough to hold an error message
errno = 0; // clear errno just in case there was an earlier error
pid_t fpid = fork();
if (fpid < 0)
{
    perror("fork failed.\n"); // prints out an error string based on errno
    exit(1);
} else if (fpid == 0)
{ // in child
    pid_t wpid = wait(NULL);                                                             
    strerror_r(errno, err_msg, sizeof(err_msg)); // gets an error string
    printf("child process (pid:%d), wpid:%d, %s\n", (int) getpid(), (int)wpid, err_msg);
} else {
    int err_val;
    wpid = wait(NULL);
    err_val = errno;
    errno = 0; // reset errno
    strerror_r(err_val , err_msg, sizeof(err_msg)); // gets an error string
    printf("parent process (pid:%d), wpid:%d, %s\n", (int) getpid(), (int)wpid, err_msg);
    if (err_val != 0)
    {
        // there was an error, so what are you going to do about it?
    }
}
#包括
#包括
#包括
#包括
#包括
#包括
...
字符错误消息[4096];//足以容纳错误消息的缓冲区
errno=0;//清除errno,以防出现早期错误
pid_t fpid=fork();
如果(fpid<0)
{
perror(“fork失败。\n”);//根据errno打印出一个错误字符串
出口(1);
}else if(fpid==0)
儿童{//
pid_t wpid=等待(NULL);
strerror_r(errno,err_msg,sizeof(err_msg));//获取错误字符串
printf(“子进程(pid:%d),wpid:%d,%s\n”,(int)getpid(),(int)wpid,err_msg);
}否则{
内部错误;
wpid=wait(NULL);
err_val=errno;
errno=0;//重置errno
strerror\u r(err\u val,err\u msg,sizeof(err\u msg));//获取错误字符串
printf(“父进程(pid:%d),wpid:%d,%s\n”,(int)getpid(),(int)wpid,err_msg);
如果(错误值!=0)
{
//有一个错误,那么你打算怎么办?
}
}
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>

...

char err_msg[4096]; // a buffer big enough to hold an error message
errno = 0; // clear errno just in case there was an earlier error
pid_t fpid = fork();
if (fpid < 0)
{
    perror("fork failed.\n"); // prints out an error string based on errno
    exit(1);
} else if (fpid == 0)
{ // in child
    pid_t wpid = wait(NULL);                                                             
    strerror_r(errno, err_msg, sizeof(err_msg)); // gets an error string
    printf("child process (pid:%d), wpid:%d, %s\n", (int) getpid(), (int)wpid, err_msg);
} else {
    int err_val;
    wpid = wait(NULL);
    err_val = errno;
    errno = 0; // reset errno
    strerror_r(err_val , err_msg, sizeof(err_msg)); // gets an error string
    printf("parent process (pid:%d), wpid:%d, %s\n", (int) getpid(), (int)wpid, err_msg);
    if (err_val != 0)
    {
        // there was an error, so what are you going to do about it?
    }
}