Make opening/closing/releasing resources available from signal handler.
[wsti_so.git] / src / process3.c
1 #include <stdio.h>
2
3 /* exit.. */
4 #include <stdlib.h>
5
6 /* open/read/write/close */
7 #include <fcntl.h>
8
9 /* Signals handling.. */
10 #include <signal.h>
11
12 /** Named pipe used to communicate with process2 */
13 char * read_pipe = "/tmp/process2pipe";
14
15 /** Descriptor of input pipe */
16 int read_descriptor;
17
18 /**
19  * Handler for signals.
20  */
21 void sig_handler(int signo)
22 {
23         if (signo == SIGUSR1) {
24                 fprintf(stderr, "[%s] Received SIGUSR1!\n", "process3");
25         }
26         else if (signo == SIGQUIT) {
27                 fprintf(stderr, "[%s] Received SIGQUIT!\n", "process3");
28                 fprintf(stderr, "[%s] > Releasing resources\n", "process3");
29                 close(read_descriptor);
30                 exit(0);
31         }
32         else if (signo == SIGINT) {
33                 fprintf(stderr, "[%s] Received SIGINT!\n", "process3");
34                 fprintf(stderr, "[%s] > Closing pipe\n", "process2");
35                 close(read_descriptor);
36         }
37         else if (signo == SIGCONT) {
38                 fprintf(stderr, "[%s] Received SIGCONT!\n", "process3");
39                 fprintf(stderr, "[%s] > Opening pipe\n", "process2");
40                 read_descriptor = open(read_pipe, O_RDONLY);
41         }
42 }
43
44 /**
45  * Program grabs data (calculated number of characters) from process2 and prints
46  * grabbed data to the standard output.
47  */
48 int main(void) {
49         /** Buffer used for storing data from input pipe */
50         int buffer = 0;
51         
52         /** Stores number of bytes read from input pipe in current iteration */
53         ssize_t count = 0;
54
55         fprintf(stderr, "[%s] Init!\n", "process3");
56
57         /**
58          * Register signals handled by process
59          */
60         if (signal(SIGUSR1, sig_handler) == SIG_ERR) {
61                 fprintf(stderr, "can't catch SIGUSR1\n");
62         }
63         if (signal(SIGQUIT, sig_handler) == SIG_ERR) {
64                 fprintf(stderr, "can't catch SIGQUIT\n");
65         }
66         if (signal(SIGINT, sig_handler) == SIG_ERR) {
67                 fprintf(stderr, "can't catch SIGINT\n");
68         }
69         if (signal(SIGCONT, sig_handler) == SIG_ERR) {
70                 fprintf(stderr, "can't catch SIGCONT\n");
71         }
72
73         /* Reading from process2 */
74         read_descriptor = open(read_pipe, O_RDONLY);
75
76         while(1) {
77                 /* Read data from input pipe */
78                 count = read(read_descriptor, &buffer, sizeof(int));
79
80                 fprintf(stderr, "[%s] Fetched: %d bytes\n", "process3", count);
81
82                 if (count > 0) {
83                         fprintf(stderr, "[%s] Process2 send: %d\n", "process3", buffer);
84                 }
85                 else {
86                         break;
87                 }
88         }
89
90         /* Release resources in normal program flow exit. */
91         close(read_descriptor);
92
93         return 0;
94 }