summaryrefslogtreecommitdiff
path: root/src/print.c
diff options
context:
space:
mode:
authoryctct <yctct@yctct.com>2026-09-13 18:58:14 +0200
committeryctct <yctct@yctct.com>2026-09-13 18:58:14 +0200
commit47beba686ff9d2aab63738fba7176a5235703d05 (patch)
treef839ed36a9e9d4476fcdc59379b17903abfa35c8 /src/print.c
Add all files, first commit
Diffstat (limited to 'src/print.c')
-rw-r--r--src/print.c45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/print.c b/src/print.c
new file mode 100644
index 0000000..db8cf69
--- /dev/null
+++ b/src/print.c
@@ -0,0 +1,45 @@
+#include "codexion.h"
+# include <stdio.h> // for printf()
+
+/*
+This functions deal with all log printing activities. Logs are serialized by
+usage of mutex_print_log. There are 2 scenarios for printing. The first one
+scenarios applies if no philo has starved, the second one only prints if a
+philo has starved; that is when flag = 1.
+*/
+void print_log(t_philo *philo, char *msg, int flag)
+{
+ int id;
+ long start;
+ t_sim *sim;
+
+ (void)pthread_mutex_lock(&philo->mutex_sim);
+ sim = philo->sim;
+ (void)pthread_mutex_unlock(&philo->mutex_sim);
+ (void)pthread_mutex_lock(&philo->mutex_id);
+ id = philo->id;
+ (void)pthread_mutex_unlock(&philo->mutex_id);
+ (void)pthread_mutex_lock(&philo->sim->mutex_start_time);
+ start = philo->sim->start_time;
+ (void)pthread_mutex_unlock(&philo->sim->mutex_start_time);
+ (void)pthread_mutex_lock(&philo->sim->mutex_print_log);
+ if (!check_if_philo_starved(sim) && flag == 0)
+ printf("%d %i %s\n", get_rel_time(sim), id, msg);
+ else if (flag == 1)
+ printf("%lu %d %d %s\n", time_ms() - start, get_rel_time(sim), id, msg);
+ (void)pthread_mutex_unlock(&philo->sim->mutex_print_log);
+}
+
+/*
+This function prints usage when the program is ran without parameters
+*/
+void print_usage(void)
+{
+ printf("Usage: $ ./codexion number_of_philos "
+ "time_to_starve "
+ "time_to_eat "
+ "time_to_reflect "
+ "time_to_think "
+ "number_of_eats_required "
+ "fork_cooldown scheduler");
+}