blob: 2fdf0e5fa172305d5beb0c3f49ce93394bc53189 [file] [log] [blame]
lh758261d2023-07-13 05:52:04 -07001#include <errno.h>
2#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5#include <unistd.h>
6#include <signal.h>
7#include <time.h>
8#include <pthread.h>
9#include <sys/prctl.h>
10#include <sys/ipc.h>
11#include <sys/shm.h>
12#include <sys/types.h>
13#include <unistd.h>
14
15/**
16 * ºê¶¨Òå
17 */
18#define SHM_TEST_ALLOC_THREAD (0)
19#define SHM_TEST_MEMGET_THREAD (1)
20#define SHM_TEST_SHMCTL_THREAD (2)
21#define SHM_TEST_WRITE_THREAD (3)
22#define SHM_TEST_THREAD_NUM (4)
23#define SHM_TEST_STR_NUM (20)
24#define SHM_TEST_CAP_INIT_VALUE (0x16)
25
26#define SHM_TEST_MARK_0X1 (0x1)
27#define SHM_TEST_MARK_0X3 (0x3)
28#define SHM_TEST_MARK_0X5 (0x5)
29#define SHM_TEST_MARK_0X7 (0x7)
30#define SHM_TEST_MARK_NUM (0xA)
31
32#define SHM_PAGE_SIZE (4096)
33#define SHM_ALLOC_ONE_PAGE_SIZE (1024)
34#define SHM_ALLOC_TWO_PAGE_SIZE (8100)
35#define SHM_ALLOC_FOUR_PAGE_SIZE (16000)
36
37#define SHM_TEST_READ_FROM_SHM_PAGE0 (0x0A)
38#define SHM_TEST_WRITE_TO_SHM_PAGE1 (0x1B)
39#define SHM_TEST_READ_FROM_SHM_PAGE2 (0x2A)
40#define SHM_TEST_WRITE_TO_SHM_PAGE3 (0x3B)
41
42/**
43 * È«¾Ö±äÁ¿¶¨Òå
44 */
45pthread_t shm_mem_thread;
46pthread_t shm_alloc_thread;
47pthread_t shm_ppcross_thread;
48pthread_t shm_shmdt_thread;
49
50int g_shmid[SHM_TEST_THREAD_NUM] = {0};
51char *g_pshm[SHM_TEST_THREAD_NUM] = {NULL};
52int g_mark_path[SHM_TEST_MARK_NUM] = {0xFFFF};
53
54void *ap_mem_shm_alloc(void* args)
55{
56 int shmid = 0;
57 char *pshm = NULL;
xf.li6c8fc1e2023-08-12 00:11:09 -070058 key_t key = 0xFFFFF801;
lh758261d2023-07-13 05:52:04 -070059
60 prctl(PR_SET_NAME, "shm_alloc_task");
61
62 /*alloc two pages*/
63 shmid = shmget(key, SHM_ALLOC_TWO_PAGE_SIZE, 0666|IPC_CREAT);
64 if(shmid < 0)
xf.li6c8fc1e2023-08-12 00:11:09 -070065 {
lh758261d2023-07-13 05:52:04 -070066 printf("shmid return error\n");
xf.li6c8fc1e2023-08-12 00:11:09 -070067 return -1;
68 }
lh758261d2023-07-13 05:52:04 -070069
70 pshm = (char *)shmat(shmid, 0, 0);
71 if (pshm == NULL)
xf.li6c8fc1e2023-08-12 00:11:09 -070072 {
lh758261d2023-07-13 05:52:04 -070073 printf("shmat return error\n");
xf.li6c8fc1e2023-08-12 00:11:09 -070074 return -1;
75 }
lh758261d2023-07-13 05:52:04 -070076
77 g_shmid[SHM_TEST_ALLOC_THREAD] = shmid;
78 g_pshm[SHM_TEST_ALLOC_THREAD] = pshm;
79 shmdt(pshm);
80 if(shmctl(shmid, IPC_RMID,0) == -1)
xf.li6c8fc1e2023-08-12 00:11:09 -070081 {
lh758261d2023-07-13 05:52:04 -070082 printf("shmctl error\n");
xf.li6c8fc1e2023-08-12 00:11:09 -070083 return -1;
84 }
lh758261d2023-07-13 05:52:04 -070085
86 while(1)
87 {
88 sleep(50);
89 }
90}
91
92void *ap_mmap_shm(void* args)
93{
94 int shmid = 0;
95 char *pshm = NULL;
xf.li6c8fc1e2023-08-12 00:11:09 -070096 key_t key = 0xFFFFF802;
lh758261d2023-07-13 05:52:04 -070097
98 prctl(PR_SET_NAME, "shm_mem_task");
99
100 /*alloc two pages*/
101 shmid = shmget(key, SHM_ALLOC_TWO_PAGE_SIZE, 0666|IPC_CREAT);
102 if(shmid < 0)
103 printf("shmid return error\n");
104
105 pshm = (char *)shmat(shmid, 0, 0);
106 if (pshm == NULL)
107 printf("shmat return error\n");
108
109 g_shmid[SHM_TEST_MEMGET_THREAD] = shmid;
110 g_pshm[SHM_TEST_MEMGET_THREAD] = pshm;
111 shmdt(pshm);
112 if(shmctl(shmid, IPC_RMID,0) == -1)
113 printf("shmctl error\n");
114
115 while(1)
116 {
117 sleep(50);
118 }
119}
120
121void *ap_write_and_shmdt(void* args)
122{
123 int shmid = 0;
124 int test_num = 0;
125 int *pshm = NULL;
xf.li6c8fc1e2023-08-12 00:11:09 -0700126 key_t key = 0xFFFFF803;
lh758261d2023-07-13 05:52:04 -0700127
128 prctl(PR_SET_NAME, "shm_task");
129
130 shmid = shmget(key, SHM_ALLOC_ONE_PAGE_SIZE, 0666|IPC_CREAT);
131
132 if(shmid < 0)
133 printf("shmid return error\n");
134
135 pshm = (int *)shmat(shmid, 0, 0);
136 if (pshm == NULL)
137 printf("shmat return error\n");
138
139 g_shmid[SHM_TEST_WRITE_THREAD] = shmid;
140 g_pshm[SHM_TEST_WRITE_THREAD] = pshm;
141
142 while(1)
143 {
144 test_num = *(int *)pshm;
145 if(test_num == SHM_TEST_CAP_INIT_VALUE)
146 {
147 *(int *)pshm = SHM_TEST_MARK_0X1 + 1;
148 g_mark_path[1] = SHM_TEST_MARK_0X1;
149 printf("shmat test_num 0x16, g_mark_path[1] = %x\n", g_mark_path[1]);
150 break;
151 }
152 sleep(1);
153 }
154
155 while(1)
156 {
157 test_num = *(int *)pshm;
158 if(test_num == SHM_TEST_MARK_0X3)
159 {
160 *(int *)pshm = SHM_TEST_MARK_0X3 + 1;
161 g_mark_path[3] = SHM_TEST_MARK_0X3;
162 printf("shmat test_num 3, g_mark_path[3] before= %x\n", g_mark_path[3]);
163 break;
164 }
165 sleep(1);
166 }
167
168 while(1)
169 {
170 test_num = *(int *)pshm;
171 if(test_num == SHM_TEST_MARK_0X5)
172 {
173 *(int *)pshm = SHM_TEST_MARK_0X5 + 1;
174 g_mark_path[5] = SHM_TEST_MARK_0X5;
175 printf("shmat test_num 5, g_mark_path5 before= %x\n", g_mark_path[5]);
176 break;
177 }
178 sleep(1);
179 }
180
181 while(1)
182 {
183 test_num = *(int *)pshm;
184 if(test_num == SHM_TEST_MARK_0X7)
185 {
186 *(int *)pshm = SHM_TEST_MARK_0X7 + 1;
187 g_mark_path[7] = SHM_TEST_MARK_0X7;
188 printf("shmat test_num 7, g_mark_path7 before= %x\n", g_mark_path[7]);
189 break;
190 }
191 sleep(1);
192 }
193 memcpy(pshm + 32, "123456", SHM_TEST_STR_NUM);
194
195 shmdt(pshm);
196
197 while(1)
198 {
199 sleep(2);
200 }
201}
202
203void *ap_fourpage_cross_shm(void* args)
204{
205 int i = 0;
206 int shmid = 0;
207 char test_num = 0;
208 char *pshm = NULL;
xf.li6c8fc1e2023-08-12 00:11:09 -0700209 key_t key = 0xFFFFF804;
lh758261d2023-07-13 05:52:04 -0700210
211 prctl(PR_SET_NAME, "shm_cross_task");
212
213 /*alloc four pages*/
214 shmid = shmget(key, SHM_ALLOC_FOUR_PAGE_SIZE, 0666|IPC_CREAT);
215 if(shmid < 0)
216 printf("shmid return error\n");
217
218 pshm = (char *)shmat(shmid, 0, 0);
219 if (pshm == NULL)
220 printf("shmat return error\n");
221
222 g_shmid[SHM_TEST_SHMCTL_THREAD] = shmid;
223 g_pshm[SHM_TEST_SHMCTL_THREAD] = pshm;
224
225 for(;;)
226 {
227 while(1)
228 {
229 test_num = *(char *)pshm;
230 if(test_num == SHM_TEST_READ_FROM_SHM_PAGE0)
231 {
232 *(char *)(pshm + SHM_PAGE_SIZE) = SHM_TEST_WRITE_TO_SHM_PAGE1;
233 g_mark_path[2] = SHM_TEST_READ_FROM_SHM_PAGE0;
234 printf("shmat test_num, AP write %x to shm\n",SHM_TEST_WRITE_TO_SHM_PAGE1);
235 break;
236 }
237 sleep(5);
238 }
239 while(1)
240 {
241 test_num = *(char *)(pshm + 2 * SHM_PAGE_SIZE);
242 if(test_num == SHM_TEST_READ_FROM_SHM_PAGE2)
243 {
244 *(char *)(pshm + 3 * SHM_PAGE_SIZE) = SHM_TEST_WRITE_TO_SHM_PAGE3;
245 g_mark_path[4] = SHM_TEST_READ_FROM_SHM_PAGE2;
246 printf("shmat test_num, AP write %x to shm\n", SHM_TEST_WRITE_TO_SHM_PAGE3);
247 break;
248 }
249 sleep(5);
250 }
251 if (i++ >= 10)
252 break;
253 }
254 shmdt(pshm);
255 if(shmctl(shmid, IPC_RMID,0) == -1)
256 printf("shmctl error\n");
257
258 printf("shmctl end\n");
259 while(1)
260 {
261 sleep(5);
262 }
263}
264
265int main (int argc, char *argv[])
266{
267 int ret = 0;
268
269 ret = pthread_create(&shm_mem_thread, NULL, ap_mem_shm_alloc, NULL);
270 if(ret != 0)
271 {
272 printf("pthread_create shm_mem_thread error\n");
273 }
274
275 ret = pthread_create(&shm_alloc_thread, NULL, ap_mmap_shm, NULL);
276 if(ret != 0)
277 {
278 printf("pthread_create shm_alloc_thread error\n");
279 }
280
281 ret = pthread_create(&shm_ppcross_thread, NULL, ap_fourpage_cross_shm, NULL);
282 if(ret != 0)
283 {
284 printf("pthread_create shm_ppcross_thread error\n");
285 }
286
287 ret = pthread_create(&shm_shmdt_thread, NULL, ap_write_and_shmdt, NULL);
288 if(ret != 0)
289 {
290 printf("pthread_create shm_shmdt_thread error\n");
291 }
292 if(shm_mem_thread > 0)
293 pthread_join(shm_mem_thread, NULL);
294
295 if(shm_alloc_thread > 0)
296 pthread_join(shm_alloc_thread, NULL);
297
298 if(shm_ppcross_thread > 0)
299 pthread_join(shm_ppcross_thread, NULL);
300
301 if(shm_shmdt_thread > 0)
302 pthread_join(shm_shmdt_thread, NULL);
303
304 return 0;
305}
306