-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomp310_a2_test2.c
133 lines (123 loc) · 4.16 KB
/
comp310_a2_test2.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
/* Written by Xiru Zhu
* This is for testing your assignment 2.
*/
#include "comp310_a2_test.h"
void kv_delete_db() {
//kill_shared_mem();
sem_unlink("writer_lock_ktagoe");
sem_unlink("reader_lock_ktagoe");
shm_unlink(__TEST_SHARED_MEM_NAME__);
}
void kill_shared_mem(){
//In case it already exists
sem_unlink(__TEST_SHARED_SEM_NAME__);
shm_unlink(__TEST_SHARED_MEM_NAME__);
}
void intHandler(int dummy) {
for(int i = 0; i < __TEST_FORK_NUM__; i++){
kill(pids[i], SIGKILL);
}
sem_close(open_sem_lock);
sem_unlink(__TEST_SHARED_SEM_NAME__);
kv_delete_db();
exit(0);
}
int main(){
int max_write, errors;
char buf[__TEST_MAX_DATA_LENGTH__];
char key[__TEST_MAX_KEY_SIZE__];
int write_table[__TEST_FORK_NUM__];
int temp_flag;
int write_count;
char **read_all;
kill_shared_mem();
open_sem_lock = sem_open(__TEST_SHARED_SEM_NAME__, O_CREAT, 0644, 0);
srand(time(NULL));
signal(SIGINT, intHandler);
signal(SIGQUIT, intHandler);
signal(SIGTSTP, intHandler);
errors = 0;
printf("-----------Multiprocess Testing Of Shared Memory Database-----------\n");
printf("-----------CAUTION. This test assumes test 1 with no errors. -----------\n");
for(int k = 0; k < RUN_ITERATIONS; k++){
printf("-----------ROUND %d-----------\n", k + 1);
temp_flag = -1;
write_count = 0;
memset(pids, -1, __TEST_FORK_NUM__ * sizeof(int));
max_write = (rand()) % __TEST_MAX_POD_ENTRY__/2 + __TEST_MAX_POD_ENTRY__/2;
for(int j = 0; j < __TEST_FORK_NUM__; j++){
write_table[j] = max_write/__TEST_FORK_NUM__ - rand() % 5;
write_count += write_table[j];
}
generate_string(key, __TEST_MAX_KEY_SIZE__);
int child_num;
for(child_num = 0; child_num < __TEST_FORK_NUM__; child_num++){
pids[child_num] = fork();
if(pids[child_num] == 0){
break;
}
}
if(pids[__TEST_FORK_NUM__ - 1] > 0){
// Wait for childs to finish up
temp_flag = kv_store_create(__TEST_SHARED_MEM_NAME__);
if(temp_flag != 0){
printf("Error! Init failed\n");
errors++;
}
for(int i = 0; i < __TEST_FORK_NUM__; i++){
sem_post(open_sem_lock);
}
for(int i = 0; i < __TEST_FORK_NUM__; i++){
waitpid(pids[i], NULL, 0);
}
}else{
open_sem_lock = sem_open(__TEST_SHARED_SEM_NAME__, 0);
errno = 0;
sem_wait(open_sem_lock);
temp_flag = kv_store_create(__TEST_SHARED_MEM_NAME__);
if(temp_flag != 0){
printf("Error! Init failed\n");
}
int i;
for(i = 0; i < write_table[child_num]; i++){
generate_string(buf, __TEST_MAX_DATA_LENGTH__);
temp_flag = kv_store_write(key, buf);
if(temp_flag < 0){
printf("Error! Write failed\n");
}
}
//printf("child %d wrote %d/%d\n", child_num, write_table[child_num], write_count);
sem_close(open_sem_lock);
exit(0);
}
//At this point we read all values
read_all = kv_store_read_all(key);
if(read_all == NULL){
printf("Error! Read failed\n");
errors++;
continue;
}
temp_flag = 0;
for(int i = 0;;i++){
if(read_all[i] == NULL){
break;
}else{
temp_flag++;
}
}
if(temp_flag != write_count){
printf("Error! Invalid number of reads. Should be %d but only received %d\n", write_count, temp_flag);
errors++;
}
for(int i = 0; i < temp_flag; i++){
free(read_all[i]);
}
free(read_all);
kv_delete_db();
}
sem_close(open_sem_lock);
sem_unlink(__TEST_SHARED_SEM_NAME__);
printf("-----------Error Count: %d-----------\n\n", errors);
printf("-----------Multiprocess Testing Of Shared Memory Database END-----------\n");
return 0;
}