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.
 
 

144 lines
3.7 KiB

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* philo_threads.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/26 07:43:48 by narnaud #+# #+# */
/* Updated: 2022/05/16 22:28:37 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "philosophers.h"
static int safe_print(char *str, t_room *room, int id)
{
int ret;
if (!room->running)
return (0);
pthread_mutex_lock(room->lock);
printf(str, room_clock(), id);
ret = room->running;
pthread_mutex_unlock(room->lock);
return (ret);
}
int safe_wait(t_room *room, int start, int duration)
{
int ret;
ret = room->running;
while (ret && (room_clock() < (start + duration)))
{
pthread_mutex_lock(room->lock);
ret = room->running;
pthread_mutex_unlock(room->lock);
usleep(50);
}
return (ret);
}
int waiter(t_room *room)
{
int *param;
int i;
int starvest;
t_philo *philo;
int ret;
ret = 1;
pthread_mutex_lock(room->lock);
i = 0;
param = room->param;
starvest = param[MEALS_AMOUNT];
while (ret && i < param[PHILO_AMOUNT])
{
philo = room->philos[i];
pthread_mutex_lock(philo->lock);
if (philo->eat_time + param[DIE_TIME] < room_clock())
{
printf("%d %d died\n", room_clock(), i);
ret = 0;
}
if (philo->eat_amount < starvest)
starvest = philo->eat_amount;
pthread_mutex_unlock(philo->lock);
i++;
}
pthread_mutex_unlock(room->lock);
if (starvest == param[MEALS_AMOUNT])
ret = 0;
return (ret);
}
void *philos_life(void *r)
{
t_room *room;
int *param;
t_philo *philo;
int id;
int id_p;
room = (t_room *)r;
param = room->param;
pthread_mutex_lock(room->lock);
id = room->i;
printf("Philo %d: \n", id);
philo = room->philos[id];
philo->eat_time = room_clock();
id_p = (id + 1) % param[PHILO_AMOUNT];
pthread_mutex_unlock(room->lock);
while (1)
{
pthread_mutex_lock(philo->fork);
if (!safe_print("%d %d has taken a fork\n", room, id))
{
pthread_mutex_unlock(philo->fork);
break ;
}
pthread_mutex_lock(room->philos[id_p]->fork);
safe_print("%d %d has taken a fork\n", room, id);
if(!safe_print("%d %d is eating\n", room, id))
{
pthread_mutex_unlock(philo->fork);
pthread_mutex_unlock(room->philos[id_p]->fork);
break;
}
pthread_mutex_lock(philo->lock);
philo->eat_time = room_clock();
pthread_mutex_unlock(philo->lock);
if (!safe_wait(room, room_clock(), param[EAT_DURATION]))
{
pthread_mutex_unlock(philo->fork);
pthread_mutex_unlock(room->philos[id_p]->fork);
break;
}
pthread_mutex_unlock(philo->fork);
pthread_mutex_unlock(room->philos[id_p]->fork);
if(!safe_print("%d %d is sleeping\n", room, id))
break ;
pthread_mutex_lock(philo->lock);
philo->eat_amount++;
pthread_mutex_unlock(philo->lock);
if (!safe_wait(room, room_clock(), param[SLEEP_DURATION]))
break ;
if (!safe_print("%d %d is thinking\n", room, id))
break ;
}
return (NULL);
}
int room_clock()
{
struct timeval time;
int ret;
static long int start_time = 0;
gettimeofday(&time, NULL);
if (!start_time)
start_time = time.tv_sec * 1000 + time.tv_usec / 1000;
ret = (int)(time.tv_sec * 1000 + time.tv_usec / 1000 - start_time);
return (ret);
}