Parent 应该收到有关孙子工作完成的信息

Parent should receive information about the grandchild work completion

标题说明了一切。我正在学习操作系统,我想用 C 编程语言编写代码,Parent 应该接收有关完成 grandchild 工作的信息。我如何使用 forkwait 来做到这一点?

returns 代码 parent 关于 child 工作完成的信息。我想对 grandchild.

做同样的事情

这是我目前尝试过的方法:

int main( int argc, char * argv[] ){
   pid_t child, rv; 
   int st;      
   child = fork();
   if( child == 0 ){
      sleep(5);
      return 5;
   }    
   printf( "Child PID = %ld\n", (long)child );  
   rv = wait( &st );    
   printf( "wait() = %ld\nst = %x\n", (long)rv, st );   
   printf( "Child exit code = %d\n", WEXITSTATUS(st) );
   return 0;

}

让我们按如下方式调用我们的流程:

t1 - Parent process
t2 - Child process
t3 - Grandchild process

执行此操作的方法如下:

  1. t1 创建子进程 t2
  2. t1等待子进程t2退出状态

现在,父 t1 进程被阻塞并等待子进程 t2 完成。

  1. 创建子进程t2创建自己的子进程t3
  2. t2 等待子进程 t3.

现在 t3 运行s,完成并 returns 退出状态到父 t2t2 将 运行 和 return 该状态传给其父级 t1。通过这种方式,您可以将 t3 的退出状态一直传播到 t1.