diff options
| Commit | 3c791c6c357115d4abf71c318c9e7cd7571ea658 (patch) | |
|---|---|---|
| Author | 2023-12-02 21:25:46 +0200 | |
| Committer | 2023-12-02 21:25:46 +0200 | |
| Files | 10705fa10c017e7c1c66d1c8feaca92758acefd0 | |
| uni-projects-3c791c6c357115d4abf71c318c9e7cd7571ea658.tar uni-projects-3c791c6c357115d4abf71c318c9e7cd7571ea658.tar.gz uni-projects-3c791c6c357115d4abf71c318c9e7cd7571ea658.zip | ||
| Parent | 952bee4ac4e4a97d30fc1d681ff0a9e5168fb9c8 (diff) | |
[SP/P05] Solved exercises
9 files changed, 309 insertions, 0 deletions
| -rw-r--r-- | Year 3/System programming/Practice05/ex1.c | 34 | ||||
| -rw-r--r-- | Year 3/System programming/Practice05/ex2.c | 34 | ||||
| -rw-r--r-- | Year 3/System programming/Practice05/ex3.c | 34 | ||||
| -rw-r--r-- | Year 3/System programming/Practice05/ex4.c | 36 | ||||
| -rw-r--r-- | Year 3/System programming/Practice05/ex5.c | 39 | ||||
| -rw-r--r-- | Year 3/System programming/Practice05/ex6.c | 15 | ||||
| -rw-r--r-- | Year 3/System programming/Practice05/ex7.c | 31 | ||||
| -rw-r--r-- | Year 3/System programming/Practice05/ex8.c | 39 | ||||
| -rw-r--r-- | Year 3/System programming/Practice05/ex9.c | 47 |
diff --git a/Year 3/System programming/Practice05/ex1.c b/Year 3/System programming/Practice05/ex1.c new file mode 100644 index 0000000..4d9966d --- /dev/null +++ b/Year 3/System programming/Practice05/ex1.c @@ -0,0 +1,34 @@ +#include <unistd.h> // fork, write, close, sleep +#include <fcntl.h> // open +#include <stdio.h> // perror + +int main(int argc, char* argv[]) { + pid_t f = fork(); + int file = open("test.txt", O_WRONLY); + if (file < 0) { + perror("open"); + return 1; + } + + if (f == 0) { // in child + char buf = 'c'; + for (int i = 0; i < 5; i++) { + sleep(2); + if(write(file, &buf, 1) < 0) { + perror("write"); + return 2; + } + } + } + else { + char buf = 'p'; + for (int i = 0; i < 5; i++) { + if(write(file, &buf, 1) < 0) { + perror("write"); + return 3; + } + sleep(1); + } + } + close(file); +} diff --git a/Year 3/System programming/Practice05/ex2.c b/Year 3/System programming/Practice05/ex2.c new file mode 100644 index 0000000..85ce0dd --- /dev/null +++ b/Year 3/System programming/Practice05/ex2.c @@ -0,0 +1,34 @@ +#include <unistd.h> // fork, write, close, sleep +#include <fcntl.h> // open +#include <stdio.h> // perror + +int main(int argc, char* argv[]) { + int file = open("test.txt", O_WRONLY); + if (file < 0) { + perror("open"); + return 1; + } + pid_t f = fork(); + + if (f == 0) { // in child + char buf = 'c'; + for (int i = 0; i < 5; i++) { + sleep(2); + if(write(file, &buf, 1) < 0) { + perror("write"); + return 2; + } + } + } + else { + char buf = 'p'; + for (int i = 0; i < 5; i++) { + if(write(file, &buf, 1) < 0) { + perror("write"); + return 3; + } + sleep(1); + } + } + close(file); +} diff --git a/Year 3/System programming/Practice05/ex3.c b/Year 3/System programming/Practice05/ex3.c new file mode 100644 index 0000000..7374411 --- /dev/null +++ b/Year 3/System programming/Practice05/ex3.c @@ -0,0 +1,34 @@ +#include <unistd.h> // fork, write, close, sleep +#include <fcntl.h> // open +#include <stdio.h> // perror + +int main(int argc, char* argv[]) { + pid_t f = fork(); + int file = open("test.txt", O_WRONLY | O_APPEND); + if (file < 0) { + perror("open"); + return 1; + } + + if (f == 0) { // in child + char buf = 'c'; + for (int i = 0; i < 5; i++) { + sleep(2); + if(write(file, &buf, 1) < 0) { + perror("write"); + return 2; + } + } + } + else { + char buf = 'p'; + for (int i = 0; i < 5; i++) { + if(write(file, &buf, 1) < 0) { + perror("write"); + return 3; + } + sleep(1); + } + } + close(file); +} diff --git a/Year 3/System programming/Practice05/ex4.c b/Year 3/System programming/Practice05/ex4.c new file mode 100644 index 0000000..2a77f41 --- /dev/null +++ b/Year 3/System programming/Practice05/ex4.c @@ -0,0 +1,36 @@ +#include <unistd.h> // fork, write, read, close, sleep +#include <fcntl.h> // open +#include <stdio.h> // perror + +int main(int argc, char* argv[]) { + int f1 = open(argv[1], O_RDONLY); + if (f1 < 0) { + perror("open"); + return 1; + } + + int f2 = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC); + if (f2 < 0) { + perror("open"); + return 2; + } + + pid_t fpid = fork(); + if (fpid == 0) { // in child + char buf = 'c'; + for (int i = 0; i < 20; i++) { + sleep(2); + write(f2, &buf, 1); + } + } + else { + char buf; + while (read(f1, &buf, 1)) { + write(f2, &buf, 1); + sleep(1); + } + } + + close(f1); + close(f2); +} diff --git a/Year 3/System programming/Practice05/ex5.c b/Year 3/System programming/Practice05/ex5.c new file mode 100644 index 0000000..8e7bf15 --- /dev/null +++ b/Year 3/System programming/Practice05/ex5.c @@ -0,0 +1,39 @@ +#include <unistd.h> // fork, write, read, close, sleep +#include <fcntl.h> // open +#include <stdio.h> // perror +#include <sys/wait.h> // waitpid, WNOHANG + +int main(int argc, char* argv[]) { + int f1 = open(argv[1], O_RDONLY); + if (f1 < 0) { + perror("open"); + return 1; + } + + int f2 = open(argv[2], O_WRONLY | O_CREAT | O_APPEND); + if (f2 < 0) { + perror("open"); + return 2; + } + + pid_t fpid = fork(); + if (fpid == 0) { // in child + char buf; + while (read(f1, &buf, 1)) { + write(f2, &buf, 1); + sleep(1); + } + } + else { + int status = -2; + char buf = 'p'; + while(status == -2) { + sleep(2); + write(f2, &buf, 1); + waitpid(fpid, &status, WNOHANG); + } + } + + close(f1); + close(f2); +} diff --git a/Year 3/System programming/Practice05/ex6.c b/Year 3/System programming/Practice05/ex6.c new file mode 100644 index 0000000..7e0376d --- /dev/null +++ b/Year 3/System programming/Practice05/ex6.c @@ -0,0 +1,15 @@ +#include <unistd.h> // execvp, fork +#include <stdio.h> // printf +#include <sys/wait.h> // wait + +int main(int argc, char* argv[]) { + pid_t fpid = fork(); + if (fpid == 0) { + execvp(argv[1], argv + 1); + } + else { + int status; + wait(&status); + printf("%d %d\n", fpid, status); + } +} diff --git a/Year 3/System programming/Practice05/ex7.c b/Year 3/System programming/Practice05/ex7.c new file mode 100644 index 0000000..08df367 --- /dev/null +++ b/Year 3/System programming/Practice05/ex7.c @@ -0,0 +1,31 @@ +#include <unistd.h> // execvp, fork +#include <stdio.h> // printf +#include <sys/wait.h> // wait +#include <fcntl.h> // open, O_WRONLY + +int main(int argc, char* argv[]) { + if (argc < 2) return 1; + + pid_t fpid = fork(); + if (fpid == 0) { + int f = open(argv[argc-1], O_WRONLY | O_TRUNC | O_CREAT, 0755); + if (f < 0) { + perror("open"); + return 2; + } + dup2(f, 1); + close(f); + + char* args[argc-1]; + for (int i = 1; i < argc - 1; i++) + args[i-1] = argv[i]; + args[argc-2] = NULL; + + execvp(argv[1], args); + } + else { + int status; + wait(&status); + printf("%d %d\n", fpid, status); + } +} diff --git a/Year 3/System programming/Practice05/ex8.c b/Year 3/System programming/Practice05/ex8.c new file mode 100644 index 0000000..51082bd --- /dev/null +++ b/Year 3/System programming/Practice05/ex8.c @@ -0,0 +1,39 @@ +#include <unistd.h> // execvp, fork +#include <stdio.h> // printf +#include <sys/wait.h> // wait +#include <fcntl.h> // open, O_WRONLY + +int main(int argc, char* argv[]) { + if (argc < 2) return 1; + + pid_t fpid = fork(); + if (fpid == 0) { + int fi = open(argv[argc-2], O_RDONLY); + if (fi < 0) { + perror("open"); + return 2; + } + dup2(fi, 0); + close(fi); + + int fo = open(argv[argc-1], O_WRONLY | O_TRUNC | O_CREAT, 0755); + if (fo < 0) { + perror("open"); + return 3; + } + dup2(fo, 1); + close(fo); + + char* args[argc-2]; + for (int i = 1; i < argc - 2; i++) + args[i-1] = argv[i]; + args[argc-3] = NULL; + + execvp(argv[1], args); + } + else { + int status; + wait(&status); + printf("%d %d\n", fpid, status); + } +} diff --git a/Year 3/System programming/Practice05/ex9.c b/Year 3/System programming/Practice05/ex9.c new file mode 100644 index 0000000..eb50676 --- /dev/null +++ b/Year 3/System programming/Practice05/ex9.c @@ -0,0 +1,47 @@ +#include <unistd.h> // execvp, fork +#include <stdio.h> // printf +#include <sys/wait.h> // wait +#include <fcntl.h> // open, O_WRONLY + +int main(int argc, char* argv[]) { + if (argc < 2) return 1; + + pid_t fpid = fork(); + if (fpid == 0) { + int fi = open(argv[argc-3], O_RDONLY); + if (fi < 0) { + perror("open"); + return 2; + } + dup2(fi, 0); + close(fi); + + int fo = open(argv[argc-2], O_WRONLY | O_TRUNC | O_CREAT, 0755); + if (fo < 0) { + perror("open"); + return 3; + } + dup2(fo, 1); + close(fo); + + int fe = open(argv[argc-1], O_WRONLY | O_TRUNC | O_CREAT, 0755); + if (fe < 0) { + perror("open"); + return 4; + } + dup2(fe, 2); + close(fe); + + char* args[argc-2]; + for (int i = 1; i < argc - 2; i++) + args[i-1] = argv[i]; + args[argc-3] = NULL; + + execvp(argv[1], args); + } + else { + int status; + wait(&status); + printf("%d %d\n", fpid, status); + } +} |
