简体   繁体   中英

C: Wait for brothers process termination

My program have to create n childs. When a signal is recieved a child is created. Then the first child wait for the others n-1 childs. The second one wait for the other n-2 childs and so on until the last child run and finish immediately. I write this code, but it don't work and i get nephews.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>
void func(int sign)
{
    printf("received signal. I create a child\n");
}

int main(int argc, char*argv[])
{
    if(argc!=3)
    {
        printf("error\n");
        return 0;
    }
    int i,pid,status;
    int n=atoi(argv[1]);
    unsigned int m=(unsigned int)atoi(argv[2]);
    signal(SIGALRM,func);
    printf("i'm father: pid %d\n",getpid());
    for(i=0; i<n; i++)
    {
        alarm(m);
        pause();
        switch(pid=fork())
        {
        case -1:
            printf("error\n");
            break;
        case 0: 
            printf("i'm the hild numer %d, my pid is %d\n",i,getpid());
            if(i!=n-1)
            {
                wait(NULL);
                break;
            }
            else
            {
                printf("%d i have fnished\n",getpid());
                exit(0);
            }
            break;
        default:
            wait(NULL); 
            break;
        }
     }
     printf("finish\n");
     return 0;
}

The way your code is structured, you are creating 2^N processes.

You need to change the code under:

default:
   wait(NULL);
   break;

to something that does not fork any more children after that. One way to do that is using a goto statement. Here's an updated version.

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

void func(int sign)
{
   printf("received signal. I create a child\n");
}

int main(int argc, char*argv[])
{
   if(argc!=3)
   {
      printf("error\n");
      return 0;
   }
   int i,pid,status;
   int n=atoi(argv[1]);
   unsigned int m=(unsigned int)atoi(argv[2]);
   signal(SIGALRM,func);
   printf("i'm father: pid %d\n",getpid());
   for(i=0; i<n; i++)
   {
      alarm(m);
      pause();
      switch(pid=fork())
      {
         case -1:
            printf("error\n");
            break;

         case 0:
            printf("i'm the child numer %d, my pid is %d\n",i,getpid());
            if(i!=n-1)
            {
               wait(NULL);
               break;
            }
            else
            {
               printf("%d i have fnished\n",getpid());
               exit(0);
            }
            break;

         default:
            wait(NULL);
            goto done;
      }
   }

done:
   printf("%d i have fnished\n",getpid());
   return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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