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
| #include <stdio.h> #include <stdlib.h> #include <string.h> #include <fcntl.h> #include <unistd.h> #include <errno.h> #include <sys/mman.h> #include <sys/stat.h>
typedef struct { int request_id; int event; unsigned int pripority; unsigned int count; } content_t;
typedef struct { int request_id; int event; unsigned int pripority; unsigned int count; char data[4]; } excontent_t;
typedef struct { int fd; size_t size; void *addr; } shm_t;
int file_get_size(int fd, size_t *size) { struct stat st; if (fstat(fd, &st) == -1) { printf("fstat error\r\n"); return -1; }
*size = st.st_size; return 0; }
int get_file_path(const char *name, char **path) { int ret = asprintf(path, "/sub2_%s", name); if (ret == -1) { return -1; }
return 0; }
int remap(shm_t *shm, size_t new_shm_size) { size_t file_size; if (!new_shm_size && (file_get_size(shm->fd, &file_size))) { return -1; }
if ((!new_shm_size && (new_shm_size == file_size)) || (new_shm_size &&new_shm_size == file_size)) { return 0; }
if (shm->addr) { munmap(shm->addr, shm->size); printf("munmap\r\n"); }
if (new_shm_size && (ftruncate(shm->fd, new_shm_size) == -1)) { shm->addr = NULL; printf("ftruncate error\r\n"); return -1; }
shm->size = new_shm_size ? new_shm_size : file_size;
shm->addr = mmap(NULL, shm->size, PROT_READ | PROT_WRITE, MAP_SHARED, shm->fd, 0); if (shm->addr == MAP_FAILED) { shm->addr = NULL; printf("mmap error\r\n"); return -1; } printf("get addr %p\r\n", shm->addr); return 0; }
void clear_map(shm_t *shm) { if (shm->addr) { munmap(shm->addr, shm->size); shm->addr = NULL; }
if (shm->fd > -1) { close(shm->fd); shm->fd = -1; } shm->size = 0; }
int open_map(const char *name, shm_t *shm, size_t struct_size) { int created = 1; char *path;
if (get_file_path(name, &path)) { printf("get_file_path error\r\n"); return -1; }
mode_t um = umask(0); shm->fd = shm_open(path, O_RDWR | O_CREAT | O_EXCL, 00666); umask(um); if ((shm->fd == -1) && (errno == EEXIST)) { created = 0; shm->fd = shm_open(path, O_RDWR, 00666); printf("file exist %s, open agine\r\n", path); } free(path); if (shm->fd == -1) { printf("open map file error\r\n"); return -1; }
if (created) { if (remap(shm, struct_size)) { return -1; }
} else { if (remap(shm, 0)) { return -1; } }
return 0; }
void main() { shm_t first, second; size_t struct_size = sizeof(content_t); memset(&first, 0, sizeof(shm_t)); memset(&second, 0, sizeof(shm_t)); int ret = open_map("hello", &first, struct_size); if (ret < 0) { return; }
content_t *pData = first.addr; pData->count = 100;
ret = open_map("hello", &second, struct_size); if (ret < 0) { clear_map(&first); return; }
pData = second.addr; printf("count is %d\r\n", pData->count);
ret = remap(&second, sizeof(excontent_t)); if (0 == ret) { excontent_t *pex = second.addr; printf("ext count is %d\n", pex->count); }
sleep(10); clear_map(&first); clear_map(&second); }
|