From 4584560f1dbf924d2a5b6df61574790b5987d888 Mon Sep 17 00:00:00 2001 From: Rui Ribeiro <42305006+ruiribeiro04@users.noreply.github.com> Date: Thu, 9 Oct 2025 20:50:32 +0100 Subject: [PATCH] Added boilerplate for `death_monitor.c`, need a refactor. - ATM the code have a struct that contains all workers threads, mutexes and last_updated values. I need to try malloc a `WorkerState` struct and pass it to each worker thread. - Need to create a monitor thread that will iterate all workers values and watch if the last_activity_time of a worker is less than 3 seconds. If not, the monitor thread will declare it as dead. --- rendu/death_monitor.c | 75 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 rendu/death_monitor.c diff --git a/rendu/death_monitor.c b/rendu/death_monitor.c new file mode 100644 index 0000000..3d84b80 --- /dev/null +++ b/rendu/death_monitor.c @@ -0,0 +1,75 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* death_monitor.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ruiferna +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/10/09 20:16:37 by ruiferna #+# #+# */ +/* Updated: 2025/10/09 20:45:22 by ruiferna ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "simple_philosophers.h" + +#define WORKERS 4 + +typedef struct s_shared +{ + int id; + pthread_t workers[WORKERS]; + long long last_updated[WORKERS]; + pthread_mutex_t workers_mutexes[WORKERS]; + pthread_mutex_t mutex_id; +} t_shared; + + +void *worker_routine(void *arg) +{ + t_shared *shared; + int worker_id; + + shared = (t_shared *) arg; + pthread_mutex_lock(&shared->mutex_id); + shared->id += 1; + worker_id = shared->id; + pthread_mutex_unlock(&shared->mutex_id); + pthread_mutex_lock(&shared->workers_mutexes[worker_id]); + shared->last_updated[worker_id] = get_current_time(); + pthread_mutex_lock(&shared->workers_mutexes[worker_id]); + + +} +int main() +{ + t_shared shared; + int i; + + i = 0; + while (i < WORKERS) + { + pthread_mutex_init(&shared.workers_mutexes[i], NULL); + i++; + } + pthread_mutex_init(&shared.mutex_id, NULL); + + i = 0; + while (i < WORKERS) + { + if (pthread_create(&shared.workers[i], NULL, worker_routine, &shared) != 0) + { + perror("pthread_create"); + exit(EXIT_FAILURE); + } + i++; + } + + i = 0; + while (i < WORKERS) + { + pthread_join(shared.workers[i], NULL); + i++; + } + + return (0); +} \ No newline at end of file