Summary Files Commits Branches @commit @diff @stats
path: root / Year 3 / System programming / Practicum6 / ex4.c
diff options
context:
space:
mode:
Commit369a723ba50fc9520b30f4d18cdb347f8b9496ee (patch)
Author
 Syndamia <kamen@syndamia.com>
2023-11-15 11:58:16 +0200
Committer
 Syndamia <kamen@syndamia.com>
2023-11-15 11:58:16 +0200
Filese6a765914bc482d694f8a7c79411e7ef28eec8f2 /Year 3/System programming/Practicum6/ex4.c
uni-projects-369a723ba50fc9520b30f4d18cdb347f8b9496ee.tar
uni-projects-369a723ba50fc9520b30f4d18cdb347f8b9496ee.tar.gz
uni-projects-369a723ba50fc9520b30f4d18cdb347f8b9496ee.zip
Parentfd31939b2f94f4d64a5a6d5d7cb84b925b778166 (diff)
[SP/P6] Solved exercises
1 files changed, 45 insertions, 0 deletions
-rw-r--r--Year 3/System programming/Practicum6/ex4.c45
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;
+}