{"id":7285,"date":"2020-06-10T14:58:49","date_gmt":"2020-06-10T14:58:49","guid":{"rendered":"http:\/\/www.max-sperling.bplaced.net\/?p=7285"},"modified":"2024-02-16T10:35:06","modified_gmt":"2024-02-16T10:35:06","slug":"pipe-posix","status":"publish","type":"post","link":"http:\/\/www.max-sperling.bplaced.net\/?p=7285","title":{"rendered":"(Unnamed) Pipe (POSIX)"},"content":{"rendered":"<p>&#8230; is user for unidirectional inter process communication on a single machine.<\/p>\n<hr>\n<p>In the following example a pipe gets created for a communication from the child to the parent.<\/p>\n<p><strong>Source code<\/strong><\/p>\n<pre class=\"brush: cpp; gutter: false; title: main.cpp; notranslate\" title=\"main.cpp\">\r\n#include &lt;sys\/types.h&gt;\r\n#include &lt;unistd.h&gt;\r\n#include &lt;cstring&gt;\r\n#include &lt;iostream&gt;\r\n\r\nint main()\r\n{\r\n  int pipefd[2]; \/\/ pipefd[0] ... read, pipefd[1] ... write\r\n  pipe(pipefd);\r\n  pid_t pid = fork();\r\n\r\n  if (pid == -1)\r\n  {\r\n    \/* Couldn&#039;t fork *\/\r\n    std::cout &lt;&lt; &quot;Couldn&#039;t fork&quot; &lt;&lt; std::endl;\r\n  }\r\n  else if (pid == 0)\r\n  {\r\n    \/* Child process *\/\r\n    close(pipefd[0]); \/\/ close read end\r\n    dup2(pipefd[1], 1); \/\/ redirect write end to stdout\r\n    close(pipefd[1]); \/\/ close origin write end\r\n    execlp(&quot;.\/child.out&quot;, &quot;child.out&quot;, NULL ); \/\/ returns only in error case\r\n    std::cout &lt;&lt; &quot;Exec failed with: &quot; &lt;&lt; errno &lt;&lt; std::endl;\r\n  }\r\n  else\r\n  {\r\n    \/* Parent process *\/\r\n    close(pipefd[1]); \/\/ close write end\r\n    char buf[1024];\r\n    while (read(pipefd[0], buf, sizeof(buf)) &gt; 0)\r\n    {\r\n      std::cout &lt;&lt; &quot;My child said: &quot; &lt;&lt; buf &lt;&lt; std::endl;\r\n      memset(buf, 0, sizeof(buf));\r\n    }\r\n    close(pipefd[0]); \/\/ close unecessary read end\r\n  }\r\n\r\n  return 0;\r\n}\r\n<\/pre>\n<pre class=\"brush: cpp; gutter: false; title: child.cpp; notranslate\" title=\"child.cpp\">\r\n#include &lt;unistd.h&gt;\r\n#include &lt;iostream&gt;\r\n\r\nint main()\r\n{\r\n  std::cout &lt;&lt; &quot;Hold&quot; &lt;&lt; std::endl;\r\n  sleep(1);\r\n  std::cout &lt;&lt; &quot;my&quot; &lt;&lt; std::endl;\r\n  sleep(1);\r\n  std::cout &lt;&lt; &quot;beer&quot; &lt;&lt; std::endl;\r\n\r\n  return 0;\r\n}\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\n$ .\/main.out \r\nMy child said: Hold\r\n\r\nMy child said: my\r\n\r\nMy child said: beer\r\n\r\n<\/pre>\n<p><strong>File descriptors<\/strong><\/p>\n<pre>\r\n   Parent          |   Child \r\n---------------------------------------\r\n   0   stdin       |   0   stdin \r\n   1   stdout      |   1   pipefd[1]\r\n   3   stderr      |   2   pipefd[1]\r\n   ...             |   ...\r\n   X   pipefd[0]   |\r\n   ...             |\r\n---------------------------------------\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>&#8230; is user for unidirectional inter process communication on a single machine. In the following example a pipe gets created<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false},"categories":[65],"tags":[],"_links":{"self":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/7285"}],"collection":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7285"}],"version-history":[{"count":1,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/7285\/revisions"}],"predecessor-version":[{"id":16797,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/7285\/revisions\/16797"}],"wp:attachment":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7285"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7285"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7285"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}