Materiale rimasto indietro e segnali real time
[gapil.git] / sources / rtsigvalsend.c
1 /* rtsigvalsend.c
2  * 
3  * Copyright (C) 2021 Simone Piccardi
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or (at
8  * your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19 /****************************************************************
20  *
21  * Program rtsigvalsend.c: 
22  * Program to test sigqueue and sigval send
23  *
24  * Author: Simone Piccardi
25  * Aug 2021
26  *
27  * Usage: ./rtsigvalsend
28  **********************************************************/
29 /* 
30  * Include needed headers
31  */
32 #include <unistd.h>      /* unix standard library */
33 #include <stdio.h>       /* standard I/O library */
34 #include <stdlib.h>      /* C standard library */
35 #include <string.h>      /* C strings library */
36 #include <signal.h>      /* signal constants, types and functions */
37 #include <errno.h>       /* error definitions and routines */
38 #include <time.h>
39
40 #define MAXLINE 256
41
42 void sig_handler(int signum, siginfo_t *info, void *ucontext) {
43     printf("Signal %d\n", signum);
44     printf("From pid %d\n", info->si_pid);
45     printf("From user %d\n", info->si_uid);
46     printf("Value %d\n", info->si_value.sival_int);
47 }
48
49 int main(int argc, char *argv[], char *envp[])
50 {
51     char buffer[MAXLINE+1];
52     int nread;
53     sigval_t value;
54     int signo = SIGRTMIN+1;
55
56     struct sigaction new_handl, old_handl;
57     sigemptyset(&new_handl.sa_mask);
58     new_handl.sa_sigaction = sig_handler;
59     new_handl.sa_flags=SA_SIGINFO;
60     /* change action for signo signal */
61     sigaction(signo, &new_handl, &old_handl);
62     while (1) {
63         nread = read(STDIN_FILENO, buffer, MAXLINE);
64         if (nread < 0) {
65             printf("Errore in lettura: %s\n", strerror(errno));
66             return 0;
67         }
68         buffer[nread]=0;
69         value.sival_int = strtol(buffer, NULL, 10);
70         if (value.sival_int > 0) {
71             sigqueue(getpid(), signo, value);
72         } else {
73             printf("Ignoring invalid input\n");
74         }
75     }
76 }