forked from sysprog21/fibdrv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.c
89 lines (73 loc) · 2.03 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
81
82
83
84
85
86
87
88
89
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#define FIB_DEV "/dev/fibonacci"
int main(int argc, char *argv[])
{
int fd, fd_out;
long long sz;
clockid_t cid = 1;
struct timespec start, end;
char write_buf[] = "testing writing";
int offset = 100;
int i = 0;
fd = open(FIB_DEV, O_RDWR);
fd_out = open(argv[1], O_RDWR | O_CREAT | O_TRUNC, 644);
if (fd < 0) {
perror("Failed to open character device");
exit(1);
}
for (i = 0; i <= offset; i++) {
sz = write(fd, write_buf, strlen(write_buf));
printf("Writing to " FIB_DEV ", returned the sequence %lld\n", sz);
}
char buffer[300];
char kbuffer[300];
for (i = 0; i <= offset; i++) {
lseek(fd, i, SEEK_SET);
clock_gettime(cid, &start);
sz = read(fd, kbuffer, 50);
clock_gettime(cid, &end);
long int sec, nanosec;
sec = end.tv_sec - start.tv_sec;
nanosec = end.tv_nsec - start.tv_nsec;
char time[8];
memcpy(time, kbuffer, 8);
snprintf(buffer, 300, "%d %ld %ld\n", i, sec * 1000000000 + nanosec,
atol(time));
write(fd_out, buffer, strlen(buffer));
printf("Reading from " FIB_DEV " at offset %d, returned the sequence ",
i);
int j = 0;
while (1) {
if (kbuffer[8 + j] == 0) {
break;
}
printf("%c", kbuffer[8 + j]);
j++;
}
printf(".\n");
}
for (i = offset; i >= 0; i--) {
lseek(fd, i, SEEK_SET);
sz = read(fd, kbuffer, 50);
printf("Reading from " FIB_DEV " at offset %d, returned the sequence ",
i);
int j = 0;
while (1) {
if (kbuffer[8 + j] == 0) {
break;
}
printf("%c", kbuffer[8 + j]);
j++;
}
printf(".\n");
}
close(fd);
close(fd_out);
return 0;
}