|
|
@ -6,7 +6,7 @@ |
|
|
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|
|
|
/* +#+#+#+#+#+ +#+ */ |
|
|
|
/* Created: 2021/11/26 07:43:48 by narnaud #+# #+# */ |
|
|
|
/* Updated: 2022/04/11 14:52:22 by narnaud ### ########.fr */ |
|
|
|
/* Updated: 2022/05/16 17:51:21 by narnaud ### ########.fr */ |
|
|
|
/* */ |
|
|
|
/* ************************************************************************** */ |
|
|
|
|
|
|
@ -25,60 +25,30 @@ void *philos_life(void *r) |
|
|
|
pthread_mutex_lock(room->safe); |
|
|
|
id = room->i; |
|
|
|
philo = room->philos[id]; |
|
|
|
philo->eat_time = room->time; |
|
|
|
philo->eat_time = gettime(); |
|
|
|
id_p = (id + 1) % param->philo_amount; |
|
|
|
pthread_mutex_unlock(room->safe); |
|
|
|
while (room->running) |
|
|
|
{ |
|
|
|
pthread_mutex_unlock(room->safe); |
|
|
|
pthread_mutex_lock(philo->fork); |
|
|
|
safe_print("%ld %d has taken a fork\n", room, id); |
|
|
|
pthread_mutex_lock(room->philos[id_p]->fork); |
|
|
|
safe_print("%ld %d has taken a fork\n", room, id); |
|
|
|
safe_print("%ld %d is eating\n", room, id); |
|
|
|
pthread_mutex_lock(philo->safe); |
|
|
|
philo->eat_time = room->time; |
|
|
|
pthread_mutex_unlock(philo->safe); |
|
|
|
if (check_end(room)) |
|
|
|
{ |
|
|
|
pthread_mutex_unlock(philo->fork); |
|
|
|
pthread_mutex_unlock(room->philos[id_p]->fork); |
|
|
|
if (!safe_wait(room, gettime(), param->eat_duration * 1000)) |
|
|
|
return (NULL); |
|
|
|
} |
|
|
|
usleep(param->eat_duration * 1000); |
|
|
|
pthread_mutex_unlock(philo->fork); |
|
|
|
pthread_mutex_unlock(room->philos[id_p]->fork); |
|
|
|
if (check_end(room)) |
|
|
|
return (NULL); |
|
|
|
safe_print("%ld %d is sleeping\n", room, id); |
|
|
|
pthread_mutex_lock(philo->safe); |
|
|
|
philo->eat_time = gettime(); |
|
|
|
philo->eat_amount++; |
|
|
|
pthread_mutex_unlock(philo->safe); |
|
|
|
usleep(param->sleep_duration * 1000); |
|
|
|
if (check_end(room)) |
|
|
|
safe_print("%ld %d is sleeping\n", room, id); |
|
|
|
if (!safe_wait(room, gettime(), param->sleep_duration * 1000)) |
|
|
|
return (NULL); |
|
|
|
safe_print("%ld %d is thinking\n", room, id); |
|
|
|
} |
|
|
|
return (NULL); |
|
|
|
} |
|
|
|
|
|
|
|
void *room_clock(void *r) |
|
|
|
{ |
|
|
|
t_room *room; |
|
|
|
struct timeval time; |
|
|
|
|
|
|
|
room = (t_room *)r; |
|
|
|
while (1) |
|
|
|
{ |
|
|
|
gettimeofday(&time, NULL); |
|
|
|
pthread_mutex_lock(room->safe); |
|
|
|
room->time = time.tv_sec * 1000 + time.tv_usec / 1000 - room->start_time; |
|
|
|
if (!room->running) |
|
|
|
{ |
|
|
|
pthread_mutex_unlock(room->safe); |
|
|
|
return(NULL); |
|
|
|
} |
|
|
|
pthread_mutex_unlock(room->safe); |
|
|
|
usleep(CLOCK_TWO); |
|
|
|
} |
|
|
|
pthread_mutex_unlock(room->safe); |
|
|
|
return (NULL); |
|
|
|
} |
|
|
|