Summary Files Commits Branches @commit @diff @stats
path: root / Year 3 / System programming / Practicum5 / ex2.c
diff options
context:
space:
mode:
Commitd4366c2eeb49a37cd50db3962d5489e453a1f18c (patch)
Author
 Syndamia <kamen@syndamia.com>
2023-11-08 13:26:46 +0200
Committer
 Syndamia <kamen@syndamia.com>
2023-11-08 13:26:46 +0200
Files672203edadf385adf5f2b2d0f47ab43abe8f479d /Year 3/System programming/Practicum5/ex2.c
uni-projects-d4366c2eeb49a37cd50db3962d5489e453a1f18c.tar
uni-projects-d4366c2eeb49a37cd50db3962d5489e453a1f18c.tar.gz
uni-projects-d4366c2eeb49a37cd50db3962d5489e453a1f18c.zip
Parentb9d200d296d362b2f3191ac8f2164a0bbc79b2a6 (diff)
[SP/P5] Added solutions to some exercises from today
1 files changed, 54 insertions, 0 deletions
-rw-r--r--Year 3/System programming/Practicum5/ex2.c54
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 <sys/types.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+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;
+ }
+ }
+}