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
|
#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]);
}
|