首页 > 解决方案 > 父进程工作直到子进程完成

问题描述

一个进程D创建了3 个子进程A/B/C和 fork。

父进程应与子进程通信,直到所有子进程完成。

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

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;
}                                               }

标签: cforkipcwait

解决方案


有什么简单的方法可以让父母与孩子一起工作,并在孩子们做的时候让他终止,那么?是的,这很简单,您需要做的就是使用fork()创建子进程,并且每个子进程都需要通过调用exit() 和父进程将其状态发送给父进程,waitpid()或者等待父进程应该等待所有子进程,即当没有子存在waitpid()返回-1

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

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给父母,因此在收到此信号后,父母了解孩子已完成并且需要释放父母占用的资源。


推荐阅读