Implement the philo actions
authorLukas Jiriste <ljiriste@student.42prague.com>
Thu, 9 May 2024 07:41:44 +0000 (09:41 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Thu, 9 May 2024 07:54:22 +0000 (09:54 +0200)
philo/Makefile
philo/philo.c [new file with mode: 0644]
philo/philo.h
philo/philo_helpers.c [new file with mode: 0644]

index 33a9055cb812e11db846f628e1a3a111cc118179..2679ee868269bdb3c8407cd829b6687eca23e30e 100644 (file)
@@ -7,6 +7,8 @@ NAME := philo
 INCDIR := .
 SRCDIR := .
 SRCS :=        main.c                          \
+               philo.c                         \
+               philo_helpers.c         \
                parsing.c                       \
                parsing_misc.c          \
                pars_arg.c                      \
diff --git a/philo/philo.c b/philo/philo.c
new file mode 100644 (file)
index 0000000..ec61597
--- /dev/null
@@ -0,0 +1,61 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   philo.c                                            :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/05/09 08:45:21 by ljiriste          #+#    #+#             */
+/*   Updated: 2024/05/09 09:53:45 by ljiriste         ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "philo.h"
+#include <unistd.h>
+
+static void    philo_think(t_philo *philo)
+{
+       print_timestamp(philo, "is thinking");
+       return ;
+}
+
+static void    philo_eat(t_philo *philo)
+{
+       mutex_lock(philo->forks[0]);
+       if (print_timestamp(philo, "has taken a fork"))
+       {
+               mutex_unlock(philo->forks[0]);
+               return ;
+       }
+       mutex_lock(philo->forks[1]);
+       print_timestamp(philo, "has taken a fork");
+       mutex_lock(&philo->philo_lock);
+       last_eaten = usecs_since_start(philo->settings->start);
+       ++philo->num_eaten;
+       mutex_unlock(&philo->philo_lock);
+       if (!print_timestamp(philo, "is eating"))
+               usleep(philo->settings->time_to_eat);
+       mutex_unlock(philo->forks[0]);
+       mutex_unlock(philo->forks[1]);
+       return ;
+}
+
+static void    philo_sleep(t_philo *philo)
+{
+       if (!print_timestamp(philo, "is sleeping"))
+               usleep(philo->settings->time_to_sleep);
+       return ;
+}
+
+void   *be_a_philosopher(void *void_philo)
+{
+       t_philo *const  philo = void_philo;
+
+       while (!end(philo->settings))
+       {
+               philo_think(philo);
+               philo_eat(philo);
+               philo_sleep(philo);
+       }
+       return (NULL);
+}
index f3f27762c1b8f2ca26ea14786282e047b1eea684..62dd21e65ec03cf399502a74739123822801c291 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/03/22 11:10:17 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/03/28 15:14:17 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/05/09 09:38:31 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -69,6 +69,9 @@ int                   mutex_lock(t_mutex *mutex);
 int                    mutex_unlock(t_mutex *mutex);
 int                    all_mutexes_initialized(t_diner *diner);
 
+int                    end(t_settings *set);
+int                    print_timestamp(t_philo *philo, const char *str);
+
 void           *be_a_philosopher(void *philo);
 
 int                    seat_philosophers(t_diner *diner, pthread_t *threads);
diff --git a/philo/philo_helpers.c b/philo/philo_helpers.c
new file mode 100644 (file)
index 0000000..4e5a625
--- /dev/null
@@ -0,0 +1,42 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   philo_helpers.c                                    :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/05/09 09:13:40 by ljiriste          #+#    #+#             */
+/*   Updated: 2024/05/09 09:40:43 by ljiriste         ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "philo.h"
+#include <sys/time.h>
+#include <stdio.h>
+
+int    end(t_settings *set)
+{
+       int     res;
+
+       mutex_lock(&set->end_lock);
+       res = set->end;
+       mutex_unlock(&set->end_lock);
+       return (res);
+}
+
+int    print_timestamp(t_philo *philo, const char *str)
+{
+       struct timeval  time;
+       useconds_t              since_start;
+
+       gettimeofday(&time, NULL);
+       since_start = (time.tv_sec - philo->settings->start.tv_sec) * 1000000 + time.tv_usec - philo->settings->start.tv_usec;
+       if (!end(philo->settings))
+       {
+               mutex_lock(&philo->settings->terminal_lock);
+               printf("%u %lu %s\n", since_start, philo->id, str);
+               mutex_unlock(&philo->settings->terminal_lock);
+               return (0);
+       }
+       return (1);
+}