forked from gsoosk/POS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shmTest.c
204 lines (186 loc) · 5.42 KB
/
shmTest.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include "types.h"
// #include "stat.h"
// #include "fcntl.h" // for using file defines
#include "user.h" // for using from strlen
#define ONLY_OWNER_WRITE 0x001
#define ONLY_CHILD_CAN_ATTACH 0x002
struct shm_cnt {
int cnt;
};
void simple_shm_test();
void shm_write_flag_test();
void shm_child_attach_flag_test();
void shm_more_than_one_page_test();
void test();
int main(int argc, char *argv[])
{
while(1) {
shm_init();
printf(1, "What test do you want to run ? \n");
printf(1, "1.shared memory between two proccess. simple\n");
printf(1, "2.shared memory with ONLY_OWNER_WRITE flag\n");
printf(1, "3.shared memory with ONLY_CHILD_CAN_ATTACH flag\n");
printf(1, "4.shared memory with more than one page\n");
printf(1, "5.shared memory open and attach in parrent. simple\n");
char buf[1024];
read(1, buf, 1024);
if(atoi(buf) == 1)
simple_shm_test();
else if(atoi(buf) == 2)
shm_write_flag_test();
else if(atoi(buf) == 3)
shm_child_attach_flag_test();
else if(atoi(buf) == 4)
shm_more_than_one_page_test();
else if(atoi(buf) == 5)
test();
}
exit();
}
void simple_shm_test()
{
struct shm_cnt *counter;
acquiresleep_syscalls();
int pid = fork();
if(pid > 0)
{
shm_open(1, 1, 0);
counter = (struct shm_cnt *) shm_attach(1);
counter ->cnt = 10;
printf(1, "counter value in parent is : %d\n", (counter->cnt));
releasesleep_syscalls();
wait();
shm_close(1);
}
else
{
acquiresleep_syscalls();
counter = (struct shm_cnt *) shm_attach(1);
counter->cnt++;
printf(1, "counter value increased in child\n");
printf(1, "counter value in child is : %d\n", (counter->cnt));
shm_close(1);
releasesleep_syscalls();
}
}
void shm_write_flag_test() {
struct shm_cnt *counter;
acquiresleep_syscalls();
int pid = fork();
if(pid > 0)
{
shm_open(1, 1, ONLY_OWNER_WRITE);
counter = (struct shm_cnt *) shm_attach(1);
counter ->cnt = 10;
printf(1, "counter value in parent is : %d\n", (counter->cnt));
releasesleep_syscalls();
wait();
shm_close(1);
}
else
{
acquiresleep_syscalls();
counter = (struct shm_cnt *) shm_attach(1);
printf(1, "counter value in child is : %d\n", (counter->cnt));
counter->cnt++;
printf(1, "counter value could not increased in child\n");
printf(1, "counter value in child is : %d\n", (counter->cnt));
shm_close(1);
releasesleep_syscalls();
}
}
void shm_child_attach_flag_test() {
struct shm_cnt *counter;
acquiresleep_syscalls();
int pid = fork();
if(pid > 0)
{
shm_open(1, 1, ONLY_CHILD_CAN_ATTACH);
counter = (struct shm_cnt *) shm_attach(1);
counter ->cnt = 10;
printf(1, "counter value in parent is : %d\n", (counter->cnt));
releasesleep_syscalls();
wait();
shm_close(1);
}
else
{
acquiresleep_syscalls();
counter = (struct shm_cnt *) shm_attach(1);
printf(1, "counter value in child is : %d\n", (counter->cnt));
counter->cnt++;
printf(1, "counter value increased in child\n");
printf(1, "counter value in child is : %d\n", (counter->cnt));
releasesleep_syscalls();
pid = fork();
if(pid > 0) {
printf(1, "child forks and waits for it\n");
wait();
shm_close(1);
}
else {
printf(1, "child of child wants to attach shared memory\n");
counter = shm_attach(1);
if(counter != 0)
counter++;
else
printf(1, "could not increase value because error on attach\n");
}
}
}
void shm_more_than_one_page_test() {
struct shm_cnt *counter;
acquiresleep_syscalls();
int pid = fork();
if(pid > 0)
{
shm_open(1, 5, 0);
counter = (struct shm_cnt *) shm_attach(1);
int i;
for(i = 0; i < 5; i++) {
(counter + i) ->cnt = i;
printf(1, "counter value %d in parent is : %d\n", i, ((counter + i)->cnt));
}
releasesleep_syscalls();
wait();
shm_close(1);
}
else
{
acquiresleep_syscalls();
counter = (struct shm_cnt *) shm_attach(1);
int i;
for(i = 0; i < 5; i++) {
(counter + i)->cnt++;
printf(1, "counter value %d increased in child\n", i);
printf(1, "counter value %d in child is : %d\n", i, ((counter + i)->cnt));
}
shm_close(1);
releasesleep_syscalls();
}
}
void test()
{
struct shm_cnt *counter;
int pid = fork();
shm_open(1, 1, 0);
counter = (struct shm_cnt *) shm_attach(1);
if(pid > 0)
{
acquiresleep_syscalls();
counter ->cnt = 10;
printf(1, "counter value in parent is : %d\n", (counter->cnt));
releasesleep_syscalls();
wait();
shm_close(1);
}
else
{
acquiresleep_syscalls();
counter->cnt++;
printf(1, "counter value increased in child\n");
printf(1, "counter value in child is : %d\n", (counter->cnt));
shm_close(1);
releasesleep_syscalls();
}
}