lh | 758261d | 2023-07-13 05:52:04 -0700 | [diff] [blame] | 1 | #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 | */ |
| 45 | pthread_t shm_mem_thread; |
| 46 | pthread_t shm_alloc_thread; |
| 47 | pthread_t shm_ppcross_thread; |
| 48 | pthread_t shm_shmdt_thread; |
| 49 | |
| 50 | int g_shmid[SHM_TEST_THREAD_NUM] = {0}; |
| 51 | char *g_pshm[SHM_TEST_THREAD_NUM] = {NULL}; |
| 52 | int g_mark_path[SHM_TEST_MARK_NUM] = {0xFFFF}; |
| 53 | |
| 54 | void *ap_mem_shm_alloc(void* args) |
| 55 | { |
| 56 | int shmid = 0; |
| 57 | char *pshm = NULL; |
| 58 | key_t key = 0xFFFFF001; |
| 59 | |
| 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) |
| 65 | printf("shmid return error\n"); |
| 66 | |
| 67 | pshm = (char *)shmat(shmid, 0, 0); |
| 68 | if (pshm == NULL) |
| 69 | printf("shmat return error\n"); |
| 70 | |
| 71 | g_shmid[SHM_TEST_ALLOC_THREAD] = shmid; |
| 72 | g_pshm[SHM_TEST_ALLOC_THREAD] = pshm; |
| 73 | shmdt(pshm); |
| 74 | if(shmctl(shmid, IPC_RMID,0) == -1) |
| 75 | printf("shmctl error\n"); |
| 76 | |
| 77 | while(1) |
| 78 | { |
| 79 | sleep(50); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | void *ap_mmap_shm(void* args) |
| 84 | { |
| 85 | int shmid = 0; |
| 86 | char *pshm = NULL; |
| 87 | key_t key = 0xFFFFF002; |
| 88 | |
| 89 | prctl(PR_SET_NAME, "shm_mem_task"); |
| 90 | |
| 91 | /*alloc two pages*/ |
| 92 | shmid = shmget(key, SHM_ALLOC_TWO_PAGE_SIZE, 0666|IPC_CREAT); |
| 93 | if(shmid < 0) |
| 94 | printf("shmid return error\n"); |
| 95 | |
| 96 | pshm = (char *)shmat(shmid, 0, 0); |
| 97 | if (pshm == NULL) |
| 98 | printf("shmat return error\n"); |
| 99 | |
| 100 | g_shmid[SHM_TEST_MEMGET_THREAD] = shmid; |
| 101 | g_pshm[SHM_TEST_MEMGET_THREAD] = pshm; |
| 102 | shmdt(pshm); |
| 103 | if(shmctl(shmid, IPC_RMID,0) == -1) |
| 104 | printf("shmctl error\n"); |
| 105 | |
| 106 | while(1) |
| 107 | { |
| 108 | sleep(50); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | void *ap_write_and_shmdt(void* args) |
| 113 | { |
| 114 | int shmid = 0; |
| 115 | int test_num = 0; |
| 116 | int *pshm = NULL; |
| 117 | key_t key = 0xFFFFF003; |
| 118 | |
| 119 | prctl(PR_SET_NAME, "shm_task"); |
| 120 | |
| 121 | shmid = shmget(key, SHM_ALLOC_ONE_PAGE_SIZE, 0666|IPC_CREAT); |
| 122 | |
| 123 | if(shmid < 0) |
| 124 | printf("shmid return error\n"); |
| 125 | |
| 126 | pshm = (int *)shmat(shmid, 0, 0); |
| 127 | if (pshm == NULL) |
| 128 | printf("shmat return error\n"); |
| 129 | |
| 130 | g_shmid[SHM_TEST_WRITE_THREAD] = shmid; |
| 131 | g_pshm[SHM_TEST_WRITE_THREAD] = pshm; |
| 132 | |
| 133 | while(1) |
| 134 | { |
| 135 | test_num = *(int *)pshm; |
| 136 | if(test_num == SHM_TEST_CAP_INIT_VALUE) |
| 137 | { |
| 138 | *(int *)pshm = SHM_TEST_MARK_0X1 + 1; |
| 139 | g_mark_path[1] = SHM_TEST_MARK_0X1; |
| 140 | printf("shmat test_num 0x16, g_mark_path[1] = %x\n", g_mark_path[1]); |
| 141 | break; |
| 142 | } |
| 143 | sleep(1); |
| 144 | } |
| 145 | |
| 146 | while(1) |
| 147 | { |
| 148 | test_num = *(int *)pshm; |
| 149 | if(test_num == SHM_TEST_MARK_0X3) |
| 150 | { |
| 151 | *(int *)pshm = SHM_TEST_MARK_0X3 + 1; |
| 152 | g_mark_path[3] = SHM_TEST_MARK_0X3; |
| 153 | printf("shmat test_num 3, g_mark_path[3] before= %x\n", g_mark_path[3]); |
| 154 | break; |
| 155 | } |
| 156 | sleep(1); |
| 157 | } |
| 158 | |
| 159 | while(1) |
| 160 | { |
| 161 | test_num = *(int *)pshm; |
| 162 | if(test_num == SHM_TEST_MARK_0X5) |
| 163 | { |
| 164 | *(int *)pshm = SHM_TEST_MARK_0X5 + 1; |
| 165 | g_mark_path[5] = SHM_TEST_MARK_0X5; |
| 166 | printf("shmat test_num 5, g_mark_path5 before= %x\n", g_mark_path[5]); |
| 167 | break; |
| 168 | } |
| 169 | sleep(1); |
| 170 | } |
| 171 | |
| 172 | while(1) |
| 173 | { |
| 174 | test_num = *(int *)pshm; |
| 175 | if(test_num == SHM_TEST_MARK_0X7) |
| 176 | { |
| 177 | *(int *)pshm = SHM_TEST_MARK_0X7 + 1; |
| 178 | g_mark_path[7] = SHM_TEST_MARK_0X7; |
| 179 | printf("shmat test_num 7, g_mark_path7 before= %x\n", g_mark_path[7]); |
| 180 | break; |
| 181 | } |
| 182 | sleep(1); |
| 183 | } |
| 184 | memcpy(pshm + 32, "123456", SHM_TEST_STR_NUM); |
| 185 | |
| 186 | shmdt(pshm); |
| 187 | |
| 188 | while(1) |
| 189 | { |
| 190 | sleep(2); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | void *ap_fourpage_cross_shm(void* args) |
| 195 | { |
| 196 | int i = 0; |
| 197 | int shmid = 0; |
| 198 | char test_num = 0; |
| 199 | char *pshm = NULL; |
| 200 | key_t key = 0xFFFFF004; |
| 201 | |
| 202 | prctl(PR_SET_NAME, "shm_cross_task"); |
| 203 | |
| 204 | /*alloc four pages*/ |
| 205 | shmid = shmget(key, SHM_ALLOC_FOUR_PAGE_SIZE, 0666|IPC_CREAT); |
| 206 | if(shmid < 0) |
| 207 | printf("shmid return error\n"); |
| 208 | |
| 209 | pshm = (char *)shmat(shmid, 0, 0); |
| 210 | if (pshm == NULL) |
| 211 | printf("shmat return error\n"); |
| 212 | |
| 213 | g_shmid[SHM_TEST_SHMCTL_THREAD] = shmid; |
| 214 | g_pshm[SHM_TEST_SHMCTL_THREAD] = pshm; |
| 215 | |
| 216 | for(;;) |
| 217 | { |
| 218 | while(1) |
| 219 | { |
| 220 | test_num = *(char *)pshm; |
| 221 | if(test_num == SHM_TEST_READ_FROM_SHM_PAGE0) |
| 222 | { |
| 223 | *(char *)(pshm + SHM_PAGE_SIZE) = SHM_TEST_WRITE_TO_SHM_PAGE1; |
| 224 | g_mark_path[2] = SHM_TEST_READ_FROM_SHM_PAGE0; |
| 225 | printf("shmat test_num, AP write %x to shm\n",SHM_TEST_WRITE_TO_SHM_PAGE1); |
| 226 | break; |
| 227 | } |
| 228 | sleep(5); |
| 229 | } |
| 230 | while(1) |
| 231 | { |
| 232 | test_num = *(char *)(pshm + 2 * SHM_PAGE_SIZE); |
| 233 | if(test_num == SHM_TEST_READ_FROM_SHM_PAGE2) |
| 234 | { |
| 235 | *(char *)(pshm + 3 * SHM_PAGE_SIZE) = SHM_TEST_WRITE_TO_SHM_PAGE3; |
| 236 | g_mark_path[4] = SHM_TEST_READ_FROM_SHM_PAGE2; |
| 237 | printf("shmat test_num, AP write %x to shm\n", SHM_TEST_WRITE_TO_SHM_PAGE3); |
| 238 | break; |
| 239 | } |
| 240 | sleep(5); |
| 241 | } |
| 242 | if (i++ >= 10) |
| 243 | break; |
| 244 | } |
| 245 | shmdt(pshm); |
| 246 | if(shmctl(shmid, IPC_RMID,0) == -1) |
| 247 | printf("shmctl error\n"); |
| 248 | |
| 249 | printf("shmctl end\n"); |
| 250 | while(1) |
| 251 | { |
| 252 | sleep(5); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | int main (int argc, char *argv[]) |
| 257 | { |
| 258 | int ret = 0; |
| 259 | |
| 260 | ret = pthread_create(&shm_mem_thread, NULL, ap_mem_shm_alloc, NULL); |
| 261 | if(ret != 0) |
| 262 | { |
| 263 | printf("pthread_create shm_mem_thread error\n"); |
| 264 | } |
| 265 | |
| 266 | ret = pthread_create(&shm_alloc_thread, NULL, ap_mmap_shm, NULL); |
| 267 | if(ret != 0) |
| 268 | { |
| 269 | printf("pthread_create shm_alloc_thread error\n"); |
| 270 | } |
| 271 | |
| 272 | ret = pthread_create(&shm_ppcross_thread, NULL, ap_fourpage_cross_shm, NULL); |
| 273 | if(ret != 0) |
| 274 | { |
| 275 | printf("pthread_create shm_ppcross_thread error\n"); |
| 276 | } |
| 277 | |
| 278 | ret = pthread_create(&shm_shmdt_thread, NULL, ap_write_and_shmdt, NULL); |
| 279 | if(ret != 0) |
| 280 | { |
| 281 | printf("pthread_create shm_shmdt_thread error\n"); |
| 282 | } |
| 283 | if(shm_mem_thread > 0) |
| 284 | pthread_join(shm_mem_thread, NULL); |
| 285 | |
| 286 | if(shm_alloc_thread > 0) |
| 287 | pthread_join(shm_alloc_thread, NULL); |
| 288 | |
| 289 | if(shm_ppcross_thread > 0) |
| 290 | pthread_join(shm_ppcross_thread, NULL); |
| 291 | |
| 292 | if(shm_shmdt_thread > 0) |
| 293 | pthread_join(shm_shmdt_thread, NULL); |
| 294 | |
| 295 | return 0; |
| 296 | } |
| 297 | |