42 school project
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.

135 lines
3.8 KiB

3 years ago
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* philo_launcher.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/24 15:44:04 by narnaud #+# #+# */
3 years ago
/* Updated: 2022/05/18 18:25:38 by narnaud@stude ### ########.fr */
3 years ago
/* */
/* ************************************************************************** */
#include "philosophers.h"
3 years ago
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);
3 years ago
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, \
3 years ago
philos_birth, (void *)room);
3 years ago
pthread_detach(*room->philos[room->i]->thd);
3 years ago
usleep(50);
pthread_mutex_lock(room->lock);
3 years ago
if ((room->i % 2) == 0)
3 years ago
{
3 years ago
room->i += 2;
3 years ago
if (room->i >= room->param[PHILO_AMOUNT])
room->i -= 1 + (room->param[PHILO_AMOUNT] % 2) * 2;
}
3 years ago
else if ((room->i % 2) == 1)
room->i -= 2;
3 years ago
pthread_mutex_unlock(room->lock);
3 years ago
}
3 years ago
while (waiter(room))
3 years ago
usleep(CLOCK_TWO);
3 years ago
pthread_mutex_lock(room->lock);
room->running = 0;
3 years ago
pthread_mutex_lock(room->lock);
sleep(2);
3 years ago
return (clean_memory(room));
}
3 years ago
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)
3 years ago
{
3 years ago
int *param;
static int i = 0;
3 years ago
3 years ago
pthread_mutex_lock(room->lock);
3 years ago
param = room->param;
3 years ago
while (i < param[PHILO_AMOUNT])
3 years ago
{
3 years ago
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]);
3 years ago
i++;
}
3 years ago
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);
3 years ago
}