/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/19 15:53:52 by ljiriste #+# #+# */
-/* Updated: 2024/03/22 09:22:19 by ljiriste ### ########.fr */
+/* Updated: 2024/03/22 09:44:44 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
suseconds_t now;
i = 0;
- now = cur_time_sus();
while (i < set->philo_count)
{
+ pthread_mutex_lock(&philo->state_lock);
+ now = cur_time_sus();
if ((now - philo->last_eaten) > set->time_to_die)
{
+ pthread_mutex_unlock(&philo->state_lock);
pthread_mutex_lock(&set->terminal);
printf("%li %lu %s\n", (cur_time_sus() - set->program_start)
/ 1000, i + 1, G_DIE_STR);
pthread_mutex_unlock(&set->terminal);
return (1);
}
+ pthread_mutex_unlock(&philo->state_lock);
++i;
}
return (0);
return (0);
while (i < set->philo_count)
{
+ pthread_mutex_lock(&philos[i].state_lock);
if (philos[i].times_eaten < set->min_eats_num)
+ {
+ pthread_mutex_unlock(&philos[i].state_lock);
return (0);
+ }
+ pthread_mutex_unlock(&philos[i].state_lock);
++i;
}
return (1);
i = 1;
while (i < set->philo_count)
{
+ pthread_mutex_lock(&philos[i].state_lock);
if (furthest_eaten > philos[i].last_eaten)
furthest_eaten = philos[i].last_eaten;
+ pthread_mutex_unlock(&philos[i].state_lock);
++i;
}
return (set->time_to_die - (cur_time_sus() - furthest_eaten));
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/19 15:48:58 by ljiriste #+# #+# */
-/* Updated: 2024/03/21 10:04:35 by ljiriste ### ########.fr */
+/* Updated: 2024/03/22 09:39:54 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
philos[count].times_eaten = 0;
philos[count].settings = set;
philos[count].forks[0] = forks + count - count % 2;
+ pthread_mutex_init(&philos[count].state_lock, NULL);
if (count > 0)
philos[count].forks[1] = forks + count - (count + 1) % 2;
}
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/05 12:49:40 by ljiriste #+# #+# */
-/* Updated: 2024/03/21 10:26:01 by ljiriste ### ########.fr */
+/* Updated: 2024/03/22 09:41:43 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
{
--count;
pthread_mutex_destroy(&forks[count]);
+ pthread_mutex_destroy(&philos[count].state_lock);
pthread_join(threads[count], NULL);
}
pthread_mutex_destroy(&set->terminal);
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/19 14:25:18 by ljiriste #+# #+# */
-/* Updated: 2024/03/21 10:16:40 by ljiriste ### ########.fr */
+/* Updated: 2024/03/22 09:40:43 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
// unsigned long would be more apropriate for min_eats_num
// but for the ease of parsing I'm using size_t (they are the same)
+// terminal mutex is also used to lock the end member
typedef struct s_settings
{
int end;
size_t times_eaten;
size_t id;
suseconds_t last_eaten;
+ pthread_mutex_t state_lock;
pthread_mutex_t *forks[2];
t_settings *settings;
} t_philo;
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/21 09:16:23 by ljiriste #+# #+# */
-/* Updated: 2024/03/21 10:26:14 by ljiriste ### ########.fr */
+/* Updated: 2024/03/22 09:42:20 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
pthread_mutex_lock(philo->forks[1]);
thread_print(philo->settings, philo->id, G_FORK_STR);
thread_print(philo->settings, philo->id, G_EAT_STR);
+ pthread_mutex_lock(&philo->state_lock);
philo->last_eaten = cur_time_sus();
- usleep(philo->settings->time_to_eat);
++philo->times_eaten;
+ pthread_mutex_unlock(&philo->state_lock);
+ usleep(philo->settings->time_to_eat);
pthread_mutex_unlock(philo->forks[0]);
pthread_mutex_unlock(philo->forks[1]);
return ;