aboutsummaryrefslogtreecommitdiff
path: root/philo/src
diff options
context:
space:
mode:
authorDominik Kaiser2025-01-18 12:50:55 +0100
committerDominik Kaiser2025-01-18 12:50:55 +0100
commit56afbb3011c346dfc0d9e6d92e76cb13fdb272c8 (patch)
treeff786f412a8ed2fd4cba316dcf11220fdf956046 /philo/src
parente3c8f02a98c2cbf73266bf232c0fb38e48adcd06 (diff)
downloadPhilosophers-56afbb3011c346dfc0d9e6d92e76cb13fdb272c8.tar.gz
Philosophers-56afbb3011c346dfc0d9e6d92e76cb13fdb272c8.zip
Add ft_log func
Diffstat (limited to 'philo/src')
-rw-r--r--philo/src/ft_utils.c12
-rw-r--r--philo/src/simulation.c10
2 files changed, 17 insertions, 5 deletions
diff --git a/philo/src/ft_utils.c b/philo/src/ft_utils.c
index 9eb660a..7570b1d 100644
--- a/philo/src/ft_utils.c
+++ b/philo/src/ft_utils.c
@@ -6,7 +6,7 @@
/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/17 11:57:11 by dkaiser #+# #+# */
-/* Updated: 2025/01/17 11:59:28 by dkaiser ### ########.fr */
+/* Updated: 2025/01/18 12:47:38 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
@@ -18,6 +18,16 @@ int ft_err(const char *str)
return (EXIT_FAILURE);
}
+void ft_log(int id, const char *str)
+{
+ int timestamp_in_ms;
+ struct timeval t;
+
+ gettimeofday(&t, NULL);
+ timestamp_in_ms = (t.tv_sec * 1000) + (t.tv_usec / 1000);
+ printf("%d %d %s\n", timestamp_in_ms, id, str);
+}
+
int ft_atoi(const char *str)
{
int result;
diff --git a/philo/src/simulation.c b/philo/src/simulation.c
index 54ac8e5..3035385 100644
--- a/philo/src/simulation.c
+++ b/philo/src/simulation.c
@@ -6,7 +6,7 @@
/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/17 14:38:04 by dkaiser #+# #+# */
-/* Updated: 2025/01/18 12:41:31 by dkaiser ### ########.fr */
+/* Updated: 2025/01/18 12:49:42 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
@@ -26,8 +26,10 @@ void philo_eat(t_philo *philo)
pthread_mutex_lock(&left_fork->mutex);
pthread_mutex_lock(&right_fork->mutex);
left_fork->available = 0;
+ ft_log(philo->id, "has taken a fork");
right_fork->available = 0;
- printf("Philo %d is eating\n", philo->id);
+ ft_log(philo->id, "has taken a fork");
+ ft_log(philo->id, "is eating");
usleep(1000000);
left_fork->available = 1;
right_fork->available = 1;
@@ -37,13 +39,13 @@ void philo_eat(t_philo *philo)
void philo_sleep(t_philo *philo)
{
- printf("Philo %d is sleeping\n", philo->id);
+ ft_log(philo->id, "is sleeping");
usleep(1000000);
}
void philo_think(t_philo *philo)
{
- printf("Philo %d is thinking\n", philo->id);
+ ft_log(philo->id, "is thinking");
}
int *process_philo(void *arg)