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.
 
 

71 lines
1.8 KiB

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* philo_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/11 10:29:50 by narnaud #+# #+# */
/* Updated: 2022/04/11 14:50:59 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "philosophers.h"
int mini_atoi(char *nbr)
{
int ret;
ret = 0;
while (*nbr >= '0' && *nbr <= '9')
{
ret = (*nbr - '0') + (10 * ret);
nbr++;
}
return (ret);
}
void safe_print(char *str, t_room *room, int id)
{
pthread_mutex_lock(room->safe);
printf(str, room->time, id);
pthread_mutex_unlock(room->safe);
}
int check_end(t_room *room)
{
int ret;
ret = 0;
pthread_mutex_lock(room->safe);
if (!room->running)
ret = 1;
pthread_mutex_unlock(room->safe);
return (ret);
}
int clean_memory(t_room *room)
{
t_param *param;
int i;
i = 0;
param = room->param;
while (i < param->philo_amount)
{
pthread_mutex_destroy(room->philos[i]->safe);
pthread_mutex_destroy(room->philos[i]->fork);
free(room->philos[i]->safe);
free(room->philos[i]->fork);
free(room->philos[i]->thd);
free(room->philos[i]);
i++;
}
pthread_mutex_destroy(room->safe);
free(room->safe);
free(room->philos);
free(room->param);
free(room);
return (0);
}