You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
120 lines
3.6 KiB
120 lines
3.6 KiB
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* philo_launcher.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2021/11/24 15:44:04 by narnaud #+# #+# */
|
|
/* Updated: 2022/05/18 19:27:01 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);
|
|
|
|
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(100);
|
|
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_unlock(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);
|
|
}
|
|
|