{"id":1651,"date":"2020-06-08T20:05:04","date_gmt":"2020-06-08T12:05:04","guid":{"rendered":"http:\/\/bloo.heing.fun\/?p=1651"},"modified":"2020-06-08T20:05:04","modified_gmt":"2020-06-08T12:05:04","slug":"linux-mkfifo-c","status":"publish","type":"post","link":"https:\/\/bloo.heing.fun\/?p=1651","title":{"rendered":"linux mkfifo c"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>#include &lt;fcntl.h>\n#include &lt;stdio.h>\n#include &lt;sys\/stat.h>\n#include &lt;sys\/types.h>\n#include &lt;sys\/wait.h>\n#include &lt;unistd.h>\n\nint c = 1;\nint main() {\n  int ret;\n  char *myfifo = \"\/tmp\/myfifosharefile\";\n  ret=mkfifo(myfifo, 0666);\n  pid_t pid = fork();\n  int fd;\n  if (pid == 0) {\n    char buf&#91;4096];\n    fd = open(myfifo, O_RDONLY);\n    read(fd, buf, 4096);\n    printf(\"received:%s\\n\", buf);\n    close(fd);\n\n  } else {\n    fd = open(myfifo, O_WRONLY); \n    char *msg = \"hi\";\n    write(fd, msg, sizeof(msg));\n    printf(\"%d\\n\", c++);\n    close(fd);\n    unlink(myfifo);\n    wait(NULL);\n  }\n}<\/code><\/pre>\n\n\n\n<p>\u8fd9\u4e2a\u4f7f\u7528\u8fc7\u7a0b\u4e2d\u6d89\u53ca \u963b\u585e<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u8fdb\u7a0b\u95f4\u901a\u4fe1\u4e4bfifo\uff0c\u5728\u963b\u585e\u6a21\u5f0f\u4e0b\uff0c\u53ea\u6709\u5f53\u8bfb\u548c\u5199\u6a21\u5f0f\u90fd\u6253\u5f00\u65f6\u624d\u8fd4\u56de\uff0c\u5426\u5219\u4e00\u76f4\u963b\u585e\uff1b\n\n\u975e\u963b\u585e\u6a21\u5f0f\u4e0b\uff0c\u5f53\u8bfb\u7aef\u6ca1\u6253\u5f00\uff0c\u5219\u6253\u5f00\u5199\u7aef\u65e0\u6548\uff0c\u8fd4\u56de\u9519\u8bef\u3002\n\n\u5efa\u8bae\u4f60\u8bfb\u4e00\u8bfbUNP\u53772\u3002\n\na. \u4e0d\u4f7f\u7528O_NONBLOCK\u6807\u5fd7\u65f6\uff0c\u53ea\u8bfbopen\u8981\u963b\u585e\u5230\u67d0\u4e2a\u5176\u5b83\u8fdb\u7a0b\u4e3a\u5199\u800c\u6253\u5f00\u5b83\u4e3a\u6b62\n\nb. \u4e0d\u4f7f\u7528O_NONBLOCK\u6807\u5fd7\u65f6\uff0c\u53ea\u5199open\u8981\u963b\u585e\u5230\u67d0\u4e2a\u5176\u5b83\u8fdb\u7a0b\u4e3a\u8bfb\u800c\u6253\u5f00\u5b83\u4e3a\u6b62\nc. \u5982\u679c\u5728open\u7684\u65f6\u5019\u6307\u5b9aO_NONBLOCK\u6807\u5fd7\uff0c\u5f53\u53ea\u8bfbopen\u65f6\uff0c\u6ca1\u6709\u8fdb\u7a0b\u4e3a\u5199\u800c\u6253\u5f00FIFO\u7684\u8bdd\uff0c\u4f1a\u8fd4\u56de-1\uff0c\u53ea\u5199open\u65f6\uff0c\u6ca1\u6709\u8fdb\u7a0b\u4e3a\u8bfb\u800c\u6253\u5f00FIFO\u7684\u8bdd\u4e5f\u4f1a\u8fd4\u56de-1\u8868\u793a\u5931\u8d25\u3002\n\n\u4ee5\u4e0a\u7684\u60c5\u51b5\u4f7fFIFO\u7684\u4f7f\u7528\u5e26\u6765\u4e86\u4e00\u4e9b\u4e0d\u4fbf\uff0c\u4f46\u6709\u4e00\u4e2a\u5e38\u7528\u7684\u6280\u5de7\u662f\uff0c\u53ea\u8981\u7528O_RDWR(\u8bfb\u5199)\u6765\u6253\u5f00\u4e00\u4e2a\u7ba1\u9053\uff0c\u5219\u4e0d\u4f1a\u8fd4\u56de\u5931\u8d25\uff0c\u800copen\u4e5f\u4e0d\u4f1a\u963b\u585e\u3002\u4e24\u4e2a\u8fdb\u7a0b\u4f7f\u7528FIFO\u6765\u8fdb\u884c\u901a\u4fe1\u65f6\uff0c\u901a\u5e38\u4f1a\u4f7f\u7528\u4e24\u4e2aFIFO\uff0c\u4e00\u4e2a\u7528\u4e8e\u53d1\u9001\u6570\u636e(\u5373\u8fdb\u884c\u5199\u64cd\u4f5c)\uff0c\u4e00\u4e2a\u7528\u4e8e\u63a5\u6536\u6570\u636e(\u5373\u8fdb\u884c\u8bfb\u64cd\u4f5c)\uff0c\u800c\u8fd9\u4e24\u4e2aFIFO\u9700\u8981\u5747\u4f7f\u7528O_RDWR\u6765\u6253\u5f00\u3002\u4f7f\u7528\u7cfb\u7edf\u8c03\u7528open, read, write\u6765\u64cd\u4f5c\u8fd9\u4e24\u4e2aFIFO\u6ca1\u6709\u4ec0\u4e48\u95ee\u9898\uff0c\u7a0b\u5e8f\u5de5\u4f5c\u5f97\u5f88\u597d\u3002\n\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\n\u7248\u6743\u58f0\u660e\uff1a\u672c\u6587\u4e3aCSDN\u535a\u4e3b\u300cMr_John_Liang\u300d\u7684\u539f\u521b\u6587\u7ae0\uff0c\u9075\u5faaCC 4.0 BY-SA\u7248\u6743\u534f\u8bae\uff0c\u8f6c\u8f7d\u8bf7\u9644\u4e0a\u539f\u6587\u51fa\u5904\u94fe\u63a5\u53ca\u672c\u58f0\u660e\u3002\n\u539f\u6587\u94fe\u63a5\uff1ahttps:\/\/blog.csdn.net\/liangzhao_jay\/article\/details\/47108977<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u4e2a\u4f7f\u7528\u8fc7\u7a0b\u4e2d\u6d89\u53ca \u963b\u585e<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[78,36],"tags":[],"class_list":["post-1651","post","type-post","status-publish","format-standard","hentry","category-linux","category-36"],"_links":{"self":[{"href":"https:\/\/bloo.heing.fun\/index.php?rest_route=\/wp\/v2\/posts\/1651","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bloo.heing.fun\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bloo.heing.fun\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bloo.heing.fun\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bloo.heing.fun\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1651"}],"version-history":[{"count":1,"href":"https:\/\/bloo.heing.fun\/index.php?rest_route=\/wp\/v2\/posts\/1651\/revisions"}],"predecessor-version":[{"id":1652,"href":"https:\/\/bloo.heing.fun\/index.php?rest_route=\/wp\/v2\/posts\/1651\/revisions\/1652"}],"wp:attachment":[{"href":"https:\/\/bloo.heing.fun\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1651"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bloo.heing.fun\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1651"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bloo.heing.fun\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1651"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}