| xf.li | 6c8fc1e | 2023-08-12 00:11:09 -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 | #include <assert.h> | 
|  | 15 | #include <sys/mman.h> | 
|  | 16 | #include <sys/stat.h> | 
|  | 17 | #include <fcntl.h> | 
|  | 18 |  | 
|  | 19 | /** | 
|  | 20 | * ºê¶¨Òå | 
|  | 21 | */ | 
|  | 22 | #define SHM_TEST_ALLOC_THREAD     (0) | 
|  | 23 | #define SHM_TEST_MEMGET_THREAD    (1) | 
|  | 24 | #define SHM_TEST_UNLINK_THREAD    (2) | 
|  | 25 | #define SHM_TEST_TRANS_THREAD     (3) | 
|  | 26 | #define SHM_TEST_THREAD_NUM       (4) | 
|  | 27 | #define SHM_TEST_STR_NUM          (20) | 
|  | 28 | #define SHM_TEST_CAP_INIT_VALUE   (0x16) | 
|  | 29 |  | 
|  | 30 | #define SHM_TEST_MARK_0X1         (0x1) | 
|  | 31 | #define SHM_TEST_MARK_0X3         (0x3) | 
|  | 32 | #define SHM_TEST_MARK_0X5         (0x5) | 
|  | 33 | #define SHM_TEST_MARK_0X7         (0x7) | 
|  | 34 | #define SHM_TEST_MARK_NUM         (0xA) | 
|  | 35 |  | 
|  | 36 | #define SHM_PAGE_SIZE             (4096) | 
|  | 37 | #define SHM_ALLOC_ONE_PAGE_SIZE   (1024) | 
|  | 38 | #define SHM_ALLOC_TWO_PAGE_SIZE   (8100) | 
|  | 39 | #define SHM_ALLOC_FOUR_PAGE_SIZE  (16000) | 
|  | 40 |  | 
|  | 41 | #define SHM_TEST_READ_FROM_SHM_PAGE0 (0x0A) | 
|  | 42 | #define SHM_TEST_WRITE_TO_SHM_PAGE1  (0x1B) | 
|  | 43 | #define SHM_TEST_READ_FROM_SHM_PAGE2 (0x2A) | 
|  | 44 | #define SHM_TEST_WRITE_TO_SHM_PAGE3  (0x3B) | 
|  | 45 |  | 
|  | 46 | #define SHM_IPC_FILE_NAME     "remote-posix_test" | 
|  | 47 | #define SHM_TRANS_FILE_NAME   "remote-posix_transfer" | 
|  | 48 |  | 
|  | 49 | /** | 
|  | 50 | * È«¾Ö±äÁ¿¶¨Òå | 
|  | 51 | */ | 
|  | 52 | pthread_t shm_pcross_thread; | 
|  | 53 | pthread_t shm_unlink_thread; | 
|  | 54 |  | 
|  | 55 | int   g_shmfd[SHM_TEST_THREAD_NUM] = {0}; | 
|  | 56 | char  *g_pshm[SHM_TEST_THREAD_NUM] = {NULL}; | 
|  | 57 | int   g_mark_path[SHM_TEST_MARK_NUM] = {0xFFFF}; | 
|  | 58 |  | 
|  | 59 | void *ap_shm_fourpage_cross(void* args) | 
|  | 60 | { | 
|  | 61 | int   i        = 0; | 
|  | 62 | int   ret      = 0; | 
|  | 63 | int   shmfd    = 0; | 
|  | 64 | char  test_num = 0; | 
|  | 65 | char  *pshm    = NULL; | 
|  | 66 | struct stat buf; | 
|  | 67 |  | 
|  | 68 | prctl(PR_SET_NAME, "shm_posix_trans_task"); | 
|  | 69 |  | 
|  | 70 | shmfd = shm_open(SHM_TRANS_FILE_NAME, O_CREAT | O_RDWR, 0666); | 
|  | 71 | if(shmfd < 0) | 
|  | 72 | { | 
|  | 73 | printf("shm_open return error\n"); | 
|  | 74 | assert(0); | 
|  | 75 | } | 
|  | 76 |  | 
|  | 77 | if(ftruncate(shmfd, SHM_ALLOC_FOUR_PAGE_SIZE) ==  -1) | 
|  | 78 | { | 
|  | 79 | perror("ftruncate failed\n"); | 
|  | 80 | exit(1); | 
|  | 81 | } | 
|  | 82 |  | 
|  | 83 | if (fstat(shmfd, &buf) == -1) | 
|  | 84 | { | 
|  | 85 | printf("fstat error\n"); | 
|  | 86 | } | 
|  | 87 | printf("size = %d, mode = %o\n",buf.st_size, buf.st_mode & 0777); | 
|  | 88 |  | 
|  | 89 | pshm = (char *)mmap(NULL, SHM_ALLOC_FOUR_PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, shmfd, 0); | 
|  | 90 | if (pshm == NULL) | 
|  | 91 | { | 
|  | 92 | perror("mmap failed\n"); | 
|  | 93 | exit(1); | 
|  | 94 | } | 
|  | 95 | g_shmfd[SHM_TEST_TRANS_THREAD] = shmfd; | 
|  | 96 | g_pshm[SHM_TEST_TRANS_THREAD]    = pshm; | 
|  | 97 |  | 
|  | 98 | for(;;) | 
|  | 99 | { | 
|  | 100 | while(1) | 
|  | 101 | { | 
|  | 102 | test_num = *(char *)pshm; | 
|  | 103 | if(test_num == SHM_TEST_READ_FROM_SHM_PAGE0) | 
|  | 104 | { | 
|  | 105 | *(char *)(pshm + SHM_PAGE_SIZE) = SHM_TEST_WRITE_TO_SHM_PAGE1; | 
|  | 106 | g_mark_path[2] =  SHM_TEST_READ_FROM_SHM_PAGE0; | 
|  | 107 | printf("shmat test_num, AP write %x to shm\n",SHM_TEST_WRITE_TO_SHM_PAGE1); | 
|  | 108 | break; | 
|  | 109 | } | 
|  | 110 | sleep(2); | 
|  | 111 | } | 
|  | 112 | while(1) | 
|  | 113 | { | 
|  | 114 | test_num = *(char *)(pshm + 2 * SHM_PAGE_SIZE); | 
|  | 115 | if(test_num == SHM_TEST_READ_FROM_SHM_PAGE2) | 
|  | 116 | { | 
|  | 117 | *(char *)(pshm + 3 * SHM_PAGE_SIZE) = SHM_TEST_WRITE_TO_SHM_PAGE3; | 
|  | 118 | g_mark_path[4] =  SHM_TEST_READ_FROM_SHM_PAGE2; | 
|  | 119 | printf("shmat test_num, AP write %x to shm\n", SHM_TEST_WRITE_TO_SHM_PAGE3); | 
|  | 120 | break; | 
|  | 121 | } | 
|  | 122 | sleep(2); | 
|  | 123 | } | 
|  | 124 | if (i++ >= 10) | 
|  | 125 | break; | 
|  | 126 | } | 
|  | 127 | ret = munmap(pshm, SHM_ALLOC_FOUR_PAGE_SIZE); | 
|  | 128 | if(ret < 0) | 
|  | 129 | { | 
|  | 130 | printf("munmap errno %d: %s\n", errno, strerror(errno)); | 
|  | 131 | exit(1); | 
|  | 132 | } | 
|  | 133 | printf("munmap return ok\n"); | 
|  | 134 |  | 
|  | 135 | ret = shm_unlink(SHM_TRANS_FILE_NAME); | 
|  | 136 | if(ret < 0) | 
|  | 137 | { | 
|  | 138 | printf("shm_unlink errno %d: %s\n", errno, strerror(errno)); | 
|  | 139 | return -1; | 
|  | 140 | } | 
|  | 141 |  | 
|  | 142 | printf("shm_unlink return ok\n"); | 
|  | 143 | close(shmfd); | 
|  | 144 | while(1) | 
|  | 145 | { | 
|  | 146 | sleep(5); | 
|  | 147 | } | 
|  | 148 | } | 
|  | 149 |  | 
|  | 150 | void *ap_write_and_unlink(void* args) | 
|  | 151 | { | 
|  | 152 | int  shmfd = 0; | 
|  | 153 | int  ret   = 0; | 
|  | 154 | char *pshm = NULL; | 
|  | 155 | struct stat buf; | 
|  | 156 | int   test_num = 0; | 
|  | 157 |  | 
|  | 158 | prctl(PR_SET_NAME, "shm_posix_task"); | 
|  | 159 |  | 
|  | 160 | shmfd = shm_open(SHM_IPC_FILE_NAME, O_CREAT | O_RDWR, 0666); | 
|  | 161 | if(shmfd < 0) | 
|  | 162 | { | 
|  | 163 | printf("shm_open return error\n"); | 
|  | 164 | assert(0); | 
|  | 165 | } | 
|  | 166 |  | 
|  | 167 | if(ftruncate(shmfd, SHM_ALLOC_TWO_PAGE_SIZE) ==  -1) | 
|  | 168 | { | 
|  | 169 | perror("ftruncate failed\n"); | 
|  | 170 | exit(1); | 
|  | 171 | } | 
|  | 172 |  | 
|  | 173 | if (fstat(shmfd, &buf) == -1) | 
|  | 174 | { | 
|  | 175 | printf("fstat error\n"); | 
|  | 176 | } | 
|  | 177 | printf("size = %d, mode = %o\n",buf.st_size, buf.st_mode & 0777); | 
|  | 178 |  | 
|  | 179 | pshm = (char *)mmap(NULL, SHM_ALLOC_TWO_PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, shmfd, 0); | 
|  | 180 | if (pshm == MAP_FAILED) | 
|  | 181 | { | 
|  | 182 | perror("mmap failed\n"); | 
|  | 183 | exit(1); | 
|  | 184 | } | 
|  | 185 |  | 
|  | 186 | g_shmfd[SHM_TEST_ALLOC_THREAD] = shmfd; | 
|  | 187 | g_pshm[SHM_TEST_ALLOC_THREAD]  = pshm; | 
|  | 188 |  | 
|  | 189 | while(1) | 
|  | 190 | { | 
|  | 191 | test_num = *(int *)pshm; | 
|  | 192 | if(test_num == SHM_TEST_CAP_INIT_VALUE) | 
|  | 193 | { | 
|  | 194 | *(int *)pshm = SHM_TEST_MARK_0X1 + 1; | 
|  | 195 | g_mark_path[1] = SHM_TEST_MARK_0X1; | 
|  | 196 | printf("shmat test_num 0x16, g_mark_path[1] = %x\n", g_mark_path[1]); | 
|  | 197 | break; | 
|  | 198 | } | 
|  | 199 | sleep(1); | 
|  | 200 | } | 
|  | 201 |  | 
|  | 202 | while(1) | 
|  | 203 | { | 
|  | 204 | test_num = *(int *)pshm; | 
|  | 205 | if(test_num == SHM_TEST_MARK_0X3) | 
|  | 206 | { | 
|  | 207 | *(int *)pshm = SHM_TEST_MARK_0X3 + 1; | 
|  | 208 | g_mark_path[3] = SHM_TEST_MARK_0X3; | 
|  | 209 | printf("shmat test_num 3, g_mark_path[3] before= %x\n", g_mark_path[3]); | 
|  | 210 | break; | 
|  | 211 | } | 
|  | 212 | sleep(1); | 
|  | 213 | } | 
|  | 214 |  | 
|  | 215 | while(1) | 
|  | 216 | { | 
|  | 217 | test_num = *(int *)pshm; | 
|  | 218 | if(test_num == SHM_TEST_MARK_0X5) | 
|  | 219 | { | 
|  | 220 | *(int *)pshm = SHM_TEST_MARK_0X5 + 1; | 
|  | 221 | g_mark_path[5] = SHM_TEST_MARK_0X5; | 
|  | 222 | printf("shmat test_num 5, g_mark_path5 before= %x\n", g_mark_path[5]); | 
|  | 223 | break; | 
|  | 224 | } | 
|  | 225 | sleep(1); | 
|  | 226 | } | 
|  | 227 |  | 
|  | 228 | while(1) | 
|  | 229 | { | 
|  | 230 | test_num = *(int *)pshm; | 
|  | 231 | if(test_num == SHM_TEST_MARK_0X7) | 
|  | 232 | { | 
|  | 233 | *(int *)pshm = SHM_TEST_MARK_0X7 + 1; | 
|  | 234 | g_mark_path[7] = SHM_TEST_MARK_0X7; | 
|  | 235 | printf("shmat test_num 7, g_mark_path7 before= %x\n", g_mark_path[7]); | 
|  | 236 | break; | 
|  | 237 | } | 
|  | 238 | sleep(1); | 
|  | 239 | } | 
|  | 240 | memcpy(pshm + 32, "123456", SHM_TEST_STR_NUM); | 
|  | 241 |  | 
|  | 242 | ret = munmap(pshm, SHM_ALLOC_TWO_PAGE_SIZE); | 
|  | 243 | if(ret < 0) | 
|  | 244 | { | 
|  | 245 | printf("munmap errno %d: %s\n", errno, strerror(errno)); | 
|  | 246 | exit(1); | 
|  | 247 | } | 
|  | 248 | ret = shm_unlink(SHM_IPC_FILE_NAME); | 
|  | 249 | if(ret < 0) | 
|  | 250 | { | 
|  | 251 | printf("shm_unlink errno %d: %s\n", errno, strerror(errno)); | 
|  | 252 | assert(0); | 
|  | 253 | } | 
|  | 254 | close(shmfd); | 
|  | 255 |  | 
|  | 256 | while(1) | 
|  | 257 | { | 
|  | 258 | sleep(5); | 
|  | 259 | } | 
|  | 260 | return 0; | 
|  | 261 | } | 
|  | 262 |  | 
|  | 263 |  | 
|  | 264 | int main (int argc, char *argv[]) | 
|  | 265 | { | 
|  | 266 | int ret = 0; | 
|  | 267 |  | 
|  | 268 | ret = pthread_create(&shm_pcross_thread, NULL, ap_shm_fourpage_cross, NULL); | 
|  | 269 | if(ret != 0) | 
|  | 270 | { | 
|  | 271 | printf("pthread_create shm_pcross_thread error\n"); | 
|  | 272 | } | 
|  | 273 |  | 
|  | 274 | ret = pthread_create(&shm_unlink_thread, NULL, ap_write_and_unlink, NULL); | 
|  | 275 | if(ret != 0) | 
|  | 276 | { | 
|  | 277 | printf("pthread_create shm_unlink_thread error\n"); | 
|  | 278 | } | 
|  | 279 |  | 
|  | 280 | if(shm_pcross_thread > 0) | 
|  | 281 | pthread_join(shm_pcross_thread, NULL); | 
|  | 282 |  | 
|  | 283 | if(shm_unlink_thread > 0) | 
|  | 284 | pthread_join(shm_unlink_thread, NULL); | 
|  | 285 |  | 
|  | 286 | return 0; | 
|  | 287 | } | 
|  | 288 |  |