Summary Files Commits Branches @commit @diff @stats
diff options
context:
space:
mode:
diff --git a/Year 3/System programming/Practicum6/ex1.c b/Year 3/System programming/Practicum6/ex1.c
new file mode 100644
index 0000000..d1950ef
--- /dev/null
+++ b/Year 3/System programming/Practicum6/ex1.c
@@ -0,0 +1,18 @@
+#include <unistd.h>
+#include <stdio.h>
+
+int main() {
+ int fileDes[2];
+ if (pipe(fileDes) < 0) {
+ perror("pipe");
+ return 1;
+ }
+ if (close(fileDes[1]) < 0) {
+ perror("close");
+ return 2;
+ }
+ char buf;
+ read(fileDes[0], &buf, 1);
+ printf("%c", buf);
+ return 0;
+}
diff --git a/Year 3/System programming/Practicum6/ex2.c b/Year 3/System programming/Practicum6/ex2.c
new file mode 100644
index 0000000..c64ac9e
--- /dev/null
+++ b/Year 3/System programming/Practicum6/ex2.c
@@ -0,0 +1,17 @@
+#include <unistd.h>
+#include <stdio.h>
+
+int main() {
+ int fileDes[2];
+ if (pipe(fileDes) < 0) {
+ perror("pipe");
+ return 1;
+ }
+ if (close(fileDes[0]) < 0) {
+ perror("close");
+ return 2;
+ }
+ char buf = 'a';
+ write(fileDes[1], &buf, 1);
+ return 0;
+}
diff --git a/Year 3/System programming/Practicum6/ex3.c b/Year 3/System programming/Practicum6/ex3.c
new file mode 100644
index 0000000..a84d2cb
--- /dev/null
+++ b/Year 3/System programming/Practicum6/ex3.c
@@ -0,0 +1,16 @@
+#include <unistd.h>
+#include <stdio.h>
+
+int main() {
+ int fileDes[2];
+ if (pipe(fileDes) < 0) {
+ perror("pipe");
+ return 1;
+ }
+ char buf = 'a';
+ write(fileDes[1], &buf, 1);
+ char obuf;
+ read(fileDes[0], &obuf, 1);
+ printf("%c", obuf);
+ return 0;
+}
diff --git a/Year 3/System programming/Practicum6/ex4.c b/Year 3/System programming/Practicum6/ex4.c
new file mode 100644
index 0000000..fe0718e
--- /dev/null
+++ b/Year 3/System programming/Practicum6/ex4.c
@@ -0,0 +1,45 @@
+#include <unistd.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+
+int main(int argc, char* argv[]) {
+ int fileDes[2];
+ if (pipe(fileDes) < 0) {
+ perror("pipe");
+ return 1;
+ }
+
+ int f = fork();
+ if (f == 0) { // child
+ if (close(fileDes[1]) < 0) {
+ perror("close");
+ return 2;
+ }
+
+ char buf[1024];
+ int count = argc - 1;
+ while (count > 0) {
+ read(fileDes[0], &buf, 1024);
+ if (buf[0] != '\0') {
+ printf("%s", buf);
+ count--;
+ }
+ }
+ }
+ else {
+ if (close(fileDes[0]) < 0) {
+ perror("close");
+ return 3;
+ }
+
+ for (size_t i = 1; i < argc; i++) {
+ write(fileDes[1], argv[i], strlen(argv[i]));
+ sleep(1);
+ }
+ int status;
+ wait(&status);
+ }
+ return 0;
+}
diff --git a/Year 3/System programming/Practicum6/ex5.c b/Year 3/System programming/Practicum6/ex5.c
new file mode 100644
index 0000000..903732d
--- /dev/null
+++ b/Year 3/System programming/Practicum6/ex5.c
@@ -0,0 +1,40 @@
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+
+int main(int argc, char* argv[]) {
+ int pf[2];
+ pipe(pf);
+ int f = fork();
+ if (f == 0) { // child
+ if (close(pf[0]) < 0) {
+ perror("close");
+ return 1;
+ }
+ dup2(pf[1], 1);
+ if (close(pf[1]) < 0) {
+ perror("close");
+ return 2;
+ }
+
+ execlp(argv[1], argv[1], (char*)NULL);
+ }
+ else {
+ if (close(pf[1]) < 0) {
+ perror("close");
+ return 3;
+ }
+ dup2(pf[0], 0);
+ if (close(pf[0]) < 0) {
+ perror("close");
+ return 4;
+ }
+
+ int status;
+ wait(&status);
+
+ execlp(argv[2], argv[2], (char*)NULL);
+ }
+ return 0;
+}