1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
|
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
void fils(int nbPetale, int numProc, int nbPetMax, int nbProcMax){
int pid;
printf("Fils nbPetale:%d numProc:%d nbPetMax:%d nbProcMax:%d\n", nbPetale, numProc, nbPetMax, nbProcMax);
if(nbProcMax>1){
pid=fork();
if(pid==0){
fils(nbPetale, numProc+1, nbPetMax, nbProcMax-1);
}else{
wait(0);
}
}
}
void creerPetale(int nbPetale, int numProc, int nbPetMax, int nbProcMax){
int i, pid;
printf("Creer petale nbPetale:%d numProc: %d nbPetMax:%d nbProcMax:%d\n", nbPetale, numProc, nbPetMax, nbProcMax);
if(nbProcMax>1){
pid=fork();
if(pid==0){
fils(nbPetale, 2, nbPetMax, nbProcMax-1);
exit(0);
}else{
waitpid(pid,0,0);
}
}
}
#define MAX_PETALES 25
int main(int argc, char* argv[]){
int petale;
int nb_petales;
char tube[MAX_PETALES]; // 25 devrait
char tube2[MAX_PETALES];
int procs_per_petale[MAX_PETALES];
int sortieTube;
if(argc==1) {
fprintf(stderr, "Erreur, usage %s <nb_petales> procs_petale1 procs_petale2 procs_petale3 ...\n",argv[0]);
exit(0);
}
nb_petales=atoi(argv[1]); // argc devrait être utilisé plutôt?
printf("Create %d petales\n", nb_petales);
if(nb_petales<=0 || nb_petales>MAX_PETALES){
fprintf(stderr, "Erreur, le nombre de pétales doit être compris entre 1 et %d\n",MAX_PETALES);
exit(0);
}
for(petale=0; petale<nb_petales; petale++){
procs_per_petale[petale]=atoi(argv[petale+2]);
printf("Pétales %d, nb_proc %d \n", petale, procs_per_petale[petale]);
}
for(petale=0; petale<nb_petales; petale++){
printf("Creer petale %d\n",petale);
creerPetale(petale+1, 1, nb_petales, procs_per_petale[petale]);
sleep(1);
}
return 0;
} |
Partager