3 /* open/read/write/close */
6 /** If buffer is too small to hold entire string, it is incremented by this value */
10 * Program grabs data from process1, calculates number of characters in each line
11 * and pass the value to process3.
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 * Buffer used for storing data from input pipe.
28 * Data is stored in chunks of BUFFER_STEP size.
29 * If data during reading is bigger than this value, then number of
30 * characters is saved, and buffer is cleared for reading another chunk.
32 char buffer[BUFFER_STEP];
34 /** Index used when iterating buffer */
37 /** Stores number of bytes read from input pipe in current iteration */
40 /* Reading from process1 */
41 read_descriptor = open(read_pipe, O_RDONLY);
43 /* Writing to process2 */
44 mkfifo(write_pipe, 0666);
45 write_descriptor = open(write_pipe, O_WRONLY);
47 int number_of_characters = 0;
50 /* Read data from input pipe */
51 count = read(read_descriptor, buffer, BUFFER_STEP);
53 fprintf(stderr, "[%s] Fetched: %d bytes\n", "process2", count);
56 for (i = 0; i < count; i++, number_of_characters++) {
57 if (buffer[i] == '\n') {
58 fprintf(stderr, "[%s] Calculated: %d characters. Sending...\n", "process2", number_of_characters);
59 write(write_descriptor, &number_of_characters, sizeof(number_of_characters));
60 write(write_descriptor, '\n', 1);
61 number_of_characters = 0;
70 close(read_descriptor);
71 close(write_descriptor);
72 unlink(write_descriptor);