#define MSGLEN 128
#define MAXBUF 1024
-#define TIMER 1000
+#define TIMER 10000
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
ts_t ts1, ts2;
-int try = 0;
int rc = 0;
void *ping (__attribute__((unused)) void *arg)
{
/* open mq */
-
+
struct mq_attr attr;
attr.mq_flags = 0;
attr.mq_maxmsg = 5;
pthread_mutex_unlock (&mutex);
usleep (TIMER);
- while (try < nb_measurements) {
+ for (int i = 0; i < nb_measurements; i++) {
+ //char *msg = "ping";
char *msg = get_msg (MSGLEN);
pthread_mutex_lock (&mutex);
pthread_mutex_unlock (&mutex);
sys_timestamp (&ts1);
- if (mq_send (mq, msg, MSGLEN, 0) == -1) {
- fprintf (stderr, "ping error: mq_send (%d)\n", try);
+ if (mq_send (mq, msg, strlen (msg) + 1, 0) == -1) {
+ fprintf (stderr, "ping error: mq_send (%d)\n", i);
rc = 1;
pthread_exit (NULL);
}
- usleep (TIMER);
+ usleep (TIMER);
}
mq_close (mq);
void *pong (__attribute__((unused)) void *arg)
{
- printf ("Responding pong...\n");
-
/* open mq */
-
+
struct mq_attr attr;
attr.mq_flags = 0;
attr.mq_maxmsg = 5;
mqd_t mq = mq_open ("/test_queue", O_CREAT | O_RDWR, 0644, &attr);
if (mq == -1) {
- fprintf (stderr, "pong: mq_open\n");
+ fprintf (stderr, "ping: mq_open\n");
rc = 1;
pthread_exit (NULL);
}
/* main loop */
- while (try < nb_measurements) {
+ printf ("Responding pong...\n");
+
+ for (int i = 0; i < nb_measurements; i++) {
pthread_mutex_unlock (&mutex);
char buffer[MAXBUF] = { 0 };
- if (mq_receive (mq, buffer, sizeof(buffer) - 1, NULL) == -1) {
- fprintf (stderr, "pong error: mq_receive (%d)\n", try);
+ if (mq_receive (mq, buffer, sizeof (buffer) - 1, NULL) == -1) {
+ fprintf (stderr, "pong error: mq_receive (%d)\n", i);
rc = 1;
pthread_exit (NULL);
}
sys_timestamp (&ts2);
- deltas[try++] = diff_timestamp (&ts2, &ts1);
-
+ deltas[i] = diff_timestamp (&ts2, &ts1);
+
pthread_mutex_lock (&mutex);
- usleep (TIMER);
+ usleep (TIMER);
}
mq_close (mq);
pthread_join (tid1, NULL);
pthread_join (tid2, NULL);
+ mq_unlink ("/test_queue");
+
pthread_mutex_destroy (&mutex);
return rc;