diff options
[SP/P05] Solved exercises
1 files changed, 34 insertions, 0 deletions
| -rw-r--r-- | Year 3/System programming/Practice05/ex2.c | 34 |
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); +} |
