{"id":309,"date":"2013-05-07T22:43:03","date_gmt":"2013-05-07T14:43:03","guid":{"rendered":"http:\/\/www.freezhongzi.info\/?p=309"},"modified":"2013-05-07T22:43:03","modified_gmt":"2013-05-07T14:43:03","slug":"linux-pipe%e8%af%bb%e5%86%99%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"https:\/\/bl4kraven.com\/index.php\/2013\/05\/07\/linux-pipe%e8%af%bb%e5%86%99%e9%97%ae%e9%a2%98\/","title":{"rendered":"Linux pipe\u8bfb\u5199\u95ee\u9898"},"content":{"rendered":"<p>\u4e4b\u524d\u7684\u90a3\u4e2amplayer\u64ad\u653emms\u7535\u53f0\u9700\u8981\u4e00\u4e2a\u63a7\u5236\u63a5\u53e3\uff0cmplayer\u53ea\u652f\u6301\u63a7\u5236\u53f0\u8f93\u5165\u547d\u4ee4\uff0c\u6240\u4ee5\u8981\u63a7\u5236mplayer\u53ea\u80fd\u7528\u7ba1\u9053\u63a7\u5236\u6807\u51c6\u8f93\u5165stdin\u3002\u4e3a\u4e86\u7075\u6d3b\u5c31\u7528FIFO\u6587\u4ef6\u4f5c\u4e3amplayer\u7684\u8f93\u5165\u7aef\u3002<\/p>\n<pre><code>mkfifo radio_pipe\ncat radio_pipe | mplayer mms:\/\/xxx\necho -n p &gt; radio_pipe\n<\/code><\/pre>\n<p>\u8fd9\u91cc\u6709\u4e2a\u95ee\u9898\u662fcat radio_pipe\u8bfb\u5230\u6587\u4ef6EOF\u4f1a\u9000\u51fa\uff0c\u5bfc\u81f4mplayer\u9000\u51fa\uff0c\u6240\u4ee5\u6bcf\u6b21\u53ea\u80fd\u63a7\u5236\u4e00\u6b21:(\u3002<\/p>\n<p>\u524d\u51e0\u5929\u770bmini2440\u9644\u5e26\u7684led-player\u7a0b\u5e8f\uff0c\u7a81\u7136\u610f\u8bc6\u5230\u53ef\u4ee5\u81ea\u5df1\u5199\u4e2apipe&#95;read\u7a0b\u5e8f\u5ffd\u7565\u6587\u4ef6EOF\uff0c\u8fd9\u6837\u5c31\u89e3\u51b3\u4e86mplayer\u7684\u63a7\u5236\u3002\u8bb0\u5f97\u4e4b\u524d\u5199TCP\u7a0b\u5e8f\u65f6\u77e5\u9053read\u8fd4\u56de0\u8bf4\u660e\u5230\u6587\u4ef6EOF\u4e86\uff0cTCP\u5c31\u4ee3\u8868\u5173\u95ed\u4e86\u3002\u6211\u53ea\u8981\u5ffd\u7565\u6389read\u8fd4\u56de0\u7684\u72b6\u6001\u5c31\u884c\u4e86\uff08\u4e4b\u540e\u8bc1\u660e\u5927\u9519\u7279\u9519-&#95;-!!!)\uff0c\u9519\u8bef\u662f\u56e0\u4e3aselect\u8ba4\u4e3a\u6587\u4ef6EOF\u662f\u8bfb\u4fe1\u53f7\uff0c\u9664\u975e\u4f60\u9000\u51fa\u5426\u5219\u4e00\u76f4\u89e6\u53d1\u3002\u6700\u540e\u7684\u89e3\u51b3\u65b9\u6cd5\u662f\u8fd9\u4e00\u53e5<\/p>\n<pre><code>int null_pipe_write_rd = open(pstrPipe, O_WRONLY | O_NONBLOCK);\n<\/code><\/pre>\n<p>\u518d\u6253\u5f00\u4e00\u4e2a\u4e0d\u64cd\u4f5c\u7684pipe\u8bfbfd\uff0c\u8fd9\u6837\u624d\u4e0d\u81f3\u8fd4\u56de\u6587\u4ef6EOF\uff0c\u5177\u4f53\u539f\u56e0\u4e0d\u662f\u5f88\u660e\u767d:(\uff0c\u4e0d\u8fc7\u8fbe\u5230\u4e86\u6211\u8981\u7684\u6548\u679c:)\u3002<\/p>\n<pre><code>mkfifo radio_pipe\n.\/pipe_read radio_pipe | mplayer mms:\/\/xxx\necho -n p &gt; radio_pipe\n<\/code><\/pre>\n<p>\u8fd9\u6837\u5c31\u4e0d\u4f1a\u9000\u51fa\u4e86\uff0c\u4e0d\u8fc7\u5c31\u5982\u6211\u4eecCEO\u8bf4\u7684\u4e00\u53e5\u8bdd\uff1a<strong>\u89e3\u51b3\u4e86\u4e00\u4e2a\u95ee\u9898\uff0c\u5f80\u5f80\u5e26\u6765\u4e86\u5176\u4ed6\u95ee\u9898<\/strong>\uff0c\u8fd9\u6837pipe&#95;read\u6c38\u8fdc\u4e5f\u4e0d\u4f1a\u9000\u51fa\u4e86\uff0c\u5c3d\u7ba1mplayer\u5df2\u7ecf\u9000\u51fa\u4e86-&#95;-!!!\u3002<\/p>\n<p>Talk is cheap, show me the code:<\/p>\n<pre><code>#include &lt;cstdio&gt;\n#include &lt;sys\/stat.h&gt;\n#include &lt;sys\/types.h&gt;\n#include &lt;fcntl.h&gt;\n#include &lt;errno.h&gt;\n#include &lt;unistd.h&gt;\n\nvoid usage(const char *pstrProgram)\n{\n    printf(\"Usage:\\n\"\n           \"  %s pipefile\\n\", pstrProgram);\n}\n\n\nint main(int argc, char *argv[])\n{\n    if (argc != 2)\n    {\n        usage(argv[0]);\n        return 1;\n    }\n\n    bool bExit = false;\n    const char *pstrPipe = argv[1];\n\n    int pipe_rd = open(pstrPipe, O_RDONLY | O_NONBLOCK);\n    if (pipe_rd == -1)\n    {\n        fprintf(stderr, \"open pipe fail\\n\");\n        return 1;\n    }\n\n    int null_pipe_write_rd = open(pstrPipe, O_WRONLY | O_NONBLOCK);\n\n    while (!bExit)\n    {\n        fd_set fdset;\n        \/\/struct timeval tv;\n\n        FD_ZERO(&amp;fdset);\n        FD_SET(pipe_rd, &amp;fdset);\n\n        int ret = select(pipe_rd+1, &amp;fdset, NULL, NULL, NULL);\n\n        if (ret &lt; 0)\n        {\n            fprintf(stderr, \"select return error\\n\");\n            return 1;\n        }\n        else\n        {\n            if (FD_ISSET(pipe_rd, &amp;fdset))\n            {\n                char buffer[128];\n                while (true)\n                {\n                    int nLen = read(pipe_rd, buffer, sizeof(buffer));\n                    if (nLen == 0)\n                    {\n                        fprintf(stderr, \"End of file\\n\");\n                        break;\n                    }\n                    else if (nLen == -1)\n                    {\n                        if (errno == EAGAIN)\n                        {\n                            fprintf(stderr, \"nonblocking read fail\\n\");\n                        }\n                        else\n                        {\n                            fprintf(stderr, \"read fail\\n\");\n                            return 1;\n                        }\n                        break;\n                    }\n                    else\n                    {\n                        \/\/ write stdout\n                        int ret = write(STDOUT_FILENO, buffer, nLen);\n                        if (ret == -1 &amp;&amp; errno != EAGAIN)\n                        {\n                            fprintf(stderr, \"write fail\\n\");\n                            return 1;\n                        }\n                    }\n                }\n            }\n        }\n    }\n\n    return 0;\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4e4b\u524d\u7684\u90a3\u4e2amplayer\u64ad\u653emms\u7535\u53f0\u9700\u8981\u4e00\u4e2a\u63a7\u5236\u63a5\u53e3\uff0cmplayer\u53ea\u652f\u6301\u63a7\u5236 &hellip; <a href=\"https:\/\/bl4kraven.com\/index.php\/2013\/05\/07\/linux-pipe%e8%af%bb%e5%86%99%e9%97%ae%e9%a2%98\/\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a> <a href=\"https:\/\/bl4kraven.com\/index.php\/2013\/05\/07\/linux-pipe%e8%af%bb%e5%86%99%e9%97%ae%e9%a2%98\/\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-309","post","type-post","status-publish","format-standard","hentry","category-program"],"_links":{"self":[{"href":"https:\/\/bl4kraven.com\/index.php\/wp-json\/wp\/v2\/posts\/309","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bl4kraven.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bl4kraven.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bl4kraven.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bl4kraven.com\/index.php\/wp-json\/wp\/v2\/comments?post=309"}],"version-history":[{"count":0,"href":"https:\/\/bl4kraven.com\/index.php\/wp-json\/wp\/v2\/posts\/309\/revisions"}],"wp:attachment":[{"href":"https:\/\/bl4kraven.com\/index.php\/wp-json\/wp\/v2\/media?parent=309"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bl4kraven.com\/index.php\/wp-json\/wp\/v2\/categories?post=309"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bl4kraven.com\/index.php\/wp-json\/wp\/v2\/tags?post=309"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}