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/ex2.c | 54 ++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 Year 3/System programming/Practicum5/ex2.c (limited to 'Year 3/System programming/Practicum5/ex2.c') diff --git a/Year 3/System programming/Practicum5/ex2.c b/Year 3/System programming/Practicum5/ex2.c new file mode 100644 index 0000000..88b337c --- /dev/null +++ b/Year 3/System programming/Practicum5/ex2.c @@ -0,0 +1,54 @@ +#include +#include +#include +#include +#include + +int main() { + int f = open("test.txt", O_CREAT | O_WRONLY | O_TRUNC, 0755); + if (f < 0) { + perror("open test.txt"); + return 2; + } + + pid_t fpid = fork(); + + if (fpid < 0) { + perror("fork"); + return 1; + } + + if (fpid == 0) { // in child + sleep(2); + int ws = write(f, "From Child 1\n", 13); + + if (ws < 0) { + perror("write from child"); + return 3; + } + + sleep(5); + ws = write(f, "From Child 2\n", 13); + + if (ws < 0) { + perror("write from child"); + return 3; + } + } + else { + int ws = write(f, "From Parent 1\n", 14); + + if (ws < 0) { + perror("write from child"); + return 3; + } + + sleep(3); + ws = write(f, "From Parent 2\n", 14); + + if (ws < 0) { + perror("write from child"); + return 3; + } + } +} -- cgit v1.3.1-26-gb76a