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_NAME_SIZE (128) |
| 23 | #define SHM_TEXT_SIZE (8192) |
| 24 | #define SHM_PAGE_SIZE (4096) |
| 25 | #define SHM_ALLOC_ONE_PAGE_SIZE (1024) |
| 26 | #define SHM_ALLOC_TWO_PAGE_SIZE (8100) |
| 27 | #define SHM_ALLOC_FOUR_PAGE_SIZE (16000) |
| 28 | #define SHM_MIN(x,y) ((x)<(y)?(x):(y)) |
| 29 | |
| 30 | typedef struct |
| 31 | { |
| 32 | unsigned int shm_size; |
| 33 | char name[SHM_NAME_SIZE]; |
| 34 | }shm_node_t; |
| 35 | |
| 36 | typedef struct |
| 37 | { |
| 38 | int write; |
| 39 | char data[SHM_TEXT_SIZE]; |
| 40 | }shm_use_remote; |
| 41 | |
| 42 | shm_node_t shm_node; |
| 43 | |
| 44 | int main (int argc, char *argv[]) |
| 45 | { |
| 46 | int opt; |
| 47 | int ret = 0; |
| 48 | int copy_size = 0; |
| 49 | unsigned int shm_size = 0; |
| 50 | int shmfd = 0; |
| 51 | char *name = NULL; |
| 52 | char *pshm = NULL; |
| 53 | char *write_data = NULL; |
| 54 | extern char *optarg; |
| 55 | shm_use_remote *shared; |
| 56 | |
| 57 | while ((opt = getopt(argc,argv,"n:d:s::")) != EOF) |
| 58 | { |
| 59 | switch (opt) |
| 60 | { |
| 61 | case 'n': |
| 62 | name = optarg; |
| 63 | break; |
| 64 | case 'd': |
| 65 | write_data = optarg; |
| 66 | break; |
| 67 | case 's': |
| 68 | shm_size = atoi(optarg); |
| 69 | break; |
| 70 | default: |
| 71 | break; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | if (!name || !write_data) |
| 76 | { |
| 77 | printf("param error: you can input \"shm_posix_write -n \"remote-shm_test\" -d \"123456\" -s1024 \n"); |
| 78 | printf("or \"shm_posix_write -n \"remote-shm_test\" -d \"123456\" \n"); |
| 79 | printf("-k -200: create a shm using name remote-shm_test\n"); |
| 80 | printf("-d \"123456\": the data write to shm: for example, if we write 123456 to shm, then can read 123456 form shm, the test case is pass! \n"); |
| 81 | printf("-s1024: set the size of shm, then the default size is 1024 \n"); |
| 82 | return -1; |
| 83 | } |
| 84 | if (shm_size == 0) |
| 85 | shm_size = SHM_ALLOC_ONE_PAGE_SIZE; |
| 86 | |
| 87 | printf("AP write test: name is %s, shm_size is %d\n", name, shm_size); |
| 88 | |
| 89 | shmfd = shm_open(name, O_CREAT | O_RDWR, 0666); |
| 90 | if(shmfd < 0) |
| 91 | { |
| 92 | printf("shm_open return error\n"); |
| 93 | assert(0); |
| 94 | } |
| 95 | printf("AP write test: shm_open shmfd %d\n",shmfd); |
| 96 | |
| 97 | if(ftruncate(shmfd, shm_size) == -1) |
| 98 | { |
| 99 | perror("ftruncate failed\n"); |
| 100 | exit(1); |
| 101 | } |
| 102 | |
| 103 | pshm = (char *)mmap(NULL, shm_size, PROT_READ|PROT_WRITE, MAP_SHARED, shmfd, 0); |
| 104 | if (pshm == MAP_FAILED) |
| 105 | { |
| 106 | perror("mmap failed\n"); |
| 107 | exit(1); |
| 108 | } |
| 109 | shm_node.shm_size = shm_size; |
| 110 | strncpy(shm_node.name, name, SHM_NAME_SIZE-1); |
| 111 | |
| 112 | printf("AP write test : mmap return ok\n"); |
| 113 | shared = (shm_use_remote *)pshm; |
| 114 | copy_size = SHM_MIN(strlen(write_data), (SHM_TEXT_SIZE - 1)); |
| 115 | |
| 116 | printf("AP write test : shared->write %d\n",shared->write); |
| 117 | |
| 118 | while(1) |
| 119 | { |
| 120 | if(shared->write == 1) |
| 121 | { |
| 122 | strncpy(shared->data, write_data, copy_size); |
| 123 | printf("AP write %s to shm\n", write_data); |
| 124 | shared->write = 0; |
| 125 | break; |
| 126 | } |
| 127 | else{ |
| 128 | sleep(2); |
| 129 | } |
| 130 | } |
| 131 | ret = munmap(pshm, shm_size); |
| 132 | if(ret < 0) |
| 133 | { |
| 134 | printf("munmap errno %d: %s\n", errno, strerror(errno)); |
| 135 | exit(1); |
| 136 | } |
| 137 | |
| 138 | ret = shm_unlink(name); |
| 139 | if(ret < 0) |
| 140 | { |
| 141 | printf("shm_unlink errno %d: %s\n", errno, strerror(errno)); |
| 142 | } |
| 143 | close(shmfd); |
| 144 | |
| 145 | printf("shm posix write end\n"); |
| 146 | |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | |