aboutsummaryrefslogtreecommitdiff
path: root/semestr-5/so/lista1/so21_lista_1/fileref.c
blob: 8ea7aad13f87868af5c51e564ac0a998873318e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#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. */
  int pid = fork();
  if (pid > 0) {
     printf("[%d] %d\n", getpid(), fcntl(fd, F_GETFD));
     close(fd);
     printf("[%d] %d\n", getpid(), fcntl(fd, F_GETFD));
  }
  else {
    wait(NULL);
    printf("[%d] %d\n", getpid(), fcntl(fd, F_GETFD));
  }
  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]);
}