{"id":3858,"date":"2020-07-17T18:47:20","date_gmt":"2020-07-17T09:47:20","guid":{"rendered":"https:\/\/now0930.pe.kr\/wordpress\/?p=3858"},"modified":"2020-07-23T09:55:03","modified_gmt":"2020-07-23T00:55:03","slug":"memory-based-semaphore-unix-network-programming-p241","status":"publish","type":"post","link":"https:\/\/now0930.pe.kr\/wordpress\/memory-based-semaphore-unix-network-programming-p241\/","title":{"rendered":"memory based semaphore, unix network programming p241"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\uc774\ub984\uc788\ub294 \uc138\ub9c8\ud3ec\uc5b4 \ub9d0\uace0 \uba54\ubaa8\ub9ac\uc5d0 \uc62c\ub824 \uc0ac\uc6a9\ud558\ub294 \ubc29\ubc95\ub3c4 \uc788\ub2e4. \ucc45\uc740 sem_init(&#8230;, \ub450\ubc88\uc9f8 arg,&#8230;)\uc5d0\uc11c \ub450\ubc88\uc9f8 \uc785\ub825\ud558\ub294 \uc22b\uc790\ub97c thread\uc5d0\uc11c \uacf5\uc720\ud560 \uac70\uba74 0, \ud504\ub85c\uc138\uc2a4\uac04 \ud560\uac70\uba74 1\ub85c \ud558\ub77c\uace0 \ud55c\ub2e4. 1\ub85c \ud558\uc5ec thread\ub3c4 \uc798 \ub41c\ub2e4.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">named semaphore\ub294 \ud3ec\uc778\ud130\ub97c \uc120\uc5b8\ud588\uace0 \ud30c\uc77c\uc5d0 \uc601\uc5ed\uc744 \ub9cc\ub4e4\uc5c8\ub2e4. \uc774\uc640 \ub2e4\ub974\uac8c \uc9c1\uc811 \ub370\uc774\ud130\ub97c \uc120\uc5b8\ud558\uace0 \uc8fc\uc18c\ub85c \ub118\uaca8\uc918\uc57c \ud55c\ub2e4.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">ipcs\ub85c \ubcf4\uc77c \ubc95 \ud55c\ub370, \uc548 \ubcf4\uc778\ub2e4. \ubb34\uc5c7\uc774 \uc798\ubabb\ub418\uc5c8\ub294\uc9c0 \ubaa8\ub974\uaca0\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 main *\/\n#include &lt;pthread.h>\n#include &lt;stdio.h>\n#include &lt;stdlib.h>\n#include &lt;fcntl.h>\n#include &lt;semaphore.h>\n#include &lt;sys\/stat.h>\n#include &lt;unistd.h>\n#define\tNBUFF\t 10\n#define\tSEM_MUTEX\t\"mutex\"\t \t\/* these are args to px_ipc_name() *\/\n#define\tSEM_NEMPTY\t\"empty\"\n#define\tSEM_NSTORED\t\"nstored\"\n\nint\t\tnitems;\t\t\t\t\t\/* read-only by producer and consumer *\/\nstruct {\t\/* data shared by producer and consumer *\/\n  int\tbuff[NBUFF];\n  \/\/\ud3ec\uc778\ud130 \ub300\uc2e0 \uc138\ub9c8\ud3ec\uc5b4\ub85c \ubcc0\uacbd.\n  \/\/sem_t\t*mutex, *nempty, *nstored;\n  sem_t\tmutex, nempty, nstored;\t\t\/* semaphores, not pointers *\/\n\n} shared;\n\nvoid\t*produce(void *), *consume(void *);\n\nint\nmain(int argc, char **argv)\n{\n\tpthread_t\ttid_produce, tid_consume;\n\n\tif (argc != 2)\n\t\tperror(\"usage: prodcons1 &lt;#items>\");\n\tnitems = atoi(argv[1]);\n\t\t\/* 4create three semaphores *\/\n\t\/\/memory based\ub85c \ubcc0\uacbd.\n\t\/*\n\tshared.mutex = sem_open(SEM_MUTEX, O_CREAT | O_EXCL,\n\t\t\t\t\t\t\t0660, 1);\n\tshared.nempty = sem_open(SEM_NEMPTY, O_CREAT | O_EXCL,\n\t\t\t\t\t\t\t 0660, NBUFF);\n\tshared.nstored = sem_open(SEM_NSTORED, O_CREAT | O_EXCL,\n\t\t\t\t\t\t\t  0660, 0);\n\t\t\t\t\t\t\t  *\/\n\n\n\t\/\/\ub450\ubc88\uc9f8 \ud30c\ub77c\ubbf8\ud130\ub97c 0\uc73c\ub85c \uc124\uc815\ud574\uc57c thread\uac04 \uc804\ub2ec.\n\t\/\/sem_init(&amp;shared.mutex, 1, 1) \uc124\uc815\ud574\ub3c4 \uc798 \ub428..\n\n\tsem_init(&amp;shared.mutex, 0, 1);\n\tsem_init(&amp;shared.nempty, 0, NBUFF);\n\tsem_init(&amp;shared.nstored, 0, 0);\n\n\n\t\t\/* 4create one producer thread and one consumer thread *\/\n\t\/\/pthread_setconcurrency(2);\n\t\/\/pthread_setconcurrency();\n\tpthread_create(&amp;tid_produce, NULL, produce, NULL);\n\tpthread_create(&amp;tid_consume, NULL, consume, NULL);\n\n\t\t\/* 4wait for the two threads *\/\n\tpthread_join(tid_produce, NULL);\n\tpthread_join(tid_consume, NULL);\n\n\t\t\/* 4remove the semaphores *\/\n\t\/\/\uba54\ubaa8\ub9ac \ubca0\uc774\uc2a4\ub85c \ubcc0\uacbd.\n\t\/*\n\tsem_unlink(SEM_MUTEX);\n\tsem_unlink(SEM_NEMPTY);\n\tsem_unlink(SEM_NSTORED);\n\t*\/\n\n\n\t\/\/ipcs\ub85c \ud655\uc778\ud558\uae30\uc704\ud574 \ub300\uae30\n\tsleep(10);\n\n\tsem_destroy(&amp;shared.mutex);\n\tsem_destroy(&amp;shared.nempty);\n\tsem_destroy(&amp;shared.nstored);\n\n\texit(0);\n}\n\/* end main *\/\n\n\/* include prodcons *\/\nvoid *\nproduce(void *arg)\n{\n\tint\t\ti;\n\n\tfor (i = 0; i &lt; nitems; i++) {\n\n\t\tsem_wait(&amp;shared.nempty);\t\/* wait for at least 1 empty slot *\/\n\t\tsem_wait(&amp;shared.mutex);\n\n\t\tshared.buff[i % NBUFF] = i;\t\/* store i into circular buffer *\/\n\t\tsem_post(&amp;shared.mutex);\n\t\tsem_post(&amp;shared.nstored);\t\/* 1 more stored item *\/\n\n\n\t}\n\treturn(NULL);\n}\n\nvoid *\nconsume(void *arg)\n{\n\tint\t\ti;\n\tint tmpnempty, tmpmutex, tmpnstored;\n\n\tfor (i = 0; i &lt; nitems; i++) {\n\n\n\t\tsem_wait(&amp;shared.nstored);\t\t\/* wait for at least 1 stored item *\/\n\t\tsem_wait(&amp;shared.mutex);\n\n\n\t\tif (shared.buff[i % NBUFF] = i)\n\t\t\tprintf(\"buff[%d] = %d\\n\", i, shared.buff[i % NBUFF]);\n\t\tsem_post(&amp;shared.mutex);\n\t\tsem_post(&amp;shared.nempty);\t\t\/* 1 more empty slot *\/\n\n\t}\n\treturn(NULL);\n}\n\/* end prodcons *\/\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 $ ipcs\n\n------ Message Queues --------\nkey        msqid      owner      perms      used-bytes   messages    \n\n------ Shared Memory Segments --------\nkey        shmid      owner      perms      bytes      nattch     status      \n\n------ Semaphore Arrays --------\nkey        semid      owner      perms      nsems     \n\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">ipcs\ub294 system V \uae30\ub2a5\uc774\ub77c posix semaphore\ub294 \ud574\ub2f9\ud558\uc9c0 \uc54a\ub294\ub2e4. \ubcf4\uc774\uc9c0 \uc54a\uc74c\uc774 \ub2f9\uc5f0\ud558\ub2e4.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\uc774\ub984\uc788\ub294 \uc138\ub9c8\ud3ec\uc5b4 \ub9d0\uace0 \uba54\ubaa8\ub9ac\uc5d0 \uc62c\ub824 \uc0ac\uc6a9\ud558\ub294 \ubc29\ubc95\ub3c4 \uc788\ub2e4. \ucc45\uc740 sem_init(&#8230;, \ub450\ubc88\uc9f8 arg,&#8230;)\uc5d0\uc11c \ub450\ubc88\uc9f8 \uc785\ub825\ud558\ub294 \uc22b\uc790\ub97c thread\uc5d0\uc11c \uacf5\uc720\ud560 \uac70\uba74 0, \ud504\ub85c\uc138\uc2a4\uac04 \ud560\uac70\uba74 1\ub85c \ud558\ub77c\uace0 \ud55c\ub2e4. 1\ub85c \ud558\uc5ec thread\ub3c4 \uc798 \ub41c\ub2e4. named semaphore\ub294 \ud3ec\uc778\ud130\ub97c \uc120\uc5b8\ud588\uace0 \ud30c\uc77c\uc5d0 \uc601\uc5ed\uc744 \ub9cc\ub4e4\uc5c8\ub2e4. \uc774\uc640 \ub2e4\ub974\uac8c \uc9c1\uc811 \ub370\uc774\ud130\ub97c \uc120\uc5b8\ud558\uace0 \uc8fc\uc18c\ub85c \ub118\uaca8\uc918\uc57c \ud55c\ub2e4. ipcs\ub85c \ubcf4\uc77c \ubc95 \ud55c\ub370, \uc548 \ubcf4\uc778\ub2e4. \ubb34\uc5c7\uc774 \uc798\ubabb\ub418\uc5c8\ub294\uc9c0 \ubaa8\ub974\uaca0\ub2e4. ipcs\ub294 system [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[12],"tags":[775,53,781,784,786],"class_list":["post-3858","post","type-post","status-publish","format-standard","hentry","category-12","tag-c","tag-linux","tag-network-program","tag-pthread","tag-semaphore"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/posts\/3858","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=3858"}],"version-history":[{"count":3,"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/posts\/3858\/revisions"}],"predecessor-version":[{"id":3877,"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/posts\/3858\/revisions\/3877"}],"wp:attachment":[{"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/media?parent=3858"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/categories?post=3858"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/tags?post=3858"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}