Added signal handlers for all processes.
[wsti_so.git] / src / process2.c
1 #include <stdio.h>
2
3 /* open/read/write/close */
4 #include <fcntl.h>
5
6 /* Signals handling.. */
7 #include <signal.h>
8
9 /** If buffer is too small to hold entire string, it is incremented by this value */
10 #define BUFFER_STEP 16
11
12 /**
13  * Handler for signals.
14  */
15 void sig_handler(int signo)
16 {
17         if (signo == SIGUSR1) {
18                 fprintf(stderr, "[%s] SIGUSR1!\n", "process1");
19         }
20         else if (signo == SIGUSR2) {
21                 fprintf(stderr, "[%s] SIGUSR2!\n", "process1");
22         }
23         else if (signo == SIGINT) {
24                 fprintf(stderr, "[%s] SIGINT!\n", "process1");
25         }
26         else if (signo == SIGCONT) {
27                 fprintf(stderr, "[%s] SIGCONT!\n", "process1");
28         }
29 }
30
31 /**
32  * Program grabs data from process1, calculates number of characters in each line
33  * and pass the value to process3.
34  */
35 int main(void) {
36         /** Named pipe used to communicate with process1 */
37         char * read_pipe = "/tmp/process1pipe";
38
39         /** Named pipe used to communicate with process3 */
40         char * write_pipe = "/tmp/process2pipe";
41
42         /** Descriptor of input pipe */
43         int read_descriptor;
44
45         /** Descriptor of output pipe */
46         int write_descriptor;
47
48         /**
49          * Buffer used for storing data from input pipe.
50          * Data is stored in chunks of BUFFER_STEP size.
51          * If data during reading is bigger than this value, then number of
52          * characters is saved, and buffer is cleared for reading another chunk.
53          */
54         char buffer[BUFFER_STEP];
55
56         /** Index used when iterating buffer */
57         int i = 0;
58
59         /** Stores number of bytes read from input pipe in current iteration */
60         ssize_t count = 0;
61
62         /* Reading from process1 */
63         read_descriptor = open(read_pipe, O_RDONLY);
64
65         /* Writing to process2 */
66         mkfifo(write_pipe, 0666);
67         write_descriptor = open(write_pipe, O_WRONLY);
68
69         int number_of_characters = 0;
70
71         fprintf(stderr, "[%s] Init!\n", "process2");
72
73         /**
74          * Register signals handled by process
75          */
76         if (signal(SIGUSR1, sig_handler) == SIG_ERR) {
77                 fprintf(stderr, "can't catch SIGUSR1\n");
78         }
79         if (signal(SIGUSR2, sig_handler) == SIG_ERR) {
80                 fprintf(stderr, "can't catch SIGUSR2\n");
81         }
82         if (signal(SIGINT, sig_handler) == SIG_ERR) {
83                 fprintf(stderr, "can't catch SIGINT\n");
84         }
85         if (signal(SIGCONT, sig_handler) == SIG_ERR) {
86                 fprintf(stderr, "can't catch SIGCONT\n");
87         }
88
89         while(1) {
90                 /* Read data from input pipe */
91                 count = read(read_descriptor, buffer, BUFFER_STEP);
92
93                 fprintf(stderr, "[%s] Fetched: %d bytes\n", "process2", count);
94
95                 if (count > 0) {
96                         for (i = 0; i < count; i++, number_of_characters++) {
97                                 if (buffer[i] == '\n') {
98                                         fprintf(stderr, "[%s] Calculated: %d characters. Sending...\n", "process2", number_of_characters);
99                                         write(write_descriptor, &number_of_characters, sizeof(number_of_characters));
100                                         write(write_descriptor, '\n', 1);
101                                         number_of_characters = 0;
102                                 }
103                         }
104                 }
105                 else {
106                         break;
107                 }
108         }
109
110         close(read_descriptor);
111         close(write_descriptor);
112         unlink(write_descriptor);
113
114         return 0;
115 }