fork로 나온 parent, child와 다르게 완전 다른 프로세스간 shared memory로 데이터를 공유할 수 있다.
pi@raspberrypi:~/Project/cCode/IPC/shared_memory $ cat make_memory.c #include <stdlib.h> #include <stdio.h> #include <errno.h> #include <sys/stat.h> #include <fcntl.h> #include <semaphore.h> #include <sys/stat.h> #include <unistd.h> #include <sys/mman.h> #define FILE_MODE 0666 int main(int argc, char **argv) { int c, fd, flags; char *ptr; off_t length; flags = O_RDWR | O_CREAT; while ( (c = getopt(argc, argv, "e")) != -1) { switch (c) { case 'e': flags |= O_EXCL; break; } } if (optind != argc - 2) perror("usage: shmcreate [ -e ] <name> <length>"); length = atoi(argv[optind + 1]); fd = shm_open(argv[optind], flags, FILE_MODE); ftruncate(fd, length); ptr = mmap(NULL, length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); exit(0); }
pi@raspberrypi:~/Project/cCode/IPC/shared_memory $ cat unlink.c #include <stdlib.h> #include <stdio.h> #include <errno.h> #include <sys/stat.h> #include <fcntl.h> #include <semaphore.h> #include <sys/stat.h> #include <unistd.h> #include <sys/mman.h> #define FILE_MODE 0666 int main(int argc, char **argv) { if (argc != 2) perror("usage: shmunlink <name>"); shm_unlink(argv[1]); exit(0); }
pi@raspberrypi:~/Project/cCode/IPC/shared_memory $ cat write.c #include <stdlib.h> #include <stdio.h> #include <errno.h> #include <sys/stat.h> #include <fcntl.h> #include <semaphore.h> #include <sys/stat.h> #include <unistd.h> #include <sys/mman.h> #define FILE_MODE 0666 int main(int argc, char **argv) { int i, fd; struct stat stat; unsigned char *ptr; if (argc != 2) perror("usage: shmwrite <name>"); /* 4open, get size, map */ fd = shm_open(argv[1], O_RDWR, FILE_MODE); fstat(fd, &stat); ptr = mmap(NULL, stat.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); close(fd); /* 4set: ptr[0] = 0, ptr[1] = 1, etc. */ for (i = 0; i < stat.st_size; i++) *ptr++ = i % 256; exit(0); }
pi@raspberrypi:~/Project/cCode/IPC/shared_memory $ cat read.c #include <stdlib.h> #include <stdio.h> #include <errno.h> #include <sys/stat.h> #include <fcntl.h> #include <semaphore.h> #include <sys/stat.h> #include <unistd.h> #include <sys/mman.h> #define FILE_MODE 0400 int main(int argc, char **argv) { int i, fd; struct stat stat; unsigned char c, *ptr; if (argc != 2) perror("usage: shmread <name>"); /* 4open, get size, map */ fd = shm_open(argv[1], O_RDONLY, FILE_MODE); fstat(fd, &stat); ptr = mmap(NULL, stat.st_size, PROT_READ, MAP_SHARED, fd, 0); close(fd); /* 4check that ptr[0] = 0, ptr[1] = 1, etc. */ for (i = 0; i < stat.st_size; i++) if ( (c = *ptr++) == (i % 256)) printf("ptr[%d] = %d\n", i, c); exit(0); }
이를 실행하면 다음과 같다. 512 길이 메모리를 할당하고, 250~259번지를 보면 다시 0으로 돌아간다. od로 확인하면 255에서 0으로 갔음을 본다. 파일 사용하는 방법과 비슷하다.
pi@raspberrypi:~/Project/cCode/IPC/shared_memory $ ./makemeory test 512 pi@raspberrypi:~/Project/cCode/IPC/shared_memory $ ./write test pi@raspberrypi:~/Project/cCode/IPC/shared_memory $ od -A d -t u1 -t x2 /dev/shm/test 0000000 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0100 0302 0504 0706 0908 0b0a 0d0c 0f0e 0000016 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 1110 1312 1514 1716 1918 1b1a 1d1c 1f1e 0000032 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 2120 2322 2524 2726 2928 2b2a 2d2c 2f2e 0000048 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 3130 3332 3534 3736 3938 3b3a 3d3c 3f3e 0000064 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 4140 4342 4544 4746 4948 4b4a 4d4c 4f4e 0000080 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 5150 5352 5554 5756 5958 5b5a 5d5c 5f5e 0000096 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 6160 6362 6564 6766 6968 6b6a 6d6c 6f6e 0000112 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 7170 7372 7574 7776 7978 7b7a 7d7c 7f7e 0000128 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 8180 8382 8584 8786 8988 8b8a 8d8c 8f8e 0000144 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 9190 9392 9594 9796 9998 9b9a 9d9c 9f9e 0000160 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 a1a0 a3a2 a5a4 a7a6 a9a8 abaa adac afae 0000176 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 b1b0 b3b2 b5b4 b7b6 b9b8 bbba bdbc bfbe 0000192 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 c1c0 c3c2 c5c4 c7c6 c9c8 cbca cdcc cfce 0000208 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 d1d0 d3d2 d5d4 d7d6 d9d8 dbda dddc dfde 0000224 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 e1e0 e3e2 e5e4 e7e6 e9e8 ebea edec efee 0000240 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 f1f0 f3f2 f5f4 f7f6 f9f8 fbfa fdfc fffe 0000256 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0100 0302 0504 0706 0908 0b0a 0d0c 0f0e 0000272 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 1110 1312 1514 1716 1918 1b1a 1d1c 1f1e 0000288 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 2120 2322 2524 2726 2928 2b2a 2d2c 2f2e 0000304 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 3130 3332 3534 3736 3938 3b3a 3d3c 3f3e 0000320 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 4140 4342 4544 4746 4948 4b4a 4d4c 4f4e 0000336 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 5150 5352 5554 5756 5958 5b5a 5d5c 5f5e 0000352 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 6160 6362 6564 6766 6968 6b6a 6d6c 6f6e 0000368 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 7170 7372 7574 7776 7978 7b7a 7d7c 7f7e 0000384 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 8180 8382 8584 8786 8988 8b8a 8d8c 8f8e 0000400 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 9190 9392 9594 9796 9998 9b9a 9d9c 9f9e 0000416 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 a1a0 a3a2 a5a4 a7a6 a9a8 abaa adac afae 0000432 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 b1b0 b3b2 b5b4 b7b6 b9b8 bbba bdbc bfbe 0000448 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 c1c0 c3c2 c5c4 c7c6 c9c8 cbca cdcc cfce 0000464 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 d1d0 d3d2 d5d4 d7d6 d9d8 dbda dddc dfde 0000480 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 e1e0 e3e2 e5e4 e7e6 e9e8 ebea edec efee 0000496 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 f1f0 f3f2 f5f4 f7f6 f9f8 fbfa fdfc fffe 0000512 pi@raspberrypi:~/Project/cCode/IPC/shared_memory $ ./read test | grep -e "ptr\[25[0-9]\]" ptr[250] = 250 ptr[251] = 251 ptr[252] = 252 ptr[253] = 253 ptr[254] = 254 ptr[255] = 255 ptr[256] = 0 ptr[257] = 1 ptr[258] = 2 ptr[259] = 3