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