-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.c
80 lines (73 loc) · 2.41 KB
/
client.c
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <fcntl.h>
#include <memory.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#define FIB_DEV "/dev/fibonacci"
int main()
{
// long long sz;
char buf[3000];
// char write_buf[] = "testing writing";
// char *test_write = malloc(2);
int offset = 100; /* TODO: try test something bigger than the limit */
/* open "FIB_DEV" as a file (O_RDWR means that it can read and write)
* https://man7.org/linux/man-pages/man2/open.2.html
*/
int fd = open(FIB_DEV, O_RDWR);
if (fd < 0) {
perror("Failed to open character device");
exit(1);
}
// sz = write(fd, write_buf, strlen(write_buf));
// printf("Writing to " FIB_DEV ", returned the sequence %lld\n", sz);
// lseek(fd, offset, SEEK_SET);
// sz = read(fd, buf, 1);
// printf("Reading from " FIB_DEV
// " at offset %d, returned the sequence "
// "%s.\n",
// offset, buf);
// printf("The string's length is %ld.\ncopy to user time spend : %lld
// ns\n\n", strlen(buf), sz);
// memset(buf, 0, sizeof(buf));
// memset(test_write, 0, 2);
// *test_write = '1';
// for (int i = 2; i <= offset; i++) {
// test_write = realloc(test_write, i);
// memset(test_write, '1', i);
// *(test_write + i - 1) = 0;
// // printf("%s\n", test_write);
// sz = write(fd, test_write, i);
// // printf("Writing to " FIB_DEV ", returned the sequence %lld\n",
// sz);
// printf("%lld\n", sz);
// }
// printf("Reading\n");
for (int i = 1; i <= offset; i++) {
lseek(fd, i, SEEK_SET);
// sz = read(fd, buf, i);
printf("Reading from " FIB_DEV
" at offset %d, returned the sequence "
"%s.\n",
i, buf);
// printf("%ld\n", strlen(buf));
// printf("%lld\n", sz);
// printf("The string's length is %ld.\ncopy to user time spend : %lld
// ns\n\n", strlen(buf), sz);
memset(buf, 0, sizeof(buf));
}
// for (int i = offset; i >= 0; i--) {
// lseek(fd, i, SEEK_SET);
// sz = read(fd, buf, 1);
// printf("Reading from " FIB_DEV
// " at offset %d, returned the sequence "
// "%s.\n",
// i, buf);
// memset(buf, 0, sizeof(buf));
// }
close(fd);
return 0;
}