/* depend: */
/* cflags: */
-/* linker: mtime.o test.o stat.o -lm -lrt */
+/* linker: mtime.o test.o stat.o -lm -lpthread -lrt */
#include <stdio.h>
#include <stdlib.h>
int (*parse_arg_ext) (char *) = NULL;
#define MAXBUF 1024
-
-ts_t ts1;
-ts_t ts2;
-int rc = 0;
-int pipefd[2] = { 0 };
+#define TIMER 100
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
+ts_t ts1, ts2;
+int pipefd[2] = { 0 };
+int rc = 0;
void *ping (__attribute__((unused)) void *arg)
{
int fdin = dup (pipefd[0]);
int fdout = dup (pipefd[1]);
- pthread_mutex_lock (&mutex);
- pthread_mutex_unlock (&mutex);
+ /* main loop */
printf ("Sending ping...\n");
+
+ pthread_mutex_unlock (&mutex);
+ usleep (TIMER);
+
for (int i = 0; i < nb_measurements; i++) {
char buffer[MAXBUF] = { 0 };
sprintf (buffer, "ping %d", i);
- sys_timestamp (&ts1);
+ pthread_mutex_lock (&mutex);
+ pthread_mutex_unlock (&mutex);
+ sys_timestamp (&ts1);
if (write (fdout, buffer, strlen (buffer) + 1) == -1) {
fprintf (stderr, "ping error: write (%d)\n", i);
rc = 1;
- return NULL;
- }
- //printf ("write '%s'\n", buffer);
-
- if (read (fdin, buffer, sizeof(buffer)) == -1) {
- fprintf (stderr, "ping error: read (%d)\n", i);
- rc = 1;
- return NULL;
+ pthread_exit (NULL);
}
- //printf ("read '%s'\n", buffer);
-
- sys_timestamp (&ts2);
- deltas[i] = diff_timestamp (&ts2, &ts1);
+ usleep (TIMER);
}
close (fdin);
close (fdout);
- return NULL;
+ pthread_exit (NULL);
}
void *pong (__attribute__((unused)) void *arg)
int fdin = dup (pipefd[0]);
int fdout = dup (pipefd[1]);
- pthread_mutex_unlock (&mutex);
-
printf ("Responding pong...\n");
+
for (int i = 0; i < nb_measurements; i++) {
- char buffer[MAXBUF] = { 0 };
+ pthread_mutex_unlock (&mutex);
+ char buffer[MAXBUF] = { 0 };
if (read (fdin, buffer, sizeof(buffer)) == -1) {
fprintf (stderr, "ping error: read (%d)\n", i);
rc = 1;
- return NULL;
+ pthread_exit (NULL);
}
- //printf ("read '%s'\n", buffer);
sys_timestamp (&ts2);
deltas[i] = diff_timestamp (&ts2, &ts1);
- sys_timestamp (&ts1);
-
- if (write (fdout, buffer, strlen (buffer) + 1) == -1) {
- fprintf (stderr, "ping error: write (%d)\n", i);
- rc = 1;
- return NULL;
- }
- //printf ("write '%s'\n", buffer);
+ pthread_mutex_lock (&mutex);
+ usleep (TIMER);
}
close (fdin);
close (fdout);
- return NULL;
+ pthread_exit (NULL);
}
int test (dts_t *buffer, int nb)
return 1;
}
+ pthread_mutex_lock (&mutex);
+
pthread_t tid2;
if (pthread_create (&tid2, NULL, pong, NULL) != 0) {
fprintf (stderr, "error on pthread_create\n");