{"id":3865,"date":"2020-07-21T23:56:11","date_gmt":"2020-07-21T14:56:11","guid":{"rendered":"https:\/\/now0930.pe.kr\/wordpress\/?p=3865"},"modified":"2020-07-21T23:56:49","modified_gmt":"2020-07-21T14:56:49","slug":"shared-memory-introduction-p303-p315","status":"publish","type":"post","link":"https:\/\/now0930.pe.kr\/wordpress\/shared-memory-introduction-p303-p315\/","title":{"rendered":"shared memory introduction, p303 ~ p315"},"content":{"rendered":"\n<p>semaphore\ub85c process\uac04 \ub370\uc774\ud130\ub97c \uc8fc\uace0 \ubc1b\uc744 \uc904 \uc54c\uc558\ub294\ub370, \uc544\ub2c8\uc5c8\ub2e4. semaphore\ub85c \ub3d9\uae30\ud558\uace0 process\uac04 \ub370\uc774\ud130 \uc804\ub2ec\uc740 \uba54\ubaa8\ub9ac \uacf5\uc720\ub85c \ud55c\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=\"\">#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\tSEM_NAME\t\"mysem\"\n#define FILE_MODE 0666\nstruct shared{\n\tsem_t mutex;\n\tint count;\n} shared;\n\nint\nmain(int argc, char **argv)\n{\n\tint\t\tfd, i, nloop, zero = 0;\n\t\/\/int\t\t*ptr;\n\t\/\/sem_t\t*mutex;\n\tstruct shared *ptr;\n\n\tif (argc != 3)\n\t\tperror(\"usage: incr2 &lt;pathname> &lt;#loops>\");\n\tnloop = atoi(argv[2]);\n\n\t\t\/* 4open file, initialize to 0, map into memory *\/\n\tfd = open(argv[1], O_RDWR | O_CREAT, FILE_MODE);\n\twrite(fd, &amp;shared, sizeof(struct shared));\n\tptr = mmap(NULL, sizeof(struct shared), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);\n\tclose(fd);\n\n\t\t\/* 4create, initialize, and unlink semaphore *\/\n\t\/\/mutex = sem_open(SEM_NAME, O_CREAT | O_EXCL, FILE_MODE, 1);\n\tsem_init(&amp;ptr->mutex,1,1);\n\n\n\tsem_unlink(SEM_NAME);\n\n\tsetbuf(stdout, NULL);\t\/* stdout is unbuffered *\/\n\tif (fork() == 0) {\t\t\/* child *\/\n\t\tfor (i = 0; i &lt; nloop; i++) {\n\t\t\tsem_wait(&amp;ptr->mutex);\n\t\t\tprintf(\"child: %d\\n\", (ptr->count)++);\n\t\t\tsem_post(&amp;ptr->mutex);\n\t\t}\n\t\texit(0);\n\t}\n\n\t\t\/* 4parent *\/\n\tfor (i = 0; i &lt; nloop; i++) {\n\t\tsem_wait(&amp;ptr->mutex);\n\t\tprintf(\"parent: %d\\n\", ptr->count++);\n\t\tsem_post(&amp;ptr->mutex);\n\t}\n\texit(0);\n}\n<\/pre>\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 $ .\/a.out tt 100\nparent: 0\nparent: 1\nparent: 2\nparent: 3\nparent: 4\nparent: 5\nchild: 6\nchild: 7\nchild: 8\nchild: 9\nchild: 10\nchild: 11\nchild: 12\nchild: 13\nchild: 14\nchild: 15\nchild: 16\nchild: 17\nchild: 18\nchild: 19\nchild: 20\nchild: 21\nchild: 22\nchild: 23\nchild: 24\nchild: 25\nchild: 26\nchild: 27\nchild: 28\nchild: 29\nchild: 30\nchild: 31\nchild: 32\nchild: 33\nchild: 34\nchild: 35\nchild: 36\nchild: 37\nchild: 38\nchild: 39\nchild: 40\nchild: 41\nchild: 42\nchild: 43\nchild: 44\nchild: 45\nchild: 46\nchild: 47\nchild: 48\nchild: 49\nchild: 50\nchild: 51\nchild: 52\nchild: 53\nchild: 54\nchild: 55\nchild: 56\nchild: 57\nchild: 58\nchild: 59\nchild: 60\nchild: 61\nchild: 62\nchild: 63\nchild: 64\nchild: 65\nchild: 66\nchild: 67\nchild: 68\nchild: 69\nchild: 70\nchild: 71\nchild: 72\nchild: 73\nchild: 74\nchild: 75\nchild: 76\nchild: 77\nchild: 78\nchild: 79\nchild: 80\nchild: 81\nchild: 82\nchild: 83\nchild: 84\nchild: 85\nchild: 86\nchild: 87\nchild: 88\nchild: 89\nchild: 90\nchild: 91\nchild: 92\nchild: 93\nchild: 94\nchild: 95\nchild: 96\nchild: 97\nchild: 98\nchild: 99\nchild: 100\nchild: 101\nchild: 102\nchild: 103\nchild: 104\nchild: 105\nparent: 106\nparent: 107\nparent: 108\nparent: 109\nparent: 110\nparent: 111\nparent: 112\nparent: 113\nparent: 114\nparent: 115\nparent: 116\nparent: 117\nparent: 118\nparent: 119\nparent: 120\nparent: 121\nparent: 122\nparent: 123\nparent: 124\nparent: 125\nparent: 126\nparent: 127\nparent: 128\nparent: 129\nparent: 130\nparent: 131\nparent: 132\nparent: 133\nparent: 134\nparent: 135\nparent: 136\nparent: 137\nparent: 138\nparent: 139\nparent: 140\nparent: 141\nparent: 142\nparent: 143\nparent: 144\nparent: 145\nparent: 146\nparent: 147\nparent: 148\nparent: 149\nparent: 150\nparent: 151\nparent: 152\nparent: 153\nparent: 154\nparent: 155\nparent: 156\nparent: 157\nparent: 158\nparent: 159\nparent: 160\nparent: 161\nparent: 162\nparent: 163\nparent: 164\nparent: 165\nparent: 166\nparent: 167\nparent: 168\nparent: 169\nparent: 170\nparent: 171\nparent: 172\nparent: 173\nparent: 174\nparent: 175\nparent: 176\nparent: 177\nparent: 178\nparent: 179\nparent: 180\nparent: 181\nparent: 182\nparent: 183\nparent: 184\nparent: 185\nparent: 186\nparent: 187\nparent: 188\nparent: 189\nparent: 190\nparent: 191\nparent: 192\nparent: 193\nparent: 194\nparent: 195\nparent: 196\nparent: 197\nparent: 198\nparent: 199\n\n\npi@raspberrypi:~\/Project\/cCode\/IPC $ od -A d -t d tt\n0000000           2         128           0           0\n0000016         200\n000002<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>semaphore\ub85c process\uac04 \ub370\uc774\ud130\ub97c \uc8fc\uace0 \ubc1b\uc744 \uc904 \uc54c\uc558\ub294\ub370, \uc544\ub2c8\uc5c8\ub2e4. semaphore\ub85c \ub3d9\uae30\ud558\uace0 process\uac04 \ub370\uc774\ud130 \uc804\ub2ec\uc740 \uba54\ubaa8\ub9ac \uacf5\uc720\ub85c \ud55c\ub2e4.<\/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-3865","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\/3865","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=3865"}],"version-history":[{"count":2,"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/posts\/3865\/revisions"}],"predecessor-version":[{"id":3867,"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/posts\/3865\/revisions\/3867"}],"wp:attachment":[{"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/media?parent=3865"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/categories?post=3865"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/tags?post=3865"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}