Aggiunto programmino di prova per i segnali real time.
[gapil.git] / sources / rtsigvalsend.c
diff --git a/sources/rtsigvalsend.c b/sources/rtsigvalsend.c
new file mode 100644 (file)
index 0000000..a9c156d
--- /dev/null
@@ -0,0 +1,76 @@
+/* test_fopen.c
+ * 
+ * Copyright (C) 2021 Simone Piccardi
+ * 
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+/****************************************************************
+ *
+ * Program rtsigvalsend.c: 
+ * Program to test sigqueue and sigval send
+ *
+ * Author: Simone Piccardi
+ * Aug 2021
+ *
+ * Usage: ./rtsigvalsend
+ **********************************************************/
+/* 
+ * Include needed headers
+ */
+#include <unistd.h>      /* unix standard library */
+#include <stdio.h>       /* standard I/O library */
+#include <stdlib.h>      /* C standard library */
+#include <string.h>      /* C strings library */
+#include <signal.h>      /* signal constants, types and functions */
+#include <errno.h>       /* error definitions and routines */
+#include <time.h>
+
+#define MAXLINE 256
+
+void SigHand(int signum, siginfo_t *info, void *ucontext) {
+    printf("Signal %d\n", signum);
+    printf("From pid %d\n", info->si_pid);
+    printf("From user %d\n", info->si_uid);
+    printf("Value %d\n", info->si_value.sival_int);
+}
+
+int main(int argc, char *argv[], char *envp[])
+{
+    char buffer[MAXLINE+1];
+    int nread;
+    sigval_t value;
+    int signo = SIGRTMIN+1;
+
+    struct sigaction new_handl, old_handl;
+    new_handl.sa_sigaction = SigHand;
+    sigemptyset(&new_handl.sa_mask);
+    new_handl.sa_flags=SA_SIGINFO;
+    /* change action for signo signal */
+    sigaction(signo, &new_handl, &old_handl);
+    while (1) {
+       nread = read(STDIN_FILENO, buffer, MAXLINE);
+       if (nread < 0) {
+           printf("Errore in lettura: %s\n", strerror(errno));
+            return 0;
+       }
+       buffer[nread]=0;
+       value.sival_int = strtol(buffer, NULL, 10);
+       if (value.sival_int > 0) {
+           sigqueue(getpid(), signo, value);
+       } else {
+           printf("Ignoring invalid input\n");
+       }
+    }
+}