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.
62 lines
2.2 KiB
62 lines
2.2 KiB
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* philo_init.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2022/04/11 10:33:01 by narnaud #+# #+# */
|
|
/* Updated: 2022/04/11 10:35:12 by narnaud ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "philosophers.h"
|
|
|
|
t_param *init_param(char **argv)
|
|
{
|
|
t_param *ret;
|
|
|
|
ret = malloc(sizeof(t_param));
|
|
ret->philo_amount = mini_atoi(argv[0]);
|
|
ret->die_time = mini_atoi(argv[1]);
|
|
ret->eat_duration = mini_atoi(argv[2]);
|
|
ret->sleep_duration = mini_atoi(argv[3]);
|
|
if (argv[4])
|
|
ret->eat_amount = mini_atoi(argv[4]);
|
|
else
|
|
ret->eat_amount = 2147483647;
|
|
return (ret);
|
|
}
|
|
|
|
t_room *init_room(char **argv)
|
|
{
|
|
t_room *room;
|
|
struct timeval time;
|
|
static int i = 0;
|
|
|
|
gettimeofday(&time, NULL);
|
|
room = malloc(sizeof(t_room));
|
|
room->param = init_param(argv);
|
|
room->safe = malloc(sizeof(pthread_mutex_t));
|
|
pthread_mutex_init(room->safe, NULL);
|
|
room->philos = \
|
|
(t_philo **)malloc(room->param->philo_amount * sizeof(t_philo *));
|
|
room->running = 1;
|
|
room->start_time = time.tv_sec * 1000 + time.tv_usec / 1000;
|
|
room->time = 0;
|
|
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]->safe = malloc(sizeof(pthread_mutex_t));
|
|
room->philos[i]->thd = malloc(sizeof(pthread_t));
|
|
room->philos[i]->eat_time = room->time;
|
|
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++]->safe, NULL);
|
|
}
|
|
return (room);
|
|
}
|
|
|
|
|