5 /* open/read/write/close */
8 /* Signals handling.. */
11 /** If buffer is too small to hold entire string, it is incremented by this value */
12 #define BUFFER_STEP 16
14 /** Named pipe used to communicate with process1 */
15 char * read_pipe = "/tmp/process1pipe";
17 /** Named pipe used to communicate with process3 */
18 char * write_pipe = "/tmp/process2pipe";
20 /** Descriptor of input pipe */
23 /** Descriptor of output pipe */
27 * Handler for signals.
29 void sig_handler(int signo)
31 if (signo == SIGUSR1) {
32 fprintf(stderr, "[%s] Received SIGUSR1!\n", "process2");
34 else if (signo == SIGTERM) {
35 fprintf(stderr, "[%s] Received SIGTERM!\n", "process2");
36 fprintf(stderr, "[%s] > Releasing resources\n", "process2");
37 close(read_descriptor);
38 close(write_descriptor);
39 unlink(write_descriptor);
42 else if (signo == SIGINT) {
43 fprintf(stderr, "[%s] Received SIGINT!\n", "process2");
44 fprintf(stderr, "[%s] > Closing pipes\n", "process2");
45 close(read_descriptor);
46 close(write_descriptor);
48 else if (signo == SIGCONT) {
49 fprintf(stderr, "[%s] Received SIGCONT!\n", "process2");
50 fprintf(stderr, "[%s] > Opening pipes\n", "process2");
51 read_descriptor = open(read_pipe, O_RDONLY);
52 write_descriptor = open(write_pipe, O_WRONLY);
57 * Program grabs data from process1, calculates number of characters in each line
58 * and pass the value to process3.
62 * Buffer used for storing data from input pipe.
63 * Data is stored in chunks of BUFFER_STEP size.
64 * If data during reading is bigger than this value, then number of
65 * characters is saved, and buffer is cleared for reading another chunk.
67 char buffer[BUFFER_STEP];
69 /** Index used when iterating buffer */
72 /** Stores number of bytes read from input pipe in current iteration */
75 int number_of_characters = 0;
77 fprintf(stderr, "[%s] Init!\n", "process2");
80 * Register signals handled by process
82 if (signal(SIGUSR1, sig_handler) == SIG_ERR) {
83 fprintf(stderr, "can't catch SIGUSR1\n");
85 if (signal(SIGTERM, sig_handler) == SIG_ERR) {
86 fprintf(stderr, "can't catch SIGTERM\n");
88 if (signal(SIGINT, sig_handler) == SIG_ERR) {
89 fprintf(stderr, "can't catch SIGINT\n");
91 if (signal(SIGCONT, sig_handler) == SIG_ERR) {
92 fprintf(stderr, "can't catch SIGCONT\n");
95 /* Reading from process1 */
96 read_descriptor = open(read_pipe, O_RDONLY);
98 /* Writing to process2 */
99 mkfifo(write_pipe, 0666);
100 write_descriptor = open(write_pipe, O_WRONLY);
103 /* Read data from input pipe */
104 count = read(read_descriptor, buffer, BUFFER_STEP);
106 fprintf(stderr, "[%s] Fetched: %d bytes\n", "process2", count);
109 for (i = 0; i < count; i++, number_of_characters++) {
110 if (buffer[i] == '\n') {
111 fprintf(stderr, "[%s] Calculated: %d characters. Sending...\n", "process2", number_of_characters);
112 write(write_descriptor, &number_of_characters, sizeof(number_of_characters));
113 write(write_descriptor, '\n', 1);
114 number_of_characters = 0;
123 /* Release resources in normal program flow exit. */
124 close(read_descriptor);
125 close(write_descriptor);
126 unlink(write_descriptor);