Working communication between process1&process2.
authorRafał Długołęcki <rafal@dlugolecki.net.pl>
Tue, 17 Jun 2014 00:17:17 +0000 (02:17 +0200)
committerRafał Długołęcki <rafal@dlugolecki.net.pl>
Tue, 17 Jun 2014 00:17:17 +0000 (02:17 +0200)
src/process1.c
src/process2.c [new file with mode: 0644]

index 9374646fb5ae6e4baf745486e96ac0fab2695425..4933b62619c0045c9e1b6fd072cd9372b4c4f654 100644 (file)
@@ -32,14 +32,14 @@ int main(void) {
        char * tmp = NULL;
 
        /** Named pipe used to communnicate with process2 */
-       char * fifo = "/tmp/process1fifo";
+       char * write_pipe = "/tmp/process1pipe";
 
        /** File descriptor of pipe */
        int file_descriptor;
 
-       mkfifo(fifo, 0666);
+       mkfifo(write_pipe, 0666);
 
-       file_descriptor = open(fifo, O_WRONLY);
+       file_descriptor = open(write_pipe, O_WRONLY);
        
        do {
                c = fgetc(stdin);
@@ -70,8 +70,8 @@ int main(void) {
                         */
                        if (c == 10) {
                                buffer[i] = '\n';
-                               write(file_descriptor, buffer, ++buffer_length);
-                               /*fprintf(stdout, "%s\n", buffer);*/
+                               write(file_descriptor, buffer, strlen(buffer));
+                               fprintf(stdout, "buffer: %s/%d\n", buffer, strlen(buffer));
 
                                buffer_length = 0;
                                i = 0;
@@ -96,8 +96,8 @@ int main(void) {
                }
        } while(c != EOF);
 
-       close(fifo);
-       unlink(fifo);
+       close(write_pipe);
+       unlink(write_pipe);
 
        if (buffer) {
                free(buffer);
diff --git a/src/process2.c b/src/process2.c
new file mode 100644 (file)
index 0000000..4309e0c
--- /dev/null
@@ -0,0 +1,64 @@
+#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/process1pipe";
+
+       /** Named pipe used to communicate with process3 */
+       char * write_pipe = "/tmp/process2pipe";
+
+       int read_descriptor;
+       int write_descriptor;
+       
+       char buffer[BUFFER_STEP];
+       
+       int i = 0;
+       ssize_t count = 0;
+
+       /* Reading from process1 */
+       read_descriptor = open(read_pipe, O_RDONLY);
+
+       /* Writing to process2 */
+       mkfifo(write_pipe, 0666);
+       write_descriptor = open(write_pipe, O_WRONLY);
+       
+       int number_of_characters = 0;
+       
+       do {
+               /* Read data from input pipe */
+               count = read(read_descriptor, buffer, BUFFER_STEP);
+
+               printf("fetched: %d bytes\n", 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));
+                                       write(write_descriptor, '\n', 1);
+                                       number_of_characters = 0;
+                               }
+                       }
+
+               }
+       } while(count > 0);
+
+       close(read_descriptor);
+       close(write_descriptor);
+       unlink(write_descriptor);
+
+       return 0;
+}
\ No newline at end of file