/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* philo_launcher.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/11/24 15:44:04 by narnaud #+# #+# */ /* Updated: 2022/05/18 18:25:38 by narnaud@stude ### ########.fr */ /* */ /* ************************************************************************** */ #include "philosophers.h" void init_param(t_room *room, char **argv); static t_room *init_room(char **argv); static int clean_memory(t_room *room); static int mini_atoi(char *str); int main(int argc, char **argv) { t_room *room; if (argc < 5 || argc > 6) return (1); room = init_room(argv + 1); while (room->i >= 0) { pthread_create(room->philos[room->i]->thd, NULL, \ philos_birth, (void *)room); pthread_detach(*room->philos[room->i]->thd); usleep(50); pthread_mutex_lock(room->lock); if ((room->i % 2) == 0) { room->i += 2; if (room->i >= room->param[PHILO_AMOUNT]) room->i -= 1 + (room->param[PHILO_AMOUNT] % 2) * 2; } else if ((room->i % 2) == 1) room->i -= 2; pthread_mutex_unlock(room->lock); } while (waiter(room)) usleep(CLOCK_TWO); pthread_mutex_lock(room->lock); room->running = 0; pthread_mutex_lock(room->lock); sleep(2); return (clean_memory(room)); } void init_param(t_room *room, char **argv) { room->param[PHILO_AMOUNT] = mini_atoi(argv[0]); room->param[DIE_TIME] = mini_atoi(argv[1]); room->param[EAT_DURATION] = mini_atoi(argv[2]); room->param[SLEEP_DURATION] = mini_atoi(argv[3]); if (argv[4]) room->param[MEALS_AMOUNT] = mini_atoi(argv[4]); else room->param[MEALS_AMOUNT] = 2147483647; } static t_room *init_room(char **argv) { t_room *room; static int i = 0; room = malloc(sizeof(t_room)); init_param(room, argv); room->lock = malloc(sizeof(pthread_mutex_t)); pthread_mutex_init(room->lock, NULL); room->philos = \ (t_philo **)malloc(room->param[PHILO_AMOUNT] * sizeof(t_philo *)); room->running = 1; room->i = 0; while (i < room->param[PHILO_AMOUNT]) { room->philos[i] = (t_philo *)malloc(sizeof(t_philo)); room->philos[i]->fork = malloc(sizeof(pthread_mutex_t)); room->philos[i]->lock = malloc(sizeof(pthread_mutex_t)); room->philos[i]->thd = malloc(sizeof(pthread_t)); room->philos[i]->eat_time = room_clock(); room->philos[i]->eat_amount = 0; room->philos[i]->i = i; pthread_mutex_init(room->philos[i]->fork, NULL); pthread_mutex_init(room->philos[i++]->lock, NULL); } return (room); } static int clean_memory(t_room *room) { int *param; static int i = 0; pthread_mutex_lock(room->lock); param = room->param; while (i < param[PHILO_AMOUNT]) { pthread_mutex_lock(room->philos[i]->lock); pthread_mutex_unlock(room->philos[i]->lock); pthread_mutex_destroy(room->philos[i]->lock); pthread_mutex_lock(room->philos[i]->fork); pthread_mutex_unlock(room->philos[i]->fork); pthread_mutex_destroy(room->philos[i]->fork); free(room->philos[i]->lock); free(room->philos[i]->fork); free(room->philos[i]->thd); free(room->philos[i]); i++; } pthread_mutex_unlock(room->lock); pthread_mutex_destroy(room->lock); free(room->lock); free(room->philos); free(room); return (0); } int mini_atoi(char *nbr) { int ret; ret = 0; while (*nbr >= '0' && *nbr <= '9') { ret = (*nbr - '0') + (10 * ret); nbr++; } return (ret); }