-
Notifications
You must be signed in to change notification settings - Fork 0
/
single_file_read_write.c
134 lines (120 loc) · 2.78 KB
/
single_file_read_write.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
#include<pthread.h>
#define FILE_PATH "/home/billy/hmfsMount/a"
#define BUFF_SIZE ((1 << 10) -1)
#define MAX_SIZE 20
#define THREAD_COUNT 20
char *r_buff, *w_buff;
FILE *file;
int fd;
long long count;
int *done;
pthread_t *t;
int seq_rand_read_write()
{
off_t pos1, pos2, pos3;
int pos, i;
int r_len, w_len, r_w_len;
pos = ((int)rand()) % MAX_SIZE + 1;
r_len = ((int)rand()) % MAX_SIZE + 1;
pos1 = lseek(fd, pos, SEEK_SET);
w_len = read(fd, r_buff, r_len);
pos2 = lseek(fd, 0, SEEK_END);
write(fd, r_buff, w_len);
pos3 = lseek(fd, pos2, SEEK_SET);
r_w_len = read(fd, w_buff, w_len);
for (i = 0; i < r_w_len; i++) {
if (r_buff[i] != w_buff[i] || w_buff == NULL) {
printf(" === error at %d ====\n", pos);
printf(" w_buff:%s\n", w_buff);
printf(" ====================\n");
printf(" r_buff:%s\n", r_buff);
printf(" ========stop========\n\n");
return 1;
}
}
return 0;
}
void *con_rand_read_write(void *arg)
{
off_t pos1, pos2, pos3;
int pos, i;
int r_len, w_len, r_w_len;
pos = ((int)rand()) % MAX_SIZE + 1;
r_len = ((int)rand()) % MAX_SIZE + 1;
pos1 = lseek(fd, pos, SEEK_SET);
w_len = read(fd, r_buff, r_len);
pos2 = lseek(fd, 0, SEEK_END); //XXX: lock free should cause error
write(fd, r_buff, w_len);
pos3 = lseek(fd, pos2, SEEK_SET);
r_w_len = read(fd, w_buff, w_len);
for (i = 0; i < r_w_len; i++) {
if (r_buff[i] != w_buff[i] || w_buff == NULL) {
printf(" === error at %d ====\n", pos);
printf(" w_buff:%s\n", w_buff);
printf(" ====================\n");
printf(" r_buff:%s\n", r_buff);
printf(" ========stop========\n\n");
return NULL;
}
}
(*done)++;
if(*done == count){
printf("no error found in concurrent RW!\n");
// exit(3);
} else {
//printf("done:%d\n", *done);
}
//exit(3);
return NULL;
}
int main(void)
{
int i;
int err;
file = fopen(FILE_PATH, "a+");
fd = fileno(file);
if (fd) {
r_buff = malloc(BUFF_SIZE);
w_buff = malloc(BUFF_SIZE);
printf("enter number of seq r/w test:");
scanf("%lld", &count);
while (count--) {
if (seq_rand_read_write()) {
printf("error in %lld times r/w\n", count);
goto free_seq;
}
if (count == 9000) {
printf("9k left!\n");
}
}
}
printf("no error found in seq RW!\n");
goto free_seq;
printf("enter number of seq r/w test:");
scanf("%lld", &count);
done = malloc(sizeof(int));
t = malloc(sizeof(pthread_t) * count);
*done = 0;
for(i=0; i<count; i++){
err = pthread_create(t+i, NULL, &con_rand_read_write, (void*)done);
if (err != 0) {
printf("can't create thread :[%d]\n", i);
goto free_con;
}
}
for(i=0; i<count; i++){
pthread_join(t[i], NULL);
}
free_con:
free(done);
free_seq:
free(r_buff);
free(w_buff);
fclose(file);
printf("free done!\n");
return 0;
}