summaryrefslogtreecommitdiff
path: root/src/starve.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/starve.c
Add all files, first commit
Diffstat (limited to 'src/starve.c')
-rw-r--r--src/starve.c84
1 files changed, 84 insertions, 0 deletions
diff --git a/src/starve.c b/src/starve.c
new file mode 100644
index 0000000..3dba831
--- /dev/null
+++ b/src/starve.c
@@ -0,0 +1,84 @@
+#include "codexion.h"
+# include <stdio.h> // for printf()
+
+static void broadcast_signals(t_sim *sim)
+{
+ t_fork **forks;
+ t_fork *fork;
+ int number_of_philos;
+
+ (void)pthread_mutex_lock(&sim->mutex_forks);
+ forks = sim->forks;
+ (void)pthread_mutex_unlock(&sim->mutex_forks);
+ (void)pthread_mutex_lock(&sim->mutex_number_of_philos);
+ number_of_philos = sim->number_of_philos;
+ (void)pthread_mutex_unlock(&sim->mutex_number_of_philos);
+ while (number_of_philos-- > 0)
+ {
+ fork = *forks++;
+ (void)pthread_mutex_lock(&fork->mutex_cond);
+ pthread_cond_broadcast(&fork->cond_signal);
+ (void)pthread_mutex_unlock(&fork->mutex_cond);
+ }
+}
+
+/* Computes a philo's time until he/she burns out */
+int time_until_starve(t_philo *philo)
+{
+ int time_until_starve;
+ int time_to_starve;
+ int beginning_last_eat_time;
+ t_sim *sim;
+
+ (void)pthread_mutex_lock(&philo->mutex_sim);
+ sim = philo->sim;
+ (void)pthread_mutex_unlock(&philo->mutex_sim);
+ (void)pthread_mutex_lock(&sim->mutex_time_to_starve);
+ time_to_starve = sim->time_to_starve;
+ (void)pthread_mutex_unlock(&sim->mutex_time_to_starve);
+ (void)pthread_mutex_lock(&philo->mutex_beginning_last_eat_time);
+ beginning_last_eat_time = philo->beginning_last_eat_time;
+ (void)pthread_mutex_unlock(&philo->mutex_beginning_last_eat_time);
+ time_until_starve = time_to_starve - (get_rel_time(sim)
+ - beginning_last_eat_time);
+ return (time_until_starve);
+}
+
+int check_if_philo_starved(t_sim *sim)
+{
+ int starved_status;
+
+ (void)pthread_mutex_lock(&sim->mutex_philo_starved);
+ starved_status = sim->philo_starved;
+ (void)pthread_mutex_unlock(&sim->mutex_philo_starved);
+ return (starved_status);
+}
+
+int check_for_starves(t_sim *sim)
+{
+ t_philo **philos;
+ t_philo *philo;
+ int number_of_philos;
+
+ (void)pthread_mutex_lock(&sim->mutex_philos);
+ philos = sim->philos;
+ (void)pthread_mutex_unlock(&sim->mutex_philos);
+ philo = *philos;
+ (void)pthread_mutex_lock(&sim->mutex_number_of_philos);
+ number_of_philos = sim->number_of_philos;
+ (void)pthread_mutex_unlock(&sim->mutex_number_of_philos);
+ while (number_of_philos-- > 0)
+ {
+ philo = *philos++;
+ if (time_until_starve(philo) == 0)
+ {
+ (void)pthread_mutex_lock(&sim->mutex_philo_starved);
+ sim->philo_starved = 1;
+ (void)pthread_mutex_unlock(&sim->mutex_philo_starved);
+ print_log(philo, "starved", 1);
+ broadcast_signals(sim);
+ return (1);
+ }
+ }
+ return (0);
+}