/* main program. Creates the philosophers and the waiter and waits for termination of its children. */ void dinner(int num_philo, int num_cycles) { if (num_philo < 2 || num_cycles < 1) { ecrire("Parameters with impossible values\n"); quitter(FAILURE); } int *philos; //to store the philosophers' pid int waiter; int my_prio = obtenir_priorite(obtenir_pid()); int child_prio = (my_prio == MAX_PRIORITY ? my_prio : my_prio + 1); //let's give an lower priority to the children in order to minimize the number of preemptions before the start of the dinner. int status; int args[4] = { 0, 0, 0, 0 }; philos = allouer(num_philo * sizeof(int)); if (philos == NULL) { ecrire("Allouer philos failed\n"); quitter(FAILURE); } int i, j; ecrire("The dining philosophers. \nBon appétit !\n"); // we need to spawn the waiter first args[0] = num_philo; waiter = fourchette(child_prio, "waiter", args); if (waiter < 1) { ecrire("An error occured. The program will stop.\n"); quitter(FAILURE); } args[0] = waiter; //the waiter's pid is a parameter of the philosophers args[2] = num_cycles; // spawns the philosophers for (i = 0; i < num_philo; i++) { args[1] = i; //philosopher's id philos[i] = fourchette(child_prio, "philosopher", args); if (philos[i] < 1) { ecrire ("An error occured. Impossible to create the philosophers. The process will exit now.\n"); //clean up for (j = 0; j < i; j++) tuer(philos[j], FAILURE); tuer(waiter, FAILURE); quitter(FAILURE); } } //everything's done, let's wait for the end for (i = 0; i < num_philo; i++) attendre(philos[i], &status); //the waiter can stop envoyer_entier(waiter, END_OF_DINNER, 10); attendre(waiter, &status); ecrire("The dinner ends.\n"); quitter(SUCCESS); }
void coquille(void) { int nb_arg, pid, status, cexit = 0; char prompt_line[255]; char buffer[255]; // char ibuf[3]; strcpy("coquille> ", prompt_line); print(prompt_line); while (!cexit) { // waiting for the user to enter a command gets(buffer, 255); printn(); // split the string nb_arg = split_args(buffer, command_arg); if (nb_arg != -1) { // if the command is exit, exit the shell if (strcmp("exit", command_arg[0]) == 0) cexit = 1; // otherwise create the desirated process else { pid = fourchette(command_arg[0], BAS_PRI, nb_arg, (char **) command_arg); if (pid > 0) wait(pid, &status); else perror("Error"); } } if (!cexit) print("coquille> "); } exit(0); }
/** * \private * Program that implements the dining philospher problem * \param argc nombre d'arguments * \param argv arguments */ void dining_philosopher(int argc, char *argv[]) { int nb_philo = stoi(get_arg(argv, 1)); int loop = stoi(get_arg(argv, 2)); int waiter; //pid waiter int philos[MAX_PHILO]; //pids philos int status; char args[2][ARG_SIZE]; char philos_args[MAX_PHILO][4][ARG_SIZE]; int i, j; char tmp[10]; char text[200]; if (argc != 3) { print ("Invalid arguments number: excepted number of philosophers and the number of cycles\n"); exit(FAILNOOB); } if (nb_philo < 2 || loop < 1) { print("Invalid arguments\n"); exit(INVARG); } /* * Creating the waiter's args */ strcpy("waiter", args[0]); /* prog_name */ strcpy(get_arg(argv, 1), args[1]); /* nb_philo */ /* * Create the waiter */ waiter = fourchette("waiter", BAS_PRI, 2, (char **) args); /* * Something went wrong ? */ if (waiter < 1) { strcpy("Error creating waiter", text); strcat(text, " : "); strcat(text, itos(waiter, tmp)); strcat(text, "\n"); print(text); exit(waiter); } /* * Spawn the philos */ for (i = 0; i < nb_philo; i++) { /* * Build the args for the philos */ strcpy("philosopher", philos_args[i][0]); itos(waiter, philos_args[i][1]); itos(i, philos_args[i][2]); /* index of the pid in the philos id array */ strcpy(get_arg(argv, 2), philos_args[i][3]); /* number of loop */ philos[i] = fourchette("philosopher", BAS_PRI, 4, (char **) philos_args[i]); /* * Something went wrong ? */ if (philos[i] < 1) { strcpy("Error creating a philosopher", text); strcat(text, " : "); strcat(text, itos(philos[i], tmp)); strcat(text, "\n"); print(text); /* * Clean it up */ for (j = 0; j < i; j++) kill(philos[j]); kill(waiter); exit(FAILNOOB); } } /* * Wait for the end */ for (i = 0; i < nb_philo; i++) wait(philos[i], &status); /* * Stop the waiter */ send((void *) END, INT_T, waiter); wait(waiter, &status); print("THE END\n"); exit(OMGROXX); }