From d4366c2eeb49a37cd50db3962d5489e453a1f18c Mon Sep 17 00:00:00 2001 From: Syndamia Date: Wed, 8 Nov 2023 13:26:46 +0200 Subject: [SP/P5] Added solutions to some exercises from today --- Year 3/System programming/Practicum5/ex3.c | 38 ++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 Year 3/System programming/Practicum5/ex3.c (limited to 'Year 3/System programming/Practicum5/ex3.c') diff --git a/Year 3/System programming/Practicum5/ex3.c b/Year 3/System programming/Practicum5/ex3.c new file mode 100644 index 0000000..b7b33e0 --- /dev/null +++ b/Year 3/System programming/Practicum5/ex3.c @@ -0,0 +1,38 @@ +#include +#include +#include + +int main() { + int f1 = open("read.txt", O_RDONLY); + if (f1 < 0) { + perror("open read.txt"); + return 1; + } + + int f2 = open("write.txt", O_CREAT | O_WRONLY | O_TRUNC, 0755); + if (f2 < 0) { + perror("open write.txt"); + return 1; + } + + pid_t fpid = fork(); + if (fpid < 0) { + perror("fork"); + return 2; + } + + if (fpid == 0) { // child + for (int i = 0; i < 5; i++) { + sleep(2); + write(f2, "abc", 3); + } + } + else { + char buff[3]; + for (int i = 0; i < 5; i++) { + read(f1, buff, 3); + write(f2, buff, 3); + sleep(1); + } + } +} -- cgit v1.3.1-26-gb76a