add mq test
authorLaurent Mazet <mazet@softndesign.org>
Sun, 28 Sep 2025 20:04:48 +0000 (22:04 +0200)
committerLaurent Mazet <mazet@softndesign.org>
Sun, 28 Sep 2025 20:04:48 +0000 (22:04 +0200)
mq.c [new file with mode: 0644]
todo/mq_receiver.c [deleted file]
todo/mq_sender.c [deleted file]

diff --git a/mq.c b/mq.c
new file mode 100644 (file)
index 0000000..7e07f89
--- /dev/null
+++ b/mq.c
@@ -0,0 +1,130 @@
+/* depend: */
+/* cflags: */
+/* linker: mtime.o test.o stat.o -lm -lrt */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+//#include <pthread.h>
+
+#include <fcntl.h>           /* For O_* constants */
+#include <sys/stat.h>        /* For mode constants */
+#include <mqueue.h>
+
+#include <signal.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+#include "mtime.h"
+#include "test.h"
+
+/* global variables */
+
+nstime_t *deltas = NULL;
+int nb_measurements = 0;
+
+char *message = "message queue";
+
+mqd_t mq = 0;
+
+#define MAXBUF 1024
+
+sec_t receiver (void)
+{
+
+    printf ("Waiting for data...\n");
+    nstime_t ts = 0, ts_old;
+    for (int i = 0; i < nb_measurements; i++) {
+        ts_old = ts;
+        ts = sys_timestamp_ns();
+    
+        char buffer[MAXBUF] = { 0 };
+        int m = mq_receive (mq, buffer, sizeof(buffer), NULL);
+        if (m == -1) {
+            fprintf (stderr, "error: mq_receive (%d)\n", i);
+            return none_e;
+        }
+        //printf ("receive '%s'\n", buffer);
+
+        deltas[i] = ts - ts_old;
+    }
+
+    return usec_e;
+}
+
+sec_t sender (void)
+{
+
+    printf ("Sending data...\n");
+    for (int i = 0; i < nb_measurements; i++) {
+
+        char buffer[32] = { 0 };
+        sprintf (buffer, "hello world %d", i);
+
+        int m = mq_send (mq, buffer, strlen (buffer) + 1, 0);
+        if (m == -1) {
+            fprintf (stderr, "error: mq_send (%d)\n", i);
+            return none_e;
+        }
+        //printf ("send '%s'\n", buffer);
+    }
+
+    return usec_e;
+}
+
+sec_t test (int64_t *buffer, int nb)
+{
+
+    /* set global variables */
+
+    deltas = buffer;
+    nb_measurements = nb;
+
+    /* mutex test */
+
+/*
+    pthread_attr_t th_attr;
+    struct sched_param param;
+
+    pthread_attr_init (&th_attr);
+    pthread_attr_setschedpolicy (&th_attr, SCHED_FIFO);
+    param.sched_priority = sched_get_priority_max (SCHED_FIFO);
+    pthread_attr_setschedparam (&th_attr, &param);
+    pthread_attr_setinheritsched (&th_attr, PTHREAD_EXPLICIT_SCHED);
+*/
+
+    struct mq_attr mq_attr;
+    mq_attr.mq_flags = 0;
+    mq_attr.mq_maxmsg = 5;
+    mq_attr.mq_msgsize = MAXBUF;
+    mq_attr.mq_curmsgs = 0;
+
+    mq = mq_open ("/test_queue", O_CREAT | O_RDWR, 0644, &mq_attr);
+    if (mq == -1) {
+        fprintf (stderr, "error: mq_open\n");
+        return none_e;
+    }
+
+    pid_t pid = fork ();
+    if (pid == -1) {
+        fprintf (stderr, "error: fork\n");
+        return none_e;
+    } else if (pid == 0) {
+        sender ();
+        mq_close (mq);
+        exit (0);
+    }
+
+    sec_t rc = receiver ();
+    mq_close (mq);
+
+    if (kill (pid, SIGTERM) == 0) {
+        int wstatus;
+        if (waitpid (pid, &wstatus, WUNTRACED | WCONTINUED) == -1) {
+            fprintf (stderr, "error: waitpid\n");
+        }
+    }
+
+    return rc;
+}
diff --git a/todo/mq_receiver.c b/todo/mq_receiver.c
deleted file mode 100644 (file)
index 686ca1b..0000000
+++ /dev/null
@@ -1,84 +0,0 @@
-//
-// Receive a string from POSIX mq
-//
-
-#include <stdio.h>
-#include <fcntl.h>           /* For O_* constants */
-#include <sys/stat.h>        /* For mode constants */
-#include <mqueue.h>
-#include <string.h>
-#include <signal.h>
-#include <stdlib.h>
-#include <time.h>
-
-mqd_t mq;
-long ts1, ts2;
-
-// SysTimestamp() emulation
-int64_t timespec_as_microseconds(struct timespec ts)
-{
-    long rv = ts.tv_sec * 1000000 + ts.tv_nsec / 1000;
-    return rv;
-}
-
-long timespec_as_nanoseconds(struct timespec ts)
-{
-    long rv = ts.tv_sec * 1000000000 + ts.tv_nsec ;
-    return rv;
-}
-
-int64_t sysTimestamp()
-{
-  struct timespec ts;
-
-  clock_gettime (CLOCK_REALTIME, &ts);
-
-  return timespec_as_microseconds(ts);
-}
-
-void got_sigint (int sig)
-{
-  mq_close(mq);
-  printf ("Got SIGINT, exiting!\n");
-  exit (0);
-}
-
-int main()
-{
-  int m;
-  char buff[8096];
-  struct mq_attr attr;
-  long ts = 0, ts_old = 0;
-  
-  signal (SIGINT, got_sigint);
-  
-  attr.mq_flags = 0;
-  attr.mq_maxmsg = 10;
-  attr.mq_msgsize = 8096;
-  attr.mq_curmsgs = 0;
-
-  mq = mq_open("/test_queue", O_CREAT | O_RDWR, 0644, &attr);
-  if (mq == -1) {
-    perror("mq_open");
-    return -1;
-  }
-
-  printf ("Waiting for data...\n");
-
-  while (1) {
-    ts_old = ts;
-    ts = sysTimestamp();
-    
-    m = mq_receive(mq, buff, sizeof(buff), NULL);
-    if (m == -1) {
-      perror("mq_receive\n");
-      return -1;
-    }
-
-    printf("%s (%d bytes) %ld\n", buff, m, ts-ts_old);
-  }
-
-  //  mq_close(mq);
-
-  return 0;
-}
diff --git a/todo/mq_sender.c b/todo/mq_sender.c
deleted file mode 100644 (file)
index 7699c51..0000000
+++ /dev/null
@@ -1,49 +0,0 @@
-//
-// Send strings with POSIX mq
-//
-
-#include <stdio.h>
-#include <fcntl.h>           /* For O_* constants */
-#include <sys/stat.h>        /* For mode constants */
-#include <mqueue.h>
-#include <string.h>
-#include <signal.h>
-#include <stdlib.h>
-#include <unistd.h>
-
-mqd_t mq;
-
-void got_sigint (int sig)
-{
-  mq_close(mq);
-  printf ("Got SIGINT, exiting!\n");
-  exit (0);
-}
-
-int main()
-{
-  int m;
-  long i = 0;
-  char buf[30];
-
-  signal(SIGINT, got_sigint);
-  
-  mq = mq_open("/test_queue", O_WRONLY|O_CREAT,0,0);
-  if (mq == -1) {
-    perror("mq_open");
-    return -1;
-  }
-
-  while (1) {
-    sprintf(buf, "hello world %ld", i++);
-    m = mq_send(mq, buf, strlen(buf), 0);
-    if (m == -1) {
-      perror("mq_send");
-      return -1;
-    }
-
-    //    usleep (100000);
-  }
-
-  return 0;
-}