From 1659e22a51d0cc58feed77c26c96f7b229398d4a Mon Sep 17 00:00:00 2001
From: TomatenMarc <marc.feger@uni-duesseldorf.de>
Date: Thu, 24 May 2018 22:41:58 +0200
Subject: [PATCH] Fork and Pipe QSum example

---
 Fork_and_Pipe/pipeline_qsum.c | 43 +++++++++++++++++++++++++++++++++++
 1 file changed, 43 insertions(+)
 create mode 100644 Fork_and_Pipe/pipeline_qsum.c

diff --git a/Fork_and_Pipe/pipeline_qsum.c b/Fork_and_Pipe/pipeline_qsum.c
new file mode 100644
index 0000000..c3bd2ca
--- /dev/null
+++ b/Fork_and_Pipe/pipeline_qsum.c
@@ -0,0 +1,43 @@
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/wait.h>
+#include <string.h>
+
+int main(int argc, char *argv[]){
+
+	pid_t pid;
+	char data[80];
+	int rb;
+	int pipe_ends[2]; // 0 read, 1 write
+	int status = 1;
+
+	pipe(pipe_ends);
+	pid = fork();
+	if(pid == (pid_t)0){
+		printf("Child: %d\n", getpid());
+
+		int qsum = 0;
+		close(pipe_ends[0]); // want to write
+		for(int i = 0; i < strlen(argv[1]); i++){
+			qsum += (int)(argv[1][i] - '0');
+		}
+		sprintf(data, "%d", qsum);
+		write(pipe_ends[1], data, strlen(data));
+		close(pipe_ends[1]);
+
+		sleep(5);		
+		exit(0);
+	}else{
+		printf("Parent: %d\n", getpid());		
+		waitpid(pid, &status, 0);
+		close(pipe_ends[1]);
+		int rd = read(pipe_ends[0], data, 79);
+		data[rd] = '\0';
+		close(pipe_ends[0]);
+		printf("Done with status: %d\nqsum: %s\n", status, data);
+
+	}
+	return 0;
+}
+
-- 
GitLab