Summary Files Commits Branches @commit @diff @stats
path: root / Year 3 / System programming
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
uni-projects-d4366c2eeb49a37cd50db3962d5489e453a1f18c.tar
uni-projects-d4366c2eeb49a37cd50db3962d5489e453a1f18c.tar.gz
uni-projects-d4366c2eeb49a37cd50db3962d5489e453a1f18c.zip
Parentb9d200d296d362b2f3191ac8f2164a0bbc79b2a6 (diff)
[SP/P5] Added solutions to some exercises from today
diff --git a/Year 3/System programming/Practicum5/ex1.c b/Year 3/System programming/Practicum5/ex1.c
new file mode 100644
index 0000000..85e86bb
--- /dev/null
+++ b/Year 3/System programming/Practicum5/ex1.c
@@ -0,0 +1,55 @@
+#include <sys/types.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+int main() {
+ pid_t fpid = fork();
+
+ if (fpid < 0) {
+ perror("fork");
+ return 1;
+ }
+
+ int f = open("test.txt", O_CREAT | O_WRONLY | O_APPEND, 0755);
+ if (f < 0) {
+ perror("open test.txt");
+ return 2;
+ }
+
+ 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;
+ }
+ sleep(5);
+ }
+}
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;
+ }
+ }
+}
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 <fcntl.h>
+#include <unistd.h>
+#include <stdio.h>
+
+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);
+ }
+ }
+}
diff --git a/Year 3/System programming/Practicum5/ex4.c b/Year 3/System programming/Practicum5/ex4.c
new file mode 100644
index 0000000..682376d
--- /dev/null
+++ b/Year 3/System programming/Practicum5/ex4.c
@@ -0,0 +1,41 @@
+#include <fcntl.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <sys/wait.h>
+
+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_APPEND, 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
+ char buff[3];
+ for (int i = 0; i < 6; i++) {
+ read(f1, buff, 3);
+ write(f2, buff, 3);
+ sleep(1);
+ }
+ }
+ else {
+ int status = -2;
+ while (status == -2) {
+ sleep(2);
+ write(f2, "abc", 3);
+ waitpid(fpid, &status, WNOHANG);
+ }
+ }
+}
diff --git a/Year 3/System programming/Practicum5/ex5.c b/Year 3/System programming/Practicum5/ex5.c
new file mode 100644
index 0000000..49fe3e2
--- /dev/null
+++ b/Year 3/System programming/Practicum5/ex5.c
@@ -0,0 +1,21 @@
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/wait.h>
+#include <stdio.h>
+
+int main(int argc, char *argv[]) {
+ pid_t f = fork();
+ if (f < 0) {
+ perror("fork");
+ return 1;
+ }
+
+ if (f == 0) {
+ execvp(argv[1], argv + 1);
+ }
+ else {
+ int status = -2;
+ wait(&status);
+ printf("Child: %d\tExit: %d\n", f, status);
+ }
+}
diff --git a/Year 3/System programming/Practicum5/ex6.c b/Year 3/System programming/Practicum5/ex6.c
new file mode 100644
index 0000000..86e206c
--- /dev/null
+++ b/Year 3/System programming/Practicum5/ex6.c
@@ -0,0 +1,30 @@
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/wait.h>
+#include <stdio.h>
+
+int main(int argc, char *argv[]) {
+ pid_t f = fork();
+ if (f < 0) {
+ perror("fork");
+ return 1;
+ }
+
+ if (f == 0) {
+ char *commargs[argc];
+ commargs[0] = argv[1];
+ for (int i = 0; i < argc - 2; i++)
+ commargs[i+1] = argv[i+1];
+ commargs[argc - 1] = NULL;
+
+ int w = open(argv[argc-1], O_WRONLY | O_CREAT, 0755);
+ dup2(w, 1);
+ close(w);
+ execvp(*commargs, commargs + 1);
+ }
+ else {
+ int status = -2;
+ wait(&status);
+ printf("Exit: %d\n", status);
+ }
+}