{"id":3823,"date":"2020-07-14T10:53:26","date_gmt":"2020-07-14T01:53:26","guid":{"rendered":"https:\/\/now0930.pe.kr\/wordpress\/?p=3823"},"modified":"2020-07-14T10:54:46","modified_gmt":"2020-07-14T01:54:46","slug":"fifo-%ec%98%88%ec%a0%9c","status":"publish","type":"post","link":"https:\/\/now0930.pe.kr\/wordpress\/fifo-%ec%98%88%ec%a0%9c\/","title":{"rendered":"fifo \uc608\uc81c. unix network programming 55p"},"content":{"rendered":"\n<p>pipe\ub294 \uc774\ub984\uc744 \uc5c6\uc5b4 parent, child process\ub9cc \ud1b5\uc2e0\uc774 \uac00\ub2a5\ud558\ub2e4. \uc774\ub97c \ubcf4\uc548\ud55c\uac8c named pipe\uc778\ub370, fifo\ub77c\uace0\ub3c4 \ud55c\ub2e4. \ucc45\uc740 \ub2e4\ub978 \uc720\ub2c9\uc2a4 \ubc84\uc804\uc73c\ub85c \uc791\uc131\ud558\uc5ec, linux\uc5d0 \ub9de\uac8c \uc218\uc815\ud588\ub2e4. \uacb0\ub860\uc801\uc73c\ub85c pipe\uc640 \ube44\uc2b7\ud558\uace0 \ub354 \uc27d\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;unistd.h>\n#include &lt;sys\/types.h>\n#include &lt;fcntl.h>\n#include &lt;sys\/wait.h>\n#include &lt;string.h>\n#include &lt;sys\/stat.h>\n#include &lt;unistd.h>\n\n\n#define\tFIFO1\t\"\/tmp\/fifo.1\"\n#define\tFIFO2\t\"\/tmp\/fifo.2\"\n#define MAXLINE 100\n\nvoid\tclient(int, int), server(int, int);\n\nvoid server(int readfd, int writefd){\n\t\/*\n\tchar\tbuff[MAXLINE];\n\tint n;\n\tn = read(readfd, buff, MAXLINE);\n\tprintf(\"\uc11c\ubc84\uac00 \uc77d\uc740 data\ub294 %s\\n\",buff);\n\t\/\/ \uc0d8\ud50c \ud14c\uc2a4\ud2b8\uc6a9..\n\n\t*\/\n\tint fd;\n\tssize_t n;\n\tchar\tbuff[MAXLINE+1];\n\n\t\/* 4read pathname from IPC channel *\/\n\tif ( (n = read(readfd, buff, MAXLINE)) == 0)\n\t\tperror(\"end-of-file while reading pathname\");\n\tbuff[n] = '\\0';\t\t\/* null terminate pathname *\/\n\n\tif ( (fd = open(buff, O_RDONLY)) &lt; 0) {\n\t\t\t\/* 4error: must tell client *\/\n\t\tsnprintf(buff + n, sizeof(buff) - n, \": can't open, %s\\n\",\n\t\t\t\t strerror(errno));\n\t\tn = strlen(buff);\n\t\twrite(writefd, buff, n);\n\n\t} else {\n\t\t\t\/* 4open succeeded: copy file to IPC channel *\/\n\t\twhile ( (n = read(fd, buff, MAXLINE)) > 0)\n\t\t\twrite(writefd, buff, n);\n\t\tclose(fd);\n\t}\n\n}\n\nvoid client(int readfd, int writefd)\n{\n\tsize_t\tlen;\n\tssize_t\tn;\n\tchar\tbuff[MAXLINE];\n\n\t\/* 4read pathname *\/\n\tfgets(buff, MAXLINE, stdin);\n\tlen = strlen(buff);\t\t\/* fgets() guarantees null byte at end *\/\n\tif (buff[len-1] == '\\n')\n\t\tlen--;\t\t\t\t\/* delete newline from fgets() *\/\n\n\t\/* 4write pathname to IPC channel *\/\n\twrite(writefd, buff, len);\n\n\t\/* 4read from IPC, write to standard output *\/\n\twhile ( (n = read(readfd, buff, MAXLINE)) > 0)\n\t\twrite(STDOUT_FILENO, buff, n);\n}\n\n\nint\nmain(int argc, char **argv)\n{\n\tint\t\treadfd, writefd;\n\tpid_t\tchildpid;\n\n\t\t\/* 4create two FIFOs; OK if they already exist *\/\n\tif ((mkfifo(FIFO1, 0666) &lt; 0) &amp;&amp; (errno != EEXIST))\n\t\tprintf(\"can't create %s\", FIFO1);\n\tif ((mkfifo(FIFO2, 0666) &lt; 0) &amp;&amp; (errno != EEXIST)) {\n\t\tunlink(FIFO1);\n\t\tprintf(\"can't create %s\", FIFO2);\n\t}\n\n\tif ( (childpid = fork()) == 0) {\t\t\/* child *\/\n\t\treadfd = open(FIFO1, O_RDONLY, 0);\n\t\twritefd = open(FIFO2, O_WRONLY, 0);\n\n\t\tserver(readfd, writefd);\n\t\texit(0);\n\t}\n\t\t\/* 4parent *\/\n\twritefd = open(FIFO1, O_WRONLY, 0);\n\treadfd = open(FIFO2, O_RDONLY, 0);\n\n\tclient(readfd, writefd);\n\n\twaitpid(childpid, NULL, 0);\t\t\/* wait for child to terminate *\/\n\n\tclose(readfd);\n\tclose(writefd);\n\n\tunlink(FIFO1);\n\tunlink(FIFO2);\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 $ ls\na.out      fifo.c  simplepipe.c  unpv22e\nfduplex.c  pipe.c  test.txt      unpv22e.tar\npi@raspberrypi:~\/Project\/cCode\/IPC $ cat test.txt \n\uc548\ub155\ud558\uc138\uc694.\nhello.\npi@raspberrypi:~\/Project\/cCode\/IPC $ .\/a.out \n.\/test.txt\n\uc548\ub155\ud558\uc138\uc694.\nhello.\npi@raspberrypi:~\/Project\/cCode\/IPC $ \n<\/pre>\n\n\n\n<p>\uc774 \uc7a5\uc5d0 \uc5ec\ub7ec \ub0b4\uc6a9\ub3c4 \ub9ce\uc740\ub370 \uc77c\ub2e8 \ub2e4\ub978 \uc720\ub2c9\uc2a4  \ubc84\uc804\ub3c4 \ubcf4\uc774\uace0 \uc2dc\uac04\ub3c4 \uc5c6\uc5b4 \ub2e4\uc74c \uc7a5\uc73c\ub85c \ub118\uc5b4\uac04\ub2e4.<\/p>\n\n\n\n<figure class=\"wp-block-embed\"><div class=\"wp-block-embed__wrapper\">\nhttps:\/\/www.geeksforgeeks.org\/named-pipe-fifo-example-c-program\/\n<\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>pipe\ub294 \uc774\ub984\uc744 \uc5c6\uc5b4 parent, child process\ub9cc \ud1b5\uc2e0\uc774 \uac00\ub2a5\ud558\ub2e4. \uc774\ub97c \ubcf4\uc548\ud55c\uac8c named pipe\uc778\ub370, fifo\ub77c\uace0\ub3c4 \ud55c\ub2e4. \ucc45\uc740 \ub2e4\ub978 \uc720\ub2c9\uc2a4 \ubc84\uc804\uc73c\ub85c \uc791\uc131\ud558\uc5ec, linux\uc5d0 [&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,782,780,53,781],"class_list":["post-3823","post","type-post","status-publish","format-standard","hentry","category-12","tag-c","tag-fifo","tag-ipc","tag-linux","tag-network-program"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/posts\/3823","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=3823"}],"version-history":[{"count":3,"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/posts\/3823\/revisions"}],"predecessor-version":[{"id":3827,"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/posts\/3823\/revisions\/3827"}],"wp:attachment":[{"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/media?parent=3823"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/categories?post=3823"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/tags?post=3823"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}