{"id":3869,"date":"2020-07-22T08:09:01","date_gmt":"2020-07-21T23:09:01","guid":{"rendered":"https:\/\/now0930.pe.kr\/wordpress\/?p=3869"},"modified":"2020-07-22T08:10:34","modified_gmt":"2020-07-21T23:10:34","slug":"posix-shared-memory-p325p335","status":"publish","type":"post","link":"https:\/\/now0930.pe.kr\/wordpress\/posix-shared-memory-p325p335\/","title":{"rendered":"posix shared memory, p325~p335"},"content":{"rendered":"\n<p>fork\ub85c \ub098\uc628 parent, child\uc640 \ub2e4\ub974\uac8c \uc644\uc804 \ub2e4\ub978 \ud504\ub85c\uc138\uc2a4\uac04 shared memory\ub85c \ub370\uc774\ud130\ub97c \uacf5\uc720\ud560 \uc218 \uc788\ub2e4.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">pi@raspberrypi:~\/Project\/cCode\/IPC\/shared_memory $ cat make_memory.c \n#include &lt;stdlib.h>\n#include &lt;stdio.h>\n#include &lt;errno.h>\n#include &lt;sys\/stat.h>\n#include &lt;fcntl.h>\n#include &lt;semaphore.h>\n#include &lt;sys\/stat.h>\n#include &lt;unistd.h>\n#include &lt;sys\/mman.h>\n#define FILE_MODE 0666\n\nint\nmain(int argc, char **argv)\n{\n\tint\t\tc, fd, flags;\n\tchar\t*ptr;\n\toff_t\tlength;\n\n\tflags = O_RDWR | O_CREAT;\n\twhile ( (c = getopt(argc, argv, \"e\")) != -1) {\n\t\tswitch (c) {\n\t\tcase 'e':\n\t\t\tflags |= O_EXCL;\n\t\t\tbreak;\n\t\t}\n\t}\n\tif (optind != argc - 2)\n\t\tperror(\"usage: shmcreate [ -e ] &lt;name> &lt;length>\");\n\tlength = atoi(argv[optind + 1]);\n\n\tfd = shm_open(argv[optind], flags, FILE_MODE);\n\tftruncate(fd, length);\n\n\tptr = mmap(NULL, length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);\n\n\texit(0);\n}<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">pi@raspberrypi:~\/Project\/cCode\/IPC\/shared_memory $ cat unlink.c \n\n#include &lt;stdlib.h>\n#include &lt;stdio.h>\n#include &lt;errno.h>\n#include &lt;sys\/stat.h>\n#include &lt;fcntl.h>\n#include &lt;semaphore.h>\n#include &lt;sys\/stat.h>\n#include &lt;unistd.h>\n#include &lt;sys\/mman.h>\n#define FILE_MODE 0666\n\n\n\nint\nmain(int argc, char **argv)\n{\n\tif (argc != 2)\n\t\tperror(\"usage: shmunlink &lt;name>\");\n\n\tshm_unlink(argv[1]);\n\n\texit(0);\n}<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">pi@raspberrypi:~\/Project\/cCode\/IPC\/shared_memory $ cat write.c \n#include &lt;stdlib.h>\n#include &lt;stdio.h>\n#include &lt;errno.h>\n#include &lt;sys\/stat.h>\n#include &lt;fcntl.h>\n#include &lt;semaphore.h>\n#include &lt;sys\/stat.h>\n#include &lt;unistd.h>\n#include &lt;sys\/mman.h>\n#define FILE_MODE 0666\n\nint\nmain(int argc, char **argv)\n{\n\tint\t\ti, fd;\n\tstruct stat\tstat;\n\tunsigned char\t*ptr;\n\n\tif (argc != 2)\n\t\tperror(\"usage: shmwrite &lt;name>\");\n\n\t\t\/* 4open, get size, map *\/\n\tfd = shm_open(argv[1], O_RDWR, FILE_MODE);\n\tfstat(fd, &amp;stat);\n\tptr = mmap(NULL, stat.st_size, PROT_READ | PROT_WRITE,\n\t\t\t   MAP_SHARED, fd, 0);\n\tclose(fd);\n\n\t\t\/* 4set: ptr[0] = 0, ptr[1] = 1, etc. *\/\n\tfor (i = 0; i &lt; stat.st_size; i++)\n\t\t*ptr++ = i % 256;\n\n\texit(0);\n}<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">pi@raspberrypi:~\/Project\/cCode\/IPC\/shared_memory $ cat read.c \n#include &lt;stdlib.h>\n#include &lt;stdio.h>\n#include &lt;errno.h>\n#include &lt;sys\/stat.h>\n#include &lt;fcntl.h>\n#include &lt;semaphore.h>\n#include &lt;sys\/stat.h>\n#include &lt;unistd.h>\n#include &lt;sys\/mman.h>\n#define FILE_MODE 0400\n\nint\nmain(int argc, char **argv)\n{\n\tint\t\ti, fd;\n\tstruct stat\tstat;\n\tunsigned char\tc, *ptr;\n\n\tif (argc != 2)\n\t\tperror(\"usage: shmread &lt;name>\");\n\n\t\t\/* 4open, get size, map *\/\n\tfd = shm_open(argv[1], O_RDONLY, FILE_MODE);\n\tfstat(fd, &amp;stat);\n\tptr = mmap(NULL, stat.st_size, PROT_READ,\n\t\t\t   MAP_SHARED, fd, 0);\n\tclose(fd);\n\n\t\t\/* 4check that ptr[0] = 0, ptr[1] = 1, etc. *\/\n\tfor (i = 0; i &lt; stat.st_size; i++)\n\t\tif ( (c = *ptr++) == (i % 256))\n\t\t\tprintf(\"ptr[%d] = %d\\n\", i, c);\n\n\texit(0);\n}<\/pre>\n\n\n\n<p>\uc774\ub97c \uc2e4\ud589\ud558\uba74 \ub2e4\uc74c\uacfc \uac19\ub2e4. 512 \uae38\uc774 \uba54\ubaa8\ub9ac\ub97c \ud560\ub2f9\ud558\uace0, 250~259\ubc88\uc9c0\ub97c \ubcf4\uba74 \ub2e4\uc2dc 0\uc73c\ub85c \ub3cc\uc544\uac04\ub2e4. od\ub85c \ud655\uc778\ud558\uba74 255\uc5d0\uc11c 0\uc73c\ub85c \uac14\uc74c\uc744 \ubcf8\ub2e4. \ud30c\uc77c \uc0ac\uc6a9\ud558\ub294 \ubc29\ubc95\uacfc \ube44\uc2b7\ud558\ub2e4.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">pi@raspberrypi:~\/Project\/cCode\/IPC\/shared_memory $ .\/makemeory test 512\npi@raspberrypi:~\/Project\/cCode\/IPC\/shared_memory $ .\/write test\npi@raspberrypi:~\/Project\/cCode\/IPC\/shared_memory $ od -A d -t u1 -t x2 \/dev\/shm\/test \n0000000   0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15\n           0100    0302    0504    0706    0908    0b0a    0d0c    0f0e\n0000016  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31\n           1110    1312    1514    1716    1918    1b1a    1d1c    1f1e\n0000032  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47\n           2120    2322    2524    2726    2928    2b2a    2d2c    2f2e\n0000048  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63\n           3130    3332    3534    3736    3938    3b3a    3d3c    3f3e\n0000064  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79\n           4140    4342    4544    4746    4948    4b4a    4d4c    4f4e\n0000080  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95\n           5150    5352    5554    5756    5958    5b5a    5d5c    5f5e\n0000096  96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111\n           6160    6362    6564    6766    6968    6b6a    6d6c    6f6e\n0000112 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127\n           7170    7372    7574    7776    7978    7b7a    7d7c    7f7e\n0000128 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143\n           8180    8382    8584    8786    8988    8b8a    8d8c    8f8e\n0000144 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159\n           9190    9392    9594    9796    9998    9b9a    9d9c    9f9e\n0000160 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175\n           a1a0    a3a2    a5a4    a7a6    a9a8    abaa    adac    afae\n0000176 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191\n           b1b0    b3b2    b5b4    b7b6    b9b8    bbba    bdbc    bfbe\n0000192 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207\n           c1c0    c3c2    c5c4    c7c6    c9c8    cbca    cdcc    cfce\n0000208 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223\n           d1d0    d3d2    d5d4    d7d6    d9d8    dbda    dddc    dfde\n0000224 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239\n           e1e0    e3e2    e5e4    e7e6    e9e8    ebea    edec    efee\n0000240 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255\n           f1f0    f3f2    f5f4    f7f6    f9f8    fbfa    fdfc    fffe\n0000256   0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15\n           0100    0302    0504    0706    0908    0b0a    0d0c    0f0e\n0000272  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31\n           1110    1312    1514    1716    1918    1b1a    1d1c    1f1e\n0000288  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47\n           2120    2322    2524    2726    2928    2b2a    2d2c    2f2e\n0000304  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63\n           3130    3332    3534    3736    3938    3b3a    3d3c    3f3e\n0000320  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79\n           4140    4342    4544    4746    4948    4b4a    4d4c    4f4e\n0000336  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95\n           5150    5352    5554    5756    5958    5b5a    5d5c    5f5e\n0000352  96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111\n           6160    6362    6564    6766    6968    6b6a    6d6c    6f6e\n0000368 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127\n           7170    7372    7574    7776    7978    7b7a    7d7c    7f7e\n0000384 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143\n           8180    8382    8584    8786    8988    8b8a    8d8c    8f8e\n0000400 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159\n           9190    9392    9594    9796    9998    9b9a    9d9c    9f9e\n0000416 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175\n           a1a0    a3a2    a5a4    a7a6    a9a8    abaa    adac    afae\n0000432 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191\n           b1b0    b3b2    b5b4    b7b6    b9b8    bbba    bdbc    bfbe\n0000448 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207\n           c1c0    c3c2    c5c4    c7c6    c9c8    cbca    cdcc    cfce\n0000464 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223\n           d1d0    d3d2    d5d4    d7d6    d9d8    dbda    dddc    dfde\n0000480 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239\n           e1e0    e3e2    e5e4    e7e6    e9e8    ebea    edec    efee\n0000496 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255\n           f1f0    f3f2    f5f4    f7f6    f9f8    fbfa    fdfc    fffe\n0000512\npi@raspberrypi:~\/Project\/cCode\/IPC\/shared_memory $ .\/read test | grep -e \"ptr\\[25[0-9]\\]\"\nptr[250] = 250\nptr[251] = 251\nptr[252] = 252\nptr[253] = 253\nptr[254] = 254\nptr[255] = 255\nptr[256] = 0\nptr[257] = 1\nptr[258] = 2\nptr[259] = 3<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>fork\ub85c \ub098\uc628 parent, child\uc640 \ub2e4\ub974\uac8c \uc644\uc804 \ub2e4\ub978 \ud504\ub85c\uc138\uc2a4\uac04 shared memory\ub85c \ub370\uc774\ud130\ub97c \uacf5\uc720\ud560 \uc218 \uc788\ub2e4. \uc774\ub97c \uc2e4\ud589\ud558\uba74 \ub2e4\uc74c\uacfc \uac19\ub2e4. 512 \uae38\uc774 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[12],"tags":[775,53,781,786,787],"class_list":["post-3869","post","type-post","status-publish","format-standard","hentry","category-12","tag-c","tag-linux","tag-network-program","tag-semaphore","tag-shared-memory"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/posts\/3869","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/comments?post=3869"}],"version-history":[{"count":2,"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/posts\/3869\/revisions"}],"predecessor-version":[{"id":3871,"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/posts\/3869\/revisions\/3871"}],"wp:attachment":[{"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/media?parent=3869"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/categories?post=3869"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/tags?post=3869"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}