-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoduletest.c
99 lines (84 loc) · 1.94 KB
/
moduletest.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
90
91
92
93
94
95
96
97
98
99
/* Test program for 3rd mandatory assignment.
*
* A process writes ITS integers to /dev/dm510-0 while
* another process read ITS integers from /dev/dm510-1.
* A checksum of the written data is compared with a
* checksum of the read data.
*
* This is done in both directions.
*/
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#define ITS 10000
void read_all(int fd, void *buf, int count) {
while (count > 0) {
int ret;
ret = read(fd, buf, count);
if (ret == -1) {
perror("read");
exit(1);
}
count -= ret;
buf += ret;
}
}
void write_all(int fd, void *buf, int count) {
while (count > 0) {
int ret;
ret = write(fd, buf, count);
if (ret == -1) {
perror("write");
exit(1);
}
count -= ret;
buf += ret;
}
}
int main(int argc, char *argv[])
{
pid_t pid;
int fd;
int sum = 0, i;
int val;
int cnt;
pid = fork();
if (pid == 0) {
fd = open("/dev/dm510-0", O_RDWR);
perror("w open");
for (i=0; i<ITS; i++) {
val++;
sum += val;
cnt = 4;
write_all(fd, &val, 4);
}
printf("1. expected result: %d\n", sum);
sum = 0;
for (i=0; i<ITS; i++) {
read_all(fd, &val, 4);
sum += val;
}
printf("2. result: %d\n", sum);
} else {
fd = open("/dev/dm510-1", O_RDWR);
perror("r open");
for (i=0; i<ITS; i++) {
read_all(fd, &val, 4);
sum += val;
}
printf("1. result: %d\n", sum);
sum = 0;
for (i=0; i<ITS; i++) {
val++;
sum += val;
write_all(fd, &val, 4);
}
printf("2. expected result: %d\n", sum);
wait(NULL);
}
return 0;
}