Replace SIGINT with SIGTSTP.
[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         fprintf(stderr, "[%s] Received %s!\n", "process3", strsignal(signo));
24         if (signo == SIGUSR1) {
25         }
26         else if (signo == SIGTERM) {
27                 fprintf(stderr, "[%s] > Releasing resources\n", "process3");
28                 close(read_descriptor);
29                 exit(0);
30         }
31         else if (signo == SIGTSTP) {
32                 fprintf(stderr, "[%s] > Closing pipe\n", "process3");
33                 close(read_descriptor);
34         }
35         else if (signo == SIGCONT) {
36                 fprintf(stderr, "[%s] > Opening pipe\n", "process3");
37                 read_descriptor = open(read_pipe, O_RDONLY);
38         }
39 }
40
41 /**
42  * Program grabs data (calculated number of characters) from process2 and prints
43  * grabbed data to the standard output.
44  */
45 int main(void) {
46         /** Buffer used for storing data from input pipe */
47         int buffer = 0;
48         
49         /** Stores number of bytes read from input pipe in current iteration */
50         ssize_t count = 0;
51
52         fprintf(stderr, "[%s] Init!\n", "process3");
53
54         /**
55          * Register signals handled by process
56          */
57         if (signal(SIGUSR1, sig_handler) == SIG_ERR) {
58                 fprintf(stderr, "can't catch SIGUSR1\n");
59         }
60         if (signal(SIGTERM, sig_handler) == SIG_ERR) {
61                 fprintf(stderr, "can't catch SIGTERM\n");
62         }
63         if (signal(SIGTSTP, sig_handler) == SIG_ERR) {
64                 fprintf(stderr, "can't catch SIGTSTP\n");
65         }
66         if (signal(SIGCONT, sig_handler) == SIG_ERR) {
67                 fprintf(stderr, "can't catch SIGCONT\n");
68         }
69
70         /* Reading from process2 */
71         read_descriptor = open(read_pipe, O_RDONLY);
72
73         while(1) {
74                 /* Read data from input pipe */
75                 count = read(read_descriptor, &buffer, sizeof(int));
76
77                 fprintf(stderr, "[%s] Fetched: %d bytes\n", "process3", count);
78
79                 if (count > 0) {
80                         fprintf(stderr, "[%s] Process2 send: %d\n", "process3", buffer);
81                 }
82                 else {
83                         break;
84                 }
85         }
86
87         /* Release resources in normal program flow exit. */
88         close(read_descriptor);
89
90         return 0;
91 }