Working communication using pipes.
authorRafał Długołęcki <rafal@dlugolecki.net.pl>
Fri, 20 Jun 2014 18:10:20 +0000 (20:10 +0200)
committerRafał Długołęcki <rafal@dlugolecki.net.pl>
Fri, 20 Jun 2014 18:10:20 +0000 (20:10 +0200)
Makefile.am
src/process1.c
src/process2.c
src/process3.c [new file with mode: 0644]

index 9ff0cfe4569b5803bdb7a73b0177ea7d72f9e5b1..a8c16f597126960b7660faa40a44b55d7125c6bf 100644 (file)
@@ -3,10 +3,14 @@ ACLOCAL_AMFLAGS = ${ACLOCAL_FLAGS}
 
 bin_PROGRAMS = \
        bin/process1 \
-       bin/process2
+       bin/process2 \
+       bin/process3
 
 bin_process1_SOURCES = \
        src/process1.c
 
 bin_process2_SOURCES = \
        src/process2.c
+
+bin_process3_SOURCES = \
+       src/process3.c
index 4933b62619c0045c9e1b6fd072cd9372b4c4f654..f4a1392d84398e72d440e0d63cfb6b64690b46a7 100644 (file)
@@ -62,16 +62,17 @@ int main(void) {
                }
 
                /*
-                * If there were no errors parse data.
+                * If there were no errors or it was not just an empty newline:
+                * parse data.
                 */
-               if (c != EOF) {
+               if (c != EOF || ((i == 0) && (c == 10))) {
                        /* If newline has been found
                         * return entire string and release the memory
                         */
-                       if (c == 10) {
+                       if (c == 10 && (i != 0)) {
                                buffer[i] = '\n';
                                write(file_descriptor, buffer, strlen(buffer));
-                               fprintf(stdout, "buffer: %s/%d\n", buffer, strlen(buffer));
+                               fprintf(stderr, "[%s] buffer: %s/%d\n", "process1", buffer, strlen(buffer));
 
                                buffer_length = 0;
                                i = 0;
index 4309e0c2a35fac44a62a4d0fd62bc0d58b6b0b14..07a24222f6fac9fe9255c5ceed9f9c218892e76a 100644 (file)
@@ -33,28 +33,29 @@ int main(void) {
        /* Writing to process2 */
        mkfifo(write_pipe, 0666);
        write_descriptor = open(write_pipe, O_WRONLY);
-       
+
        int number_of_characters = 0;
        
-       do {
+       while(1) {
                /* Read data from input pipe */
                count = read(read_descriptor, buffer, BUFFER_STEP);
 
-               printf("fetched: %d bytes\n", count);
+               fprintf(stderr, "[%s] Fetched: %d bytes\n", "process2", count);
 
                if (count > 0) {
-                       int j = 0;
-                       for (; j < count; j++, number_of_characters++) {
-                               if (buffer[j] == '\n') {
-                                       fprintf(stderr, "Writting about: %d\n", number_of_characters);
-                                       write(write_descriptor, number_of_characters, sizeof(number_of_characters));
+                       for (i = 0; i < count; i++, number_of_characters++) {
+                               if (buffer[i] == '\n') {
+                                       fprintf(stderr, "[%s] Calculated: %d characters. Sending...\n", "process2", number_of_characters);
+                                       write(write_descriptor, &number_of_characters, sizeof(number_of_characters));
                                        write(write_descriptor, '\n', 1);
                                        number_of_characters = 0;
                                }
                        }
-
                }
-       } while(count > 0);
+               else {
+                       break;
+               }
+       }
 
        close(read_descriptor);
        close(write_descriptor);
diff --git a/src/process3.c b/src/process3.c
new file mode 100644 (file)
index 0000000..d5be26a
--- /dev/null
@@ -0,0 +1,48 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+/* open/read/write/close */
+#include <fcntl.h>
+
+/** If buffer is too small to hold entire string, it is incremented by this value */
+#define BUFFER_STEP 16
+
+/**
+ * Program grabs data from process1, calculates number of characters in each line
+ * and pass the value to process3.
+ */
+int main(void) {
+       /** Named pipe used to communicate with process1 */
+       char * read_pipe = "/tmp/process2pipe";
+
+       int read_descriptor;
+       
+       int buffer = 0;
+       
+       int i = 0;
+       ssize_t count = 0;
+
+       /* Reading from process2 */
+       read_descriptor = open(read_pipe, O_RDONLY);
+
+       int number_of_characters = 0;
+       
+       while(1) {
+               /* Read data from input pipe */
+               count = read(read_descriptor, &buffer, sizeof(int));
+
+               fprintf(stderr, "[%s] Fetched: %d bytes\n", "process3", count);
+
+               if (count > 0) {
+                       fprintf(stderr, "[%s] Process2 send: %d\n", "process3", buffer);
+               }
+               else {
+                       break;
+               }
+       }
+
+       close(read_descriptor);
+
+       return 0;
+}
\ No newline at end of file