updated death monitor
This commit is contained in:
parent
9349239c84
commit
69d8209b12
@ -6,7 +6,7 @@
|
|||||||
/* By: ruiferna <ruiferna@student.42porto.com> +#+ +:+ +#+ */
|
/* By: ruiferna <ruiferna@student.42porto.com> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/10/09 20:16:37 by ruiferna #+# #+# */
|
/* 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
|
#define WORKERS 4
|
||||||
|
|
||||||
// typedef struct {
|
static pthread_mutex_t random_mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||||
// pthread_mutex_t mutex;
|
|
||||||
// struct timeval last_activity_time;
|
|
||||||
// int alive; // 1 if alive, 0 if dead
|
|
||||||
// } WorkerState;
|
|
||||||
|
|
||||||
|
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
|
typedef struct s_shared
|
||||||
{
|
{
|
||||||
int id;
|
int id;
|
||||||
|
int is_dead;
|
||||||
pthread_t workers[WORKERS];
|
pthread_t workers[WORKERS];
|
||||||
long long last_updated[WORKERS];
|
long long last_updated[WORKERS];
|
||||||
pthread_mutex_t workers_mutexes[WORKERS];
|
pthread_mutex_t workers_mutexes[WORKERS];
|
||||||
@ -35,17 +57,24 @@ void *worker_routine(void *arg)
|
|||||||
{
|
{
|
||||||
t_shared *shared;
|
t_shared *shared;
|
||||||
int worker_id;
|
int worker_id;
|
||||||
|
int sleep_time;
|
||||||
|
|
||||||
shared = (t_shared *) arg;
|
shared = (t_shared *) arg;
|
||||||
pthread_mutex_lock(&shared->mutex_id);
|
pthread_mutex_lock(&shared->mutex_id);
|
||||||
shared->id += 1;
|
shared->id += 1;
|
||||||
worker_id = shared->id;
|
worker_id = shared->id;
|
||||||
pthread_mutex_unlock(&shared->mutex_id);
|
pthread_mutex_unlock(&shared->mutex_id);
|
||||||
pthread_mutex_lock(&shared->workers_mutexes[worker_id]);
|
while (1)
|
||||||
shared->last_updated[worker_id] = get_current_time();
|
{
|
||||||
pthread_mutex_lock(&shared->workers_mutexes[worker_id]);
|
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()
|
int main()
|
||||||
{
|
{
|
||||||
@ -53,6 +82,7 @@ int main()
|
|||||||
int i;
|
int i;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
|
shared.id = 0;
|
||||||
while (i < WORKERS)
|
while (i < WORKERS)
|
||||||
{
|
{
|
||||||
pthread_mutex_init(&shared.workers_mutexes[i], NULL);
|
pthread_mutex_init(&shared.workers_mutexes[i], NULL);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user