/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/05 12:49:40 by ljiriste #+# #+# */
-/* Updated: 2024/03/19 14:14:15 by ljiriste ### ########.fr */
+/* Updated: 2024/03/19 14:20:33 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
static const char *g_fed_str = "All philosophers are well fed";
+static const char *g_think_str = "is thinking";
+static const char *g_fork_str = "has taken a fork";
+static const char *g_eat_str = "is eating";
+static const char *g_sleep_str = "is sleeping";
+static const char *g_die_str = "died";
// unsigned long would be more apropriate for min_eats_num
// but for the ease of parsing I'm using size_t
typedef struct s_settings
void philo_sleep(t_settings *set, size_t id)
{
- thread_print(set, id, "is sleeping");
+ thread_print(set, id, g_sleep_str);
usleep(set->time_to_sleep);
return ;
}
void philo_think(t_settings *set, size_t id)
{
- thread_print(set, id, "is thinking");
+ thread_print(set, id, g_think_str);
return ;
}
void philo_eat(t_philo *philo)
{
pthread_mutex_lock(philo->forks[0]);
- thread_print(philo->settings, philo->id, "has taken a fork");
+ thread_print(philo->settings, philo->id, g_fork_str);
pthread_mutex_lock(philo->forks[1]);
- thread_print(philo->settings, philo->id, "has taken a fork");
- thread_print(philo->settings, philo->id, "is eating");
+ thread_print(philo->settings, philo->id, g_fork_str);
+ thread_print(philo->settings, philo->id, g_eat_str);
philo->last_eaten = cur_time_sus();
usleep(philo->settings->time_to_eat);
++philo->times_eaten;
if((now - philos[i].last_eaten) > set->time_to_die)
{
pthread_mutex_lock(&set->terminal);
- printf("%li %lu %s\n", (cur_time_sus() - set->program_start) / 1000, i + 1, "died");
+ printf("%li %lu %s\n", (cur_time_sus() - set->program_start) / 1000, i + 1, g_die_str);
set->end = 1;
pthread_mutex_unlock(&set->terminal);
return ;
int handle_single_philo(t_settings *set)
{
set->program_start = cur_time_sus();
- thread_print(set, 1, "is thinking");
- thread_print(set, 1, "has taken a fork");
+ thread_print(set, 1, g_think_str);
+ thread_print(set, 1, g_fork_str);
usleep(set->time_to_die);
- thread_print(set, 1, "died");
+ thread_print(set, 1, g_die_str);
return (0);
}