add pipe test
authorLaurent Mazet <mazet@softndesign.org>
Sun, 28 Sep 2025 20:33:40 +0000 (22:33 +0200)
committerLaurent Mazet <mazet@softndesign.org>
Sun, 28 Sep 2025 20:33:40 +0000 (22:33 +0200)
pipe.c [new file with mode: 0644]

diff --git a/pipe.c b/pipe.c
new file mode 100644 (file)
index 0000000..4abbf26
--- /dev/null
+++ b/pipe.c
@@ -0,0 +1,119 @@
+/* depend: */
+/* cflags: */
+/* linker: mtime.o test.o stat.o -lm */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+//#include <pthread.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 = "pipe";
+
+#define MAXBUF 1024
+
+sec_t receiver (int fd)
+{
+
+    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 };
+        if (read (fd, buffer, sizeof(buffer)) == -1) {
+            fprintf (stderr, "error: read (%d)\n", i);
+            return none_e;
+        }
+        //printf ("receive '%s'\n", buffer);
+
+        deltas[i] = ts - ts_old;
+    }
+
+    return usec_e;
+}
+
+sec_t sender (int fd)
+{
+
+    printf ("Sending data...\n");
+    for (int i = 0; i < nb_measurements; i++) {
+
+        char buffer[32] = { 0 };
+        sprintf (buffer, "hello world %d", i);
+
+        if (write (fd, buffer, strlen (buffer) + 1) == -1) {
+            fprintf (stderr, "error: write (%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);
+*/
+
+    int pipefd[2] = { 0 };
+
+    if (pipe (pipefd) == -1) {
+        fprintf (stderr, "error: pipe\n");
+        return none_e;
+    }
+
+    pid_t pid = fork ();
+    if (pid == -1) {
+        fprintf (stderr, "error: fork\n");
+        return none_e;
+    } else if (pid == 0) {
+        close (pipefd[0]);
+        sender (pipefd[1]);
+        close (pipefd[1]);
+        exit (0);
+    }
+
+    close (pipefd[1]);
+    sec_t rc = receiver (pipefd[0]);
+    close (pipefd[0]);
+
+    if (kill (pid, SIGTERM) == 0) {
+        int wstatus;
+        if (waitpid (pid, &wstatus, WUNTRACED | WCONTINUED) == -1) {
+            fprintf (stderr, "error: waitpid\n");
+        }
+    }
+
+    return rc;
+}