diff options
Diffstat (limited to 'semestr-5/so/lista1/so21_lista_1/fileref.c')
-rw-r--r-- | semestr-5/so/lista1/so21_lista_1/fileref.c | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/semestr-5/so/lista1/so21_lista_1/fileref.c b/semestr-5/so/lista1/so21_lista_1/fileref.c new file mode 100644 index 0000000..b5d6f9f --- /dev/null +++ b/semestr-5/so/lista1/so21_lista_1/fileref.c @@ -0,0 +1,50 @@ +#include "csapp.h" + +static char buf[256]; + +#define LINE1 49 +#define LINE2 33 +#define LINE3 78 + +static void do_read(int fd) { + /* TODO: Spawn a child. Read from the file descriptor in both parent and + * child. Check how file cursor value has changed in both processes. */ + int pid = fork(); + pid = getpid(); + int offset = lseek(fd, 0, SEEK_CUR); + printf("[%d] Offset: %d\n", pid, offset); + + int aux = read(fd, buf, LINE1); + offset = lseek(fd, 0, SEEK_CUR); + printf("[%d] Offset: %d\n", pid, offset); + + aux = read(fd, buf, LINE2); + offset = lseek(fd, 0, SEEK_CUR); + printf("[%d] Offset: %d\n", pid, offset); + + aux = read(fd, buf, LINE3); + offset = lseek(fd, 0, SEEK_CUR); + printf("[%d] Offset: %d\n", pid, offset); + printf("%d\n", aux); + exit(0); +} + +static void do_close(int fd) { + /* TODO: In the child close file descriptor, in the parent wait for child to + * die and check if the file descriptor is still accessible. */ + + exit(0); +} + +int main(int argc, char **argv) { + if (argc != 2) + app_error("Usage: %s [read|close]", argv[0]); + + int fd = Open("test.txt", O_RDONLY, 0); + + if (!strcmp(argv[1], "read")) + do_read(fd); + if (!strcmp(argv[1], "close")) + do_close(fd); + app_error("Unknown variant '%s'", argv[1]); +} |