|
|
@ -6,40 +6,12 @@ |
|
|
|
/* 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 */ |
|
|
|
/* Updated: 2022/05/18 18:26:14 by narnaud@stude ### ########.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; |
|
|
@ -73,72 +45,58 @@ int waiter(t_room *room) |
|
|
|
return (ret); |
|
|
|
} |
|
|
|
|
|
|
|
void *philos_life(void *r) |
|
|
|
static int philo_life(t_room *room, t_philo *philo, int id, int id_p, int *param) |
|
|
|
{ |
|
|
|
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 ; |
|
|
|
} |
|
|
|
return (1); |
|
|
|
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; |
|
|
|
} |
|
|
|
return (2); |
|
|
|
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; |
|
|
|
} |
|
|
|
return (2); |
|
|
|
pthread_mutex_unlock(philo->fork); |
|
|
|
pthread_mutex_unlock(room->philos[id_p]->fork); |
|
|
|
if(!safe_print("%d %d is sleeping\n", room, id)) |
|
|
|
break ; |
|
|
|
return (-1); |
|
|
|
pthread_mutex_lock(philo->lock); |
|
|
|
philo->eat_amount++; |
|
|
|
pthread_mutex_unlock(philo->lock); |
|
|
|
if (!safe_wait(room, room_clock(), param[SLEEP_DURATION])) |
|
|
|
break ; |
|
|
|
return (-1); |
|
|
|
if (!safe_print("%d %d is thinking\n", room, id)) |
|
|
|
break ; |
|
|
|
} |
|
|
|
return (NULL); |
|
|
|
return (-1); |
|
|
|
return (0); |
|
|
|
} |
|
|
|
|
|
|
|
int room_clock() |
|
|
|
void *philos_birth(void *r) |
|
|
|
{ |
|
|
|
struct timeval time; |
|
|
|
int ret; |
|
|
|
static long int start_time = 0; |
|
|
|
t_room *room; |
|
|
|
int *param; |
|
|
|
t_philo *philo; |
|
|
|
int id; |
|
|
|
int id_p; |
|
|
|
int stop; |
|
|
|
|
|
|
|
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); |
|
|
|
stop = 0; |
|
|
|
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 (!stop) |
|
|
|
stop = philo_life(room, philo, id, id_p, param); |
|
|
|
if (stop-- > 0) |
|
|
|
pthread_mutex_unlock(philo->fork); |
|
|
|
if (stop-- > 0) |
|
|
|
pthread_mutex_unlock(room->philos[id_p]->fork); |
|
|
|
return (NULL); |
|
|
|
} |
|
|
|