From a607349b22f8a201512582e6341fe020527baf62 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sat, 2 Dec 2023 21:48:54 +0200 Subject: [SP/P06] Solved most exercises --- Year 3/System programming/Practice06/ex1.c | 14 ++++++++++++ Year 3/System programming/Practice06/ex2.c | 14 ++++++++++++ Year 3/System programming/Practice06/ex3.c | 16 +++++++++++++ Year 3/System programming/Practice06/ex4.c | 36 ++++++++++++++++++++++++++++++ 4 files changed, 80 insertions(+) create mode 100644 Year 3/System programming/Practice06/ex1.c create mode 100644 Year 3/System programming/Practice06/ex2.c create mode 100644 Year 3/System programming/Practice06/ex3.c create mode 100644 Year 3/System programming/Practice06/ex4.c (limited to 'Year 3/System programming') diff --git a/Year 3/System programming/Practice06/ex1.c b/Year 3/System programming/Practice06/ex1.c new file mode 100644 index 0000000..bcb22d5 --- /dev/null +++ b/Year 3/System programming/Practice06/ex1.c @@ -0,0 +1,14 @@ +#include // pipe +#include + +int main(int argc, char* argv[]) { + int filedes[2]; + if (pipe(filedes) < 0) { + perror("pipe"); + return 1; + } + + close(filedes[1]); + char buf; + read(filedes[1], &buf, 1); +} diff --git a/Year 3/System programming/Practice06/ex2.c b/Year 3/System programming/Practice06/ex2.c new file mode 100644 index 0000000..83749e1 --- /dev/null +++ b/Year 3/System programming/Practice06/ex2.c @@ -0,0 +1,14 @@ +#include // pipe +#include + +int main(int argc, char* argv[]) { + int filedes[2]; + if (pipe(filedes) < 0) { + perror("pipe"); + return 1; + } + + close(filedes[0]); + char buf = 'a'; + write(filedes[1], &buf, 1); +} diff --git a/Year 3/System programming/Practice06/ex3.c b/Year 3/System programming/Practice06/ex3.c new file mode 100644 index 0000000..a1ef2e2 --- /dev/null +++ b/Year 3/System programming/Practice06/ex3.c @@ -0,0 +1,16 @@ +#include // pipe +#include + +int main(int argc, char* argv[]) { + int filedes[2]; + if (pipe(filedes) < 0) { + perror("pipe"); + return 1; + } + + char buf = 'a'; + write(filedes[0], &buf, 1); + char bufin; + read(filedes[1], &bufin, 1); + printf("%c\n", bufin); +} diff --git a/Year 3/System programming/Practice06/ex4.c b/Year 3/System programming/Practice06/ex4.c new file mode 100644 index 0000000..c499cb3 --- /dev/null +++ b/Year 3/System programming/Practice06/ex4.c @@ -0,0 +1,36 @@ +#include // pipe +#include +#include +#include +#include + +int main(int argc, char* argv[]) { + int filedes[2]; + if (pipe(filedes) < 0) { + perror("pipe"); + return 1; + } + + pid_t fpid = fork(); + if (fpid == 0) { + close(filedes[1]); + char buf[1024]; + int count = 2; + while (count < argc) { + read(filedes[0], buf, 1024); + if (buf[0] != '\0') { + printf("%s\n", buf); + count++; + // TODO: изчистване на buf + } + } + } + else { + close(filedes[0]); + for (int i = 1; i < argc; i++) { + write(filedes[1], argv[i], strlen(argv[i])); + sleep(1); + } + wait(NULL); + } +} -- cgit v1.3.1-26-gb76a