From ca42d8fb98e8cc34bc2707a0aa728d1c3b73bad7 Mon Sep 17 00:00:00 2001 From: Laurent MAZET Date: Wed, 1 Oct 2025 19:03:43 +0200 Subject: [PATCH] test cleaning --- mutex.c | 4 ++-- semaphore.c | 17 +++++++++++------ 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/mutex.c b/mutex.c index 7382a75..39f7861 100644 --- a/mutex.c +++ b/mutex.c @@ -18,7 +18,7 @@ void (*usage_ext) (FILE *) = NULL; int (*parse_arg_ext) (char *) = NULL; pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; -int shared_flag = 0; +volatile int shared_flag = 0; ts_t timestamp1, timestamp2; int try = 0; @@ -26,8 +26,8 @@ void *mutex_giver_task (__attribute__((unused)) void *arg) { while (try < nb_measurements) { pthread_mutex_lock (&mutex); - shared_flag = 1; sys_timestamp (×tamp1); + shared_flag = 1; pthread_mutex_unlock (&mutex); } pthread_exit (NULL); diff --git a/semaphore.c b/semaphore.c index 04eacf7..4bcd736 100644 --- a/semaphore.c +++ b/semaphore.c @@ -5,6 +5,7 @@ #include #include #include +#include #include "mtime.h" #include "test.h" @@ -22,25 +23,27 @@ int (*parse_arg_ext) (char *) = NULL; int counter = 0; int try = 0; sem_t sem; -ts_t ts1, ts2; void* trythis (__attribute__((unused)) void *arg) { sem_wait (&sem); - counter += 1; + static ts_t tsi; + ts_t ts; + sys_timestamp (&ts); /* thread 1 took the lock -> timestamp 1 */ - if (counter % 2 != 0) { - sys_timestamp (&ts1); + if (counter % 2 == 0) { + set_timestamp (&tsi, &ts); } /* thread 2 took the lock -> timestamp 2 */ else { - sys_timestamp (&ts2); - measure_tab[try++] = diff_timestamp (&ts2, &ts1); + measure_tab[try++] = diff_timestamp (&ts, &tsi); } + counter += 1; + sem_post (&sem); return NULL; @@ -59,6 +62,8 @@ int test (dts_t *buffer, int nb) for (int i = 0; i < nb; i++) { + //bzero (&tsi, sizeof (ts1)); + pthread_t tid1; if (pthread_create (&tid1, NULL, trythis, NULL) != 0) { fprintf (stderr, "error on pthread_create\n"); -- 2.30.2