Working communication using pipes.
[wsti_so.git] / src / process3.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 /* open/read/write/close */
6 #include <fcntl.h>
7
8 /** If buffer is too small to hold entire string, it is incremented by this value */
9 #define BUFFER_STEP 16
10
11 /**
12  * Program grabs data from process1, calculates number of characters in each line
13  * and pass the value to process3.
14  */
15 int main(void) {
16         /** Named pipe used to communicate with process1 */
17         char * read_pipe = "/tmp/process2pipe";
18
19         int read_descriptor;
20         
21         int buffer = 0;
22         
23         int i = 0;
24         ssize_t count = 0;
25
26         /* Reading from process2 */
27         read_descriptor = open(read_pipe, O_RDONLY);
28
29         int number_of_characters = 0;
30         
31         while(1) {
32                 /* Read data from input pipe */
33                 count = read(read_descriptor, &buffer, sizeof(int));
34
35                 fprintf(stderr, "[%s] Fetched: %d bytes\n", "process3", count);
36
37                 if (count > 0) {
38                         fprintf(stderr, "[%s] Process2 send: %d\n", "process3", buffer);
39                 }
40                 else {
41                         break;
42                 }
43         }
44
45         close(read_descriptor);
46
47         return 0;
48 }