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