Comment puis-je utiliser fork dans une fonction récursive?

J’essaie d’écrire un programme récursif afin de construire une arborescence de processus en utilisant fork. Je pense que je construis l’arbre correctement en fonction du nombre de niveaux et du nombre d’enfants. Cependant, quand je veux sortir, je continue à construire sur l’arbre et je dépasse le nombre d’enfants et de niveaux, et je finis donc par sortir de manière incorrecte.

Voici mon code et un exemple d’entrée et de sortie. Veuillez noter que le nombre d’enfants et le nombre de niveaux doivent être spécifiés dans les arguments de la ligne de commande.

code:

#include  #include  #include  #include  #include  int level; static void run_process(const char *child, int lev) { fprintf(stderr, "ALIVE: Level %d process with pid= %ld child of ppid=%ld\n", lev, (long)getpid(), (long)getppid()); } int fork_level (int l, int c){ int i; pid_t rightchild; if (l>=level){ fprintf(stderr, "Exiting: Level %d with pid= %ld, child of ppid= %ld\n",l, (long)getpid(), (long)getppid()); exit (1) ; } else { for (i=0; i<c; i++){ if ((rightchild = fork()) < 0) { fprintf(stderr, "can't fork, error %d\n", errno); return 0; } else if (rightchild == 0) { run_process("Right", l); fork_level (++l, c); } } for (i=0; i<c; i++){ wait (NULL); } fprintf(stderr, "Exiting: Level %d with pid= %ld, child of ppid= %ld\n",l, (long)getpid(), (long)getppid()); } } main(int argc, char *argv[]) { int i, N=0, M=0, I=0, pflag=0, uflag =0; char Nchar [3], Mchar [3], ichar [3]; for (i = 1; i < argc; i++) { if (strcmp (argv[i], "-u")==0 ){ uflag=1; } else if (strcmp (argv[i], "-N")==0){ i++; strcpy (Nchar, argv[i]); N= atoi (Nchar); } else if (strcmp (argv[i], "-M")==0){ i++; strcpy (Nchar, argv[i]); level= atoi (Nchar); } else if (strcmp (argv[i], "-p")==0){ pflag=1; } else if (strcmp (argv[i], "-s")==0){ i++; strcpy (ichar, argv[i]); I= atoi (ichar); } } if (uflag==1){ printf ("u.....\n"); } fork_level (0, N); } 

Voici comment je lance mon code ./main -u -N 2 -M 2 et voici l’exemple de sortie:

  ALIVE: Level 0 process with pid= 26534 child of ppid=26533 ALIVE: Level 0 process with pid= 26535 child of ppid=26533 ALIVE: Level 1 process with pid= 26536 child of ppid=26534 ALIVE: Level 1 process with pid= 26538 child of ppid=26534 ALIVE: Level 1 process with pid= 26537 child of ppid=26535 Exiting: Level 2 with pid= 26536, child of ppid= 26534 Exiting: Level 2 with pid= 26538, child of ppid= 26534 Exiting: Level 2 with pid= 26537, child of ppid= 26535 ALIVE: Level 1 process with pid= 26539 child of ppid=26535 Exiting: Level 2 with pid= 26539, child of ppid= 26535 Exiting: Level 1 with pid= 26534, child of ppid= 26533 Exiting: Level 1 with pid= 26535, child of ppid= 26533 Exiting: Level 1 with pid= 26535, child of ppid= 26533 ALIVE: Level 1 process with pid= 26540 child of ppid=26534 Exiting: Level 2 with pid= 26540, child of ppid= 26534 Exiting: Level 1 with pid= 26534, child of ppid= 26533 Exiting: Level 0 with pid= 26533, child of ppid= 25113