【问题标题】:Parent process working until child processes finish父进程工作直到子进程完成
【发布时间】:2018-09-13 16:36:05
【问题描述】:

进程D创建3进程,A/B/C ,带叉子。

父进程应与子进程通信,直到所有子进程结束。

有什么简单的方法可以让父母与孩子一起工作,并让他在孩子做的时候终止,所以?

Parent Process                                  Child Process
--------------                                  --------------
int main(){                                     int main(){
  // create children ... 
  while( all children are alive ){               for(sometime){
      // ipc with children                         // ipc with parent
   }                                              }

   return 0;                                      return 0;
}                                               }

【问题讨论】:

  • 是的,你会使用wait(也可能是SIGCHLD 处理程序)。
  • 你可以使用wait和waitpid来做到这一点。

标签: c fork ipc wait


【解决方案1】:

有没有什么简单的方法可以让父母和孩子一起工作,让他在孩子做的时候终止,所以?是的,很简单,你需要做的就是使用fork()来创建孩子进程,每个子进程需要通过调用exit() 将他们的状态发送给父进程,并且在父进程中使用waitpid()wait 这样父进程应该等待所有子进程,即当不存在子进程时waitpid() 返回@987654326 @。

例如下面是示例子父代码。

int a[3]; //to store pid's of children
int main(void) {
        if( (a[0]=fork()) == 0) { /* 1st child */
                int randNo;
                srand(getpid());
                randNo=rand()%10+1;
                printf("1st_child sleep for %d sec\n",randNo);
                sleep(randNo);
                exit(1); /* sending the exit status to parent */
        }
        else {
                if( (a[1]=fork()) == 0) { /* 1nd child */
                        int randNo;
                        srand(getpid());
                        randNo=rand()%10+1;
                        printf("2nd_child sleep for %d sec\n",randNo);
                        sleep(randNo);
                        exit(2);
                }
                else {
                        if( (a[3]=fork()) == 0) { /*1rd child */
                                int randNo;
                                srand(getpid());
                                randNo=rand()%10+1;
                                printf("3rd_child sleep for %d sec\n",randNo);
                                sleep(randNo);
                                exit(3);
                        }
                        else { /* common parent for all 3 child */
                                int status; //exit status of child
                                //signal(SIGCHLD,my_isr);
                                while(wait(&status) != -1) { /* when there is no child, wait() returns -1 */
                                        if( status>>8 == 1 ) { /* if 1st child status received */
                                                a[0] = 0;
                                                printf("child_1 removed from zombie \n");
                                        }
                                        else if( status>>8 == 2) {
                                                a[1] = 0;
                                                printf("child_2 removed from zombie \n");
                                        }
                                        else if( status>>8 == 3) {
                                                a[2] = 0;
                                                printf("child_3 removed from zombie \n");
                                        }
                                }
                        }
                }
        }
        return 0;
}

您也可以设置一个signal handler,当孩子完成时,它会向父母发送SIGCHLD,因此在收到此信号后,父母会明白孩子已经完成并且需要释放父母占用的资源。

【讨论】:

    猜你喜欢
    • 2013-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-17
    • 1970-01-01
    • 2011-04-14
    • 1970-01-01
    相关资源
    最近更新 更多