updated death monitor
This commit is contained in:
parent
9349239c84
commit
69d8209b12
@ -6,7 +6,7 @@
|
||||
/* By: ruiferna <ruiferna@student.42porto.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/10/09 20:16:37 by ruiferna #+# #+# */
|
||||
/* Updated: 2025/10/09 20:47:14 by ruiferna ### ########.fr */
|
||||
/* Updated: 2025/10/09 21:23:37 by ruiferna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -14,16 +14,38 @@
|
||||
|
||||
#define WORKERS 4
|
||||
|
||||
// typedef struct {
|
||||
// pthread_mutex_t mutex;
|
||||
// struct timeval last_activity_time;
|
||||
// int alive; // 1 if alive, 0 if dead
|
||||
// } WorkerState;
|
||||
static pthread_mutex_t random_mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
|
||||
int get_random_sleep_duration() {
|
||||
// Seed variable to ensure unique seed per thread
|
||||
unsigned int seed;
|
||||
|
||||
// Acquire mutex to ensure thread-safe random number generation
|
||||
pthread_mutex_lock(&random_mutex);
|
||||
|
||||
// Use thread-safe method to generate seed
|
||||
seed = time(NULL) ^ pthread_self();
|
||||
|
||||
// Generate random number between 1000-4000 milliseconds
|
||||
int duration = 1000 + rand_r(&seed) % 3001;
|
||||
|
||||
// Release mutex
|
||||
pthread_mutex_unlock(&random_mutex);
|
||||
|
||||
return duration;
|
||||
}
|
||||
|
||||
|
||||
typedef struct {
|
||||
pthread_mutex_t mutex;
|
||||
struct timeval last_activity_time;
|
||||
int alive; // 1 if alive, 0 if dead
|
||||
} WorkerState;
|
||||
|
||||
typedef struct s_shared
|
||||
{
|
||||
int id;
|
||||
int is_dead;
|
||||
pthread_t workers[WORKERS];
|
||||
long long last_updated[WORKERS];
|
||||
pthread_mutex_t workers_mutexes[WORKERS];
|
||||
@ -35,17 +57,24 @@ void *worker_routine(void *arg)
|
||||
{
|
||||
t_shared *shared;
|
||||
int worker_id;
|
||||
int sleep_time;
|
||||
|
||||
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]);
|
||||
|
||||
while (1)
|
||||
{
|
||||
pthread_mutex_lock(&shared->workers_mutexes[worker_id]);
|
||||
shared->last_updated[worker_id] = get_current_time();
|
||||
pthread_mutex_unlock(&shared->workers_mutexes[worker_id]);
|
||||
sleep_time = get_random_sleep_duration();
|
||||
printf("Worker %i will stop for %i seconds.\n", worker_id, (int)(sleep_time / 1000));
|
||||
precise_sleep(sleep_time);
|
||||
}
|
||||
|
||||
return (NULL);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
@ -53,6 +82,7 @@ int main()
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
shared.id = 0;
|
||||
while (i < WORKERS)
|
||||
{
|
||||
pthread_mutex_init(&shared.workers_mutexes[i], NULL);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user