{"id":68678,"date":"2026-01-30T12:57:21","date_gmt":"2026-01-30T04:57:21","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/68678.html"},"modified":"2026-01-30T12:57:21","modified_gmt":"2026-01-30T04:57:21","slug":"linux_%e8%bf%9b%e7%a8%8b%e9%97%b4%e9%80%9a%e4%bf%a1","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/68678.html","title":{"rendered":"Linux_\u8fdb\u7a0b\u95f4\u901a\u4fe1"},"content":{"rendered":"<h2><span style=\"color:#333333\">\u672c\u8282\u91cd\u70b9&#xff1a; <\/span><\/h2>\n<p> <span style=\"color:#777777\">\u8fdb\u7a0b\u95f4\u901a\u4fe1\u4ecb\u7ecd <\/span><\/p>\n<p> <span style=\"color:#777777\">\u7ba1\u9053 <\/span><\/p>\n<p> <span style=\"color:#777777\">\u6d88\u606f\u961f\u5217 <\/span><\/p>\n<p> <span style=\"color:#777777\">\u5171\u4eab\u5185\u5b58 <\/span><\/p>\n<p> <span style=\"color:#777777\">\u4fe1\u53f7\u91cf<\/span><\/p>\n<h2><span style=\"color:#333333\">\u4e00.\u8fdb\u7a0b\u95f4\u901a\u4fe1\u4ecb\u7ecd<\/span><\/h2>\n<h3><span style=\"color:#333333\">1.\u8fdb\u7a0b\u95f4\u901a\u4fe1\u76ee\u7684<\/span><\/h3>\n<p>  <span style=\"color:#777777\">\u6570\u636e\u4f20\u8f93&#xff1a;\u4e00\u4e2a\u8fdb\u7a0b\u9700\u8981\u5c06\u5b83\u7684\u6570\u636e\u53d1\u9001\u7ed9\u53e6\u4e00\u4e2a\u8fdb\u7a0b <\/span><\/p>\n<p>  <span style=\"color:#777777\">\u8d44\u6e90\u5171\u4eab&#xff1a;\u591a\u4e2a\u8fdb\u7a0b\u4e4b\u95f4\u5171\u4eab\u540c\u6837\u7684\u8d44\u6e90\u3002 <\/span><\/p>\n<p>  <span style=\"color:#777777\">\u901a\u77e5\u4e8b\u4ef6&#xff1a;\u4e00\u4e2a\u8fdb\u7a0b\u9700\u8981\u5411\u53e6\u4e00\u4e2a\u6216\u4e00\u7ec4\u8fdb\u7a0b\u53d1\u9001\u6d88\u606f&#xff0c;\u901a\u77e5\u5b83&#xff08;\u5b83\u4eec&#xff09;\u53d1\u751f\u4e86\u67d0\u79cd\u4e8b\u4ef6&#xff08;\u5982\u8fdb\u7a0b\u7ec8\u6b62\u65f6\u8981\u901a\u77e5\u7236\u8fdb\u7a0b&#xff09;\u3002 <\/span><\/p>\n<p>  <span style=\"color:#777777\">\u8fdb\u7a0b\u63a7\u5236&#xff1a;\u6709\u4e9b\u8fdb\u7a0b\u5e0c\u671b\u5b8c\u5168\u63a7\u5236\u53e6\u4e00\u4e2a\u8fdb\u7a0b\u7684\u6267\u884c&#xff08;\u5982<\/span><br \/>\n  <span style=\"color:#777777\">Debug<\/span><br \/>\n  <span style=\"color:#777777\">\u8fdb\u7a0b&#xff09;&#xff0c;\u6b64\u65f6\u63a7\u5236\u8fdb\u7a0b\u5e0c\u671b\u80fd\u591f\u62e6\u622a\u53e6\u4e00\u4e2a\u8fdb\u7a0b\u7684\u6240\u6709\u9677\u5165\u548c\u5f02\u5e38&#xff0c;\u5e76\u80fd\u591f\u53ca\u65f6\u77e5\u9053\u5b83\u7684\u72b6\u6001\u6539\u53d8\u3002<\/span><\/p>\n<h3><span style=\"color:#333333\">2.\u8fdb\u7a0b\u95f4\u901a\u4fe1\u53d1\u5c55<\/span><\/h3>\n<p>   <span style=\"color:#777777\">\u7ba1\u9053 <\/span><\/p>\n<p>   <span style=\"color:#777777\">System V<\/span><br \/>\n   <span style=\"color:#777777\">\u8fdb\u7a0b\u95f4\u901a\u4fe1 <\/span><\/p>\n<p>   <span style=\"color:#777777\">POSIX<\/span><br \/>\n   <span style=\"color:#777777\">\u8fdb\u7a0b\u95f4\u901a\u4fe1<\/span><\/p>\n<h3>3.\u8fdb\u7a0b\u95f4\u901a\u4fe1\u5206\u7c7b<\/h3>\n<h4><span style=\"color:#333333\">(1)\u7ba1\u9053<\/span><\/h4>\n<p>    <span style=\"color:#777777\">\u533f\u540d\u7ba1\u9053<\/span><br \/>\n    <span style=\"color:#777777\">pipe <\/span><\/p>\n<p>    <span style=\"color:#777777\">\u547d\u540d\u7ba1\u9053<\/span><\/p>\n<h3><span style=\"color:#333333\">4.System V IPC<\/span><\/h3>\n<h4><\/h4>\n<p><span style=\"color:#777777\">System V <\/span><span style=\"color:#777777\">\u6d88\u606f\u961f\u5217 <\/span><\/p>\n<p>   <span style=\"color:#777777\">System V <\/span><br \/>\n   <span style=\"color:#777777\">\u5171\u4eab\u5185\u5b58<\/span><\/p>\n<p>   <span style=\"color:#777777\">System V <\/span><br \/>\n   <span style=\"color:#777777\">\u4fe1\u53f7\u91cf<\/span><\/p>\n<h3><span style=\"color:#333333\">5.POSIX IPC<\/span><\/h3>\n<p>   <span style=\"color:#777777\">\u6d88\u606f\u961f\u5217 <\/span><\/p>\n<p>   <span style=\"color:#777777\">\u5171\u4eab\u5185\u5b58 <\/span><\/p>\n<p>   <span style=\"color:#777777\">\u4fe1\u53f7\u91cf <\/span><\/p>\n<p>   <span style=\"color:#777777\">\u4e92\u65a5\u91cf <\/span><\/p>\n<p>   <span style=\"color:#777777\">\u6761\u4ef6\u53d8\u91cf <\/span><\/p>\n<p>   <span style=\"color:#777777\">\u8bfb\u5199\u9501<\/span><\/p>\n<h2><span style=\"color:#333333\">\u4e8c.\u7ba1\u9053<\/span><\/h2>\n<h3><span style=\"color:#333333\">1.\u4ec0\u4e48\u662f\u7ba1\u9053<\/span><\/h3>\n<p>   <span style=\"color:#777777\">\u7ba1\u9053\u662f<\/span><br \/>\n   <span style=\"color:#777777\">Unix<\/span><br \/>\n   <span style=\"color:#777777\">\u4e2d\u6700\u53e4\u8001\u7684\u8fdb\u7a0b\u95f4\u901a\u4fe1\u7684\u5f62\u5f0f\u3002 <\/span><\/p>\n<p>   <span style=\"color:#777777\">\u6211\u4eec\u628a\u4ece\u4e00\u4e2a\u8fdb\u7a0b\u8fde\u63a5\u5230\u53e6\u4e00\u4e2a\u8fdb\u7a0b\u7684\u4e00\u4e2a\u6570\u636e\u6d41\u79f0\u4e3a\u4e00\u4e2a<\/span><br \/>\n   <span style=\"color:#777777\">\u201c<\/span><br \/>\n   <span style=\"color:#777777\">\u7ba1\u9053<\/span><br \/>\n   <span style=\"color:#777777\">\u201d<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"456\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260130045718-697c3a2ed215b.png\" width=\"898\" \/><\/p>\n<p>\u8fd9\u5f20\u56fe\u5c55\u793a\u7684\u662f Linux \u533f\u540d\u7ba1\u9053&#xff08;Anonymous Pipe&#xff09; \u7684\u5de5\u4f5c\u539f\u7406&#xff0c;\u4ee5\u547d\u4ee4 who | wc -l \u4e3a\u4f8b&#xff0c;\u76f4\u89c2\u5448\u73b0\u4e86\u7ba1\u9053\u5982\u4f55\u8fde\u63a5\u4e24\u4e2a\u8fdb\u7a0b\u5b9e\u73b0\u6570\u636e\u4f20\u9012\u3002<\/p>\n<li>\n<p>\u7ba1\u9053\u521b\u5efa\u5f53\u6267\u884c who | wc -l \u65f6&#xff0c;Shell \u4f1a\u5728\u5185\u6838\u4e2d\u521b\u5efa\u4e00\u4e2a\u533f\u540d\u7ba1\u9053&#xff0c;\u5b83\u5305\u542b\u4e00\u4e2a\u8bfb\u7aef\u548c\u4e00\u4e2a\u5199\u7aef\u3002<\/p>\n<\/li>\n<li>\n<p>\u8fdb\u7a0b\u5173\u8054<\/p>\n<ul>\n<li>who \u8fdb\u7a0b&#xff1a;\u5b83\u7684\u6807\u51c6\u8f93\u51fa&#xff08;stdout&#xff09;\u88ab\u91cd\u5b9a\u5411\u5230\u7ba1\u9053\u7684\u5199\u7aef&#xff0c;\u4e0d\u518d\u76f4\u63a5\u8f93\u51fa\u5230\u7ec8\u7aef\u3002<\/li>\n<li>wc -l \u8fdb\u7a0b&#xff1a;\u5b83\u7684\u6807\u51c6\u8f93\u5165&#xff08;stdin&#xff09;\u88ab\u91cd\u5b9a\u5411\u5230\u7ba1\u9053\u7684\u8bfb\u7aef&#xff0c;\u4e0d\u518d\u4ece\u7ec8\u7aef\u8bfb\u53d6\u8f93\u5165\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u6570\u636e\u4f20\u9012<\/p>\n<ul>\n<li>who \u547d\u4ee4\u6267\u884c\u540e&#xff0c;\u4f1a\u5c06\u5f53\u524d\u767b\u5f55\u7528\u6237\u7684\u4fe1\u606f\u5199\u5165\u7ba1\u9053\u3002<\/li>\n<li>wc -l \u547d\u4ee4\u4ece\u7ba1\u9053\u8bfb\u53d6\u6570\u636e&#xff0c;\u5e76\u7edf\u8ba1\u8f93\u5165\u7684\u884c\u6570&#xff0c;\u6700\u7ec8\u5c06\u7ed3\u679c\u8f93\u51fa\u5230\u7ec8\u7aef\u3002<\/li>\n<\/ul>\n<\/li>\n<p>\u5173\u952e\u7279\u70b9<\/p>\n<ul>\n<li>\u534a\u53cc\u5de5\u901a\u4fe1&#xff1a;\u6570\u636e\u53ea\u80fd\u5355\u5411\u6d41\u52a8&#xff08;\u4ece who \u5230 wc -l&#xff09;\u3002<\/li>\n<li>\u5185\u6838\u7f13\u51b2\u533a&#xff1a;\u7ba1\u9053\u672c\u8d28\u662f\u5185\u6838\u4e2d\u7684\u4e00\u5757\u73af\u5f62\u7f13\u51b2\u533a&#xff0c;\u6570\u636e\u5728\u5185\u6838\u4e2d\u4e34\u65f6\u5b58\u50a8&#xff0c;\u65e0\u9700\u7528\u6237\u7a7a\u95f4\u62f7\u8d1d\u3002<\/li>\n<li>\u751f\u547d\u5468\u671f\u4e0e\u8fdb\u7a0b\u7ed1\u5b9a&#xff1a;\u533f\u540d\u7ba1\u9053\u4ec5\u5728\u521b\u5efa\u5b83\u7684\u8fdb\u7a0b\u53ca\u5176\u5b50\u8fdb\u7a0b\u95f4\u6709\u6548&#xff0c;\u8fdb\u7a0b\u9000\u51fa\u540e\u7ba1\u9053\u4f1a\u88ab\u81ea\u52a8\u9500\u6bc1\u3002<\/li>\n<li>\u81ea\u52a8\u540c\u6b65&#xff1a;\u8bfb\u8fdb\u7a0b\u4f1a\u963b\u585e\u7b49\u5f85\u6570\u636e\u5199\u5165&#xff0c;\u5199\u8fdb\u7a0b\u4f1a\u963b\u585e\u7b49\u5f85\u6570\u636e\u88ab\u8bfb\u53d6&#xff0c;\u5b9e\u73b0\u8fdb\u7a0b\u95f4\u7684\u81ea\u52a8\u540c\u6b65\u3002<\/li>\n<\/ul>\n<h3><span style=\"color:#333333\">2.\u533f\u540d\u7ba1\u9053<\/span><\/h3>\n<p>  #include &lt;unistd.h&gt;<br \/>\n\u529f\u80fd:\u521b\u5efa\u4e00\u65e0\u540d\u7ba1\u9053<br \/>\n\u539f\u578b<br \/>\nint pipe(int fd[2]);<br \/>\n\u53c2\u6570<br \/>\nfd&#xff1a;\u6587\u4ef6\u63cf\u8ff0\u7b26\u6570\u7ec4,\u5176\u4e2dfd[0]\u8868\u793a\u8bfb\u7aef, fd[1]\u8868\u793a\u5199\u7aef<br \/>\n\u8fd4\u56de\u503c:\u6210\u529f\u8fd4\u56de0&#xff0c;\u5931\u8d25\u8fd4\u56de\u9519\u8bef\u4ee3\u7801 <\/p>\n<p>\u5173\u4e8epipe\u51fd\u6570\u7684\u8be6\u7ec6\u8bb2\u89e3&#xff1a;<\/p>\n<h4>1. \u51fd\u6570\u57fa\u7840\u4fe1\u606f<\/h4>\n<p>pipe() \u662f Linux \u7cfb\u7edf\u8c03\u7528&#xff0c;\u7528\u4e8e\u5728\u5185\u6838\u4e2d\u521b\u5efa\u4e00\u4e2a\u533f\u540d\u7ba1\u9053&#xff08;\u65e0\u540d\u7ba1\u9053&#xff09;&#xff0c;\u5b9e\u73b0\u6709\u4eb2\u7f18\u5173\u7cfb\u7684\u8fdb\u7a0b&#xff08;\u5982\u7236\u5b50\u8fdb\u7a0b&#xff09;\u4e4b\u95f4\u7684\u5355\u5411\u901a\u4fe1\u3002<\/p>\n<table>\n<tr>\u6838\u5fc3\u4fe1\u606f\u5177\u4f53\u5185\u5bb9<\/tr>\n<tbody>\n<tr>\n<td>\u51fd\u6570\u539f\u578b<\/td>\n<td>int pipe(int pipefd[2]);<\/td>\n<\/tr>\n<tr>\n<td>\u6240\u9700\u5934\u6587\u4ef6<\/td>\n<td>C&#xff1a;&lt;unistd.h&gt;&#xff1b;C&#043;&#043;&#xff1a;&lt;unistd.h&gt;&#xff08;\u517c\u5bb9&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u51fd\u6570\u5f52\u5c5e<\/td>\n<td>\u7cfb\u7edf\u8c03\u7528&#xff08;\u5185\u6838\u6001\u64cd\u4f5c&#xff09;&#xff0c;\u5c5e\u4e8e POSIX \u6807\u51c6<\/td>\n<\/tr>\n<tr>\n<td>\u6838\u5fc3\u4f5c\u7528<\/td>\n<td>\u521b\u5efa\u533f\u540d\u7ba1\u9053&#xff0c;\u8fd4\u56de\u4e24\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff1a;\u8bfb\u7aef\u548c\u5199\u7aef&#xff0c;\u7528\u4e8e\u8fdb\u7a0b\u95f4\u6570\u636e\u4f20\u9012<\/td>\n<\/tr>\n<tr>\n<td>\u9002\u7528\u573a\u666f<\/td>\n<td>\u4ec5\u652f\u6301\u6709\u4eb2\u7f18\u5173\u7cfb\u7684\u8fdb\u7a0b&#xff08;\u7236\u5b50\u3001\u5144\u5f1f\u8fdb\u7a0b&#xff09;&#xff0c;\u4e0d\u652f\u6301\u65e0\u5173\u8054\u8fdb\u7a0b\u901a\u4fe1<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>2. \u53c2\u6570\u8be6\u89e3<\/h4>\n<p>pipe() \u7684\u53c2\u6570\u662f\u4e00\u4e2a\u957f\u5ea6\u4e3a 2 \u7684\u6574\u578b\u6570\u7ec4 pipefd&#xff0c;\u7528\u4e8e\u5b58\u50a8\u7ba1\u9053\u7684\u4e24\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff1a;<\/p>\n<table>\n<tr>\u6570\u7ec4\u4e0b\u6807\u542b\u4e49\u64cd\u4f5c\u65b9\u5f0f<\/tr>\n<tbody>\n<tr>\n<td>pipefd[0]<\/td>\n<td>\u7ba1\u9053\u7684\u8bfb\u7aef<\/td>\n<td>\u53ea\u80fd\u7528 read() \u8bfb\u53d6\u6570\u636e&#xff0c;\u4e0d\u80fd\u5199<\/td>\n<\/tr>\n<tr>\n<td>pipefd[1]<\/td>\n<td>\u7ba1\u9053\u7684\u5199\u7aef<\/td>\n<td>\u53ea\u80fd\u7528 write() \u5199\u5165\u6570\u636e&#xff0c;\u4e0d\u80fd\u8bfb<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h5>\u5173\u952e\u89c4\u5219&#xff1a;<\/h5>\n<ul>\n<li>\u7ba1\u9053\u662f\u534a\u53cc\u5de5\u7684&#xff1a;\u6570\u636e\u53ea\u80fd\u4ece\u5199\u7aef&#xff08;pipefd[1]&#xff09;\u5199\u5165&#xff0c;\u4ece\u8bfb\u7aef&#xff08;pipefd[0]&#xff09;\u8bfb\u51fa&#xff0c;\u5355\u5411\u6d41\u52a8&#xff1b;<\/li>\n<li>\u6570\u7ec4\u5fc5\u987b\u9884\u5148\u5206\u914d\u7a7a\u95f4&#xff08;\u5982 int fd[2];&#xff09;&#xff0c;\u51fd\u6570\u4f1a\u81ea\u52a8\u586b\u5145\u4e24\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff1b;<\/li>\n<li>\u82e5\u53c2\u6570\u4e3a NULL&#xff0c;\u51fd\u6570\u4f1a\u5931\u8d25\u5e76\u8bbe\u7f6e errno &#061; EINVAL\u3002<\/li>\n<\/ul>\n<h4>3. \u8fd4\u56de\u503c\u4e0e\u9519\u8bef\u5904\u7406<\/h4>\n<h5>&#xff08;1&#xff09;\u8fd4\u56de\u503c<\/h5>\n<ul>\n<li>\u6210\u529f&#xff1a;\u8fd4\u56de 0&#xff1b;<\/li>\n<li>\u5931\u8d25&#xff1a;\u8fd4\u56de -1&#xff0c;\u5e76\u8bbe\u7f6e\u5168\u5c40\u53d8\u91cf errno \u6807\u8bc6\u9519\u8bef\u7c7b\u578b\u3002<\/li>\n<\/ul>\n<h5>&#xff08;2&#xff09;\u5e38\u89c1\u9519\u8bef\u7801&#xff08;errno&#xff09;<\/h5>\n<table>\n<tr>\u9519\u8bef\u7801\u542b\u4e49\u89e3\u51b3\u65b9\u6cd5<\/tr>\n<tbody>\n<tr>\n<td>EMFILE<\/td>\n<td>\u8fdb\u7a0b\u6253\u5f00\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u8fbe\u5230\u4e0a\u9650<\/td>\n<td>\u5173\u95ed\u65e0\u7528\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u6216\u63d0\u5347\u8fdb\u7a0b\u6587\u4ef6\u63cf\u8ff0\u7b26\u9650\u5236<\/td>\n<\/tr>\n<tr>\n<td>ENFILE<\/td>\n<td>\u7cfb\u7edf\u6253\u5f00\u7684\u6587\u4ef6\u603b\u6570\u8fbe\u5230\u4e0a\u9650<\/td>\n<td>\u6e05\u7406\u7cfb\u7edf\u4e2d\u65e0\u7528\u7684\u6587\u4ef6 \/ \u8fdb\u7a0b<\/td>\n<\/tr>\n<tr>\n<td>EINVAL<\/td>\n<td>\u53c2\u6570\u65e0\u6548&#xff08;\u5982 pipefd \u4e3a NULL&#xff09;<\/td>\n<td>\u786e\u4fdd\u4f20\u5165\u957f\u5ea6\u4e3a 2 \u7684\u6709\u6548\u6570\u7ec4<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>4. \u6838\u5fc3\u5de5\u4f5c\u539f\u7406<\/h4>\n<h5>&#xff08;1&#xff09;\u7ba1\u9053\u7684\u672c\u8d28<\/h5>\n<p>\u533f\u540d\u7ba1\u9053\u662f\u5185\u6838\u4e2d\u7684\u4e00\u5757\u73af\u5f62\u7f13\u51b2\u533a&#xff08;\u9ed8\u8ba4\u5927\u5c0f\u901a\u5e38\u4e3a 4KB \u6216 64KB&#xff0c;\u53ef\u901a\u8fc7 fcntl() \u4fee\u6539&#xff09;&#xff0c;\u6570\u636e\u5199\u5165\u540e\u6682\u5b58\u4e8e\u5185\u6838&#xff0c;\u8bfb\u53d6\u540e\u4ece\u7f13\u51b2\u533a\u79fb\u9664\u3002<\/p>\n<h5>&#xff08;2&#xff09;\u5173\u952e\u7279\u6027<\/h5>\n<table>\n<tr>\u7279\u6027\u8bf4\u660e<\/tr>\n<tbody>\n<tr>\n<td>\u963b\u585e\u6027<\/td>\n<td>\u2460 \u8bfb\u7aef\u8bfb\u53d6\u65f6&#xff0c;\u82e5\u7ba1\u9053\u4e3a\u7a7a \u2192 \u8bfb\u8fdb\u7a0b\u963b\u585e&#xff0c;\u76f4\u5230\u6709\u6570\u636e\u5199\u5165&#xff1b;\u2461 \u5199\u7aef\u5199\u5165\u65f6&#xff0c;\u82e5\u7ba1\u9053\u6ee1 \u2192 \u5199\u8fdb\u7a0b\u963b\u585e&#xff0c;\u76f4\u5230\u6570\u636e\u88ab\u8bfb\u53d6&#xff1b;\u2462 \u82e5\u5199\u7aef\u5173\u95ed&#xff0c;\u8bfb\u7aef\u8bfb\u53d6\u5b8c\u6570\u636e\u540e\u4f1a\u8fd4\u56de 0&#xff08;\u8868\u793a EOF&#xff09;&#xff1b;\u2463 \u82e5\u8bfb\u7aef\u5173\u95ed&#xff0c;\u5199\u7aef\u5199\u5165\u4f1a\u89e6\u53d1 SIGPIPE \u4fe1\u53f7&#xff0c;\u8fdb\u7a0b\u9ed8\u8ba4\u7ec8\u6b62<\/td>\n<\/tr>\n<tr>\n<td>\u6570\u636e\u539f\u5b50\u6027<\/td>\n<td>\u5199\u5165\u6570\u636e\u91cf \u2264 PIPE_BUF&#xff08;\u7ba1\u9053\u7f13\u51b2\u533a\u5927\u5c0f&#xff0c;\u901a\u5e38 4096 \u5b57\u8282&#xff09;\u65f6&#xff0c;\u5199\u5165\u662f\u539f\u5b50\u7684&#xff08;\u4e0d\u4f1a\u88ab\u6253\u65ad&#xff09;&#xff1b;\u8d85\u8fc7\u5219\u53ef\u80fd\u88ab\u62c6\u5206<\/td>\n<\/tr>\n<tr>\n<td>\u751f\u547d\u5468\u671f<\/td>\n<td>\u7ba1\u9053\u968f\u8fdb\u7a0b\u751f\u547d\u5468\u671f\u7ed3\u675f\u800c\u9500\u6bc1&#xff1a;\u6240\u6709\u6301\u6709\u7ba1\u9053\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u8fdb\u7a0b\u9000\u51fa\u540e&#xff0c;\u5185\u6838\u81ea\u52a8\u91ca\u653e\u7ba1\u9053\u7f13\u51b2\u533a<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"373\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260130045719-697c3a2f2da2b.png\" width=\"827\" \/><\/p>\n<p>\u8fd9\u5f20\u56fe\u4e5f\u5145\u5206\u8bf4\u660e\u4e86\u7ba1\u9053\u672c\u8d28\u662f\u5185\u6838\u4e2d\u7684\u4e00\u5757\u73af\u5f62\u7f13\u51b2\u533a.<\/p>\n<p>\u56e0\u4e3a\u533f\u540d\u7ba1\u9053\u7684\u5b9e\u73b0\u9700\u8981\u6709\u8840\u7f18\u5173\u7cfb\u7684\u8fdb\u7a0b&#xff0c;\u56e0\u6b64\u9700\u8981\u4f7f\u7528fork\u51fd\u6570\u6765\u5b9e\u73b0.<\/p>\n<p>  #include &lt;iostream&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;sys\/wait.h&gt;<br \/>\n#include &lt;cstring&gt;<br \/>\n#include &lt;cstdio&gt;<\/p>\n<p>using namespace std;<\/p>\n<p>int main() {<br \/>\n    \/\/ 1. \u521b\u5efa\u7ba1\u9053<br \/>\n    int pipefd[2];<br \/>\n    if (pipe(pipefd) &#061;&#061; -1) {<br \/>\n        perror(&#034;pipe failed&#034;);<br \/>\n        return 1;<br \/>\n    }<\/p>\n<p>    \/\/ 2. \u521b\u5efa\u5b50\u8fdb\u7a0b<br \/>\n    pid_t pid &#061; fork();<br \/>\n    if (pid &#061;&#061; -1) {<br \/>\n        perror(&#034;fork failed&#034;);<br \/>\n        return 1;<br \/>\n    }<\/p>\n<p>    \/\/ &#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061; \u5b50\u8fdb\u7a0b&#xff08;\u8bfb\u6570\u636e&#xff09;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;<br \/>\n    if (pid &#061;&#061; 0) {<br \/>\n        \/\/ 3. \u5173\u95ed\u5b50\u8fdb\u7a0b\u4e0d\u9700\u8981\u7684\u5199\u7aef&#xff08;\u6838\u5fc3&#xff01;\u5426\u5219\u8bfb\u7aef\u4f1a\u4e00\u76f4\u963b\u585e&#xff09;<br \/>\n        close(pipefd[1]);<\/p>\n<p>        char buf[1024] &#061; {0};<br \/>\n        \/\/ 4. \u4ece\u8bfb\u7aef\u8bfb\u53d6\u6570\u636e&#xff08;\u963b\u585e\u7b49\u5f85&#xff09;<br \/>\n        ssize_t read_len &#061; read(pipefd[0], buf, sizeof(buf)-1);<br \/>\n        if (read_len &gt; 0) {<br \/>\n            cout &lt;&lt; &#034;[\u5b50\u8fdb\u7a0b] \u8bfb\u53d6\u5230\u6570\u636e&#xff1a;&#034; &lt;&lt; buf &lt;&lt; endl;<br \/>\n        } else if (read_len &#061;&#061; 0) {<br \/>\n            cout &lt;&lt; &#034;[\u5b50\u8fdb\u7a0b] \u7ba1\u9053\u5199\u7aef\u5df2\u5168\u90e8\u5173\u95ed&#034; &lt;&lt; endl;<br \/>\n        } else {<br \/>\n            perror(&#034;read failed&#034;);<br \/>\n        }<\/p>\n<p>        \/\/ 5. \u5173\u95ed\u8bfb\u7aef<br \/>\n        close(pipefd[0]);<br \/>\n        return 0;<br \/>\n    }<\/p>\n<p>    \/\/ &#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061; \u7236\u8fdb\u7a0b&#xff08;\u5199\u6570\u636e&#xff09;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;<br \/>\n    \/\/ 3. \u5173\u95ed\u7236\u8fdb\u7a0b\u4e0d\u9700\u8981\u7684\u8bfb\u7aef<br \/>\n    close(pipefd[0]);<\/p>\n<p>    \/\/ 4. \u5411\u5199\u7aef\u5199\u5165\u6570\u636e<br \/>\n    const char* msg &#061; &#034;Hello, this is parent process!&#034;;<br \/>\n    ssize_t write_len &#061; write(pipefd[1], msg, strlen(msg)&#043;1); \/\/ &#043;1 \u5305\u542b\u5b57\u7b26\u4e32\u7ed3\u675f\u7b26<br \/>\n    if (write_len &#061;&#061; -1) {<br \/>\n        perror(&#034;write failed&#034;);<br \/>\n    } else {<br \/>\n        cout &lt;&lt; &#034;[\u7236\u8fdb\u7a0b] \u5199\u5165\u6570\u636e\u6210\u529f&#xff0c;\u957f\u5ea6&#xff1a;&#034; &lt;&lt; write_len &lt;&lt; endl;<br \/>\n    }<\/p>\n<p>    \/\/ 5. \u5173\u95ed\u5199\u7aef&#xff08;\u89e6\u53d1\u5b50\u8fdb\u7a0b\u8bfb\u7aef\u8fd4\u56deEOF&#xff09;<br \/>\n    close(pipefd[1]);<\/p>\n<p>    \/\/ \u7b49\u5f85\u5b50\u8fdb\u7a0b\u6267\u884c\u5b8c\u6bd5<br \/>\n    wait(nullptr);<br \/>\n    cout &lt;&lt; &#034;[\u7236\u8fdb\u7a0b] \u5b50\u8fdb\u7a0b\u5df2\u9000\u51fa&#034; &lt;&lt; endl;<\/p>\n<p>    return 0;<br \/>\n} <\/p>\n<h4><span style=\"color:#333333\">5.\u7528<\/span><span style=\"color:#333333\">fork<\/span><span style=\"color:#333333\">\u6765\u89e3\u91ca\u5171\u4eab\u7ba1\u9053\u539f\u7406(\u56fe\u52a0\u6587\u5b57\u89e3\u6790)<\/span><\/h4>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"292\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260130045719-697c3a2fad8a4.png\" width=\"902\" \/><\/p>\n<h5>1. \u5de6\u56fe&#xff1a;fork() \u4e4b\u540e\u7684\u521d\u59cb\u72b6\u6001<\/h5>\n<ul>\n<li>\u7236\u8fdb\u7a0b\u8c03\u7528 pipe() \u521b\u5efa\u7ba1\u9053&#xff0c;\u5f97\u5230\u8bfb\u7aef fd[0] \u548c\u5199\u7aef fd[1]\u3002<\/li>\n<li>\u8c03\u7528 fork() \u521b\u5efa\u5b50\u8fdb\u7a0b\u540e&#xff0c;\u5b50\u8fdb\u7a0b\u4f1a\u5b8c\u6574\u7ee7\u627f\u7236\u8fdb\u7a0b\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u8868&#xff0c;\u56e0\u6b64\u4e5f\u62e5\u6709 fd[0] \u548c fd[1]\u3002<\/li>\n<li>\u6b64\u65f6\u7236\u5b50\u8fdb\u7a0b\u90fd\u80fd\u540c\u65f6\u8bfb\u5199\u7ba1\u9053&#xff0c;\u4f46\u8fd9\u6837\u65e0\u6cd5\u5f62\u6210\u5355\u5411\u901a\u4fe1&#xff0c;\u4e14\u4f1a\u5bfc\u81f4\u8d44\u6e90\u6cc4\u6f0f\u548c\u963b\u585e\u95ee\u9898\u3002<\/li>\n<\/ul>\n<h5>2. \u53f3\u56fe&#xff1a;\u5173\u95ed\u65e0\u7528\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff08;\u5173\u952e\u4e00\u6b65&#xff09;<\/h5>\n<p>\u4e3a\u4e86\u5b9e\u73b0\u5355\u5411\u901a\u4fe1&#xff0c;\u5fc5\u987b\u8ba9\u7236\u5b50\u8fdb\u7a0b\u5404\u81ea\u5173\u95ed\u4e0d\u9700\u8981\u7684\u7ba1\u9053\u7aef&#xff1a;<\/p>\n<ul>\n<li>\u7236\u8fdb\u7a0b\u4fdd\u7559\u5199\u7aef fd[1]&#xff0c;\u5173\u95ed\u8bfb\u7aef fd[0] \u2192 \u53ea\u8d1f\u8d23\u5411\u7ba1\u9053\u5199\u5165\u6570\u636e\u3002<\/li>\n<li>\u5b50\u8fdb\u7a0b\u4fdd\u7559\u8bfb\u7aef fd[0]&#xff0c;\u5173\u95ed\u5199\u7aef fd[1] \u2192 \u53ea\u8d1f\u8d23\u4ece\u7ba1\u9053\u8bfb\u53d6\u6570\u636e\u3002<\/li>\n<li>\u8fd9\u6837\u5c31\u5f62\u6210\u4e86\u7236\u5199\u5b50\u8bfb\u7684\u5355\u5411\u901a\u4fe1\u901a\u9053&#xff0c;\u907f\u514d\u4e86\u8d44\u6e90\u6cc4\u6f0f\u548c\u4e0d\u5fc5\u8981\u7684\u963b\u585e\u3002<\/li>\n<\/ul>\n<h5>\u4e3a\u4ec0\u4e48\u5fc5\u987b\u5173\u95ed\u65e0\u7528\u7aef&#xff1f;<\/h5>\n<ul>\n<li>\u907f\u514d\u8d44\u6e90\u6cc4\u6f0f&#xff1a;\u6587\u4ef6\u63cf\u8ff0\u7b26\u662f\u6709\u9650\u8d44\u6e90&#xff0c;\u4e0d\u5173\u95ed\u4f1a\u5bfc\u81f4\u8fdb\u7a0b\u6253\u5f00\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u6570\u91cf\u8d85\u9650\u3002<\/li>\n<li>\u9632\u6b62\u9519\u8bef\u963b\u585e&#xff1a;\u5982\u679c\u5b50\u8fdb\u7a0b\u4e0d\u5173\u95ed\u5199\u7aef&#xff0c;\u5373\u4f7f\u7236\u8fdb\u7a0b\u5173\u95ed\u4e86\u5199\u7aef&#xff0c;\u5b50\u8fdb\u7a0b\u7684\u8bfb\u7aef\u4f1a\u4e00\u76f4\u963b\u585e&#xff08;\u5185\u6838\u8ba4\u4e3a\u8fd8\u6709\u8fdb\u7a0b\u53ef\u80fd\u5199\u5165\u6570\u636e&#xff09;\u3002<\/li>\n<li>\u89e6\u53d1 EOF \u4fe1\u53f7&#xff1a;\u5f53\u6240\u6709\u5199\u7aef\u90fd\u5173\u95ed\u65f6&#xff0c;\u8bfb\u7aef\u8bfb\u53d6\u5b8c\u5269\u4f59\u6570\u636e\u540e\u4f1a\u8fd4\u56de 0&#xff08;\u8868\u793a EOF&#xff09;&#xff0c;\u8ba9\u8bfb\u8fdb\u7a0b\u77e5\u9053\u6570\u636e\u5df2\u4f20\u8f93\u5b8c\u6bd5\u3002<\/li>\n<\/ul>\n<h4><span style=\"color:#333333\">6.\u7ad9\u5728\u6587\u4ef6\u63cf\u8ff0\u7b26\u89d2\u5ea6<\/span><span style=\"color:#333333\">&#8211;<\/span><span style=\"color:#333333\">\u6df1\u5ea6\u7406\u89e3\u7ba1\u9053(\u56fe&#043;\u6587\u5b57)<\/span><\/h4>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"649\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260130045720-697c3a3020150.png\" width=\"700\" \/><\/p>\n<p>\u4e3a\u4ec0\u4e48\u4ece3\u5f00\u59cb&#xff0c;\u56e0\u4e3a0&#xff0c;1&#xff0c;2\u5206\u522b\u5bf9\u5e94\u6807\u51c6\u8f93\u5165\u3001\u6807\u51c6\u8f93\u51fa\u548c\u6807\u51c6\u9519\u8bef\u3002\u5e76\u4e14\u662f\u5728\u8fdb\u7a0b\u542f\u52a8\u65f6\u5c31\u9ed8\u8ba4\u6253\u5f00\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u3002<\/p>\n<h5>1. \u7b2c\u4e00\u6b65&#xff1a;\u7236\u8fdb\u7a0b\u521b\u5efa\u7ba1\u9053<\/h5>\n<ul>\n<li>\u7236\u8fdb\u7a0b\u8c03\u7528 pipe()&#xff0c;\u5185\u6838\u521b\u5efa\u4e00\u4e2a\u7ba1\u9053&#xff0c;\u5e76\u8fd4\u56de\u4e24\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff1a;\n<ul>\n<li>fd[0] &#061; 3&#xff1a;\u7ba1\u9053\u7684\u8bfb\u7aef<\/li>\n<li>fd[1] &#061; 4&#xff1a;\u7ba1\u9053\u7684\u5199\u7aef<\/li>\n<\/ul>\n<\/li>\n<li>\u6b64\u65f6\u7236\u8fdb\u7a0b\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u8868\u4e2d&#xff0c;3 \u548c 4 \u5206\u522b\u6307\u5411\u7ba1\u9053\u7684\u8bfb\u7aef\u548c\u5199\u7aef&#xff0c;\u8fdb\u7a0b\u53ef\u4ee5\u540c\u65f6\u8bfb\u5199\u3002<\/li>\n<\/ul>\n<h5>2. \u7b2c\u4e8c\u6b65&#xff1a;\u7236\u8fdb\u7a0b fork() \u51fa\u5b50\u8fdb\u7a0b<\/h5>\n<ul>\n<li>\u5b50\u8fdb\u7a0b\u4f1a\u5b8c\u6574\u7ee7\u627f\u7236\u8fdb\u7a0b\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u8868&#xff0c;\u56e0\u6b64\u4e5f\u62e5\u6709 fd[0] &#061; 3 \u548c fd[1] &#061; 4\u3002<\/li>\n<li>\u6b64\u65f6\u7236\u5b50\u8fdb\u7a0b\u90fd\u80fd\u540c\u65f6\u8bbf\u95ee\u7ba1\u9053\u7684\u8bfb\u7aef\u548c\u5199\u7aef&#xff0c;\u4f46\u8fd9\u4f1a\u5bfc\u81f4\u901a\u4fe1\u6df7\u4e71\u548c\u8d44\u6e90\u6cc4\u6f0f\u3002<\/li>\n<\/ul>\n<h5>3. \u7b2c\u4e09\u6b65&#xff1a;\u5173\u95ed\u65e0\u7528\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff08;\u5173\u952e&#xff09;<\/h5>\n<ul>\n<li>\u7236\u8fdb\u7a0b\u5173\u95ed\u8bfb\u7aef fd[0] &#061; 3&#xff0c;\u53ea\u4fdd\u7559\u5199\u7aef fd[1] &#061; 4 \u2192 \u8d1f\u8d23\u5411\u7ba1\u9053\u5199\u5165\u6570\u636e\u3002<\/li>\n<li>\u5b50\u8fdb\u7a0b\u5173\u95ed\u5199\u7aef fd[1] &#061; 4&#xff0c;\u53ea\u4fdd\u7559\u8bfb\u7aef fd[0] &#061; 3 \u2192 \u8d1f\u8d23\u4ece\u7ba1\u9053\u8bfb\u53d6\u6570\u636e\u3002<\/li>\n<li>\u8fd9\u6837\u5c31\u5f62\u6210\u4e86\u7236\u5199\u5b50\u8bfb\u7684\u5355\u5411\u901a\u4fe1\u901a\u9053&#xff0c;\u907f\u514d\u4e86\u963b\u585e\u548c\u8d44\u6e90\u6cc4\u6f0f\u3002<\/li>\n<\/ul>\n<h5>\u6838\u5fc3\u610f\u4e49<\/h5>\n<ul>\n<li>\u8fd9\u662f Linux \u4e2d\u5b9e\u73b0\u7236\u5b50\u8fdb\u7a0b\u901a\u4fe1\u7684\u6807\u51c6\u6d41\u7a0b&#xff0c;\u4e5f\u662f | \u7ba1\u9053\u7b26&#xff08;\u5982 who | wc -l&#xff09;\u7684\u5e95\u5c42\u5b9e\u73b0\u539f\u7406\u3002<\/li>\n<li>\u5173\u95ed\u65e0\u7528\u7aef\u662f\u5173\u952e&#xff0c;\u5b83\u786e\u4fdd\u4e86\u6570\u636e\u5355\u5411\u6d41\u52a8&#xff0c;\u5e76\u4e14\u8ba9\u5185\u6838\u80fd\u591f\u6b63\u786e\u8bc6\u522b\u7ba1\u9053\u7684\u6d3b\u8dc3\u72b6\u6001&#xff08;\u5982\u5199\u7aef\u5168\u90e8\u5173\u95ed\u65f6&#xff0c;\u8bfb\u7aef\u4f1a\u8fd4\u56de EOF&#xff09;\u3002<\/li>\n<\/ul>\n<h4><span style=\"color:#333333\">7.\u7ad9\u5728\u5185\u6838\u89d2\u5ea6<\/span><span style=\"color:#333333\">&#8211;<\/span><span style=\"color:#333333\">\u7ba1\u9053\u672c\u8d28(\u56fe&#043;\u6587)<\/span><\/h4>\n<h5>1. \u6838\u5fc3\u7ed3\u6784\u89e3\u6790<\/h5>\n<ul>\n<li>\n<p>\u8fdb\u7a0b 1\/2 \u7684 file \u7ed3\u6784\u6bcf\u4e2a\u8fdb\u7a0b\u5728\u6253\u5f00\u7ba1\u9053\u65f6&#xff0c;\u90fd\u4f1a\u521b\u5efa\u4e00\u4e2a file \u7ed3\u6784\u4f53\u5b9e\u4f8b&#xff0c;\u7528\u6765\u63cf\u8ff0\u8fdb\u7a0b\u5bf9\u7ba1\u9053\u7684\u8bbf\u95ee\u72b6\u6001&#xff1a;<\/p>\n<ul>\n<li>f_mode&#xff1a;\u8bbf\u95ee\u6a21\u5f0f&#xff08;\u8bfb \/ \u5199&#xff09;<\/li>\n<li>f_pos&#xff1a;\u5f53\u524d\u8bfb\u5199\u4f4d\u7f6e<\/li>\n<li>f_flags&#xff1a;\u6587\u4ef6\u72b6\u6001\u6807\u5fd7&#xff08;\u5982 O_RDONLY\u3001O_WRONLY&#xff09;<\/li>\n<li>f_inode&#xff1a;\u6307\u5411\u7ba1\u9053\u7684 inode \u7ed3\u6784\u4f53&#xff0c;\u662f\u4e24\u4e2a\u8fdb\u7a0b\u5171\u4eab\u7ba1\u9053\u7684\u6838\u5fc3\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u7ba1\u9053\u7684 inode \u7ed3\u6784\u8fd9\u662f\u7ba1\u9053\u7684\u5185\u6838\u5143\u6570\u636e&#xff0c;\u5b58\u50a8\u4e86\u7ba1\u9053\u7684\u5168\u5c40\u72b6\u6001&#xff08;\u5982\u7f13\u51b2\u533a\u5927\u5c0f\u3001\u5f15\u7528\u8ba1\u6570&#xff09;&#xff0c;\u662f\u4e24\u4e2a\u8fdb\u7a0b\u7684 file \u7ed3\u6784\u7684\u5171\u540c\u6307\u5411&#xff0c;\u786e\u4fdd\u5b83\u4eec\u8bbf\u95ee\u7684\u662f\u540c\u4e00\u4e2a\u7ba1\u9053\u3002<\/p>\n<\/li>\n<li>\n<p>\u6570\u636e\u9875\u7ba1\u9053\u7684\u5b9e\u9645\u6570\u636e\u5b58\u50a8\u533a&#xff0c;\u662f\u5185\u6838\u4e2d\u7684\u4e00\u5757\u5185\u5b58\u9875&#xff08;\u901a\u5e38 4KB&#xff09;&#xff0c;\u8fdb\u7a0b 1 \u7684\u5199\u64cd\u4f5c\u5c06\u6570\u636e\u5199\u5165\u8fd9\u91cc&#xff0c;\u8fdb\u7a0b 2 \u7684\u8bfb\u64cd\u4f5c\u4ece\u8fd9\u91cc\u8bfb\u53d6\u6570\u636e\u3002<\/p>\n<\/li>\n<\/ul>\n<h5>2. \u901a\u4fe1\u6d41\u7a0b<\/h5>\n<li>\n<p>\u8fdb\u7a0b 1 \u5199\u64cd\u4f5c\u8fdb\u7a0b 1 \u901a\u8fc7\u5176 file \u7ed3\u6784\u627e\u5230\u7ba1\u9053\u7684 inode&#xff0c;\u7136\u540e\u8c03\u7528 write \u5c06\u6570\u636e\u5199\u5165\u5185\u6838\u7684\u6570\u636e\u9875\u3002<\/p>\n<\/li>\n<li>\n<p>\u8fdb\u7a0b 2 \u8bfb\u64cd\u4f5c\u8fdb\u7a0b 2 \u901a\u8fc7\u5176 file \u7ed3\u6784\u627e\u5230\u540c\u4e00\u4e2a inode&#xff0c;\u7136\u540e\u8c03\u7528 read \u4ece\u6570\u636e\u9875\u4e2d\u8bfb\u53d6\u6570\u636e\u3002<\/p>\n<\/li>\n<li>\n<p>\u6838\u5fc3\u7279\u70b9<\/p>\n<ul>\n<li>\u6570\u636e\u76f4\u63a5\u5728\u5185\u6838\u4e2d\u4f20\u9012&#xff0c;\u65e0\u9700\u5728\u7528\u6237\u7a7a\u95f4\u4e4b\u95f4\u62f7\u8d1d&#xff0c;\u6548\u7387\u8f83\u9ad8\u3002<\/li>\n<li>\u4e24\u4e2a\u8fdb\u7a0b\u7684 file \u7ed3\u6784\u5171\u4eab\u540c\u4e00\u4e2a inode \u548c\u6570\u636e\u9875&#xff0c;\u8fd9\u662f\u7ba1\u9053\u80fd\u5b9e\u73b0\u8fdb\u7a0b\u95f4\u901a\u4fe1\u7684\u672c\u8d28\u3002<\/li>\n<\/ul>\n<\/li>\n<h5>3. \u5173\u952e\u610f\u4e49<\/h5>\n<p>\u8fd9\u5f20\u56fe\u63ed\u793a\u4e86\u7ba1\u9053\u7684\u5e95\u5c42\u5b9e\u73b0&#xff1a;\u5b83\u5e76\u975e\u4e00\u4e2a\u771f\u5b9e\u7684\u6587\u4ef6&#xff0c;\u800c\u662f\u5185\u6838\u4e2d\u7684\u4e00\u5757\u5185\u5b58\u7f13\u51b2\u533a&#xff0c;\u901a\u8fc7 file \u548c inode \u7ed3\u6784\u8ba9\u591a\u4e2a\u8fdb\u7a0b\u53ef\u4ee5\u5171\u4eab\u8bbf\u95ee\u3002\u8fd9\u4e5f\u89e3\u91ca\u4e86\u4e3a\u4ec0\u4e48\u533f\u540d\u7ba1\u9053\u4ec5\u80fd\u5728\u6709\u4eb2\u7f18\u5173\u7cfb\u7684\u8fdb\u7a0b\u95f4\u4f7f\u7528 \u2014\u2014 \u56e0\u4e3a\u5b83\u4eec\u9700\u8981\u7ee7\u627f\u8fd9\u4e9b\u5185\u6838\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<p><span style=\"color:#333333\">\u6240\u4ee5&#xff0c;\u770b\u5f85\u7ba1\u9053&#xff0c;\u5c31\u5982\u540c\u770b\u5f85\u6587\u4ef6\u4e00\u6837&#xff01;\u7ba1\u9053\u7684\u4f7f\u7528\u548c\u6587\u4ef6\u4e00\u81f4&#xff0c;\u8fce\u5408\u4e86<\/span><span style=\"color:#333333\">\u201cLinux<\/span><span style=\"color:#333333\">\u4e00\u5207\u7686\u6587\u4ef6\u601d\u60f3&#034;.<\/span><\/p>\n<p>   <span style=\"color:#333333\">\u4f8b\u5b50<\/span><br \/>\n   <span style=\"color:#333333\">1. <\/span><br \/>\n   <span style=\"color:#333333\">\u5728<\/span><br \/>\n   <span style=\"color:#333333\">minishell<\/span><br \/>\n   <span style=\"color:#333333\">\u4e2d\u6dfb\u52a0\u7ba1\u9053\u7684\u5b9e\u73b0&#xff1a;<\/span><\/p>\n<p>   # include &lt;stdio.h&gt;<br \/>\n# include &lt;stdlib.h&gt;<br \/>\n# include &lt;unistd.h&gt;<br \/>\n# include &lt;string.h&gt;<br \/>\n# include &lt;fcntl.h&gt;<br \/>\n# define MAX_CMD 1024<br \/>\nchar command[MAX_CMD];<br \/>\nint do_face()<br \/>\n{<br \/>\n memset(command, 0x00, MAX_CMD);<br \/>\n printf(&#034;minishell$ &#034;);<br \/>\n fflush(stdout);<br \/>\n if (scanf(&#034;%[^\\\\n]%*c&#034;, command) &#061;&#061; 0) {<br \/>\n getchar();<br \/>\n return -1;<br \/>\n }<br \/>\n return 0;<br \/>\n}<br \/>\nchar **do_parse(char *buff)<br \/>\n{<br \/>\n int argc &#061; 0;<br \/>\n static char *argv[32];<br \/>\n char *ptr &#061; buff;<br \/>\n while(*ptr !&#061; &#039;\\\\0&#039;) {<br \/>\n if (!isspace(*ptr)) {<br \/>\n argv[argc&#043;&#043;] &#061; ptr;<br \/>\n while((!isspace(*ptr)) &amp;&amp; (*ptr) !&#061; &#039;\\\\0&#039;) {<br \/>\n ptr&#043;&#043;;<br \/>\n }<br \/>\n }else {<br \/>\n while(isspace(*ptr)) {<br \/>\n *ptr &#061; &#039;\\\\0&#039;;<br \/>\n ptr&#043;&#043;;<br \/>\n }<br \/>\n }<br \/>\n }<br \/>\n argv[argc] &#061; NULL;<br \/>\n return argv;<br \/>\n}<br \/>\nint do_redirect(char *buff)<br \/>\n{<br \/>\n char *ptr &#061; buff, *file &#061; NULL;<br \/>\n int type &#061; 0, fd, redirect_type &#061; -1;<br \/>\n while(*ptr !&#061; &#039;\\\\0&#039;) {<br \/>\n if (*ptr &#061;&#061; &#039;&gt;&#039;) {<br \/>\n *ptr&#043;&#043; &#061; &#039;\\\\0&#039;;<br \/>\n redirect_type&#043;&#043;;<br \/>\nif (*ptr &#061;&#061; &#039;&gt;&#039;) {<br \/>\n *ptr&#043;&#043; &#061; &#039;\\\\0&#039;;<br \/>\n redirect_type&#043;&#043;;<br \/>\n }<br \/>\n while(isspace(*ptr)) {<br \/>\n ptr&#043;&#043;;<br \/>\n }<br \/>\n file &#061; ptr;<br \/>\n while((!isspace(*ptr)) &amp;&amp; *ptr !&#061; &#039;\\\\0&#039;) {<br \/>\n ptr&#043;&#043;;<br \/>\n }<br \/>\n *ptr &#061; &#039;\\\\0&#039;;<br \/>\n if (redirect_type &#061;&#061; 0) {<br \/>\n fd &#061; open(file, O_CREAT|O_TRUNC|O_WRONLY, 0664);<br \/>\n }else {<br \/>\n fd &#061; open(file, O_CREAT|O_APPEND|O_WRONLY, 0664);<br \/>\n }<br \/>\n dup2(fd, 1);<br \/>\n }<br \/>\n ptr&#043;&#043;;<br \/>\n }<br \/>\n return 0;<br \/>\n}<br \/>\nint do_command(char *buff)<br \/>\n{<br \/>\n int pipe_num &#061; 0, i;<br \/>\n char *ptr &#061; buff;<br \/>\n int pipefd[32][2] &#061; {{-1}};<br \/>\n int pid &#061; -1;<br \/>\n pipe_command[pipe_num] &#061; ptr;<br \/>\n while(*ptr !&#061; &#039;\\\\0&#039;) {<br \/>\n if (*ptr &#061;&#061; &#039;|&#039;) {<br \/>\n pipe_num&#043;&#043;;<br \/>\n *ptr&#043;&#043; &#061; &#039;\\\\0&#039;;<br \/>\n pipe_command[pipe_num] &#061; ptr;<br \/>\n continue;<br \/>\n }<br \/>\n ptr&#043;&#043;;<br \/>\n }<br \/>\n pipe_command[pipe_num &#043; 1] &#061; NULL;<br \/>\n return pipe_num;<br \/>\n}<br \/>\nint do_pipe(int pipe_num)<br \/>\n{<br \/>\n int pid &#061; 0, i;<br \/>\n int pipefd[10][2] &#061; {{0}};<br \/>\n char **argv &#061; {NULL};<br \/>\n for (i &#061; 0; i &lt;&#061; pipe_num; i&#043;&#043;) {<br \/>\n pipe(pipefd[i]);<br \/>\n }<br \/>\n for (i &#061; 0; i &lt;&#061; pipe_num; i&#043;&#043;) {<br \/>\npid &#061; fork();<br \/>\n if (pid &#061;&#061; 0) {<br \/>\n do_redirect(pipe_command[i]);<br \/>\n argv &#061; do_parse(pipe_command[i]);<br \/>\n if (i !&#061; 0) {<br \/>\n close(pipefd[i][1]);<br \/>\n dup2(pipefd[i][0], 0);<br \/>\n }<br \/>\n if (i !&#061; pipe_num) {<br \/>\n close(pipefd[i &#043; 1][0]);<br \/>\n dup2(pipefd[i &#043; 1][1], 1);<br \/>\n }<br \/>\n execvp(argv[0], argv);<br \/>\n }else {<br \/>\n close(pipefd[i][0]);<br \/>\n close(pipefd[i][1]);<br \/>\n waitpid(pid, NULL, 0);<br \/>\n }<br \/>\n }<br \/>\n return 0;<br \/>\n}<br \/>\nint main(int argc, char *argv[])<br \/>\n{<br \/>\n int num &#061; 0;<br \/>\n while(1) {<br \/>\n if (do_face() &lt; 0)<br \/>\n continue;<br \/>\n num &#061; do_command(command);<br \/>\n do_pipe(num);<br \/>\n }<br \/>\n return 0;<br \/>\n} <\/p>\n<h4><span style=\"color:#333333\">8.\u7ba1\u9053\u8bfb\u5199\u89c4\u5219<\/span><\/h4>\n<p>    <span style=\"color:#777777\">\u5f53\u6ca1\u6709\u6570\u636e\u53ef\u8bfb\u65f6 <\/span><\/p>\n<p>    <span style=\"color:#777777\">O_NONBLOCK disable<\/span><br \/>\n    <span style=\"color:#777777\">&#xff1a;<\/span><br \/>\n    <span style=\"color:#777777\">read<\/span><br \/>\n    <span style=\"color:#777777\">\u8c03\u7528\u963b\u585e&#xff0c;\u5373\u8fdb\u7a0b\u6682\u505c\u6267\u884c&#xff0c;\u4e00\u76f4\u7b49\u5230\u6709\u6570\u636e\u6765\u5230\u4e3a\u6b62\u3002 <\/span><\/p>\n<p>    <span style=\"color:#777777\">O_NONBLOCK enable<\/span><br \/>\n    <span style=\"color:#777777\">&#xff1a;<\/span><br \/>\n    <span style=\"color:#777777\">read<\/span><br \/>\n    <span style=\"color:#777777\">\u8c03\u7528\u8fd4\u56de<\/span><br \/>\n    <span style=\"color:#777777\">-1<\/span><br \/>\n    <span style=\"color:#777777\">&#xff0c;<\/span><br \/>\n    <span style=\"color:#777777\">errno<\/span><br \/>\n    <span style=\"color:#777777\">\u503c\u4e3a<\/span><br \/>\n    <span style=\"color:#777777\">EAGAIN<\/span><br \/>\n    <span style=\"color:#777777\">\u3002 <\/span><\/p>\n<p>    <span style=\"color:#777777\">\u5f53\u7ba1\u9053\u6ee1\u7684\u65f6\u5019 <\/span><\/p>\n<p>    <span style=\"color:#777777\">O_NONBLOCK disable<\/span><br \/>\n    <span style=\"color:#777777\">&#xff1a;<\/span><br \/>\n    <span style=\"color:#777777\"> write<\/span><br \/>\n    <span style=\"color:#777777\">\u8c03\u7528\u963b\u585e&#xff0c;\u76f4\u5230\u6709\u8fdb\u7a0b\u8bfb\u8d70\u6570\u636e <\/span><\/p>\n<p>    <span style=\"color:#777777\">O_NONBLOCK enable<\/span><br \/>\n    <span style=\"color:#777777\">&#xff1a;\u8c03\u7528\u8fd4\u56de<\/span><br \/>\n    <span style=\"color:#777777\">-1<\/span><br \/>\n    <span style=\"color:#777777\">&#xff0c;<\/span><br \/>\n    <span style=\"color:#777777\">errno<\/span><br \/>\n    <span style=\"color:#777777\">\u503c\u4e3a<\/span><br \/>\n    <span style=\"color:#777777\">EAGAIN <\/span><\/p>\n<p>    <span style=\"color:#777777\">\u5982\u679c\u6240\u6709\u7ba1\u9053\u5199\u7aef\u5bf9\u5e94\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u88ab\u5173\u95ed&#xff0c;\u5219<\/span><br \/>\n    <span style=\"color:#777777\">read<\/span><br \/>\n    <span style=\"color:#777777\">\u8fd4\u56de<\/span><br \/>\n    <span style=\"color:#777777\">0 <\/span><\/p>\n<p>    <span style=\"color:#777777\">\u5982\u679c\u6240\u6709\u7ba1\u9053\u8bfb\u7aef\u5bf9\u5e94\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u88ab\u5173\u95ed&#xff0c;\u5219<\/span><br \/>\n    <span style=\"color:#777777\">write<\/span><br \/>\n    <span style=\"color:#777777\">\u64cd\u4f5c\u4f1a\u4ea7\u751f\u4fe1\u53f7<\/span><br \/>\n    <span style=\"color:#777777\">SIGPIPE,<\/span><br \/>\n    <span style=\"color:#777777\">\u8fdb\u800c\u53ef\u80fd\u5bfc\u81f4<\/span><br \/>\n    <span style=\"color:#777777\">write<\/span><br \/>\n    <span style=\"color:#777777\">\u8fdb\u7a0b\u9000\u51fa <\/span><\/p>\n<p>    <span style=\"color:#777777\">\u5f53\u8981\u5199\u5165\u7684\u6570\u636e\u91cf\u4e0d\u5927\u4e8e<\/span><br \/>\n    <span style=\"color:#777777\">PIPE_BUF<\/span><br \/>\n    <span style=\"color:#777777\">\u65f6&#xff0c;<\/span><br \/>\n    <span style=\"color:#777777\">linux<\/span><br \/>\n    <span style=\"color:#777777\">\u5c06\u4fdd\u8bc1\u5199\u5165\u7684\u539f\u5b50\u6027\u3002 <\/span><\/p>\n<p>    <span style=\"color:#777777\">\u5f53\u8981\u5199\u5165\u7684\u6570\u636e\u91cf\u5927\u4e8e<\/span><br \/>\n    <span style=\"color:#777777\">PIPE_BUF<\/span><br \/>\n    <span style=\"color:#777777\">\u65f6&#xff0c;<\/span><br \/>\n    <span style=\"color:#777777\">linux<\/span><br \/>\n    <span style=\"color:#777777\">\u5c06\u4e0d\u518d\u4fdd\u8bc1\u5199\u5165\u7684\u539f\u5b50\u6027\u3002<\/span><\/p>\n<h4><span style=\"color:#333333\">9.\u7ba1\u9053\u7279\u70b9<\/span><\/h4>\n<\/p>\n<p>     <span style=\"color:#777777\">\u53ea\u80fd\u7528\u4e8e\u5177\u6709\u5171\u540c\u7956\u5148\u7684\u8fdb\u7a0b&#xff08;\u5177\u6709\u4eb2\u7f18\u5173\u7cfb\u7684\u8fdb\u7a0b&#xff09;\u4e4b\u95f4\u8fdb\u884c\u901a\u4fe1&#xff1b;\u901a\u5e38&#xff0c;\u4e00\u4e2a\u7ba1\u9053\u7531\u4e00\u4e2a\u8fdb\u7a0b\u521b\u5efa&#xff0c;\u7136\u540e\u8be5\u8fdb\u7a0b\u8c03\u7528fork<\/span><br \/>\n     <span style=\"color:#777777\">&#xff0c;\u6b64\u540e\u7236\u3001\u5b50\u8fdb\u7a0b\u4e4b\u95f4\u5c31\u53ef\u5e94\u7528\u8be5\u7ba1\u9053\u3002 <\/span><\/p>\n<p>     <span style=\"color:#777777\">\u7ba1\u9053\u63d0\u4f9b\u6d41\u5f0f\u670d\u52a1 <\/span><\/p>\n<p>     <span style=\"color:#777777\">\u4e00\u822c\u800c\u8a00&#xff0c;\u8fdb\u7a0b\u9000\u51fa&#xff0c;\u7ba1\u9053\u91ca\u653e&#xff0c;\u6240\u4ee5\u7ba1\u9053\u7684\u751f\u547d\u5468\u671f\u968f\u8fdb\u7a0b <\/span><\/p>\n<p>     <span style=\"color:#777777\">\u4e00\u822c\u800c\u8a00&#xff0c;\u5185\u6838\u4f1a\u5bf9\u7ba1\u9053\u64cd\u4f5c\u8fdb\u884c\u540c\u6b65\u4e0e\u4e92\u65a5 <\/span><\/p>\n<p>     <span style=\"color:#777777\">\u7ba1\u9053\u662f\u534a\u53cc\u5de5\u7684&#xff0c;\u6570\u636e\u53ea\u80fd\u5411\u4e00\u4e2a\u65b9\u5411\u6d41\u52a8&#xff1b;\u9700\u8981\u53cc\u65b9\u901a\u4fe1\u65f6&#xff0c;\u9700\u8981\u5efa\u7acb\u8d77\u4e24\u4e2a\u7ba1\u9053<\/span><\/p>\n<p>     <img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"388\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260130045720-697c3a305ecb7.png\" width=\"855\" \/><\/p>\n<h2><span style=\"color:#333333\">\u4e09.\u547d\u540d\u7ba1\u9053<\/span><\/h2>\n<p>      <span style=\"color:#777777\">\u7ba1\u9053\u5e94\u7528\u7684\u4e00\u4e2a\u9650\u5236\u5c31\u662f\u53ea\u80fd\u5728\u5177\u6709\u5171\u540c\u7956\u5148&#xff08;\u5177\u6709\u4eb2\u7f18\u5173\u7cfb&#xff09;\u7684\u8fdb\u7a0b\u95f4\u901a\u4fe1\u3002 <\/span><\/p>\n<p>      <span style=\"color:#777777\">\u5982\u679c\u6211\u4eec\u60f3\u5728\u4e0d\u76f8\u5173\u7684\u8fdb\u7a0b\u4e4b\u95f4\u4ea4\u6362\u6570\u636e&#xff0c;\u53ef\u4ee5\u4f7f\u7528<\/span><br \/>\n      <span style=\"color:#777777\">FIFO<\/span><br \/>\n      <span style=\"color:#777777\">\u6587\u4ef6\u6765\u505a\u8fd9\u9879\u5de5\u4f5c&#xff0c;\u5b83\u7ecf\u5e38\u88ab\u79f0\u4e3a\u547d\u540d\u7ba1\u9053\u3002 <\/span><\/p>\n<p>      <span style=\"color:#777777\">\u547d\u540d\u7ba1\u9053\u662f\u4e00\u79cd\u7279\u6b8a\u7c7b\u578b\u7684\u6587\u4ef6<\/span><\/p>\n<h3><span style=\"color:#333333\">1.\u521b\u5efa\u4e00\u4e2a\u547d\u540d\u7ba1\u9053<\/span><\/h3>\n<p>       <span style=\"color:#777777\">\u547d\u540d\u7ba1\u9053\u53ef\u4ee5\u4ece\u547d\u4ee4\u884c\u4e0a\u521b\u5efa&#xff0c;\u547d\u4ee4\u884c\u65b9\u6cd5\u662f\u4f7f\u7528\u4e0b\u9762\u8fd9\u4e2a\u547d\u4ee4&#xff1a;<\/span><\/p>\n<p>       $ mkfifo filename <\/p>\n<h4>\u4e00\u3001\u547d\u4ee4\u6838\u5fc3\u5b9a\u4f4d<\/h4>\n<p>mkfifo filename \u662f Linux \u7cfb\u7edf\u4e2d\u521b\u5efa\u547d\u540d\u7ba1\u9053&#xff08;Named Pipe\/FIFO&#xff09; \u7684\u547d\u4ee4&#xff0c;\u89e3\u51b3\u4e86\u533f\u540d\u7ba1\u9053&#xff08;pipe() \u51fd\u6570\u521b\u5efa&#xff09;\u4ec5\u80fd\u5728\u4eb2\u7f18\u8fdb\u7a0b\u95f4\u901a\u4fe1\u7684\u9650\u5236&#xff0c;\u5b9e\u73b0\u4efb\u610f\u8fdb\u7a0b&#xff08;\u65e0\u5173\u8054&#xff09; \u4e4b\u95f4\u7684\u53cc\u5411 \/ \u5355\u5411\u901a\u4fe1\u3002<\/p>\n<ul>\n<li>&#x1f4cc; \u547d\u540d\u7ba1\u9053\u672c\u8d28&#xff1a;\u6587\u4ef6\u7cfb\u7edf\u4e2d\u53ef\u89c1\u7684\u7279\u6b8a\u6587\u4ef6&#xff08;\u7c7b\u578b\u4e3a p&#xff09;&#xff0c;\u4f46\u4e0d\u5b58\u50a8\u5b9e\u9645\u6570\u636e&#xff0c;\u4ec5\u4f5c\u4e3a\u8fdb\u7a0b\u95f4\u901a\u4fe1\u7684 \u201c\u6865\u6881\u201d&#xff0c;\u6570\u636e\u4ecd\u5b58\u50a8\u5728\u5185\u6838\u7f13\u51b2\u533a\u3002<\/li>\n<li>&#x1f4cc; \u6838\u5fc3\u7279\u6027&#xff1a;\u6709\u6587\u4ef6\u540d\u3001\u8de8\u4efb\u610f\u8fdb\u7a0b\u3001\u751f\u547d\u5468\u671f\u4e0e\u6587\u4ef6\u7cfb\u7edf\u7ed1\u5b9a&#xff08;\u9700\u624b\u52a8\u5220\u9664&#xff09;\u3002<\/li>\n<\/ul>\n<h4>\u4e8c\u3001\u547d\u4ee4\u57fa\u7840\u4fe1\u606f<\/h4>\n<table>\n<tr>\u9879\u5177\u4f53\u5185\u5bb9<\/tr>\n<tbody>\n<tr>\n<td>\u547d\u4ee4\u683c\u5f0f<\/td>\n<td>mkfifo [\u9009\u9879] \u6587\u4ef6\u540d&#xff08;\u6700\u5e38\u7528&#xff1a;mkfifo filename&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u6838\u5fc3\u4f5c\u7528<\/td>\n<td>\u5728\u6587\u4ef6\u7cfb\u7edf\u4e2d\u521b\u5efa\u4e00\u4e2a\u547d\u540d\u7ba1\u9053\u6587\u4ef6&#xff0c;\u4f9b\u4efb\u610f\u8fdb\u7a0b\u901a\u8fc7\u6587\u4ef6\u540d\u8bbf\u95ee\u7ba1\u9053<\/td>\n<\/tr>\n<tr>\n<td>\u7ba1\u9053\u6587\u4ef6\u7c7b\u578b<\/td>\n<td>ls -l \u67e5\u770b\u65f6&#xff0c;\u6743\u9650\u4f4d\u5f00\u5934\u4e3a p&#xff08;\u5982 prw-rw-r&#8211;&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u6240\u5c5e\u5206\u7c7b<\/td>\n<td>POSIX \u6807\u51c6\u547d\u4ee4&#xff0c;\u6240\u6709 Linux\/Unix \u7cfb\u7edf\u5747\u652f\u6301<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>\u4e09\u3001\u547d\u4ee4\u53c2\u6570\u8be6\u89e3<\/h4>\n<h5>1. \u57fa\u7840\u7528\u6cd5&#xff08;\u65e0\u53c2\u6570&#xff09;<\/h5>\n<p>bash<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>       mkfifo myfifo  # \u521b\u5efa\u540d\u4e3a myfifo \u7684\u547d\u540d\u7ba1\u9053<\/p>\n<ul>\n<li>\u6267\u884c\u540e&#xff0c;\u6587\u4ef6\u7cfb\u7edf\u4e2d\u4f1a\u751f\u6210\u4e00\u4e2a\u540d\u4e3a myfifo \u7684\u7279\u6b8a\u6587\u4ef6&#xff0c;ls -l \u67e5\u770b\u793a\u4f8b&#xff1a;\n<p>plaintext<\/p>\n<p> prw-rw-r&#8211; 1 user user 0 Jan 30 10:00 myfifo<\/p>\n<ul>\n<li>p&#xff1a;\u6807\u8bc6\u8fd9\u662f\u547d\u540d\u7ba1\u9053&#xff08;FIFO&#xff09;\u6587\u4ef6&#xff1b;<\/li>\n<li>\u5927\u5c0f\u4e3a 0&#xff1a;\u56e0\u4e3a\u547d\u540d\u7ba1\u9053\u4e0d\u5b58\u50a8\u5b9e\u9645\u6570\u636e&#xff0c;\u4ec5\u4f5c\u4e3a\u901a\u4fe1\u5165\u53e3&#xff1b;<\/li>\n<li>\u6743\u9650\u4e0e\u666e\u901a\u6587\u4ef6\u4e00\u81f4&#xff08;\u53ef\u901a\u8fc7 chmod \u4fee\u6539&#xff09;\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h5>2. \u5e38\u7528\u9009\u9879<\/h5>\n<table>\n<tr>\u9009\u9879\u542b\u4e49\u793a\u4f8b<\/tr>\n<tbody>\n<tr>\n<td>-m\/&#8211;mode<\/td>\n<td>\u8bbe\u7f6e\u7ba1\u9053\u6587\u4ef6\u7684\u6743\u9650&#xff08;\u516b\u8fdb\u5236&#xff09;<\/td>\n<td>mkfifo -m 0600 myfifo&#xff08;\u4ec5\u5f53\u524d\u7528\u6237\u53ef\u8bfb\u53ef\u5199&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>-Z\/&#8211;context<\/td>\n<td>\u8bbe\u7f6e SELinux \u5b89\u5168\u4e0a\u4e0b\u6587&#xff08;\u4ec5 selinux \u7cfb\u7edf&#xff09;<\/td>\n<td>mkfifo -Z unconfined_u:object_r:user_tmp_t:s0 myfifo<\/td>\n<\/tr>\n<tr>\n<td>&#8211;help<\/td>\n<td>\u67e5\u770b\u5e2e\u52a9\u4fe1\u606f<\/td>\n<td>mkfifo &#8211;help<\/td>\n<\/tr>\n<tr>\n<td>&#8211;version<\/td>\n<td>\u67e5\u770b\u7248\u672c<\/td>\n<td>mkfifo &#8211;version<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>        <span style=\"color:#777777\">\u547d\u540d\u7ba1\u9053\u4e5f\u53ef\u4ee5\u4ece\u7a0b\u5e8f\u91cc\u521b\u5efa&#xff0c;\u76f8\u5173\u51fd\u6570\u6709&#xff1a;<\/span><\/p>\n<p>        int mkfifo(const char *filename,mode_t mode); <\/p>\n<h4>\u4e00\u3001mkfifo () \u51fd\u6570\u6838\u5fc3\u8bb2\u89e3<\/h4>\n<p>mkfifo() \u662f Linux \u7cfb\u7edf\u8c03\u7528&#xff0c;\u7528\u4e8e\u5728\u5185\u6838\u4e2d\u521b\u5efa\u547d\u540d\u7ba1\u9053&#xff08;FIFO&#xff09;\u6587\u4ef6&#xff0c;\u662f\u7528\u6237\u6001\u4ee3\u7801\u521b\u5efa\u547d\u540d\u7ba1\u9053\u7684\u6838\u5fc3\u63a5\u53e3&#xff0c;\u5bf9\u5e94\u547d\u4ee4\u884c\u7684 mkfifo filename\u3002<\/p>\n<h5>1. \u57fa\u7840\u4fe1\u606f<\/h5>\n<table>\n<tr>\u9879\u5177\u4f53\u5185\u5bb9<\/tr>\n<tbody>\n<tr>\n<td>\u51fd\u6570\u539f\u578b<\/td>\n<td>int mkfifo(const char *pathname, mode_t mode);<\/td>\n<\/tr>\n<tr>\n<td>\u6240\u9700\u5934\u6587\u4ef6<\/td>\n<td>&lt;sys\/types.h&gt; &#043; &lt;sys\/stat.h&gt;&#xff08;C\/C&#043;&#043; \u901a\u7528&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u8fd4\u56de\u503c<\/td>\n<td>\u6210\u529f\u8fd4\u56de0&#xff1b;\u5931\u8d25\u8fd4\u56de-1&#xff0c;\u5e76\u8bbe\u7f6eerrno<\/td>\n<\/tr>\n<tr>\n<td>\u6838\u5fc3\u4f5c\u7528<\/td>\n<td>\u5728\u6307\u5b9a\u8def\u5f84\u521b\u5efa\u547d\u540d\u7ba1\u9053\u6587\u4ef6&#xff0c;\u4f9b\u4efb\u610f\u8fdb\u7a0b\u901a\u8fc7\u6587\u4ef6\u540d\u8bbf\u95ee\u7ba1\u9053<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h5>2. \u53c2\u6570\u8be6\u89e3<\/h5>\n<table>\n<tr>\u53c2\u6570\u542b\u4e49 &amp; \u89c4\u5219<\/tr>\n<tbody>\n<tr>\n<td>pathname<\/td>\n<td>\u7ba1\u9053\u6587\u4ef6\u7684\u8def\u5f84 &#043; \u540d\u79f0&#xff08;\u5982 &#034;.\/myfifo&#034;\u3001&#034;\/tmp\/fifo_test&#034;&#xff09;&#xff1a;\u2705 \u76f8\u5bf9\u8def\u5f84 \/ \u7edd\u5bf9\u8def\u5f84\u5747\u53ef&#xff1b;\u274c \u8def\u5f84\u4e0d\u5b58\u5728\u4f1a\u62a5\u9519&#xff08;ENOENT&#xff09;\u3002<\/td>\n<\/tr>\n<tr>\n<td>mode<\/td>\n<td>\u7ba1\u9053\u6587\u4ef6\u7684\u6743\u9650&#xff08;\u516b\u8fdb\u5236\u6570&#xff0c;\u5982 0666\u30010644&#xff09;&#xff1a;\u2705 \u6700\u7ec8\u6743\u9650 &#061; mode &amp; ~umask&#xff08;\u4e0e\u666e\u901a\u6587\u4ef6\u6743\u9650\u89c4\u5219\u4e00\u81f4&#xff09;&#xff1b;\u2705 \u5e38\u7528 0666&#xff08;\u6240\u6709\u7528\u6237\u53ef\u8bfb\u53ef\u5199&#xff09;\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h5>3. \u5e38\u89c1\u9519\u8bef\u7801&#xff08;errno&#xff09;<\/h5>\n<table>\n<tr>\u9519\u8bef\u7801\u542b\u4e49\u89e3\u51b3\u65b9\u6cd5<\/tr>\n<tbody>\n<tr>\n<td>EEXIST<\/td>\n<td>\u7ba1\u9053\u6587\u4ef6\u5df2\u5b58\u5728<\/td>\n<td>\u5148\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728&#xff0c;\u6216\u7528 unlink() \u5220\u9664\u540e\u91cd\u5efa<\/td>\n<\/tr>\n<tr>\n<td>ENOENT<\/td>\n<td>\u8def\u5f84\u4e0d\u5b58\u5728&#xff08;\u5982 \/tmp\/a\/myfifo \u4e2d a \u76ee\u5f55\u4e0d\u5b58\u5728&#xff09;<\/td>\n<td>\u5148\u521b\u5efa\u76ee\u5f55&#xff08;mkdir()&#xff09;&#xff0c;\u518d\u521b\u5efa\u7ba1\u9053<\/td>\n<\/tr>\n<tr>\n<td>EACCES<\/td>\n<td>\u65e0\u6743\u9650\u521b\u5efa\u6587\u4ef6<\/td>\n<td>\u68c0\u67e5\u76ee\u5f55\u6743\u9650&#xff0c;\u63d0\u5347\u6743\u9650&#xff08;sudo&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>ENOSPC<\/td>\n<td>\u6587\u4ef6\u7cfb\u7edf\u7a7a\u95f4\u4e0d\u8db3<\/td>\n<td>\u6e05\u7406\u78c1\u76d8\u7a7a\u95f4<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h5>4. \u57fa\u7840\u4f7f\u7528\u793a\u4f8b<\/h5>\n<p>cpp<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>        #include &lt;iostream&gt;<br \/>\n#include &lt;sys\/stat.h&gt;<br \/>\n#include &lt;sys\/types.h&gt;<br \/>\n#include &lt;cstdio&gt;<br \/>\n#include &lt;cerrno&gt;<\/p>\n<p>using namespace std;<\/p>\n<p>#define FIFO_PATH &#034;.\/myfifo&#034;<\/p>\n<p>int main() {<br \/>\n    \/\/ \u521b\u5efa\u547d\u540d\u7ba1\u9053&#xff0c;\u6743\u9650 0666<br \/>\n    if (mkfifo(FIFO_PATH, 0666) &#061;&#061; -1) {<br \/>\n        if (errno &#061;&#061; EEXIST) {<br \/>\n            cout &lt;&lt; &#034;\u7ba1\u9053\u6587\u4ef6\u5df2\u5b58\u5728&#xff0c;\u65e0\u9700\u91cd\u590d\u521b\u5efa&#034; &lt;&lt; endl;<br \/>\n        } else {<br \/>\n            perror(&#034;mkfifo failed&#034;);<br \/>\n            return 1;<br \/>\n        }<br \/>\n    } else {<br \/>\n        cout &lt;&lt; &#034;\u7ba1\u9053\u6587\u4ef6\u521b\u5efa\u6210\u529f&#xff1a;&#034; &lt;&lt; FIFO_PATH &lt;&lt; endl;<br \/>\n    }<br \/>\n    return 0;<br \/>\n}<\/p>\n<h4>\u4e8c\u3001\u4e0e mkfifo () \u914d\u5957\u7684\u6838\u5fc3\u51fd\u6570<\/h4>\n<p>\u547d\u540d\u7ba1\u9053\u521b\u5efa\u540e&#xff0c;\u9700\u901a\u8fc7\u4e00\u7cfb\u5217\u51fd\u6570\u5b8c\u6210 \u201c\u6253\u5f00\u2192\u8bfb\u5199\u2192\u5173\u95ed\u2192\u5220\u9664\u201d \u7684\u5b8c\u6574\u6d41\u7a0b&#xff0c;\u4ee5\u4e0b\u662f\u6700\u5e38\u7528\u7684\u914d\u5957\u51fd\u6570&#xff1a;<\/p>\n<h5>1. open ()&#xff1a;\u6253\u5f00\u547d\u540d\u7ba1\u9053&#xff08;\u6838\u5fc3&#xff01;&#xff09;<\/h5>\n<p>\u547d\u540d\u7ba1\u9053\u521b\u5efa\u540e\u662f \u201c\u7a7a\u6587\u4ef6\u201d&#xff0c;\u5fc5\u987b\u901a\u8fc7 open() \u6253\u5f00\u624d\u80fd\u8fdb\u884c\u8bfb\u5199&#xff0c;\u8fd9\u662f\u4e0e\u533f\u540d\u7ba1\u9053\u7684\u5173\u952e\u533a\u522b\u3002<\/p>\n<table>\n<tr>\u9879\u5177\u4f53\u5185\u5bb9<\/tr>\n<tbody>\n<tr>\n<td>\u51fd\u6570\u539f\u578b<\/td>\n<td>int open(const char *pathname, int flags);<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>\u6838\u5fc3\u53c2\u6570&#xff08;flags&#xff09;<\/td>\n<td>\u2460 O_RDONLY&#xff1a;\u53ea\u8bfb\u6253\u5f00&#xff08;\u8bfb\u8fdb\u7a0b&#xff09;&#xff1b;\u2461 O_WRONLY&#xff1a;\u53ea\u5199\u6253\u5f00&#xff08;\u5199\u8fdb\u7a0b&#xff09;&#xff1b;\u2462 O_NONBLOCK&#xff1a;\u975e\u963b\u585e\u6a21\u5f0f&#xff08;\u53ef\u9009&#xff09;&#xff1b;\u2705 \u5e38\u7528\u7ec4\u5408&#xff1a;&#096;O_RDONLY<\/td>\n<td>O_NONBLOCK&#096;&#xff08;\u975e\u963b\u585e\u8bfb&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u5173\u952e\u7279\u6027<\/td>\n<td>&#x1f534; \u9ed8\u8ba4\u963b\u585e\u6a21\u5f0f&#xff1a;- \u4ec5\u6253\u5f00\u8bfb\u7aef \u2192 \u963b\u585e&#xff0c;\u76f4\u5230\u6709\u8fdb\u7a0b\u6253\u5f00\u5199\u7aef&#xff1b;- \u4ec5\u6253\u5f00\u5199\u7aef \u2192 \u963b\u585e&#xff0c;\u76f4\u5230\u6709\u8fdb\u7a0b\u6253\u5f00\u8bfb\u7aef&#xff1b;&#x1f534; \u975e\u963b\u585e\u6a21\u5f0f&#xff1a;- \u4ec5\u6253\u5f00\u8bfb\u7aef \u2192 \u7acb\u5373\u8fd4\u56de&#xff08;\u6210\u529f&#xff09;&#xff1b;- \u4ec5\u6253\u5f00\u5199\u7aef \u2192 \u62a5\u9519&#xff08;ENXIO&#xff09;\u3002<\/td>\n<td><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u793a\u4f8b&#xff1a;\u963b\u585e\u6a21\u5f0f\u6253\u5f00\u7ba1\u9053<\/p>\n<p>cpp<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>        \/\/ \u8bfb\u8fdb\u7a0b&#xff1a;\u963b\u585e\u6253\u5f00\u8bfb\u7aef<br \/>\nint fd_read &#061; open(FIFO_PATH, O_RDONLY);<br \/>\nif (fd_read &#061;&#061; -1) { perror(&#034;open read failed&#034;); return 1; }<\/p>\n<p>\/\/ \u5199\u8fdb\u7a0b&#xff1a;\u963b\u585e\u6253\u5f00\u5199\u7aef<br \/>\nint fd_write &#061; open(FIFO_PATH, O_WRONLY);<br \/>\nif (fd_write &#061;&#061; -1) { perror(&#034;open write failed&#034;); return 1; }<\/p>\n<p>\u793a\u4f8b&#xff1a;\u975e\u963b\u585e\u6a21\u5f0f\u6253\u5f00\u7ba1\u9053<\/p>\n<p>cpp<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>        \/\/ \u975e\u963b\u585e\u6253\u5f00\u8bfb\u7aef&#xff08;\u5373\u4f7f\u65e0\u5199\u8fdb\u7a0b\u4e5f\u7acb\u5373\u8fd4\u56de&#xff09;<br \/>\nint fd_read &#061; open(FIFO_PATH, O_RDONLY | O_NONBLOCK);<\/p>\n<h5>2. read ()\/write ()&#xff1a;\u8bfb\u5199\u7ba1\u9053\u6570\u636e<\/h5>\n<p>\u547d\u540d\u7ba1\u9053\u7684\u8bfb\u5199\u4e0e\u666e\u901a\u6587\u4ef6\u4e00\u81f4&#xff0c;\u4f46\u9075\u5faa\u7ba1\u9053\u7684\u963b\u585e \/ \u539f\u5b50\u6027\u89c4\u5219\u3002<\/p>\n<table>\n<tr>\u51fd\u6570\u539f\u578b\u6838\u5fc3\u89c4\u5219<\/tr>\n<tbody>\n<tr>\n<td>read()<\/td>\n<td>ssize_t read(int fd, void *buf, size_t count);<\/td>\n<td>\u2460 \u963b\u585e\u6a21\u5f0f&#xff1a;\u65e0\u6570\u636e\u5219\u963b\u585e&#xff0c;\u6709\u6570\u636e\u5219\u8bfb\u53d6&#xff1b;\u2461 \u975e\u963b\u585e\u6a21\u5f0f&#xff1a;\u65e0\u6570\u636e\u8fd4\u56de -1&#xff08;errno&#061;EAGAIN&#xff09;&#xff1b;\u2462 \u5199\u7aef\u5173\u95ed&#xff1a;\u8bfb\u53d6\u5269\u4f59\u6570\u636e\u540e\u8fd4\u56de 0&#xff08;EOF&#xff09;\u3002<\/td>\n<\/tr>\n<tr>\n<td>write()<\/td>\n<td>ssize_t write(int fd, const void *buf, size_t count);<\/td>\n<td>\u2460 \u963b\u585e\u6a21\u5f0f&#xff1a;\u7f13\u51b2\u533a\u6ee1\u5219\u963b\u585e&#xff0c;\u5426\u5219\u5199\u5165&#xff1b;\u2461 \u975e\u963b\u585e\u6a21\u5f0f&#xff1a;\u7f13\u51b2\u533a\u6ee1\u8fd4\u56de -1&#xff08;errno&#061;EAGAIN&#xff09;&#xff1b;\u2462 \u8bfb\u7aef\u5173\u95ed&#xff1a;\u89e6\u53d1 SIGPIPE \u4fe1\u53f7&#xff08;\u8fdb\u7a0b\u9ed8\u8ba4\u7ec8\u6b62&#xff09;&#xff1b;\u2463 \u539f\u5b50\u6027&#xff1a;\u5199\u5165 \u2264 PIPE_BUF&#xff08;4KB&#xff09;\u65f6\u539f\u5b50\u5199\u5165\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u793a\u4f8b&#xff1a;\u8bfb\u5199\u7ba1\u9053\u6570\u636e<\/p>\n<p>cpp<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>        \/\/ \u5199\u8fdb\u7a0b&#xff1a;\u5199\u5165\u6570\u636e<br \/>\nconst char* msg &#061; &#034;Hello FIFO!&#034;;<br \/>\nssize_t write_len &#061; write(fd_write, msg, strlen(msg)&#043;1);<br \/>\nif (write_len &#061;&#061; -1) { perror(&#034;write failed&#034;); }<\/p>\n<p>\/\/ \u8bfb\u8fdb\u7a0b&#xff1a;\u8bfb\u53d6\u6570\u636e<br \/>\nchar buf[1024] &#061; {0};<br \/>\nssize_t read_len &#061; read(fd_read, buf, sizeof(buf)-1);<br \/>\nif (read_len &gt; 0) {<br \/>\n    cout &lt;&lt; &#034;\u8bfb\u53d6\u5230&#xff1a;&#034; &lt;&lt; buf &lt;&lt; endl;<br \/>\n} else if (read_len &#061;&#061; 0) {<br \/>\n    cout &lt;&lt; &#034;\u5199\u7aef\u5df2\u5173\u95ed&#034; &lt;&lt; endl;<br \/>\n}<\/p>\n<h5>3. close ()&#xff1a;\u5173\u95ed\u7ba1\u9053\u6587\u4ef6\u63cf\u8ff0\u7b26<\/h5>\n<p>\u5b8c\u6210\u8bfb\u5199\u540e\u5fc5\u987b\u5173\u95ed\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u91ca\u653e\u8d44\u6e90&#xff0c;\u5426\u5219\u4f1a\u5bfc\u81f4\u7ba1\u9053\u5f15\u7528\u8ba1\u6570\u4e0d\u4e3a 0&#xff0c;\u65e0\u6cd5\u6b63\u5e38\u9500\u6bc1\u3002<\/p>\n<table>\n<tr>\u9879\u5177\u4f53\u5185\u5bb9<\/tr>\n<tbody>\n<tr>\n<td>\u51fd\u6570\u539f\u578b<\/td>\n<td>int close(int fd);<\/td>\n<\/tr>\n<tr>\n<td>\u8fd4\u56de\u503c<\/td>\n<td>\u6210\u529f\u8fd4\u56de0&#xff1b;\u5931\u8d25\u8fd4\u56de-1&#xff08;errno&#061;EBADF \u8868\u793a fd \u65e0\u6548&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u6838\u5fc3\u4f5c\u7528<\/td>\n<td>\u5173\u95ed\u7ba1\u9053\u7684\u8bfb \/ \u5199\u7aef&#xff0c;\u51cf\u5c11\u7ba1\u9053\u7684\u5f15\u7528\u8ba1\u6570&#xff1b;\u5f53\u6240\u6709\u7aef\u90fd\u5173\u95ed\u65f6&#xff0c;\u5185\u6838\u91ca\u653e\u7ba1\u9053\u7f13\u51b2\u533a\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u793a\u4f8b&#xff1a;\u5173\u95ed\u6587\u4ef6\u63cf\u8ff0\u7b26<\/p>\n<p>cpp<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>        close(fd_read);  \/\/ \u5173\u95ed\u8bfb\u7aef<br \/>\nclose(fd_write); \/\/ \u5173\u95ed\u5199\u7aef<\/p>\n<h5>4. unlink ()&#xff1a;\u5220\u9664\u547d\u540d\u7ba1\u9053\u6587\u4ef6<\/h5>\n<p>\u547d\u540d\u7ba1\u9053\u662f\u6587\u4ef6\u7cfb\u7edf\u4e2d\u7684\u5b9e\u4f53\u6587\u4ef6&#xff0c;\u8fdb\u7a0b\u9000\u51fa\u540e\u4e0d\u4f1a\u81ea\u52a8\u9500\u6bc1&#xff0c;\u9700\u7528 unlink() \u624b\u52a8\u5220\u9664&#xff08;\u5bf9\u5e94\u547d\u4ee4\u884c rm&#xff09;\u3002<\/p>\n<table>\n<tr>\u9879\u5177\u4f53\u5185\u5bb9<\/tr>\n<tbody>\n<tr>\n<td>\u51fd\u6570\u539f\u578b<\/td>\n<td>int unlink(const char *pathname);<\/td>\n<\/tr>\n<tr>\n<td>\u8fd4\u56de\u503c<\/td>\n<td>\u6210\u529f\u8fd4\u56de0&#xff1b;\u5931\u8d25\u8fd4\u56de-1&#xff08;errno&#061;ENOENT \u8868\u793a\u6587\u4ef6\u4e0d\u5b58\u5728&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u6838\u5fc3\u4f5c\u7528<\/td>\n<td>\u5220\u9664\u6587\u4ef6\u7cfb\u7edf\u4e2d\u7684\u7ba1\u9053\u6587\u4ef6&#xff0c;\u5373\u4f7f\u6709\u8fdb\u7a0b\u4ecd\u5728\u4f7f\u7528\u8be5\u7ba1\u9053&#xff0c;\u4e5f\u4f1a\u5148\u6807\u8bb0\u5220\u9664&#xff0c;\u5f85\u6240\u6709\u8fdb\u7a0b\u5173\u95ed\u540e\u5f7b\u5e95\u6e05\u7406\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u793a\u4f8b&#xff1a;\u5220\u9664\u7ba1\u9053\u6587\u4ef6<\/p>\n<p>        if (unlink(FIFO_PATH) &#061;&#061; -1) {<br \/>\n    perror(&#034;unlink failed&#034;);<br \/>\n} else {<br \/>\n    cout &lt;&lt; &#034;\u7ba1\u9053\u6587\u4ef6\u5df2\u5220\u9664&#034; &lt;&lt; endl;<br \/>\n}<\/p>\n<h4>\u4e09\u3001\u5b8c\u6574\u5b9e\u6218\u6848\u4f8b&#xff1a;\u4efb\u610f\u8fdb\u7a0b\u95f4\u901a\u4fe1<\/h4>\n<p>\u4ee5\u4e0b\u662f\u4e24\u4e2a\u65e0\u5173\u8054\u8fdb\u7a0b\u901a\u8fc7\u547d\u540d\u7ba1\u9053\u901a\u4fe1\u7684\u5b8c\u6574\u4ee3\u7801&#xff0c;\u8986\u76d6 mkfifo() &#043; \u6240\u6709\u914d\u5957\u51fd\u6570&#xff1a;<\/p>\n<h5>1. \u5199\u8fdb\u7a0b&#xff08;fifo_writer.cpp&#xff09;<\/h5>\n<p>        #include &lt;iostream&gt;<br \/>\n#include &lt;sys\/stat.h&gt;<br \/>\n#include &lt;sys\/types.h&gt;<br \/>\n#include &lt;fcntl.h&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;cstring&gt;<br \/>\n#include &lt;cstdio&gt;<br \/>\n#include &lt;cerrno&gt;<\/p>\n<p>using namespace std;<\/p>\n<p>#define FIFO_PATH &#034;.\/myfifo&#034;<\/p>\n<p>int main() {<br \/>\n    \/\/ 1. \u521b\u5efa\u547d\u540d\u7ba1\u9053&#xff08;\u5df2\u5b58\u5728\u5219\u5ffd\u7565&#xff09;<br \/>\n    if (mkfifo(FIFO_PATH, 0666) &#061;&#061; -1) {<br \/>\n        if (errno !&#061; EEXIST) {<br \/>\n            perror(&#034;mkfifo failed&#034;);<br \/>\n            return 1;<br \/>\n        }<br \/>\n        cout &lt;&lt; &#034;\u7ba1\u9053\u6587\u4ef6\u5df2\u5b58\u5728&#xff0c;\u76f4\u63a5\u4f7f\u7528&#034; &lt;&lt; endl;<br \/>\n    } else {<br \/>\n        cout &lt;&lt; &#034;\u7ba1\u9053\u6587\u4ef6\u521b\u5efa\u6210\u529f&#034; &lt;&lt; endl;<br \/>\n    }<\/p>\n<p>    \/\/ 2. \u963b\u585e\u6253\u5f00\u5199\u7aef&#xff08;\u7b49\u5f85\u8bfb\u8fdb\u7a0b\u8fde\u63a5&#xff09;<br \/>\n    cout &lt;&lt; &#034;\u7b49\u5f85\u8bfb\u8fdb\u7a0b\u8fde\u63a5&#8230;&#034; &lt;&lt; endl;<br \/>\n    int fd &#061; open(FIFO_PATH, O_WRONLY);<br \/>\n    if (fd &#061;&#061; -1) {<br \/>\n        perror(&#034;open failed&#034;);<br \/>\n        return 1;<br \/>\n    }<br \/>\n    cout &lt;&lt; &#034;\u8bfb\u8fdb\u7a0b\u5df2\u8fde\u63a5&#xff0c;\u5f00\u59cb\u5199\u5165\u6570\u636e&#034; &lt;&lt; endl;<\/p>\n<p>    \/\/ 3. \u5199\u5165\u6570\u636e<br \/>\n    const char* msg &#061; &#034;Hello from FIFO writer (\u4efb\u610f\u8fdb\u7a0b\u901a\u4fe1)!&#034;;<br \/>\n    ssize_t write_len &#061; write(fd, msg, strlen(msg)&#043;1);<br \/>\n    if (write_len &#061;&#061; -1) {<br \/>\n        perror(&#034;write failed&#034;);<br \/>\n    } else {<br \/>\n        cout &lt;&lt; &#034;\u6210\u529f\u5199\u5165 &#034; &lt;&lt; write_len &lt;&lt; &#034; \u5b57\u8282\u6570\u636e&#xff1a;&#034; &lt;&lt; msg &lt;&lt; endl;<br \/>\n    }<\/p>\n<p>    \/\/ 4. \u5173\u95ed\u6587\u4ef6\u63cf\u8ff0\u7b26<br \/>\n    close(fd);<br \/>\n    cout &lt;&lt; &#034;\u5199\u7aef\u5df2\u5173\u95ed&#034; &lt;&lt; endl;<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<h5>2. \u8bfb\u8fdb\u7a0b&#xff08;fifo_reader.cpp&#xff09;<\/h5>\n<p>        #include &lt;iostream&gt;<br \/>\n#include &lt;sys\/stat.h&gt;<br \/>\n#include &lt;sys\/types.h&gt;<br \/>\n#include &lt;fcntl.h&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;cstring&gt;<br \/>\n#include &lt;cstdio&gt;<\/p>\n<p>using namespace std;<\/p>\n<p>#define FIFO_PATH &#034;.\/myfifo&#034;<\/p>\n<p>int main() {<br \/>\n    \/\/ 1. \u963b\u585e\u6253\u5f00\u8bfb\u7aef&#xff08;\u7b49\u5f85\u5199\u8fdb\u7a0b\u8fde\u63a5&#xff09;<br \/>\n    cout &lt;&lt; &#034;\u7b49\u5f85\u5199\u8fdb\u7a0b\u8fde\u63a5&#8230;&#034; &lt;&lt; endl;<br \/>\n    int fd &#061; open(FIFO_PATH, O_RDONLY);<br \/>\n    if (fd &#061;&#061; -1) {<br \/>\n        perror(&#034;open failed&#034;);<br \/>\n        return 1;<br \/>\n    }<br \/>\n    cout &lt;&lt; &#034;\u5199\u8fdb\u7a0b\u5df2\u8fde\u63a5&#xff0c;\u5f00\u59cb\u8bfb\u53d6\u6570\u636e&#034; &lt;&lt; endl;<\/p>\n<p>    \/\/ 2. \u8bfb\u53d6\u6570\u636e<br \/>\n    char buf[1024] &#061; {0};<br \/>\n    ssize_t read_len &#061; read(fd, buf, sizeof(buf)-1);<br \/>\n    if (read_len &#061;&#061; -1) {<br \/>\n        perror(&#034;read failed&#034;);<br \/>\n    } else if (read_len &#061;&#061; 0) {<br \/>\n        cout &lt;&lt; &#034;\u5199\u7aef\u5df2\u5173\u95ed&#xff0c;\u65e0\u6570\u636e\u53ef\u8bfb&#034; &lt;&lt; endl;<br \/>\n    } else {<br \/>\n        cout &lt;&lt; &#034;\u6210\u529f\u8bfb\u53d6 &#034; &lt;&lt; read_len &lt;&lt; &#034; \u5b57\u8282\u6570\u636e&#xff1a;&#034; &lt;&lt; buf &lt;&lt; endl;<br \/>\n    }<\/p>\n<p>    \/\/ 3. \u5173\u95ed\u6587\u4ef6\u63cf\u8ff0\u7b26<br \/>\n    close(fd);<br \/>\n    cout &lt;&lt; &#034;\u8bfb\u7aef\u5df2\u5173\u95ed&#034; &lt;&lt; endl;<\/p>\n<p>    \/\/ 4. \u5220\u9664\u7ba1\u9053\u6587\u4ef6&#xff08;\u6700\u540e\u4e00\u4e2a\u8fdb\u7a0b\u8d1f\u8d23&#xff09;<br \/>\n    if (unlink(FIFO_PATH) &#061;&#061; -1) {<br \/>\n        perror(&#034;unlink failed&#034;);<br \/>\n    } else {<br \/>\n        cout &lt;&lt; &#034;\u7ba1\u9053\u6587\u4ef6\u5df2\u5220\u9664&#034; &lt;&lt; endl;<br \/>\n    }<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<h5>\u7f16\u8bd1\u8fd0\u884c<\/h5>\n<p>bash<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>        # \u7f16\u8bd1<br \/>\ng&#043;&#043; fifo_writer.cpp -o writer -std&#061;c&#043;&#043;11<br \/>\ng&#043;&#043; fifo_reader.cpp -o reader -std&#061;c&#043;&#043;11<\/p>\n<p># \u7ec8\u7aef1&#xff1a;\u8fd0\u884c\u5199\u8fdb\u7a0b&#xff08;\u963b\u585e\u7b49\u5f85\u8bfb\u8fdb\u7a0b&#xff09;<br \/>\n.\/writer<\/p>\n<p># \u7ec8\u7aef2&#xff1a;\u8fd0\u884c\u8bfb\u8fdb\u7a0b&#xff08;\u8fde\u63a5\u540e\u8bfb\u5199\u6570\u636e&#xff09;<br \/>\n.\/reader<\/p>\n<h5>\u8f93\u51fa\u7ed3\u679c<\/h5>\n<ul>\n<li>\u5199\u8fdb\u7a0b\u7ec8\u7aef&#xff1a;\n<p>plaintext<\/p>\n<p> \u7ba1\u9053\u6587\u4ef6\u521b\u5efa\u6210\u529f<br \/>\n\u7b49\u5f85\u8bfb\u8fdb\u7a0b\u8fde\u63a5&#8230;<br \/>\n\u8bfb\u8fdb\u7a0b\u5df2\u8fde\u63a5&#xff0c;\u5f00\u59cb\u5199\u5165\u6570\u636e<br \/>\n\u6210\u529f\u5199\u5165 41 \u5b57\u8282\u6570\u636e&#xff1a;Hello from FIFO writer (\u4efb\u610f\u8fdb\u7a0b\u901a\u4fe1)!<br \/>\n\u5199\u7aef\u5df2\u5173\u95ed\n <\/li>\n<li>\u8bfb\u8fdb\u7a0b\u7ec8\u7aef&#xff1a;\n<p>plaintext<\/p>\n<p> \u7b49\u5f85\u5199\u8fdb\u7a0b\u8fde\u63a5&#8230;<br \/>\n\u5199\u8fdb\u7a0b\u5df2\u8fde\u63a5&#xff0c;\u5f00\u59cb\u8bfb\u53d6\u6570\u636e<br \/>\n\u6210\u529f\u8bfb\u53d6 41 \u5b57\u8282\u6570\u636e&#xff1a;Hello from FIFO writer (\u4efb\u610f\u8fdb\u7a0b\u901a\u4fe1)!<br \/>\n\u8bfb\u7aef\u5df2\u5173\u95ed<br \/>\n\u7ba1\u9053\u6587\u4ef6\u5df2\u5220\u9664\n <\/li>\n<\/ul>\n<h4>\u56db\u3001\u914d\u5957\u51fd\u6570\u4f7f\u7528\u6ce8\u610f\u4e8b\u9879<\/h4>\n<h5>1. open () \u963b\u585e vs \u975e\u963b\u585e\u9009\u62e9<\/h5>\n<ul>\n<li>\u2705 \u63a8\u8350\u963b\u585e\u6a21\u5f0f&#xff1a;\u9002\u7528\u4e8e\u5927\u591a\u6570\u573a\u666f&#xff0c;\u81ea\u52a8\u7b49\u5f85\u5bf9\u65b9\u8fdb\u7a0b\u8fde\u63a5&#xff0c;\u65e0\u9700\u624b\u52a8\u8f6e\u8be2\u3002<\/li>\n<li>\u274c \u975e\u963b\u585e\u6a21\u5f0f&#xff1a;\u4ec5\u9002\u7528\u4e8e \u201c\u65e0\u9700\u7b49\u5f85\u5bf9\u65b9\u201d \u7684\u573a\u666f&#xff08;\u5982\u5b9a\u65f6\u68c0\u67e5\u7ba1\u9053&#xff09;&#xff0c;\u9700\u5904\u7406 EAGAIN \u9519\u8bef\u3002<\/li>\n<\/ul>\n<h5>2. write () \u907f\u514d\u89e6\u53d1 SIGPIPE<\/h5>\n<p>\u82e5\u8bfb\u7aef\u5df2\u5173\u95ed&#xff0c;\u5199\u8fdb\u7a0b\u8c03\u7528 write() \u4f1a\u89e6\u53d1 SIGPIPE \u4fe1\u53f7&#xff0c;\u8fdb\u7a0b\u9ed8\u8ba4\u7ec8\u6b62&#xff1a;<\/p>\n<p>cpp<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>        \/\/ \u53ef\u9009&#xff1a;\u6355\u83b7 SIGPIPE \u4fe1\u53f7&#xff0c;\u907f\u514d\u8fdb\u7a0b\u7ec8\u6b62<br \/>\n#include &lt;signal.h&gt;<br \/>\nsignal(SIGPIPE, SIG_IGN); \/\/ \u5ffd\u7565 SIGPIPE \u4fe1\u53f7<\/p>\n<h5>3. unlink () \u65f6\u673a<\/h5>\n<ul>\n<li>\u5efa\u8bae\u7531\u6700\u540e\u9000\u51fa\u7684\u8fdb\u7a0b\u8c03\u7528 unlink()&#xff0c;\u907f\u514d\u63d0\u524d\u5220\u9664\u5bfc\u81f4\u5176\u4ed6\u8fdb\u7a0b\u65e0\u6cd5\u8bbf\u95ee\u7ba1\u9053\u3002<\/li>\n<li>\u5373\u4f7f\u8c03\u7528 unlink() \u540e&#xff0c;\u5df2\u6253\u5f00\u7ba1\u9053\u7684\u8fdb\u7a0b\u4ecd\u53ef\u6b63\u5e38\u8bfb\u5199&#xff0c;\u76f4\u5230\u6240\u6709\u8fdb\u7a0b\u5173\u95ed\u7ba1\u9053\u3002<\/li>\n<\/ul>\n<h4>\u603b\u7ed3<\/h4>\n<li>mkfifo () \u6838\u5fc3&#xff1a;\u521b\u5efa\u547d\u540d\u7ba1\u9053\u6587\u4ef6&#xff0c;\u53c2\u6570\u4e3a\u8def\u5f84 &#043; \u6743\u9650&#xff0c;\u9700\u5904\u7406 \u201c\u6587\u4ef6\u5df2\u5b58\u5728\u201d \u9519\u8bef&#xff1b;<\/li>\n<li>\u6838\u5fc3\u914d\u5957\u51fd\u6570&#xff1a;\n<ul>\n<li>open()&#xff1a;\u6253\u5f00\u7ba1\u9053&#xff08;\u963b\u585e \/ \u975e\u963b\u585e&#xff0c;\u8bfb\u5199\u7aef\u9700\u914d\u5bf9&#xff09;&#xff1b;<\/li>\n<li>read()\/write()&#xff1a;\u8bfb\u5199\u6570\u636e&#xff08;\u9075\u5faa\u7ba1\u9053\u963b\u585e \/ \u539f\u5b50\u6027\u89c4\u5219&#xff09;&#xff1b;<\/li>\n<li>close()&#xff1a;\u5173\u95ed\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u91ca\u653e\u8d44\u6e90&#xff1b;<\/li>\n<li>unlink()&#xff1a;\u5220\u9664\u7ba1\u9053\u6587\u4ef6&#xff0c;\u6e05\u7406\u6587\u4ef6\u7cfb\u7edf&#xff1b;<\/li>\n<\/ul>\n<\/li>\n<li>\u4f7f\u7528\u6d41\u7a0b&#xff1a;\u521b\u5efa\u7ba1\u9053 \u2192 \u6253\u5f00\u8bfb\u5199\u7aef \u2192 \u8bfb\u5199\u6570\u636e \u2192 \u5173\u95ed\u63cf\u8ff0\u7b26 \u2192 \u5220\u9664\u7ba1\u9053&#xff1b;<\/li>\n<li>\u6838\u5fc3\u4f18\u52bf&#xff1a;\u7a81\u7834\u533f\u540d\u7ba1\u9053\u7684\u4eb2\u7f18\u8fdb\u7a0b\u9650\u5236&#xff0c;\u5b9e\u73b0\u4efb\u610f\u8fdb\u7a0b\u95f4\u901a\u4fe1\u3002<\/li>\n<h2><span style=\"color:#333333\">\u56db.\u533f\u540d\u7ba1\u9053\u4e0e\u547d\u540d\u7ba1\u9053\u7684\u533a\u522b<\/span><\/h2>\n<\/p>\n<table>\n<tr>\u5bf9\u6bd4\u7ef4\u5ea6\u533f\u540d\u7ba1\u9053&#xff08;Anonymous Pipe&#xff09;\u547d\u540d\u7ba1\u9053&#xff08;Named Pipe\/FIFO&#xff09;<\/tr>\n<tbody>\n<tr>\n<td>\u521b\u5efa\u65b9\u5f0f<\/td>\n<td>1. \u7cfb\u7edf\u8c03\u7528&#xff1a;pipe(int pipefd[2])2. \u65e0\u547d\u4ee4\u884c\u65b9\u5f0f&#xff08;\u4ec5\u4ee3\u7801\u4e2d\u521b\u5efa&#xff09;<\/td>\n<td>1. \u7cfb\u7edf\u8c03\u7528&#xff1a;mkfifo(const char *pathname, mode_t mode)2. \u547d\u4ee4\u884c&#xff1a;mkfifo filename<\/td>\n<\/tr>\n<tr>\n<td>\u6807\u8bc6 \/ \u8bbf\u95ee\u65b9\u5f0f<\/td>\n<td>\u65e0\u6587\u4ef6\u540d&#xff0c;\u4ec5\u901a\u8fc7\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff08;pipefd [0]\/pipefd [1]&#xff09;\u8bbf\u95ee<\/td>\n<td>\u6709\u6587\u4ef6\u7cfb\u7edf\u4e2d\u53ef\u89c1\u7684\u6587\u4ef6\u540d&#xff08;\u5982 .\/myfifo&#xff09;&#xff0c;\u901a\u8fc7 open() \u6253\u5f00\u540e\u7528\u6587\u4ef6\u63cf\u8ff0\u7b26\u8bbf\u95ee<\/td>\n<\/tr>\n<tr>\n<td>\u901a\u4fe1\u8fdb\u7a0b\u8303\u56f4<\/td>\n<td>\u4ec5\u652f\u6301\u6709\u4eb2\u7f18\u5173\u7cfb\u7684\u8fdb\u7a0b&#xff08;\u7236\u5b50 \/ \u5144\u5f1f\u8fdb\u7a0b&#xff0c;\u9700\u7ee7\u627f\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff09;<\/td>\n<td>\u652f\u6301\u4efb\u610f\u8fdb\u7a0b&#xff08;\u65e0\u5173\u8054\u8fdb\u7a0b\u4e5f\u53ef&#xff0c;\u53ea\u8981\u80fd\u8bbf\u95ee\u7ba1\u9053\u6587\u4ef6\u8def\u5f84&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u751f\u547d\u5468\u671f<\/td>\n<td>\u968f\u8fdb\u7a0b\u751f\u547d\u5468\u671f\u9500\u6bc1&#xff1a;\u6240\u6709\u6301\u6709\u7ba1\u9053\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u8fdb\u7a0b\u9000\u51fa\u540e&#xff0c;\u5185\u6838\u81ea\u52a8\u91ca\u653e\u7ba1\u9053<\/td>\n<td>\u968f\u6587\u4ef6\u7cfb\u7edf\u5b58\u5728&#xff1a;\u7ba1\u9053\u6587\u4ef6\u4f1a\u4e00\u76f4\u4fdd\u7559&#xff0c;\u9700\u624b\u52a8 rm\/unlink() \u5220\u9664&#xff0c;\u5373\u4f7f\u8fdb\u7a0b\u9000\u51fa<\/td>\n<\/tr>\n<tr>\n<td>\u6587\u4ef6\u7cfb\u7edf\u53ef\u89c1\u6027<\/td>\n<td>\u4e0d\u53ef\u89c1&#xff08;\u4ec5\u5b58\u5728\u4e8e\u5185\u6838\u4e2d&#xff0c;\u65e0\u6587\u4ef6\u5b9e\u4f53&#xff09;<\/td>\n<td>\u53ef\u89c1&#xff08;ls -l \u53ef\u67e5&#xff0c;\u6587\u4ef6\u7c7b\u578b\u4e3a p&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u6253\u5f00\u65b9\u5f0f<\/td>\n<td>\u65e0\u9700 open()&#xff1a;pipe() \u521b\u5efa\u540e\u76f4\u63a5\u5f97\u5230\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;fork \u540e\u7ee7\u627f<\/td>\n<td>\u9700\u663e\u5f0f open()&#xff1a;\u5148\u521b\u5efa\u7ba1\u9053\u6587\u4ef6&#xff0c;\u518d\u7528 open(pathname, flags) \u6253\u5f00\u5f97\u5230\u6587\u4ef6\u63cf\u8ff0\u7b26<\/td>\n<\/tr>\n<tr>\n<td>\u963b\u585e\u7279\u6027&#xff08;\u9ed8\u8ba4&#xff09;<\/td>\n<td>\u8bfb\u5199\u9ed8\u8ba4\u963b\u585e&#xff1a;\u8bfb\u7a7a\u7ba1\u9053\u963b\u585e\u3001\u5199\u6ee1\u7ba1\u9053\u963b\u585e<\/td>\n<td>\u6253\u5f00 \/ \u8bfb\u5199\u9ed8\u8ba4\u963b\u585e&#xff1a;\u2460 \u4ec5\u6253\u5f00\u4e00\u7aef&#xff08;\u8bfb \/ \u5199&#xff09;\u4f1a\u963b\u585e\u5728 open()&#xff1b;\u2461 \u8bfb\u5199\u89c4\u5219\u540c\u533f\u540d\u7ba1\u9053<\/td>\n<\/tr>\n<tr>\n<td>\u6838\u5fc3\u9002\u7528\u573a\u666f<\/td>\n<td>\n<p>1. \u7236\u5b50 \/ \u5144\u5f1f\u8fdb\u7a0b\u95f4\u4e34\u65f6\u901a\u4fe12. Shell \u7ba1\u9053\u7b26&#xff08;\u5982 &#096;who|wc -l&#096;&#xff09;\u5e95\u5c42\u5b9e\u73b0<\/p>\n<\/p>\n<\/td>\n<td>\n<p>1. \u65e0\u5173\u8054\u8fdb\u7a0b\u95f4\u957f\u671f\u901a\u4fe12. \u8de8\u7a0b\u5e8f \/ \u8de8\u7528\u6237\u7684\u8fdb\u7a0b\u901a\u4fe1&#xff08;\u5982\u670d\u52a1\u7aef &#8211; \u5ba2\u6237\u7aef\u901a\u4fe1&#xff09;<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\u6570\u636e\u5b58\u50a8<\/td>\n<td>\u6570\u636e\u6682\u5b58\u5185\u6838\u7f13\u51b2\u533a&#xff0c;\u8bfb\u53d6\u540e\u7acb\u5373\u5220\u9664&#xff0c;\u65e0\u6301\u4e45\u5316<\/td>\n<td>\u6570\u636e\u6682\u5b58\u5185\u6838\u7f13\u51b2\u533a&#xff0c;\u7ba1\u9053\u6587\u4ef6\u672c\u8eab\u5927\u5c0f\u59cb\u7ec8\u4e3a 0&#xff0c;\u65e0\u6301\u4e45\u5316<\/td>\n<\/tr>\n<tr>\n<td>\u6743\u9650\u63a7\u5236<\/td>\n<td>\u65e0\u72ec\u7acb\u6743\u9650&#xff08;\u7ee7\u627f\u521b\u5efa\u8fdb\u7a0b\u7684\u6743\u9650&#xff09;<\/td>\n<td>\u6709\u6587\u4ef6\u6743\u9650&#xff08;\u521b\u5efa\u65f6\u6307\u5b9a mode&#xff0c;\u53ef\u901a\u8fc7 chmod \u4fee\u6539&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u5178\u578b\u4ee3\u7801\u6d41\u7a0b<\/td>\n<td>1. pipe() \u521b\u5efa\u7ba1\u90532. fork() \u521b\u5efa\u5b50\u8fdb\u7a0b3. \u5173\u95ed\u65e0\u7528\u7aef4. \u8bfb\u5199\u6570\u636e<\/td>\n<td>1. mkfifo() \u521b\u5efa\u7ba1\u9053\u6587\u4ef62. \u8fdb\u7a0b A open() \u5199\u7aef3. \u8fdb\u7a0b B open() \u8bfb\u7aef4. \u8bfb\u5199\u6570\u636e5. unlink() \u5220\u9664\u7ba1\u9053<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>\u5173\u952e\u5dee\u5f02\u6df1\u5ea6\u89e3\u6790<\/h3>\n<h4>1. \u6700\u6838\u5fc3\u5dee\u5f02&#xff1a;\u901a\u4fe1\u8fdb\u7a0b\u8303\u56f4<\/h4>\n<p>\u8fd9\u662f\u4e24\u8005\u6700\u672c\u8d28\u7684\u533a\u522b&#xff0c;\u51b3\u5b9a\u4e86\u4f7f\u7528\u573a\u666f&#xff1a;<\/p>\n<ul>\n<li>\u533f\u540d\u7ba1\u9053&#xff1a;\u4f9d\u8d56 fork() \u7ee7\u627f\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u53ea\u80fd\u5728\u7236\u5b50 \/ \u5144\u5f1f\u8fdb\u7a0b\u95f4\u901a\u4fe1&#xff08;\u6bd4\u5982\u7236\u8fdb\u7a0b\u521b\u5efa\u7ba1\u9053\u540e fork \u5b50\u8fdb\u7a0b&#xff0c;\u5b50\u8fdb\u7a0b\u7ee7\u627f pipefd&#xff09;&#xff1b;<\/li>\n<li>\u547d\u540d\u7ba1\u9053&#xff1a;\u901a\u8fc7\u6587\u4ef6\u7cfb\u7edf\u7684\u6587\u4ef6\u540d\u6807\u8bc6&#xff0c;\u4efb\u4f55\u8fdb\u7a0b\u53ea\u8981\u80fd\u8bbf\u95ee\u8be5\u6587\u4ef6\u8def\u5f84&#xff08;\u6709\u6743\u9650&#xff09;&#xff0c;\u5c31\u80fd\u6253\u5f00\u5e76\u901a\u4fe1&#xff08;\u6bd4\u5982\u8fdb\u7a0b A \u5728\u7ec8\u7aef 1 \u521b\u5efa&#xff0c;\u8fdb\u7a0b B \u5728\u7ec8\u7aef 2 \u6253\u5f00&#xff09;\u3002<\/li>\n<\/ul>\n<h4>2. \u751f\u547d\u5468\u671f\u5dee\u5f02&#xff08;\u6613\u8e29\u5751\u70b9&#xff09;<\/h4>\n<ul>\n<li>\u533f\u540d\u7ba1\u9053&#xff1a;\u5f00\u53d1\u4e2d\u65e0\u9700\u624b\u52a8\u6e05\u7406&#xff0c;\u8fdb\u7a0b\u9000\u51fa\u540e\u5185\u6838\u81ea\u52a8\u91ca\u653e&#xff0c;\u4e0d\u4f1a\u6b8b\u7559\u8d44\u6e90&#xff1b;<\/li>\n<li>\u547d\u540d\u7ba1\u9053&#xff1a;\u82e5\u5fd8\u8bb0 rm\/unlink()&#xff0c;\u7ba1\u9053\u6587\u4ef6\u4f1a\u4e00\u76f4\u7559\u5728\u6587\u4ef6\u7cfb\u7edf\u4e2d&#xff0c;\u4e0b\u6b21\u521b\u5efa\u4f1a\u62a5\u9519 File exists&#xff0c;\u9700\u624b\u52a8\u6e05\u7406\u3002<\/li>\n<\/ul>\n<h4>3. \u6253\u5f00\u65b9\u5f0f\u5dee\u5f02<\/h4>\n<ul>\n<li>\u533f\u540d\u7ba1\u9053&#xff1a;pipe() \u8c03\u7528\u540e\u76f4\u63a5\u5f97\u5230\u53ef\u7528\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u65e0\u9700\u989d\u5916\u6253\u5f00&#xff1b;<\/li>\n<li>\u547d\u540d\u7ba1\u9053&#xff1a;mkfifo() \u4ec5\u521b\u5efa\u7ba1\u9053\u6587\u4ef6&#xff0c;\u5fc5\u987b\u901a\u8fc7 open() \u6253\u5f00&#xff08;\u6307\u5b9a O_RDONLY\/O_WRONLY&#xff09;\u624d\u80fd\u8bfb\u5199&#xff0c;\u4e14\u9ed8\u8ba4\u60c5\u51b5\u4e0b&#xff0c;\u4ec5\u6253\u5f00\u4e00\u7aef\u4f1a\u963b\u585e\u5728 open() \u8c03\u7528&#xff08;\u7b49\u5f85\u53e6\u4e00\u7aef\u6253\u5f00&#xff09;\u3002<\/li>\n<\/ul>\n<h3>\u9002\u7528\u573a\u666f\u9009\u62e9\u5efa\u8bae<\/h3>\n<table>\n<tr>\u573a\u666f\u63a8\u8350\u9009\u62e9\u539f\u56e0<\/tr>\n<tbody>\n<tr>\n<td>\u7236\u5b50\u8fdb\u7a0b\u95f4\u4e34\u65f6\u4f20\u9012\u5c11\u91cf\u6570\u636e<\/td>\n<td>\u533f\u540d\u7ba1\u9053<\/td>\n<td>\u65e0\u9700\u521b\u5efa\u6587\u4ef6&#xff0c;\u81ea\u52a8\u9500\u6bc1&#xff0c;\u6d41\u7a0b\u66f4\u7b80\u5355<\/td>\n<\/tr>\n<tr>\n<td>\n<p>Shell \u547d\u4ee4\u62fc\u63a5&#xff08;\u5982 &#096;ls<\/p>\n<p>grep&#096;&#xff09;<\/p>\n<\/td>\n<td>\u533f\u540d\u7ba1\u9053<\/td>\n<td>Shell \u5e95\u5c42\u81ea\u52a8\u4e3a\u7ba1\u9053\u7b26\u521b\u5efa\u533f\u540d\u7ba1\u9053&#xff0c;\u65e0\u9700\u624b\u52a8\u7ba1\u7406<\/td>\n<\/tr>\n<tr>\n<td>\u4e24\u4e2a\u65e0\u5173\u8054\u8fdb\u7a0b&#xff08;\u5982\u670d\u52a1\u7aef &#043; \u5ba2\u6237\u7aef&#xff09;\u901a\u4fe1<\/td>\n<td>\u547d\u540d\u7ba1\u9053<\/td>\n<td>\u7a81\u7834\u4eb2\u7f18\u8fdb\u7a0b\u9650\u5236&#xff0c;\u901a\u8fc7\u6587\u4ef6\u540d\u5373\u53ef\u901a\u4fe1<\/td>\n<\/tr>\n<tr>\n<td>\u8de8\u7ec8\u7aef \/ \u8de8\u7528\u6237\u7684\u8fdb\u7a0b\u901a\u4fe1<\/td>\n<td>\u547d\u540d\u7ba1\u9053<\/td>\n<td>\u53ea\u8981\u6709\u6743\u9650\u8bbf\u95ee\u7ba1\u9053\u6587\u4ef6\u8def\u5f84&#xff0c;\u4efb\u610f\u8fdb\u7a0b\u90fd\u80fd\u53c2\u4e0e\u901a\u4fe1<\/td>\n<\/tr>\n<tr>\n<td>\u957f\u671f\u8fd0\u884c\u7684\u8fdb\u7a0b\u95f4\u901a\u4fe1<\/td>\n<td>\u547d\u540d\u7ba1\u9053<\/td>\n<td>\u751f\u547d\u5468\u671f\u53ef\u63a7&#xff0c;\u8fdb\u7a0b\u91cd\u542f\u540e\u4ecd\u53ef\u901a\u8fc7\u6587\u4ef6\u540d\u91cd\u65b0\u8fde\u63a5<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>\u603b\u7ed3<\/h3>\n<li>\u6838\u5fc3\u533a\u522b&#xff1a;\u533f\u540d\u7ba1\u9053\u65e0\u6587\u4ef6\u540d\u3001\u4ec5\u9650\u4eb2\u7f18\u8fdb\u7a0b\u3001\u81ea\u52a8\u9500\u6bc1&#xff1b;\u547d\u540d\u7ba1\u9053\u6709\u6587\u4ef6\u540d\u3001\u652f\u6301\u4efb\u610f\u8fdb\u7a0b\u3001\u9700\u624b\u52a8\u5220\u9664&#xff1b;<\/li>\n<li>\u9009\u62e9\u539f\u5219&#xff1a;\u4e34\u65f6\u7684\u4eb2\u7f18\u8fdb\u7a0b\u901a\u4fe1\u7528\u533f\u540d\u7ba1\u9053&#xff0c;\u957f\u671f\u7684\u4efb\u610f\u8fdb\u7a0b\u901a\u4fe1\u7528\u547d\u540d\u7ba1\u9053&#xff1b;<\/li>\n<li>\u5171\u6027&#xff1a;\u4e24\u8005\u6570\u636e\u90fd\u6682\u5b58\u5185\u6838\u7f13\u51b2\u533a&#xff0c;\u8bfb\u5199\u89c4\u5219\u4e00\u81f4&#xff08;\u9ed8\u8ba4\u963b\u585e\u3001\u539f\u5b50\u5199\u5165\u22644KB&#xff09;&#xff0c;\u5747\u4e3a\u534a\u53cc\u5de5\u901a\u4fe1\u3002<\/li>\n<h2><span style=\"color:#333333\">\u4e94.\u547d\u540d\u7ba1\u9053\u7684\u6253\u5f00\u89c4\u5219<\/span><\/h2>\n<\/p>\n<p>\u547d\u540d\u7ba1\u9053\u7684\u6253\u5f00\u89c4\u5219\u662f\u5176\u4e0e\u533f\u540d\u7ba1\u9053\u6700\u6838\u5fc3\u7684\u533a\u522b\u4e4b\u4e00&#xff0c;\u6838\u5fc3\u7531 open() \u51fd\u6570\u7684 flags \u53c2\u6570\u63a7\u5236&#xff0c;\u5206\u4e3a\u963b\u585e\u6a21\u5f0f&#xff08;\u9ed8\u8ba4&#xff09;\u548c\u975e\u963b\u585e\u6a21\u5f0f&#xff08;O_NONBLOCK&#xff09;\u4e24\u7c7b\u3002<\/p>\n<h4>\u4e00\u3001\u6838\u5fc3\u524d\u63d0<\/h4>\n<p>\u5728\u8bb2\u89e3\u6253\u5f00\u89c4\u5219\u524d&#xff0c;\u5148\u660e\u786e\u4e24\u4e2a\u57fa\u7840\u6982\u5ff5&#xff1a;<\/p>\n<li>\u547d\u540d\u7ba1\u9053\u7684 \u201c\u4e24\u7aef\u201d&#xff1a;\u8bfb\u7aef&#xff08;O_RDONLY&#xff09;\u548c\u5199\u7aef&#xff08;O_WRONLY&#xff09;&#xff0c;\u5fc5\u987b\u81f3\u5c11\u6253\u5f00\u4e00\u7aef\u624d\u80fd\u901a\u4fe1&#xff1b;<\/li>\n<li>\u6253\u5f00\u6807\u5fd7&#xff1a;\n<ul>\n<li>O_RDONLY&#xff1a;\u53ea\u8bfb\u6253\u5f00&#xff08;\u8bfb\u8fdb\u7a0b&#xff09;&#xff1b;<\/li>\n<li>O_WRONLY&#xff1a;\u53ea\u5199\u6253\u5f00&#xff08;\u5199\u8fdb\u7a0b&#xff09;&#xff1b;<\/li>\n<li>O_NONBLOCK&#xff1a;\u975e\u963b\u585e\u6a21\u5f0f&#xff08;\u53ef\u9009&#xff0c;\u53e0\u52a0\u5728 O_RDONLY\/O_WRONLY \u4e0a&#xff09;&#xff1b;<\/li>\n<li>\u274c \u7981\u6b62\u4f7f\u7528 O_RDWR \u6253\u5f00&#xff1a;\u4f1a\u7834\u574f\u7ba1\u9053\u7684\u534a\u53cc\u5de5\u7279\u6027&#xff0c;\u4e14\u884c\u4e3a\u672a\u5b9a\u4e49&#xff08;\u4e0d\u540c\u7cfb\u7edf\u8868\u73b0\u4e0d\u540c&#xff09;\u3002<\/li>\n<\/ul>\n<\/li>\n<h4>\u4e8c\u3001\u9ed8\u8ba4\u6a21\u5f0f&#xff08;\u963b\u585e\u6a21\u5f0f&#xff0c;\u65e0 O_NONBLOCK&#xff09;<\/h4>\n<p>\u8fd9\u662f\u6700\u5e38\u7528\u7684\u6a21\u5f0f&#xff0c;\u4e5f\u662f\u6700\u7b26\u5408\u76f4\u89c9\u7684\u6a21\u5f0f&#xff0c;\u6838\u5fc3\u89c4\u5219&#xff1a;\u4ec5\u6253\u5f00\u4e00\u7aef&#xff08;\u8bfb \/ \u5199&#xff09;\u65f6&#xff0c;open() \u4f1a\u963b\u585e&#xff0c;\u76f4\u5230\u53e6\u4e00\u7aef\u88ab\u6253\u5f00&#xff1b;\u4e24\u7aef\u90fd\u6253\u5f00\u540e&#xff0c;open() \u624d\u8fd4\u56de\u3002<\/p>\n<h5>1. \u573a\u666f\u62c6\u89e3&#xff08;\u963b\u585e\u6a21\u5f0f&#xff09;<\/h5>\n<table>\n<tr>\u6253\u5f00\u987a\u5e8f\u8fdb\u7a0b A&#xff08;\u5148\u6253\u5f00&#xff09;\u884c\u4e3a\u8fdb\u7a0b B&#xff08;\u540e\u6253\u5f00&#xff09;\u884c\u4e3a<\/tr>\n<tbody>\n<tr>\n<td>1. \u8fdb\u7a0b A \u4ee5 O_RDONLY \u6253\u5f00<\/td>\n<td>open() \u963b\u585e&#xff0c;\u76f4\u5230\u6709\u8fdb\u7a0b\u4ee5 O_WRONLY \u6253\u5f00\u5199\u7aef<\/td>\n<td>\u4ee5 O_WRONLY \u6253\u5f00\u540e&#xff0c;\u8fdb\u7a0b A \u7684 open() \u7acb\u5373\u8fd4\u56de&#xff0c;\u53cc\u65b9\u90fd\u5f97\u5230\u6587\u4ef6\u63cf\u8ff0\u7b26<\/td>\n<\/tr>\n<tr>\n<td>2. \u8fdb\u7a0b A \u4ee5 O_WRONLY \u6253\u5f00<\/td>\n<td>open() \u963b\u585e&#xff0c;\u76f4\u5230\u6709\u8fdb\u7a0b\u4ee5 O_RDONLY \u6253\u5f00\u8bfb\u7aef<\/td>\n<td>\u4ee5 O_RDONLY \u6253\u5f00\u540e&#xff0c;\u8fdb\u7a0b A \u7684 open() \u7acb\u5373\u8fd4\u56de&#xff0c;\u53cc\u65b9\u90fd\u5f97\u5230\u6587\u4ef6\u63cf\u8ff0\u7b26<\/td>\n<\/tr>\n<tr>\n<td>3. \u540c\u65f6\u6253\u5f00\u8bfb\u5199\u7aef<\/td>\n<td>\u4e24\u4e2a\u8fdb\u7a0b\u7684 open() \u90fd\u7acb\u5373\u8fd4\u56de<\/td>\n<td><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h5>2. \u4ee3\u7801\u793a\u4f8b&#xff08;\u963b\u585e\u6a21\u5f0f&#xff09;<\/h5>\n<p>        \/\/ \u8bfb\u8fdb\u7a0b&#xff08;\u963b\u585e\u6253\u5f00\u8bfb\u7aef&#xff0c;\u7b49\u5f85\u5199\u8fdb\u7a0b&#xff09;<br \/>\nint fd_read &#061; open(&#034;.\/myfifo&#034;, O_RDONLY);<br \/>\n\/\/ \u82e5\u6ca1\u6709\u5199\u8fdb\u7a0b\u6253\u5f00&#xff0c;\u4f1a\u4e00\u76f4\u963b\u585e\u5728\u8fd9\u91cc&#xff0c;\u76f4\u5230\u6709\u5199\u8fdb\u7a0b\u8fde\u63a5<\/p>\n<p>\/\/ \u5199\u8fdb\u7a0b&#xff08;\u963b\u585e\u6253\u5f00\u5199\u7aef&#xff0c;\u7b49\u5f85\u8bfb\u8fdb\u7a0b&#xff09;<br \/>\nint fd_write &#061; open(&#034;.\/myfifo&#034;, O_WRONLY);<br \/>\n\/\/ \u82e5\u6ca1\u6709\u8bfb\u8fdb\u7a0b\u6253\u5f00&#xff0c;\u4f1a\u4e00\u76f4\u963b\u585e\u5728\u8fd9\u91cc&#xff0c;\u76f4\u5230\u6709\u8bfb\u8fdb\u7a0b\u8fde\u63a5<\/p>\n<h5>3. \u5173\u952e\u7279\u6027<\/h5>\n<ul>\n<li>\u963b\u585e\u53d1\u751f\u5728 open() \u9636\u6bb5&#xff0c;\u800c\u975e\u8bfb\u5199\u9636\u6bb5&#xff1b;<\/li>\n<li>\u53ea\u8981\u8bfb\u5199\u4e24\u7aef\u90fd\u6709\u8fdb\u7a0b\u6253\u5f00&#xff0c;open() \u5c31\u4f1a\u6210\u529f\u8fd4\u56de&#xff0c;\u540e\u7eed\u8bfb\u5199\u9075\u5faa\u7ba1\u9053\u7684\u666e\u901a\u963b\u585e\u89c4\u5219&#xff08;\u8bfb\u7a7a\u963b\u585e\u3001\u5199\u6ee1\u963b\u585e&#xff09;&#xff1b;<\/li>\n<li>\u82e5\u4e00\u7aef\u8fdb\u7a0b\u9000\u51fa&#xff08;\u5173\u95ed\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff09;&#xff0c;\u53e6\u4e00\u7aef\u7684 open() \u4e0d\u4f1a\u91cd\u65b0\u963b\u585e&#xff0c;\u4ec5\u8bfb\u5199\u884c\u4e3a\u53d7\u5f71\u54cd\u3002<\/li>\n<\/ul>\n<h4>\u4e09\u3001\u975e\u963b\u585e\u6a21\u5f0f&#xff08;O_NONBLOCK&#xff09;<\/h4>\n<p>\u901a\u8fc7\u5728 open() \u7684 flags \u4e2d\u6dfb\u52a0 O_NONBLOCK&#xff0c;\u53ef\u4ee5\u53d6\u6d88\u6253\u5f00\u9636\u6bb5\u7684\u963b\u585e&#xff0c;\u6838\u5fc3\u89c4\u5219&#xff1a;\u65e0\u8bba\u53e6\u4e00\u7aef\u662f\u5426\u6253\u5f00&#xff0c;open() \u90fd\u4f1a\u7acb\u5373\u8fd4\u56de&#xff08;\u6210\u529f \/ \u5931\u8d25&#xff09;\u3002<\/p>\n<h5>1. \u573a\u666f\u62c6\u89e3&#xff08;\u975e\u963b\u585e\u6a21\u5f0f&#xff09;<\/h5>\n<table>\n<tr>\u6253\u5f00\u65b9\u5f0f\u53e6\u4e00\u7aef\u672a\u6253\u5f00\u65f6\u884c\u4e3a\u53e6\u4e00\u7aef\u5df2\u6253\u5f00\u65f6\u884c\u4e3a<\/tr>\n<tbody>\n<tr>\n<td>1. &#096;O_RDONLY<\/td>\n<td>O_NONBLOCK&#096;<\/td>\n<td>open() \u7acb\u5373\u6210\u529f\u8fd4\u56de&#xff08;\u5f97\u5230\u8bfb\u7aef\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff09;<\/td>\n<td>open() \u7acb\u5373\u6210\u529f\u8fd4\u56de<\/td>\n<\/tr>\n<tr>\n<td>2. &#096;O_WRONLY<\/td>\n<td>O_NONBLOCK&#096;<\/td>\n<td>open() \u5931\u8d25&#xff0c;\u8fd4\u56de -1&#xff0c;errno &#061; ENXIO<\/td>\n<td>open() \u7acb\u5373\u6210\u529f\u8fd4\u56de<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h5>2. \u4ee3\u7801\u793a\u4f8b&#xff08;\u975e\u963b\u585e\u6a21\u5f0f&#xff09;<\/h5>\n<p>        \/\/ \u975e\u963b\u585e\u6253\u5f00\u8bfb\u7aef&#xff08;\u5373\u4f7f\u65e0\u5199\u8fdb\u7a0b\u4e5f\u6210\u529f&#xff09;<br \/>\nint fd_read &#061; open(&#034;.\/myfifo&#034;, O_RDONLY | O_NONBLOCK);<br \/>\nif (fd_read &#061;&#061; -1) {<br \/>\n    perror(&#034;open read failed&#034;);<br \/>\n    return 1;<br \/>\n}<\/p>\n<p>\/\/ \u975e\u963b\u585e\u6253\u5f00\u5199\u7aef&#xff08;\u65e0\u8bfb\u8fdb\u7a0b\u5219\u5931\u8d25&#xff09;<br \/>\nint fd_write &#061; open(&#034;.\/myfifo&#034;, O_WRONLY | O_NONBLOCK);<br \/>\nif (fd_write &#061;&#061; -1) {<br \/>\n    if (errno &#061;&#061; ENXIO) {<br \/>\n        cout &lt;&lt; &#034;\u65e0\u8bfb\u8fdb\u7a0b\u6253\u5f00\u7ba1\u9053&#xff0c;\u5199\u7aef\u6253\u5f00\u5931\u8d25&#034; &lt;&lt; endl;<br \/>\n    }<br \/>\n    return 1;<br \/>\n}<\/p>\n<h5>3. \u5173\u952e\u7279\u6027<\/h5>\n<ul>\n<li>\u975e\u963b\u585e\u6a21\u5f0f\u4ec5\u5f71\u54cd open() \u9636\u6bb5&#xff0c;\u8bfb\u5199\u9636\u6bb5\u7684\u963b\u585e\u89c4\u5219\u4ecd\u9002\u7528&#xff08;\u5982\u8bfb\u7a7a\u7ba1\u9053\u65f6&#xff0c;\u975e\u963b\u585e\u8bfb\u4f1a\u8fd4\u56de -1&#xff0c;errno &#061; EAGAIN&#xff09;&#xff1b;<\/li>\n<li>\u5199\u7aef\u975e\u963b\u585e\u6253\u5f00\u7684 \u201c\u82db\u523b\u6027\u201d&#xff1a;\u5fc5\u987b\u6709\u8bfb\u7aef\u5df2\u6253\u5f00\u624d\u80fd\u6210\u529f&#xff0c;\u5426\u5219\u76f4\u63a5\u5931\u8d25&#xff08;\u8fd9\u662f\u4e3a\u4e86\u907f\u514d\u5199\u8fdb\u7a0b\u5411\u65e0\u8bfb\u8005\u7684\u7ba1\u9053\u5199\u6570\u636e&#xff0c;\u89e6\u53d1 SIGPIPE&#xff09;&#xff1b;<\/li>\n<li>\u9002\u7528\u4e8e \u201c\u65e0\u9700\u7b49\u5f85\u5bf9\u65b9\u8fdb\u7a0b\u201d \u7684\u573a\u666f&#xff08;\u5982\u5b9a\u65f6\u68c0\u67e5\u7ba1\u9053\u662f\u5426\u6709\u8bfb\u8005 \/ \u5199\u8005&#xff09;\u3002<\/li>\n<\/ul>\n<h4>\u56db\u3001\u7279\u6b8a\u573a\u666f&#xff1a;O_RDWR \u6253\u5f00&#xff08;\u7981\u6b62\u4f7f\u7528&#xff09;<\/h4>\n<p>\u90e8\u5206\u5f00\u53d1\u8005\u4f1a\u5c1d\u8bd5\u7528 O_RDWR \u6253\u5f00\u547d\u540d\u7ba1\u9053&#xff0c;\u8fd9\u662f\u5f3a\u70c8\u4e0d\u63a8\u8350\u7684&#xff0c;\u539f\u56e0&#xff1a;<\/p>\n<li>\u7834\u574f\u7ba1\u9053\u7684\u534a\u53cc\u5de5\u7279\u6027&#xff0c;\u8fdb\u7a0b\u53ef\u540c\u65f6\u8bfb\u5199&#xff0c;\u6613\u5bfc\u81f4\u6570\u636e\u9519\u4e71&#xff1b;<\/li>\n<li>\u884c\u4e3a\u672a\u5b9a\u4e49&#xff1a;\u4e0d\u540c Linux \u7248\u672c\u8868\u73b0\u4e0d\u540c&#xff08;\u6bd4\u5982 open(O_RDWR) \u4f1a\u7acb\u5373\u8fd4\u56de&#xff0c;\u65e0\u8bba\u53e6\u4e00\u7aef\u662f\u5426\u6253\u5f00&#xff09;&#xff1b;<\/li>\n<li>\u5931\u53bb\u7ba1\u9053\u7684\u963b\u585e\u540c\u6b65\u7279\u6027&#xff0c;\u65e0\u6cd5\u4fdd\u8bc1\u8fdb\u7a0b\u95f4\u7684\u901a\u4fe1\u65f6\u5e8f\u3002<\/li>\n<h4>\u4e94\u3001\u6253\u5f00\u89c4\u5219\u5e38\u89c1\u5751\u70b9\u4e0e\u907f\u5751\u6307\u5357<\/h4>\n<h5>1. \u963b\u585e\u6253\u5f00\u65f6\u8fdb\u7a0b\u5361\u6b7b<\/h5>\n<ul>\n<li>\u95ee\u9898&#xff1a;\u4ec5\u542f\u52a8\u8bfb\u8fdb\u7a0b \/ \u5199\u8fdb\u7a0b&#xff0c;open() \u4e00\u76f4\u963b\u585e&#xff0c;\u8fdb\u7a0b\u770b\u4f3c \u201c\u5361\u6b7b\u201d&#xff1b;<\/li>\n<li>\u907f\u5751&#xff1a;\u786e\u4fdd\u8bfb\u5199\u8fdb\u7a0b\u914d\u5bf9\u542f\u52a8&#xff0c;\u6216\u4f7f\u7528\u975e\u963b\u585e\u6a21\u5f0f\u5e76\u5904\u7406\u5931\u8d25\u573a\u666f\u3002<\/li>\n<\/ul>\n<h5>2. \u975e\u963b\u585e\u5199\u7aef\u6253\u5f00\u5931\u8d25<\/h5>\n<ul>\n<li>\u95ee\u9898&#xff1a;\u7528 O_WRONLY | O_NONBLOCK \u6253\u5f00\u65f6&#xff0c;\u8fd4\u56de -1 \u4e14 errno&#061;ENXIO&#xff1b;<\/li>\n<li>\u539f\u56e0&#xff1a;\u65e0\u8bfb\u8fdb\u7a0b\u6253\u5f00\u7ba1\u9053&#xff1b;<\/li>\n<li>\u907f\u5751&#xff1a;\u5148\u68c0\u67e5\u8bfb\u8fdb\u7a0b\u662f\u5426\u5b58\u5728&#xff0c;\u6216\u5148\u542f\u52a8\u8bfb\u8fdb\u7a0b\u518d\u542f\u52a8\u5199\u8fdb\u7a0b\u3002<\/li>\n<\/ul>\n<h5>3. \u591a\u6b21\u6253\u5f00\u540c\u4e00\u7aef<\/h5>\n<ul>\n<li>\u95ee\u9898&#xff1a;\u540c\u4e00\u8fdb\u7a0b\u591a\u6b21\u6253\u5f00\u540c\u4e00\u7ba1\u9053\u7684\u8bfb \/ \u5199\u7aef&#xff0c;\u5bfc\u81f4\u7ba1\u9053\u5f15\u7528\u8ba1\u6570\u589e\u52a0&#xff1b;<\/li>\n<li>\u540e\u679c&#xff1a;\u5173\u95ed\u4e00\u7aef\u65f6&#xff0c;\u82e5\u5f15\u7528\u8ba1\u6570\u4e0d\u4e3a 0&#xff0c;\u5185\u6838\u4e0d\u4f1a\u89e6\u53d1 EOF&#xff0c;\u8bfb\u8fdb\u7a0b\u4f1a\u4e00\u76f4\u963b\u585e&#xff1b;<\/li>\n<li>\u907f\u5751&#xff1a;\u4e00\u4e2a\u8fdb\u7a0b\u4ec5\u6253\u5f00\u4e00\u6b21\u7ba1\u9053\u7684\u67d0\u4e00\u7aef&#xff0c;\u907f\u514d\u91cd\u590d\u6253\u5f00\u3002<\/li>\n<\/ul>\n<h4>\u516d\u3001\u6253\u5f00\u89c4\u5219\u4e0e\u8bfb\u5199\u89c4\u5219\u7684\u5173\u8054<\/h4>\n<p>\u547d\u540d\u7ba1\u9053\u7684\u6253\u5f00\u89c4\u5219\u662f \u201c\u524d\u7f6e\u6761\u4ef6\u201d&#xff0c;\u8bfb\u5199\u89c4\u5219\u662f \u201c\u540e\u7eed\u884c\u4e3a\u201d&#xff0c;\u4e24\u8005\u7ed3\u5408\u7684\u5b8c\u6574\u903b\u8f91&#xff1a;<\/p>\n<li>\u963b\u585e\u6a21\u5f0f&#xff1a;\n<ul>\n<li>\u6253\u5f00\u6210\u529f \u2192 \u8bfb\u7a7a\u7ba1\u9053\u963b\u585e\u3001\u5199\u6ee1\u7ba1\u9053\u963b\u585e&#xff1b;<\/li>\n<li>\u5199\u7aef\u5168\u90e8\u5173\u95ed \u2192 \u8bfb\u8fdb\u7a0b\u8bfb\u53d6\u5269\u4f59\u6570\u636e\u540e\u8fd4\u56de 0&#xff08;EOF&#xff09;&#xff1b;<\/li>\n<li>\u8bfb\u7aef\u5168\u90e8\u5173\u95ed \u2192 \u5199\u8fdb\u7a0b\u5199\u6570\u636e\u89e6\u53d1 SIGPIPE\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u975e\u963b\u585e\u6a21\u5f0f&#xff1a;\n<ul>\n<li>\u6253\u5f00\u6210\u529f \u2192 \u8bfb\u7a7a\u7ba1\u9053\u8fd4\u56de -1&#xff08;EAGAIN&#xff09;\u3001\u5199\u6ee1\u7ba1\u9053\u8fd4\u56de -1&#xff08;EAGAIN&#xff09;&#xff1b;<\/li>\n<li>\u5176\u4ed6\u8bfb\u5199\u89c4\u5219\u4e0e\u963b\u585e\u6a21\u5f0f\u4e00\u81f4\u3002<\/li>\n<\/ul>\n<\/li>\n<h4>\u603b\u7ed3<\/h4>\n<li>\u9ed8\u8ba4\u963b\u585e\u6a21\u5f0f&#xff1a;\u8bfb\u5199\u7aef\u9700\u914d\u5bf9\u6253\u5f00&#xff0c;open() \u963b\u585e\u7b49\u5f85\u5bf9\u65b9&#xff0c;\u662f\u6700\u5e38\u7528\u3001\u6700\u5b89\u5168\u7684\u6a21\u5f0f&#xff1b;<\/li>\n<li>\u975e\u963b\u585e\u6a21\u5f0f&#xff1a;\u8bfb\u7aef\u6253\u5f00\u603b\u662f\u6210\u529f&#xff0c;\u5199\u7aef\u6253\u5f00\u9700\u8bfb\u7aef\u5df2\u5b58\u5728&#xff0c;\u9002\u7528\u4e8e\u65e0\u9700\u7b49\u5f85\u7684\u573a\u666f&#xff1b;<\/li>\n<li>\u6838\u5fc3\u7981\u5fcc&#xff1a;\u7981\u6b62\u7528 O_RDWR \u6253\u5f00\u547d\u540d\u7ba1\u9053&#xff0c;\u907f\u514d\u7834\u574f\u901a\u4fe1\u903b\u8f91&#xff1b;<\/li>\n<li>\u907f\u5751\u5173\u952e&#xff1a;\u786e\u4fdd\u8bfb\u5199\u8fdb\u7a0b\u914d\u5bf9\u542f\u52a8&#xff0c;\u907f\u514d\u91cd\u590d\u6253\u5f00\u540c\u4e00\u7aef&#xff0c;\u5904\u7406\u975e\u963b\u585e\u6a21\u5f0f\u7684\u9519\u8bef\u7801\u3002<\/li>\n<h3><span style=\"color:#333333\">\u4f8b\u5b50<\/span><span style=\"color:#333333\">1-<\/span><span style=\"color:#333333\">\u7528\u547d\u540d\u7ba1\u9053\u5b9e\u73b0\u6587\u4ef6\u62f7\u8d1d<\/span><\/h3>\n<p>          <span style=\"color:#333333\">\u8bfb\u53d6\u6587\u4ef6&#xff0c;\u5199\u5165\u547d\u540d\u7ba1\u9053<\/span><br \/>\n          <span style=\"color:#333333\">:<\/span><\/p>\n<h4>1. \u516c\u5171\u5934\u6587\u4ef6&#xff08;fifo_copy.h&#xff09;<\/h4>\n<p>\u5b9a\u4e49\u7ba1\u9053\u540d\u79f0\u548c\u7f13\u51b2\u533a\u5927\u5c0f&#xff0c;\u65b9\u4fbf\u4e24\u4e2a\u8fdb\u7a0b\u5171\u7528&#xff1a;<\/p>\n<p>          #ifndef FIFO_COPY_H<br \/>\n#define FIFO_COPY_H<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;sys\/stat.h&gt;<br \/>\n#include &lt;sys\/types.h&gt;<br \/>\n#include &lt;fcntl.h&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;cstring&gt;<br \/>\n#include &lt;cstdio&gt;<br \/>\n#include &lt;cerrno&gt;<\/p>\n<p>\/\/ \u547d\u540d\u7ba1\u9053\u540d\u79f0<br \/>\n#define FIFO_NAME &#034;.\/file_copy_fifo&#034;<br \/>\n\/\/ \u7f13\u51b2\u533a\u5927\u5c0f&#xff08;\u5efa\u8bae\u7b49\u4e8ePIPE_BUF&#xff0c;\u4fdd\u8bc1\u539f\u5b50\u5199\u5165&#xff09;<br \/>\n#define BUF_SIZE 4096<\/p>\n<p>#endif \/\/ FIFO_COPY_H<\/p>\n<h4>2. \u53d1\u9001\u8fdb\u7a0b&#xff08;fifo_sender.cpp&#xff09;<\/h4>\n<p>\u8d1f\u8d23\u8bfb\u53d6\u6e90\u6587\u4ef6&#xff0c;\u5c06\u5185\u5bb9\u5199\u5165\u547d\u540d\u7ba1\u9053&#xff1a;<\/p>\n<p>          #include &#034;fifo_copy.h&#034;<\/p>\n<p>int main(int argc, char *argv[]) {<br \/>\n    \/\/ 1. \u68c0\u67e5\u547d\u4ee4\u884c\u53c2\u6570<br \/>\n    if (argc !&#061; 2) {<br \/>\n        std::cerr &lt;&lt; &#034;\u7528\u6cd5&#xff1a;&#034; &lt;&lt; argv[0] &lt;&lt; &#034; &lt;\u6e90\u6587\u4ef6\u8def\u5f84&gt;&#034; &lt;&lt; std::endl;<br \/>\n        return 1;<br \/>\n    }<br \/>\n    const char* src_file &#061; argv[1];<\/p>\n<p>    \/\/ 2. \u521b\u5efa\u547d\u540d\u7ba1\u9053&#xff08;\u5df2\u5b58\u5728\u5219\u5ffd\u7565&#xff09;<br \/>\n    if (mkfifo(FIFO_NAME, 0666) &#061;&#061; -1) {<br \/>\n        if (errno !&#061; EEXIST) {<br \/>\n            perror(&#034;mkfifo failed&#034;);<br \/>\n            return 1;<br \/>\n        }<br \/>\n        std::cout &lt;&lt; &#034;\u547d\u540d\u7ba1\u9053\u5df2\u5b58\u5728&#xff0c;\u76f4\u63a5\u4f7f\u7528&#034; &lt;&lt; std::endl;<br \/>\n    } else {<br \/>\n        std::cout &lt;&lt; &#034;\u547d\u540d\u7ba1\u9053\u521b\u5efa\u6210\u529f&#xff1a;&#034; &lt;&lt; FIFO_NAME &lt;&lt; std::endl;<br \/>\n    }<\/p>\n<p>    \/\/ 3. \u6253\u5f00\u6e90\u6587\u4ef6&#xff08;\u53ea\u8bfb&#xff09;<br \/>\n    int fd_src &#061; open(src_file, O_RDONLY);<br \/>\n    if (fd_src &#061;&#061; -1) {<br \/>\n        perror(&#034;open src file failed&#034;);<br \/>\n        unlink(FIFO_NAME); \/\/ \u6e05\u7406\u7ba1\u9053<br \/>\n        return 1;<br \/>\n    }<br \/>\n    std::cout &lt;&lt; &#034;\u6e90\u6587\u4ef6\u6253\u5f00\u6210\u529f&#xff1a;&#034; &lt;&lt; src_file &lt;&lt; std::endl;<\/p>\n<p>    \/\/ 4. \u963b\u585e\u6253\u5f00\u547d\u540d\u7ba1\u9053\u5199\u7aef&#xff08;\u7b49\u5f85\u63a5\u6536\u8fdb\u7a0b\u8fde\u63a5&#xff09;<br \/>\n    std::cout &lt;&lt; &#034;\u7b49\u5f85\u63a5\u6536\u8fdb\u7a0b\u8fde\u63a5&#8230;&#034; &lt;&lt; std::endl;<br \/>\n    int fd_fifo &#061; open(FIFO_NAME, O_WRONLY);<br \/>\n    if (fd_fifo &#061;&#061; -1) {<br \/>\n        perror(&#034;open fifo write failed&#034;);<br \/>\n        close(fd_src);<br \/>\n        unlink(FIFO_NAME);<br \/>\n        return 1;<br \/>\n    }<br \/>\n    std::cout &lt;&lt; &#034;\u63a5\u6536\u8fdb\u7a0b\u5df2\u8fde\u63a5&#xff0c;\u5f00\u59cb\u62f7\u8d1d\u6587\u4ef6&#8230;&#034; &lt;&lt; std::endl;<\/p>\n<p>    \/\/ 5. \u9010\u6bb5\u8bfb\u53d6\u6e90\u6587\u4ef6\u5e76\u5199\u5165\u7ba1\u9053<br \/>\n    char buf[BUF_SIZE] &#061; {0};<br \/>\n    ssize_t read_len, write_len;<br \/>\n    while ((read_len &#061; read(fd_src, buf, BUF_SIZE)) &gt; 0) {<br \/>\n        \/\/ \u5199\u5165\u7ba1\u9053&#xff08;\u786e\u4fdd\u5168\u90e8\u6570\u636e\u5199\u5165&#xff09;<br \/>\n        write_len &#061; write(fd_fifo, buf, read_len);<br \/>\n        if (write_len &#061;&#061; -1) {<br \/>\n            perror(&#034;write fifo failed&#034;);<br \/>\n            break;<br \/>\n        }<br \/>\n        if (write_len !&#061; read_len) {<br \/>\n            std::cerr &lt;&lt; &#034;\u8b66\u544a&#xff1a;\u90e8\u5206\u6570\u636e\u672a\u5199\u5165&#xff0c;\u5df2\u5199 &#034; &lt;&lt; write_len &lt;&lt; &#034; \/ \u5e94\u5199 &#034; &lt;&lt; read_len &lt;&lt; std::endl;<br \/>\n        }<br \/>\n        std::cout &lt;&lt; &#034;\u5df2\u62f7\u8d1d &#034; &lt;&lt; read_len &lt;&lt; &#034; \u5b57\u8282&#034; &lt;&lt; std::endl;<br \/>\n    }<\/p>\n<p>    \/\/ 6. \u68c0\u67e5\u8bfb\u53d6\u7ed3\u679c<br \/>\n    if (read_len &#061;&#061; -1) {<br \/>\n        perror(&#034;read src file failed&#034;);<br \/>\n    } else {<br \/>\n        std::cout &lt;&lt; &#034;\u6587\u4ef6\u62f7\u8d1d\u5b8c\u6210&#xff01;\u603b\u5199\u5165\u7ba1\u9053\u6570\u636e&#xff1a;&#034; &lt;&lt; (ssize_t)(lseek(fd_src, 0, SEEK_END)) &lt;&lt; &#034; \u5b57\u8282&#034; &lt;&lt; std::endl;<br \/>\n    }<\/p>\n<p>    \/\/ 7. \u5173\u95ed\u6587\u4ef6\u63cf\u8ff0\u7b26<br \/>\n    close(fd_src);<br \/>\n    close(fd_fifo);<br \/>\n    std::cout &lt;&lt; &#034;\u53d1\u9001\u8fdb\u7a0b&#xff1a;\u5df2\u5173\u95ed\u6587\u4ef6\u548c\u7ba1\u9053&#034; &lt;&lt; std::endl;<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<h4>3. \u63a5\u6536\u8fdb\u7a0b&#xff08;fifo_receiver.cpp&#xff09;<\/h4>\n<p>\u8d1f\u8d23\u4ece\u547d\u540d\u7ba1\u9053\u8bfb\u53d6\u5185\u5bb9&#xff0c;\u5199\u5165\u76ee\u6807\u6587\u4ef6&#xff1a;<\/p>\n<p>          #include &#034;fifo_copy.h&#034;<\/p>\n<p>int main(int argc, char *argv[]) {<br \/>\n    \/\/ 1. \u68c0\u67e5\u547d\u4ee4\u884c\u53c2\u6570<br \/>\n    if (argc !&#061; 2) {<br \/>\n        std::cerr &lt;&lt; &#034;\u7528\u6cd5&#xff1a;&#034; &lt;&lt; argv[0] &lt;&lt; &#034; &lt;\u76ee\u6807\u6587\u4ef6\u8def\u5f84&gt;&#034; &lt;&lt; std::endl;<br \/>\n        return 1;<br \/>\n    }<br \/>\n    const char* dst_file &#061; argv[1];<\/p>\n<p>    \/\/ 2. \u963b\u585e\u6253\u5f00\u547d\u540d\u7ba1\u9053\u8bfb\u7aef&#xff08;\u7b49\u5f85\u53d1\u9001\u8fdb\u7a0b\u8fde\u63a5&#xff09;<br \/>\n    std::cout &lt;&lt; &#034;\u7b49\u5f85\u53d1\u9001\u8fdb\u7a0b\u8fde\u63a5&#8230;&#034; &lt;&lt; std::endl;<br \/>\n    int fd_fifo &#061; open(FIFO_NAME, O_RDONLY);<br \/>\n    if (fd_fifo &#061;&#061; -1) {<br \/>\n        perror(&#034;open fifo read failed&#034;);<br \/>\n        return 1;<br \/>\n    }<br \/>\n    std::cout &lt;&lt; &#034;\u53d1\u9001\u8fdb\u7a0b\u5df2\u8fde\u63a5&#xff0c;\u51c6\u5907\u63a5\u6536\u6570\u636e&#8230;&#034; &lt;&lt; std::endl;<\/p>\n<p>    \/\/ 3. \u6253\u5f00\/\u521b\u5efa\u76ee\u6807\u6587\u4ef6&#xff08;\u53ea\u5199&#xff0c;\u521b\u5efa&#xff0c;\u622a\u65ad&#xff09;<br \/>\n    int fd_dst &#061; open(dst_file, O_WRONLY | O_CREAT | O_TRUNC, 0666);<br \/>\n    if (fd_dst &#061;&#061; -1) {<br \/>\n        perror(&#034;open dst file failed&#034;);<br \/>\n        close(fd_fifo);<br \/>\n        unlink(FIFO_NAME);<br \/>\n        return 1;<br \/>\n    }<br \/>\n    std::cout &lt;&lt; &#034;\u76ee\u6807\u6587\u4ef6\u6253\u5f00\/\u521b\u5efa\u6210\u529f&#xff1a;&#034; &lt;&lt; dst_file &lt;&lt; std::endl;<\/p>\n<p>    \/\/ 4. \u9010\u6bb5\u4ece\u7ba1\u9053\u8bfb\u53d6\u5e76\u5199\u5165\u76ee\u6807\u6587\u4ef6<br \/>\n    char buf[BUF_SIZE] &#061; {0};<br \/>\n    ssize_t read_len, write_len;<br \/>\n    while ((read_len &#061; read(fd_fifo, buf, BUF_SIZE)) &gt; 0) {<br \/>\n        \/\/ \u5199\u5165\u76ee\u6807\u6587\u4ef6<br \/>\n        write_len &#061; write(fd_dst, buf, read_len);<br \/>\n        if (write_len &#061;&#061; -1) {<br \/>\n            perror(&#034;write dst file failed&#034;);<br \/>\n            break;<br \/>\n        }<br \/>\n        if (write_len !&#061; read_len) {<br \/>\n            std::cerr &lt;&lt; &#034;\u8b66\u544a&#xff1a;\u90e8\u5206\u6570\u636e\u672a\u5199\u5165\u76ee\u6807\u6587\u4ef6&#xff0c;\u5df2\u5199 &#034; &lt;&lt; write_len &lt;&lt; &#034; \/ \u5e94\u5199 &#034; &lt;&lt; read_len &lt;&lt; std::endl;<br \/>\n        }<br \/>\n        std::cout &lt;&lt; &#034;\u5df2\u63a5\u6536 &#034; &lt;&lt; read_len &lt;&lt; &#034; \u5b57\u8282\u5e76\u5199\u5165\u76ee\u6807\u6587\u4ef6&#034; &lt;&lt; std::endl;<br \/>\n    }<\/p>\n<p>    \/\/ 5. \u68c0\u67e5\u8bfb\u53d6\u7ed3\u679c<br \/>\n    if (read_len &#061;&#061; -1) {<br \/>\n        perror(&#034;read fifo failed&#034;);<br \/>\n    } else if (read_len &#061;&#061; 0) {<br \/>\n        std::cout &lt;&lt; &#034;\u7ba1\u9053\u5199\u7aef\u5df2\u5173\u95ed&#xff0c;\u63a5\u6536\u5b8c\u6210&#xff01;\u603b\u5199\u5165\u76ee\u6807\u6587\u4ef6\u6570\u636e&#xff1a;&#034; &lt;&lt; (ssize_t)(lseek(fd_dst, 0, SEEK_END)) &lt;&lt; &#034; \u5b57\u8282&#034; &lt;&lt; std::endl;<br \/>\n    }<\/p>\n<p>    \/\/ 6. \u5173\u95ed\u6587\u4ef6\u63cf\u8ff0\u7b26<br \/>\n    close(fd_dst);<br \/>\n    close(fd_fifo);<br \/>\n    std::cout &lt;&lt; &#034;\u63a5\u6536\u8fdb\u7a0b&#xff1a;\u5df2\u5173\u95ed\u6587\u4ef6\u548c\u7ba1\u9053&#034; &lt;&lt; std::endl;<\/p>\n<p>    \/\/ 7. \u5220\u9664\u547d\u540d\u7ba1\u9053&#xff08;\u6700\u540e\u9000\u51fa\u7684\u8fdb\u7a0b\u8d1f\u8d23\u6e05\u7406&#xff09;<br \/>\n    if (unlink(FIFO_NAME) &#061;&#061; -1) {<br \/>\n        perror(&#034;unlink fifo failed&#034;);<br \/>\n    } else {<br \/>\n        std::cout &lt;&lt; &#034;\u547d\u540d\u7ba1\u9053\u5df2\u5220\u9664&#xff1a;&#034; &lt;&lt; FIFO_NAME &lt;&lt; std::endl;<br \/>\n    }<\/p>\n<p>    return 0;<br \/>\n} <\/p>\n<h3><span style=\"color:#333333\">\u4f8b\u5b50<\/span><span style=\"color:#333333\">2-<\/span><span style=\"color:#333333\">\u7528\u547d\u540d\u7ba1\u9053\u5b9e\u73b0<\/span><span style=\"color:#333333\">server&amp;client<\/span><span style=\"color:#333333\">\u901a\u4fe1<\/span><\/h3>\n<h4>1. \u516c\u5171\u5934\u6587\u4ef6&#xff08;fifo_ipc.h&#xff09;<\/h4>\n<p>\u5b9a\u4e49\u7ba1\u9053\u540d\u79f0\u3001\u7f13\u51b2\u533a\u5927\u5c0f\u548c\u6838\u5fc3\u5e38\u91cf&#xff0c;\u4f9b\u670d\u52a1\u7aef \/ \u5ba2\u6237\u7aef\u5171\u7528&#xff1a;<\/p>\n<p>#ifndef FIFO_IPC_H<br \/>\n#define FIFO_IPC_H<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;sys\/stat.h&gt;<br \/>\n#include &lt;sys\/types.h&gt;<br \/>\n#include &lt;fcntl.h&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;cstring&gt;<br \/>\n#include &lt;cstdio&gt;<br \/>\n#include &lt;cerrno&gt;<br \/>\n#include &lt;string&gt;<\/p>\n<p>\/\/ \u547d\u540d\u7ba1\u9053\u540d\u79f0&#xff08;\u533a\u5206\u670d\u52a1\u7aef\u63a5\u6536\/\u56de\u590d\u7ba1\u9053&#xff09;<br \/>\n#define SERVER_FIFO &#034;.\/server_fifo&#034;  \/\/ Client \u2192 Server<br \/>\n#define CLIENT_FIFO &#034;.\/client_fifo&#034;  \/\/ Server \u2192 Client<br \/>\n\/\/ \u7f13\u51b2\u533a\u5927\u5c0f&#xff08;PIPE_BUF&#xff0c;\u4fdd\u8bc1\u539f\u5b50\u5199\u5165&#xff09;<br \/>\n#define BUF_SIZE 4096<br \/>\n\/\/ \u9000\u51fa\u6307\u4ee4<br \/>\n#define EXIT_CMD &#034;exit&#034;<\/p>\n<p>\/\/ \u5c01\u88c5\u9519\u8bef\u5904\u7406\u51fd\u6570<br \/>\ninline void handle_error(const std::string&amp; msg) {<br \/>\n    perror(msg.c_str());<br \/>\n    exit(EXIT_FAILURE);<br \/>\n}<\/p>\n<p>#endif \/\/ FIFO_IPC_H<\/p>\n<h4>2. \u670d\u52a1\u7aef\u4ee3\u7801&#xff08;fifo_server.cpp&#xff09;<\/h4>\n<p>\u6301\u7eed\u76d1\u542c\u5ba2\u6237\u7aef\u6d88\u606f&#xff0c;\u5904\u7406\u540e\u56de\u590d&#xff0c;\u652f\u6301\u591a\u5ba2\u6237\u7aef&#xff08;\u4e32\u884c\u5904\u7406&#xff09;&#xff1a;<\/p>\n<p>#include &#034;fifo_ipc.h&#034;<\/p>\n<p>int main() {<br \/>\n    \/\/ 1. \u521b\u5efa\u670d\u52a1\u7aef\u63a5\u6536\u7ba1\u9053&#xff08;Client\u2192Server&#xff09;<br \/>\n    if (mkfifo(SERVER_FIFO, 0666) &#061;&#061; -1) {<br \/>\n        if (errno !&#061; EEXIST) {<br \/>\n            handle_error(&#034;mkfifo server_fifo failed&#034;);<br \/>\n        }<br \/>\n        std::cout &lt;&lt; &#034;[Server] \u670d\u52a1\u7aef\u7ba1\u9053\u5df2\u5b58\u5728&#xff0c;\u590d\u7528\u8be5\u7ba1\u9053&#034; &lt;&lt; std::endl;<br \/>\n    } else {<br \/>\n        std::cout &lt;&lt; &#034;[Server] \u670d\u52a1\u7aef\u7ba1\u9053\u521b\u5efa\u6210\u529f&#xff1a;&#034; &lt;&lt; SERVER_FIFO &lt;&lt; std::endl;<br \/>\n    }<\/p>\n<p>    \/\/ 2. \u963b\u585e\u6253\u5f00\u670d\u52a1\u7aef\u7ba1\u9053\u8bfb\u7aef&#xff08;\u6301\u7eed\u76d1\u542c&#xff09;<br \/>\n    int fd_server &#061; open(SERVER_FIFO, O_RDONLY);<br \/>\n    if (fd_server &#061;&#061; -1) {<br \/>\n        handle_error(&#034;open server_fifo read failed&#034;);<br \/>\n    }<br \/>\n    std::cout &lt;&lt; &#034;[Server] \u670d\u52a1\u7aef\u5df2\u542f\u52a8&#xff0c;\u7b49\u5f85\u5ba2\u6237\u7aef\u6d88\u606f&#8230;&#034; &lt;&lt; std::endl;<\/p>\n<p>    char recv_buf[BUF_SIZE] &#061; {0};<br \/>\n    ssize_t recv_len;<\/p>\n<p>    \/\/ 3. \u6301\u7eed\u8bfb\u53d6\u5ba2\u6237\u7aef\u6d88\u606f<br \/>\n    while (true) {<br \/>\n        \/\/ \u6e05\u7a7a\u7f13\u51b2\u533a<br \/>\n        memset(recv_buf, 0, sizeof(recv_buf));<br \/>\n        \/\/ \u963b\u585e\u8bfb\u53d6\u5ba2\u6237\u7aef\u6d88\u606f&#xff08;\u65e0\u6d88\u606f\u5219\u7b49\u5f85&#xff09;<br \/>\n        recv_len &#061; read(fd_server, recv_buf, BUF_SIZE &#8211; 1);<\/p>\n<p>        if (recv_len &#061;&#061; -1) {<br \/>\n            handle_error(&#034;read server_fifo failed&#034;);<br \/>\n        } else if (recv_len &#061;&#061; 0) {<br \/>\n            \/\/ \u5ba2\u6237\u7aef\u5173\u95ed\u5199\u7aef&#xff0c;\u7ee7\u7eed\u7b49\u5f85\u4e0b\u4e00\u4e2a\u5ba2\u6237\u7aef<br \/>\n            std::cout &lt;&lt; &#034;[Server] \u5ba2\u6237\u7aef\u8fde\u63a5\u65ad\u5f00&#xff0c;\u7b49\u5f85\u65b0\u5ba2\u6237\u7aef&#8230;&#034; &lt;&lt; std::endl;<br \/>\n            continue;<br \/>\n        }<\/p>\n<p>        \/\/ 4. \u5904\u7406\u5ba2\u6237\u7aef\u6d88\u606f<br \/>\n        std::string client_msg(recv_buf);<br \/>\n        std::cout &lt;&lt; &#034;[Server] \u6536\u5230\u5ba2\u6237\u7aef\u6d88\u606f&#xff1a;&#034; &lt;&lt; client_msg &lt;&lt; std::endl;<\/p>\n<p>        \/\/ \u68c0\u6d4b\u9000\u51fa\u6307\u4ee4&#xff08;\u670d\u52a1\u7aef\u4e0d\u9000\u51fa&#xff0c;\u4ec5\u63d0\u793a&#xff09;<br \/>\n        if (client_msg &#061;&#061; EXIT_CMD) {<br \/>\n            std::cout &lt;&lt; &#034;[Server] \u5ba2\u6237\u7aef\u53d1\u8d77\u9000\u51fa\u8bf7\u6c42&#034; &lt;&lt; std::endl;<br \/>\n            continue;<br \/>\n        }<\/p>\n<p>        \/\/ 5. \u51c6\u5907\u56de\u590d\u6d88\u606f<br \/>\n        std::string reply_msg &#061; &#034;Server\u56de\u590d&#xff1a;\u5df2\u6536\u5230\u4f60\u7684\u6d88\u606f\u3010&#034; &#043; client_msg &#043; &#034;\u3011&#034;;<br \/>\n        std::cout &lt;&lt; &#034;[Server] \u51c6\u5907\u53d1\u9001\u56de\u590d&#xff1a;&#034; &lt;&lt; reply_msg &lt;&lt; std::endl;<\/p>\n<p>        \/\/ 6. \u6253\u5f00\u5ba2\u6237\u7aef\u7ba1\u9053\u5199\u7aef&#xff0c;\u53d1\u9001\u56de\u590d<br \/>\n        int fd_client &#061; open(CLIENT_FIFO, O_WRONLY);<br \/>\n        if (fd_client &#061;&#061; -1) {<br \/>\n            handle_error(&#034;open client_fifo write failed&#034;);<br \/>\n        }<br \/>\n        write(fd_client, reply_msg.c_str(), reply_msg.length() &#043; 1); \/\/ &#043;1 \u5305\u542b\u7ed3\u675f\u7b26<br \/>\n        close(fd_client);<br \/>\n        std::cout &lt;&lt; &#034;[Server] \u56de\u590d\u5df2\u53d1\u9001&#034; &lt;&lt; std::endl;<br \/>\n    }<\/p>\n<p>    \/\/ 7. \u6e05\u7406\u8d44\u6e90&#xff08;\u5b9e\u9645\u4e0d\u4f1a\u6267\u884c&#xff0c;\u9700\u624b\u52a8\u7ec8\u6b62\u670d\u52a1\u7aef&#xff09;<br \/>\n    close(fd_server);<br \/>\n    unlink(SERVER_FIFO);<br \/>\n    unlink(CLIENT_FIFO);<br \/>\n    return 0;<br \/>\n}<\/p>\n<h4>3. \u5ba2\u6237\u7aef\u4ee3\u7801&#xff08;fifo_client.cpp&#xff09;<\/h4>\n<p>\u4e3b\u52a8\u8fde\u63a5\u670d\u52a1\u7aef&#xff0c;\u53d1\u9001\u6d88\u606f\u5e76\u63a5\u6536\u56de\u590d&#xff0c;\u652f\u6301\u624b\u52a8\u8f93\u5165\u591a\u6761\u6d88\u606f&#xff1a;<\/p>\n<p>#include &#034;fifo_ipc.h&#034;<\/p>\n<p>int main() {<br \/>\n    \/\/ 1. \u521b\u5efa\u5ba2\u6237\u7aef\u63a5\u6536\u7ba1\u9053&#xff08;Server\u2192Client&#xff09;<br \/>\n    if (mkfifo(CLIENT_FIFO, 0666) &#061;&#061; -1) {<br \/>\n        if (errno !&#061; EEXIST) {<br \/>\n            handle_error(&#034;mkfifo client_fifo failed&#034;);<br \/>\n        }<br \/>\n        std::cout &lt;&lt; &#034;[Client] \u5ba2\u6237\u7aef\u7ba1\u9053\u5df2\u5b58\u5728&#xff0c;\u590d\u7528\u8be5\u7ba1\u9053&#034; &lt;&lt; std::endl;<br \/>\n    } else {<br \/>\n        std::cout &lt;&lt; &#034;[Client] \u5ba2\u6237\u7aef\u7ba1\u9053\u521b\u5efa\u6210\u529f&#xff1a;&#034; &lt;&lt; CLIENT_FIFO &lt;&lt; std::endl;<br \/>\n    }<\/p>\n<p>    \/\/ 2. \u6253\u5f00\u670d\u52a1\u7aef\u7ba1\u9053\u5199\u7aef&#xff08;\u53d1\u9001\u6d88\u606f\u7528&#xff09;<br \/>\n    int fd_server &#061; open(SERVER_FIFO, O_WRONLY);<br \/>\n    if (fd_server &#061;&#061; -1) {<br \/>\n        handle_error(&#034;open server_fifo write failed&#034;);<br \/>\n    }<br \/>\n    std::cout &lt;&lt; &#034;[Client] \u5df2\u8fde\u63a5\u670d\u52a1\u7aef&#xff0c;\u53ef\u8f93\u5165\u6d88\u606f&#xff08;\u8f93\u5165exit\u9000\u51fa&#xff09;&#xff1a;&#034; &lt;&lt; std::endl;<\/p>\n<p>    std::string input_msg;<br \/>\n    char reply_buf[BUF_SIZE] &#061; {0};<br \/>\n    ssize_t reply_len;<\/p>\n<p>    \/\/ 3. \u5faa\u73af\u8f93\u5165\u6d88\u606f\u5e76\u53d1\u9001<br \/>\n    while (true) {<br \/>\n        \/\/ \u8bfb\u53d6\u7528\u6237\u8f93\u5165<br \/>\n        std::cout &lt;&lt; &#034;[Client] \u8bf7\u8f93\u5165\u6d88\u606f&#xff1a;&#034;;<br \/>\n        std::getline(std::cin, input_msg);<\/p>\n<p>        \/\/ \u53d1\u9001\u6d88\u606f\u5230\u670d\u52a1\u7aef<br \/>\n        write(fd_server, input_msg.c_str(), input_msg.length() &#043; 1);<\/p>\n<p>        \/\/ \u68c0\u6d4b\u9000\u51fa\u6307\u4ee4<br \/>\n        if (input_msg &#061;&#061; EXIT_CMD) {<br \/>\n            std::cout &lt;&lt; &#034;[Client] \u9000\u51fa\u5ba2\u6237\u7aef&#034; &lt;&lt; std::endl;<br \/>\n            break;<br \/>\n        }<\/p>\n<p>        \/\/ 4. \u6253\u5f00\u5ba2\u6237\u7aef\u7ba1\u9053\u8bfb\u7aef&#xff0c;\u63a5\u6536\u670d\u52a1\u7aef\u56de\u590d<br \/>\n        int fd_client &#061; open(CLIENT_FIFO, O_RDONLY);<br \/>\n        if (fd_client &#061;&#061; -1) {<br \/>\n            handle_error(&#034;open client_fifo read failed&#034;);<br \/>\n        }<br \/>\n        memset(reply_buf, 0, sizeof(reply_buf));<br \/>\n        reply_len &#061; read(fd_client, reply_buf, BUF_SIZE &#8211; 1);<br \/>\n        if (reply_len &gt; 0) {<br \/>\n            std::cout &lt;&lt; &#034;[Client] \u6536\u5230\u670d\u52a1\u7aef\u56de\u590d&#xff1a;&#034; &lt;&lt; reply_buf &lt;&lt; std::endl;<br \/>\n        }<br \/>\n        close(fd_client);<br \/>\n    }<\/p>\n<p>    \/\/ 5. \u6e05\u7406\u8d44\u6e90<br \/>\n    close(fd_server);<br \/>\n    unlink(CLIENT_FIFO); \/\/ \u5ba2\u6237\u7aef\u9000\u51fa\u65f6\u5220\u9664\u81ea\u8eab\u7ba1\u9053<br \/>\n    std::cout &lt;&lt; &#034;[Client] \u8d44\u6e90\u5df2\u6e05\u7406&#xff0c;\u9000\u51fa&#034; &lt;&lt; std::endl;<br \/>\n    return 0;<br \/>\n} <\/p>\n<h2><span style=\"color:#333333\">\u516d.system V<\/span><span style=\"color:#333333\">\u5171\u4eab\u5185\u5b58<\/span><\/h2>\n<p> <span style=\"color:#333333\">\u5171\u4eab\u5185\u5b58\u533a\u662f\u6700\u5feb\u7684<\/span><br \/>\n <span style=\"color:#333333\">IPC<\/span><br \/>\n <span style=\"color:#333333\">\u5f62\u5f0f\u3002\u4e00\u65e6\u8fd9\u6837\u7684\u5185\u5b58\u6620\u5c04\u5230\u5171\u4eab\u5b83\u7684\u8fdb\u7a0b\u7684\u5730\u5740\u7a7a\u95f4&#xff0c;\u8fd9\u4e9b\u8fdb\u7a0b\u95f4\u6570\u636e\u4f20\u9012\u4e0d\u518d\u6d89\u53ca\u5230\u5185\u6838&#xff0c;\u6362\u53e5\u8bdd\u8bf4\u662f\u8fdb\u7a0b\u4e0d\u518d\u901a\u8fc7\u6267\u884c\u8fdb\u5165\u5185\u6838\u7684\u7cfb\u7edf\u8c03\u7528\u6765\u4f20\u9012\u5f7c\u6b64\u7684\u6570\u636e\u3002<\/span><\/p>\n<h3><span style=\"color:#333333\">1.\u5171\u4eab\u5185\u5b58\u793a\u610f\u56fe&#043;\u6587\u5b57\u89e3\u6790<\/span><\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"532\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260130045720-697c3a309bff6.png\" width=\"950\" \/><\/p>\n<h4>1. \u8fdb\u7a0b\u5730\u5740\u7a7a\u95f4\u5e03\u5c40&#xff08;\u4ece\u4f4e\u5230\u9ad8&#xff09;<\/h4>\n<p>\u8fd9\u662f\u4e00\u4e2a 32 \u4f4d Linux \u7cfb\u7edf\u7684\u5178\u578b\u5e03\u5c40&#xff0c;\u4ece\u5730\u5740 0x00000000 \u5230 0xC0000000 \u4e3a\u7528\u6237\u7a7a\u95f4&#xff0c;0xC0000000 \u4ee5\u4e0a\u4e3a\u5185\u6838\u7a7a\u95f4\u3002<\/p>\n<table>\n<tr>\u5730\u5740\u8303\u56f4\u6bb5\u540d\u4f5c\u7528<\/tr>\n<tbody>\n<tr>\n<td>0x08048000 \u9644\u8fd1<\/td>\n<td>\u6587\u672c\u6bb5&#xff08;Text&#xff09;<\/td>\n<td>\u5b58\u653e\u7a0b\u5e8f\u7684\u53ef\u6267\u884c\u4ee3\u7801&#xff0c;\u53ea\u8bfb\u3002<\/td>\n<\/tr>\n<tr>\n<td>0x08048000 \u4ee5\u4e0a<\/td>\n<td>\u521d\u59cb\u5316\u6570\u636e\u6bb5&#xff08;Data&#xff09;<\/td>\n<td>\u5b58\u653e\u5df2\u521d\u59cb\u5316\u7684\u5168\u5c40\u53d8\u91cf\u548c\u9759\u6001\u53d8\u91cf\u3002<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>\u672a\u521d\u59cb\u5316\u6570\u636e\u6bb5&#xff08;BSS&#xff09;<\/td>\n<td>\u5b58\u653e\u672a\u521d\u59cb\u5316\u7684\u5168\u5c40\u53d8\u91cf\u548c\u9759\u6001\u53d8\u91cf&#xff0c;\u7a0b\u5e8f\u542f\u52a8\u65f6\u4f1a\u88ab\u6e05\u96f6\u3002<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>\u5806&#xff08;Heap&#xff09;<\/td>\n<td>\u52a8\u6001\u5185\u5b58\u5206\u914d\u533a\u57df&#xff08;malloc\/new&#xff09;&#xff0c;\u5411\u4e0a\u589e\u957f\u3002<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>\u4e3a\u6269\u5c55\u4fdd\u7559<\/td>\n<td>\u9884\u7559\u533a\u57df&#xff0c;\u7528\u4e8e\u5806\u7684\u6269\u5c55\u3002<\/td>\n<\/tr>\n<tr>\n<td>0x40000000 \u9644\u8fd1<\/td>\n<td>\u5171\u4eab\u5185\u5b58 \/ \u5185\u5b58\u6620\u5c04\u533a<\/td>\n<td>\u5b58\u653e\u5171\u4eab\u5185\u5b58\u3001\u5185\u5b58\u6620\u5c04\u6587\u4ef6&#xff08;mmap&#xff09;\u548c\u5171\u4eab\u5e93\u3002<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>\u6808&#xff08;Stack&#xff09;<\/td>\n<td>\u5b58\u653e\u5c40\u90e8\u53d8\u91cf\u548c\u51fd\u6570\u8c03\u7528\u6808\u5e27&#xff0c;\u5411\u4e0b\u589e\u957f\u3002<\/td>\n<\/tr>\n<tr>\n<td>0xC0000000<\/td>\n<td>argv\/environ<\/td>\n<td>\u5b58\u653e\u547d\u4ee4\u884c\u53c2\u6570\u548c\u73af\u5883\u53d8\u91cf\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>2. \u5171\u4eab\u5185\u5b58\u7684\u6838\u5fc3\u4f5c\u7528<\/h4>\n<p>\u56fe\u4e2d\u7070\u8272\u7684 \u201c\u5171\u4eab\u5185\u5b58\u3001\u5185\u5b58\u6620\u5c04\u548c\u5171\u4eab\u5e93\u4f4d\u4e8e\u6b64\u5904\u201d \u662f\u5173\u952e&#xff0c;\u5b83\u89e3\u91ca\u4e86\u8fdb\u7a0b\u95f4\u901a\u4fe1\u7684\u539f\u7406&#xff1a;<\/p>\n<ul>\n<li>\u5730\u5740\u6620\u5c04&#xff1a;\u8fdb\u7a0b A \u548c\u8fdb\u7a0b B \u4f1a\u5c06\u540c\u4e00\u5757\u7269\u7406\u5185\u5b58&#xff0c;\u6620\u5c04\u5230\u5404\u81ea\u5730\u5740\u7a7a\u95f4\u7684 \u201c\u5171\u4eab\u5185\u5b58\u533a\u201d\u3002<\/li>\n<li>\u76f4\u63a5\u8bbf\u95ee&#xff1a;\u4e24\u4e2a\u8fdb\u7a0b\u53ef\u4ee5\u76f4\u63a5\u8bfb\u5199\u8fd9\u5757\u5185\u5b58&#xff0c;\u65e0\u9700\u6570\u636e\u62f7\u8d1d&#xff0c;\u662f\u6548\u7387\u6700\u9ad8\u7684 IPC&#xff08;\u8fdb\u7a0b\u95f4\u901a\u4fe1&#xff09;\u65b9\u5f0f\u3002<\/li>\n<li>\u6570\u636e\u540c\u6b65&#xff1a;\u4e00\u4e2a\u8fdb\u7a0b\u5199\u5165\u7684\u6570\u636e&#xff0c;\u53e6\u4e00\u4e2a\u8fdb\u7a0b\u53ef\u4ee5\u7acb\u5373\u770b\u5230&#xff0c;\u5b9e\u73b0\u4e86\u9ad8\u6548\u7684\u5b9e\u65f6\u901a\u4fe1\u3002<\/li>\n<\/ul>\n<h4>3. \u6838\u5fc3\u610f\u4e49<\/h4>\n<p>\u8fd9\u5f20\u56fe\u6e05\u6670\u5730\u89e3\u91ca\u4e86\u4e3a\u4ec0\u4e48\u5171\u4eab\u5185\u5b58\u662f\u6700\u5feb\u7684 IPC \u65b9\u5f0f&#xff1a;<\/p>\n<ul>\n<li>\u6570\u636e\u76f4\u63a5\u5728\u7269\u7406\u5185\u5b58\u4e2d\u5171\u4eab&#xff0c;\u4e0d\u9700\u8981\u50cf\u7ba1\u9053 \/ \u6d88\u606f\u961f\u5217\u90a3\u6837\u5728\u5185\u6838\u548c\u7528\u6237\u7a7a\u95f4\u4e4b\u95f4\u6765\u56de\u62f7\u8d1d\u3002<\/li>\n<li>\u5171\u4eab\u5185\u5b58\u533a\u7684\u5730\u5740\u7a7a\u95f4\u662f\u8fdb\u7a0b\u95f4\u53ef\u5171\u4eab\u7684&#xff0c;\u8fd9\u4e5f\u662f\u5b83\u4e0e\u5806\u3001\u6808\u7b49\u79c1\u6709\u533a\u57df\u7684\u672c\u8d28\u533a\u522b\u3002<\/li>\n<\/ul>\n<h3><span style=\"color:#333333\">2.\u5171\u4eab\u5185\u5b58\u6570\u636e\u7ed3\u6784<\/span><\/h3>\n<p>struct shmid_ds {<br \/>\n struct ipc_perm shm_perm; \/* operation perms *\/<br \/>\n int shm_segsz; \/* size of segment (bytes) *\/<br \/>\n __kernel_time_t shm_atime; \/* last attach time *\/<br \/>\n __kernel_time_t shm_dtime; \/* last detach time *\/<br \/>\n __kernel_time_t shm_ctime; \/* last change time *\/<br \/>\n __kernel_ipc_pid_t shm_cpid; \/* pid of creator *\/<br \/>\n __kernel_ipc_pid_t shm_lpid; \/* pid of last operator *\/<br \/>\n unsigned short shm_nattch; \/* no. of current attaches *\/<br \/>\n unsigned short shm_unused; \/* compatibility *\/<br \/>\n void *shm_unused2; \/* ditto &#8211; used by DIPC *\/<br \/>\n void *shm_unused3; \/* unused *\/<br \/>\n}; <\/p>\n<h3><span style=\"color:#333333\">3.\u5171\u4eab\u5185\u5b58\u51fd\u6570(\u8be6\u7ec6\u8bb2\u89e3)<\/span><\/h3>\n<h4>\u4e00\u3001shmget ()&#xff1a;\u521b\u5efa \/ \u83b7\u53d6\u5171\u4eab\u5185\u5b58\u6bb5<\/h4>\n<h5>1. \u57fa\u7840\u4fe1\u606f<\/h5>\n<table>\n<tr>\u9879\u5177\u4f53\u5185\u5bb9<\/tr>\n<tbody>\n<tr>\n<td>\u51fd\u6570\u539f\u578b<\/td>\n<td>int shmget(key_t key, size_t size, int shmflg);<\/td>\n<\/tr>\n<tr>\n<td>\u6240\u9700\u5934\u6587\u4ef6<\/td>\n<td>&lt;sys\/ipc.h&gt; &#043; &lt;sys\/shm.h&gt;&#xff08;C\/C&#043;&#043; \u901a\u7528&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u8fd4\u56de\u503c<\/td>\n<td>\u6210\u529f\u8fd4\u56de\u5171\u4eab\u5185\u5b58\u6807\u8bc6\u7b26&#xff08;shmid&#xff09;&#xff1b;\u5931\u8d25\u8fd4\u56de-1&#xff0c;\u8bbe\u7f6eerrno<\/td>\n<\/tr>\n<tr>\n<td>\u6838\u5fc3\u4f5c\u7528<\/td>\n<td>\u521b\u5efa\u65b0\u7684\u5171\u4eab\u5185\u5b58\u6bb5&#xff0c;\u6216\u83b7\u53d6\u5df2\u5b58\u5728\u7684\u5171\u4eab\u5185\u5b58\u6bb5\u7684\u6807\u8bc6\u7b26<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h5>2. \u53c2\u6570\u8be6\u89e3<\/h5>\n<table>\n<tr>\u53c2\u6570\u542b\u4e49 &amp; \u89c4\u5219<\/tr>\n<tbody>\n<tr>\n<td>key<\/td>\n<td>\u5171\u4eab\u5185\u5b58\u7684\u552f\u4e00\u6807\u8bc6&#xff08;\u952e\u503c&#xff09;&#xff1a;\u2460 IPC_PRIVATE&#xff08;0&#xff09;&#xff1a;\u521b\u5efa\u79c1\u6709\u5171\u4eab\u5185\u5b58&#xff08;\u4ec5\u4eb2\u7f18\u8fdb\u7a0b\u53ef\u7528&#xff09;&#xff1b;\u2461 \u81ea\u5b9a\u4e49\u952e\u503c&#xff08;\u5982 0x1234&#xff09;&#xff1a;\u901a\u8fc7 ftok() \u751f\u6210\u552f\u4e00\u952e\u503c&#xff0c;\u4f9b\u4efb\u610f\u8fdb\u7a0b\u8bbf\u95ee\u3002<\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>size<\/td>\n<td>\u5171\u4eab\u5185\u5b58\u6bb5\u7684\u5927\u5c0f&#xff08;\u5b57\u8282&#xff09;&#xff1a;\u2705 \u5efa\u8bae\u6309\u7cfb\u7edf\u9875\u5927\u5c0f&#xff08;getpagesize()&#xff0c;\u901a\u5e38 4KB&#xff09;\u5bf9\u9f50&#xff1b;\u274c \u6700\u5c0f\u4e3a 1 \u5b57\u8282&#xff0c;\u6700\u5927\u53d7\u7cfb\u7edf\u9650\u5236&#xff08;\/proc\/sys\/kernel\/shmmax&#xff09;\u3002<\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>shmflg<\/td>\n<td>\u6807\u5fd7\u4f4d&#xff08;\u6743\u9650 &#043; \u884c\u4e3a&#xff09;&#xff1a;\u2460 \u6743\u9650\u4f4d&#xff1a;\u5982 0666&#xff08;\u6240\u6709\u7528\u6237\u53ef\u8bfb\u53ef\u5199&#xff09;&#xff0c;\u4e0e\u6587\u4ef6\u6743\u9650\u89c4\u5219\u4e00\u81f4&#xff1b;\u2461 \u884c\u4e3a\u4f4d&#xff1a;- IPC_CREAT&#xff1a;\u4e0d\u5b58\u5728\u5219\u521b\u5efa&#xff0c;\u5b58\u5728\u5219\u83b7\u53d6&#xff1b;- IPC_EXCL&#xff1a;\u4e0e IPC_CREAT \u914d\u5408&#xff0c;\u82e5\u5df2\u5b58\u5728\u5219\u62a5\u9519&#xff08;\u786e\u4fdd\u521b\u5efa\u65b0\u6bb5&#xff09;&#xff1b;\u2705 \u5e38\u7528\u7ec4\u5408&#xff1a;&#096;IPC_CREAT<\/td>\n<td>0666\u3001IPC_CREAT<\/td>\n<td>IPC_EXCL<\/td>\n<td>0666&#096;\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h5>3. \u5e38\u89c1\u9519\u8bef\u7801&#xff08;errno&#xff09;<\/h5>\n<table>\n<tr>\u9519\u8bef\u7801\u542b\u4e49\u89e3\u51b3\u65b9\u6cd5<\/tr>\n<tbody>\n<tr>\n<td>EEXIST<\/td>\n<td>\u5df2\u5b58\u5728\u8be5\u5171\u4eab\u5185\u5b58\u6bb5&#xff08;\u4f7f\u7528 IPC_EXCL \u65f6&#xff09;<\/td>\n<td>\u6539\u7528 IPC_CREAT \u6216\u5220\u9664\u5df2\u6709\u6bb5&#xff08;ipcrm -m shmid&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>EINVAL<\/td>\n<td>size \u65e0\u6548&#xff08;\u8fc7\u5927 \/ \u8fc7\u5c0f&#xff09;\u6216 shmid \u65e0\u6548<\/td>\n<td>\u8c03\u6574 size \u4e3a\u9875\u5927\u5c0f\u6574\u6570\u500d&#xff0c;\u68c0\u67e5 shmid<\/td>\n<\/tr>\n<tr>\n<td>ENOMEM<\/td>\n<td>\u7cfb\u7edf\u5185\u5b58\u4e0d\u8db3<\/td>\n<td>\u91ca\u653e\u7cfb\u7edf\u5185\u5b58&#xff0c;\u51cf\u5c0f size<\/td>\n<\/tr>\n<tr>\n<td>EACCES<\/td>\n<td>\u65e0\u6743\u9650\u8bbf\u95ee<\/td>\n<td>\u8c03\u6574 shmflg \u7684\u6743\u9650\u4f4d&#xff08;\u5982 0666&#xff09;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h5>4. \u57fa\u7840\u4f7f\u7528\u793a\u4f8b<\/h5>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;sys\/ipc.h&gt;<br \/>\n#include &lt;sys\/shm.h&gt;<br \/>\n#include &lt;cstdio&gt;<br \/>\n#include &lt;unistd.h&gt;<\/p>\n<p>using namespace std;<\/p>\n<p>#define SHM_KEY 0x1234   \/\/ \u81ea\u5b9a\u4e49\u952e\u503c<br \/>\n#define SHM_SIZE 4096    \/\/ \u5171\u4eab\u5185\u5b58\u5927\u5c0f&#xff08;4KB&#xff0c;\u9875\u5bf9\u9f50&#xff09;<\/p>\n<p>int main() {<br \/>\n    \/\/ \u521b\u5efa\u5171\u4eab\u5185\u5b58&#xff08;\u4e0d\u5b58\u5728\u5219\u521b\u5efa&#xff0c;\u5b58\u5728\u5219\u83b7\u53d6&#xff09;<br \/>\n    int shmid &#061; shmget(SHM_KEY, SHM_SIZE, IPC_CREAT | 0666);<br \/>\n    if (shmid &#061;&#061; -1) {<br \/>\n        perror(&#034;shmget failed&#034;);<br \/>\n        return 1;<br \/>\n    }<br \/>\n    cout &lt;&lt; &#034;\u5171\u4eab\u5185\u5b58\u521b\u5efa\/\u83b7\u53d6\u6210\u529f&#xff0c;shmid &#061; &#034; &lt;&lt; shmid &lt;&lt; endl;<br \/>\n    return 0;<br \/>\n}<\/p>\n<h4>\u4e8c\u3001shmat ()&#xff1a;\u5c06\u5171\u4eab\u5185\u5b58\u6620\u5c04\u5230\u8fdb\u7a0b\u5730\u5740\u7a7a\u95f4<\/h4>\n<h5>1. \u57fa\u7840\u4fe1\u606f<\/h5>\n<table>\n<tr>\u9879\u5177\u4f53\u5185\u5bb9<\/tr>\n<tbody>\n<tr>\n<td>\u51fd\u6570\u539f\u578b<\/td>\n<td>void *shmat(int shmid, const void *shmaddr, int shmflg);<\/td>\n<\/tr>\n<tr>\n<td>\u8fd4\u56de\u503c<\/td>\n<td>\u6210\u529f\u8fd4\u56de\u5171\u4eab\u5185\u5b58\u7684\u865a\u62df\u5730\u5740&#xff1b;\u5931\u8d25\u8fd4\u56de(void*)-1&#xff0c;\u8bbe\u7f6eerrno<\/td>\n<\/tr>\n<tr>\n<td>\u6838\u5fc3\u4f5c\u7528<\/td>\n<td>\u5c06\u5185\u6838\u4e2d\u7684\u5171\u4eab\u5185\u5b58\u6bb5&#xff0c;\u6620\u5c04\u5230\u5f53\u524d\u8fdb\u7a0b\u7684\u7528\u6237\u5730\u5740\u7a7a\u95f4&#xff08;\u5982\u56fe\u4e2d \u201c\u5171\u4eab\u5185\u5b58\u533a\u201d&#xff09;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h5>2. \u53c2\u6570\u8be6\u89e3<\/h5>\n<table>\n<tr>\u53c2\u6570\u542b\u4e49 &amp; \u89c4\u5219<\/tr>\n<tbody>\n<tr>\n<td>shmid<\/td>\n<td>shmget() \u8fd4\u56de\u7684\u5171\u4eab\u5185\u5b58\u6807\u8bc6\u7b26<\/td>\n<\/tr>\n<tr>\n<td>shmaddr<\/td>\n<td>\u6307\u5b9a\u6620\u5c04\u7684\u865a\u62df\u5730\u5740&#xff1a;\u2460 NULL&#xff08;\u63a8\u8350&#xff09;&#xff1a;\u7531\u7cfb\u7edf\u81ea\u52a8\u5206\u914d\u5408\u9002\u7684\u5730\u5740&#xff1b;\u2461 \u975e NULL&#xff1a;\u6307\u5b9a\u5730\u5740&#xff08;\u9700\u5bf9\u9f50&#xff0c;\u4e0d\u63a8\u8350&#xff0c;\u79fb\u690d\u6027\u5dee&#xff09;\u3002<\/td>\n<\/tr>\n<tr>\n<td>shmflg<\/td>\n<td>\u6620\u5c04\u6807\u5fd7&#xff1a;\u2460 0&#xff1a;\u9ed8\u8ba4&#xff0c;\u8bfb\u5199\u6743\u9650&#xff1b;\u2461 SHM_RDONLY&#xff1a;\u53ea\u8bfb\u6620\u5c04&#xff08;\u4ec5\u80fd\u8bfb\u53d6&#xff0c;\u4e0d\u80fd\u5199\u5165&#xff09;&#xff1b;\u2462 SHM_REMAP&#xff1a;\u66ff\u6362\u5df2\u6709\u6620\u5c04&#xff08;\u614e\u7528&#xff09;\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h5>3. \u5173\u952e\u7279\u6027<\/h5>\n<ul>\n<li>\u6620\u5c04\u540e&#xff0c;\u8fdb\u7a0b\u53ef\u4ee5\u50cf\u64cd\u4f5c\u666e\u901a\u5185\u5b58\u4e00\u6837&#xff08;*ptr\u3001memcpy&#xff09;\u8bfb\u5199\u5171\u4eab\u5185\u5b58&#xff1b;<\/li>\n<li>\u591a\u4e2a\u8fdb\u7a0b\u6620\u5c04\u540c\u4e00\u4e2a shmid&#xff0c;\u4f1a\u6307\u5411\u540c\u4e00\u5757\u7269\u7406\u5185\u5b58&#xff0c;\u5b9e\u73b0\u6570\u636e\u5171\u4eab&#xff1b;<\/li>\n<li>\u6620\u5c04\u5730\u5740\u5c5e\u4e8e\u8fdb\u7a0b\u79c1\u6709&#xff0c;\u4f46\u6307\u5411\u7684\u7269\u7406\u5185\u5b58\u662f\u5171\u4eab\u7684\u3002<\/li>\n<\/ul>\n<h5>4. \u4ee3\u7801\u793a\u4f8b<\/h5>\n<p>\/\/ \u6620\u5c04\u5171\u4eab\u5185\u5b58&#xff08;\u7cfb\u7edf\u81ea\u52a8\u5206\u914d\u5730\u5740&#xff0c;\u8bfb\u5199\u6743\u9650&#xff09;<br \/>\nvoid* shm_addr &#061; shmat(shmid, NULL, 0);<br \/>\nif (shm_addr &#061;&#061; (void*)-1) {<br \/>\n    perror(&#034;shmat failed&#034;);<br \/>\n    shmctl(shmid, IPC_RMID, NULL); \/\/ \u6e05\u7406\u5171\u4eab\u5185\u5b58<br \/>\n    return 1;<br \/>\n}<br \/>\ncout &lt;&lt; &#034;\u5171\u4eab\u5185\u5b58\u6620\u5c04\u6210\u529f&#xff0c;\u865a\u62df\u5730\u5740&#xff1a;&#034; &lt;&lt; shm_addr &lt;&lt; endl;<\/p>\n<h4>\u4e09\u3001shmdt ()&#xff1a;\u89e3\u9664\u5171\u4eab\u5185\u5b58\u6620\u5c04<\/h4>\n<h5>1. \u57fa\u7840\u4fe1\u606f<\/h5>\n<table>\n<tr>\u9879\u5177\u4f53\u5185\u5bb9<\/tr>\n<tbody>\n<tr>\n<td>\u51fd\u6570\u539f\u578b<\/td>\n<td>int shmdt(const void *shmaddr);<\/td>\n<\/tr>\n<tr>\n<td>\u8fd4\u56de\u503c<\/td>\n<td>\u6210\u529f\u8fd4\u56de0&#xff1b;\u5931\u8d25\u8fd4\u56de-1&#xff0c;\u8bbe\u7f6eerrno&#xff08;EINVAL \u8868\u793a\u5730\u5740\u65e0\u6548&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u6838\u5fc3\u4f5c\u7528<\/td>\n<td>\u5c06\u5171\u4eab\u5185\u5b58\u4ece\u8fdb\u7a0b\u5730\u5740\u7a7a\u95f4\u4e2d\u89e3\u9664\u6620\u5c04&#xff0c;\u4e0d\u518d\u8bbf\u95ee\u8be5\u5185\u5b58&#xff0c;\u4f46\u5171\u4eab\u5185\u5b58\u6bb5\u672c\u8eab\u4ecd\u5b58\u5728<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h5>2. \u53c2\u6570\u8be6\u89e3<\/h5>\n<table>\n<tr>\u53c2\u6570\u542b\u4e49 &amp; \u89c4\u5219<\/tr>\n<tbody>\n<tr>\n<td>shmaddr<\/td>\n<td>shmat() \u8fd4\u56de\u7684\u5171\u4eab\u5185\u5b58\u865a\u62df\u5730\u5740&#xff08;\u5fc5\u987b\u662f\u6709\u6548\u7684\u6620\u5c04\u5730\u5740&#xff09;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h5>3. \u5173\u952e\u7279\u6027<\/h5>\n<ul>\n<li>shmdt() \u4ec5\u89e3\u9664\u5f53\u524d\u8fdb\u7a0b\u7684\u6620\u5c04&#xff0c;\u4e0d\u4f1a\u5220\u9664\u5171\u4eab\u5185\u5b58\u6bb5&#xff1b;<\/li>\n<li>\u8fdb\u7a0b\u9000\u51fa\u65f6&#xff0c;\u5185\u6838\u4f1a\u81ea\u52a8\u89e3\u9664\u6620\u5c04&#xff0c;\u4f46\u5efa\u8bae\u624b\u52a8\u8c03\u7528&#xff08;\u89c4\u8303&#xff09;&#xff1b;<\/li>\n<li>\u89e3\u9664\u6620\u5c04\u540e&#xff0c;\u8be5\u5730\u5740\u4e0d\u53ef\u518d\u8bbf\u95ee&#xff0c;\u5426\u5219\u4f1a\u89e6\u53d1\u6bb5\u9519\u8bef&#xff08;SIGSEGV&#xff09;\u3002<\/li>\n<\/ul>\n<h5>4. \u4ee3\u7801\u793a\u4f8b<\/h5>\n<p>\/\/ \u89e3\u9664\u5171\u4eab\u5185\u5b58\u6620\u5c04<br \/>\nif (shmdt(shm_addr) &#061;&#061; -1) {<br \/>\n    perror(&#034;shmdt failed&#034;);<br \/>\n    return 1;<br \/>\n}<br \/>\ncout &lt;&lt; &#034;\u5171\u4eab\u5185\u5b58\u6620\u5c04\u5df2\u89e3\u9664&#034; &lt;&lt; endl;<\/p>\n<h4>\u56db\u3001shmctl ()&#xff1a;\u63a7\u5236\u5171\u4eab\u5185\u5b58\u6bb5&#xff08;\u6838\u5fc3\u7ba1\u7406\u51fd\u6570&#xff09;<\/h4>\n<h5>1. \u57fa\u7840\u4fe1\u606f<\/h5>\n<table>\n<tr>\u9879\u5177\u4f53\u5185\u5bb9<\/tr>\n<tbody>\n<tr>\n<td>\u51fd\u6570\u539f\u578b<\/td>\n<td>int shmctl(int shmid, int cmd, struct shmid_ds *buf);<\/td>\n<\/tr>\n<tr>\n<td>\u8fd4\u56de\u503c<\/td>\n<td>\u6210\u529f\u8fd4\u56de0&#xff08;\u6216\u5bf9\u5e94\u4fe1\u606f&#xff09;&#xff1b;\u5931\u8d25\u8fd4\u56de-1&#xff0c;\u8bbe\u7f6eerrno<\/td>\n<\/tr>\n<tr>\n<td>\u6838\u5fc3\u4f5c\u7528<\/td>\n<td>\u5bf9\u5171\u4eab\u5185\u5b58\u6bb5\u8fdb\u884c\u7ba1\u7406&#xff08;\u83b7\u53d6\u4fe1\u606f\u3001\u4fee\u6539\u6743\u9650\u3001\u5220\u9664\u6bb5&#xff09;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h5>2. \u53c2\u6570\u8be6\u89e3<\/h5>\n<table>\n<tr>\u53c2\u6570\u542b\u4e49 &amp; \u89c4\u5219<\/tr>\n<tbody>\n<tr>\n<td>shmid<\/td>\n<td>shmget() \u8fd4\u56de\u7684\u5171\u4eab\u5185\u5b58\u6807\u8bc6\u7b26<\/td>\n<\/tr>\n<tr>\n<td>cmd<\/td>\n<td>\u63a7\u5236\u547d\u4ee4&#xff08;\u6838\u5fc3&#xff09;&#xff1a;\u2460 IPC_STAT&#xff1a;\u83b7\u53d6\u5171\u4eab\u5185\u5b58\u72b6\u6001&#xff0c;\u5b58\u5165 buf&#xff1b;\u2461 IPC_SET&#xff1a;\u4fee\u6539\u5171\u4eab\u5185\u5b58\u5c5e\u6027&#xff08;\u5982\u6743\u9650&#xff09;&#xff0c;\u4ece buf \u8bfb\u53d6&#xff1b;\u2462 IPC_RMID&#xff1a;\u5220\u9664\u5171\u4eab\u5185\u5b58\u6bb5&#xff08;\u6700\u5e38\u7528&#xff09;&#xff1b;\u2463 SHM_LOCK&#xff1a;\u9501\u5b9a\u5171\u4eab\u5185\u5b58&#xff08;\u4e0d\u6362\u51fa\u5230\u4ea4\u6362\u533a&#xff09;&#xff1b;\u2464 SHM_UNLOCK&#xff1a;\u89e3\u9501\u5171\u4eab\u5185\u5b58\u3002<\/td>\n<\/tr>\n<tr>\n<td>buf<\/td>\n<td>\u6307\u5411 struct shmid_ds \u7ed3\u6784\u4f53\u7684\u6307\u9488&#xff1a;\u2460 IPC_STAT\/IPC_SET&#xff1a;\u9700\u4f20\u5165\u6709\u6548\u6307\u9488&#xff1b;\u2461 IPC_RMID&#xff1a;\u53ef\u4f20\u5165 NULL&#xff08;\u65e0\u9700\u7ed3\u6784\u4f53&#xff09;\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h5>3. \u6838\u5fc3\u7ed3\u6784\u4f53 shmid_ds&#xff08;\u5173\u952e\u5b57\u6bb5&#xff09;<\/h5>\n<p>struct shmid_ds {<br \/>\n    struct ipc_perm shm_perm;  \/\/ \u6743\u9650\u7ed3\u6784\u4f53&#xff08;uid\/gid\/\u6a21\u5f0f&#xff09;<br \/>\n    size_t          shm_segsz; \/\/ \u5171\u4eab\u5185\u5b58\u6bb5\u5927\u5c0f&#xff08;\u5b57\u8282&#xff09;<br \/>\n    pid_t           shm_lpid;  \/\/ \u6700\u540e\u64cd\u4f5c\u8be5\u6bb5\u7684\u8fdb\u7a0bID<br \/>\n    pid_t           shm_cpid;  \/\/ \u521b\u5efa\u8be5\u6bb5\u7684\u8fdb\u7a0bID<br \/>\n    shmatt_t        shm_nattch;\/\/ \u5f53\u524d\u6620\u5c04\u7684\u8fdb\u7a0b\u6570<br \/>\n    time_t          shm_atime; \/\/ \u6700\u540e\u6620\u5c04\u65f6\u95f4<br \/>\n    time_t          shm_dtime; \/\/ \u6700\u540e\u89e3\u9664\u6620\u5c04\u65f6\u95f4<br \/>\n    time_t          shm_ctime; \/\/ \u6700\u540e\u4fee\u6539\u65f6\u95f4<br \/>\n};<\/p>\n<h5>4. \u5e38\u7528\u573a\u666f\u793a\u4f8b<\/h5>\n<h6>\u573a\u666f 1&#xff1a;\u5220\u9664\u5171\u4eab\u5185\u5b58\u6bb5&#xff08;\u6700\u5e38\u7528&#xff09;<\/h6>\n<p>\/\/ \u5220\u9664\u5171\u4eab\u5185\u5b58\u6bb5&#xff08;\u6240\u6709\u8fdb\u7a0b\u89e3\u9664\u6620\u5c04\u540e&#xff0c;\u5185\u6838\u91ca\u653e\u7269\u7406\u5185\u5b58&#xff09;<br \/>\nif (shmctl(shmid, IPC_RMID, NULL) &#061;&#061; -1) {<br \/>\n    perror(&#034;shmctl IPC_RMID failed&#034;);<br \/>\n    return 1;<br \/>\n}<br \/>\ncout &lt;&lt; &#034;\u5171\u4eab\u5185\u5b58\u6bb5\u5df2\u5220\u9664&#034; &lt;&lt; endl;<\/p>\n<h6>\u573a\u666f 2&#xff1a;\u83b7\u53d6\u5171\u4eab\u5185\u5b58\u72b6\u6001<\/h6>\n<p>struct shmid_ds shm_info;<br \/>\n\/\/ \u83b7\u53d6\u5171\u4eab\u5185\u5b58\u4fe1\u606f<br \/>\nif (shmctl(shmid, IPC_STAT, &amp;shm_info) &#061;&#061; -1) {<br \/>\n    perror(&#034;shmctl IPC_STAT failed&#034;);<br \/>\n    return 1;<br \/>\n}<br \/>\ncout &lt;&lt; &#034;\u5171\u4eab\u5185\u5b58\u5927\u5c0f&#xff1a;&#034; &lt;&lt; shm_info.shm_segsz &lt;&lt; &#034; \u5b57\u8282&#034; &lt;&lt; endl;<br \/>\ncout &lt;&lt; &#034;\u5f53\u524d\u6620\u5c04\u8fdb\u7a0b\u6570&#xff1a;&#034; &lt;&lt; shm_info.shm_nattch &lt;&lt; endl;<br \/>\ncout &lt;&lt; &#034;\u521b\u5efa\u8fdb\u7a0bID&#xff1a;&#034; &lt;&lt; shm_info.shm_cpid &lt;&lt; endl;<\/p>\n<h4>\u4e94\u3001\u8f85\u52a9\u51fd\u6570&#xff1a;ftok ()&#xff08;\u751f\u6210\u552f\u4e00\u952e\u503c&#xff09;<\/h4>\n<h5>1. \u57fa\u7840\u4fe1\u606f<\/h5>\n<table>\n<tr>\u9879\u5177\u4f53\u5185\u5bb9<\/tr>\n<tbody>\n<tr>\n<td>\u51fd\u6570\u539f\u578b<\/td>\n<td>key_t ftok(const char *pathname, int proj_id);<\/td>\n<\/tr>\n<tr>\n<td>\u8fd4\u56de\u503c<\/td>\n<td>\u6210\u529f\u8fd4\u56de\u552f\u4e00key_t\u952e\u503c&#xff1b;\u5931\u8d25\u8fd4\u56de-1&#xff0c;\u8bbe\u7f6eerrno<\/td>\n<\/tr>\n<tr>\n<td>\u6838\u5fc3\u4f5c\u7528<\/td>\n<td>\u6839\u636e\u6587\u4ef6\u8def\u5f84\u548c\u9879\u76ee ID&#xff0c;\u751f\u6210\u552f\u4e00\u7684key&#xff0c;\u4f9bshmget()\u521b\u5efa\u5171\u4eab\u5185\u5b58<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h5>2. \u4ee3\u7801\u793a\u4f8b<\/h5>\n<p>\/\/ \u751f\u6210\u552f\u4e00\u952e\u503c&#xff08;pathname\u9700\u662f\u5b58\u5728\u7684\u6587\u4ef6&#xff0c;proj_id\u53d61-255&#xff09;<br \/>\nkey_t key &#061; ftok(&#034;.\/test.file&#034;, 1);<br \/>\nif (key &#061;&#061; -1) {<br \/>\n    perror(&#034;ftok failed&#034;);<br \/>\n    return 1;<br \/>\n}<br \/>\ncout &lt;&lt; &#034;\u751f\u6210\u7684\u952e\u503c&#xff1a;&#034; &lt;&lt; hex &lt;&lt; key &lt;&lt; endl;<\/p>\n<h4>\u516d\u3001\u5171\u4eab\u5185\u5b58\u5b8c\u6574\u5b9e\u6218\u6848\u4f8b&#xff08;\u4e24\u4e2a\u8fdb\u7a0b\u901a\u4fe1&#xff09;<\/h4>\n<h5>1. \u5199\u8fdb\u7a0b&#xff08;shm_writer.cpp&#xff09;<\/h5>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;sys\/ipc.h&gt;<br \/>\n#include &lt;sys\/shm.h&gt;<br \/>\n#include &lt;cstring&gt;<br \/>\n#include &lt;cstdio&gt;<\/p>\n<p>using namespace std;<\/p>\n<p>#define SHM_KEY 0x1234<br \/>\n#define SHM_SIZE 4096<\/p>\n<p>int main() {<br \/>\n    \/\/ 1. \u521b\u5efa\u5171\u4eab\u5185\u5b58<br \/>\n    int shmid &#061; shmget(SHM_KEY, SHM_SIZE, IPC_CREAT | IPC_EXCL | 0666);<br \/>\n    if (shmid &#061;&#061; -1) {<br \/>\n        perror(&#034;shmget failed (exist: use IPC_CREAT only)&#034;);<br \/>\n        \/\/ \u82e5\u5df2\u5b58\u5728&#xff0c;\u5c1d\u8bd5\u83b7\u53d6<br \/>\n        shmid &#061; shmget(SHM_KEY, SHM_SIZE, 0666);<br \/>\n        if (shmid &#061;&#061; -1) {<br \/>\n            perror(&#034;shmget get failed&#034;);<br \/>\n            return 1;<br \/>\n        }<br \/>\n    }<br \/>\n    cout &lt;&lt; &#034;\u5199\u8fdb\u7a0b&#xff1a;\u5171\u4eab\u5185\u5b58shmid &#061; &#034; &lt;&lt; shmid &lt;&lt; endl;<\/p>\n<p>    \/\/ 2. \u6620\u5c04\u5171\u4eab\u5185\u5b58<br \/>\n    void* shm_addr &#061; shmat(shmid, NULL, 0);<br \/>\n    if (shm_addr &#061;&#061; (void*)-1) {<br \/>\n        perror(&#034;shmat failed&#034;);<br \/>\n        shmctl(shmid, IPC_RMID, NULL);<br \/>\n        return 1;<br \/>\n    }<br \/>\n    cout &lt;&lt; &#034;\u5199\u8fdb\u7a0b&#xff1a;\u6620\u5c04\u5730\u5740 &#061; &#034; &lt;&lt; shm_addr &lt;&lt; endl;<\/p>\n<p>    \/\/ 3. \u5199\u5165\u6570\u636e<br \/>\n    const char* msg &#061; &#034;Hello from Shared Memory Writer!&#034;;<br \/>\n    memcpy(shm_addr, msg, strlen(msg)&#043;1); \/\/ &#043;1 \u5305\u542b\u7ed3\u675f\u7b26<br \/>\n    cout &lt;&lt; &#034;\u5199\u8fdb\u7a0b&#xff1a;\u5df2\u5199\u5165\u6570\u636e&#xff1a;&#034; &lt;&lt; msg &lt;&lt; endl;<\/p>\n<p>    \/\/ 4. \u7b49\u5f85\u8bfb\u8fdb\u7a0b\u8bfb\u53d6&#xff08;\u6309\u56de\u8f66\u7ee7\u7eed&#xff09;<br \/>\n    cout &lt;&lt; &#034;\u5199\u8fdb\u7a0b&#xff1a;\u6309\u56de\u8f66\u89e3\u9664\u6620\u5c04\u5e76\u5220\u9664\u5171\u4eab\u5185\u5b58&#8230;&#034; &lt;&lt; endl;<br \/>\n    getchar();<\/p>\n<p>    \/\/ 5. \u89e3\u9664\u6620\u5c04<br \/>\n    if (shmdt(shm_addr) &#061;&#061; -1) {<br \/>\n        perror(&#034;shmdt failed&#034;);<br \/>\n        return 1;<br \/>\n    }<\/p>\n<p>    \/\/ 6. \u5220\u9664\u5171\u4eab\u5185\u5b58\u6bb5<br \/>\n    if (shmctl(shmid, IPC_RMID, NULL) &#061;&#061; -1) {<br \/>\n        perror(&#034;shmctl IPC_RMID failed&#034;);<br \/>\n        return 1;<br \/>\n    }<br \/>\n    cout &lt;&lt; &#034;\u5199\u8fdb\u7a0b&#xff1a;\u5171\u4eab\u5185\u5b58\u5df2\u5220\u9664&#034; &lt;&lt; endl;<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<h5>2. \u8bfb\u8fdb\u7a0b&#xff08;shm_reader.cpp&#xff09;<\/h5>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;sys\/ipc.h&gt;<br \/>\n#include &lt;sys\/shm.h&gt;<br \/>\n#include &lt;cstring&gt;<br \/>\n#include &lt;cstdio&gt;<\/p>\n<p>using namespace std;<\/p>\n<p>#define SHM_KEY 0x1234<br \/>\n#define SHM_SIZE 4096<\/p>\n<p>int main() {<br \/>\n    \/\/ 1. \u83b7\u53d6\u5171\u4eab\u5185\u5b58&#xff08;\u4ec5\u83b7\u53d6&#xff0c;\u4e0d\u521b\u5efa&#xff09;<br \/>\n    int shmid &#061; shmget(SHM_KEY, SHM_SIZE, 0666);<br \/>\n    if (shmid &#061;&#061; -1) {<br \/>\n        perror(&#034;shmget failed (no such shm)&#034;);<br \/>\n        return 1;<br \/>\n    }<br \/>\n    cout &lt;&lt; &#034;\u8bfb\u8fdb\u7a0b&#xff1a;\u5171\u4eab\u5185\u5b58shmid &#061; &#034; &lt;&lt; shmid &lt;&lt; endl;<\/p>\n<p>    \/\/ 2. \u6620\u5c04\u5171\u4eab\u5185\u5b58<br \/>\n    void* shm_addr &#061; shmat(shmid, NULL, 0);<br \/>\n    if (shm_addr &#061;&#061; (void*)-1) {<br \/>\n        perror(&#034;shmat failed&#034;);<br \/>\n        return 1;<br \/>\n    }<br \/>\n    cout &lt;&lt; &#034;\u8bfb\u8fdb\u7a0b&#xff1a;\u6620\u5c04\u5730\u5740 &#061; &#034; &lt;&lt; shm_addr &lt;&lt; endl;<\/p>\n<p>    \/\/ 3. \u8bfb\u53d6\u6570\u636e<br \/>\n    char buf[SHM_SIZE] &#061; {0};<br \/>\n    memcpy(buf, shm_addr, SHM_SIZE);<br \/>\n    cout &lt;&lt; &#034;\u8bfb\u8fdb\u7a0b&#xff1a;\u8bfb\u53d6\u5230\u6570\u636e&#xff1a;&#034; &lt;&lt; buf &lt;&lt; endl;<\/p>\n<p>    \/\/ 4. \u89e3\u9664\u6620\u5c04<br \/>\n    if (shmdt(shm_addr) &#061;&#061; -1) {<br \/>\n        perror(&#034;shmdt failed&#034;);<br \/>\n        return 1;<br \/>\n    }<br \/>\n    cout &lt;&lt; &#034;\u8bfb\u8fdb\u7a0b&#xff1a;\u6620\u5c04\u5df2\u89e3\u9664&#034; &lt;&lt; endl;<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<h5>\u7f16\u8bd1\u8fd0\u884c<\/h5>\n<p># \u7f16\u8bd1<br \/>\ng&#043;&#043; shm_writer.cpp -o shm_writer -std&#061;c&#043;&#043;11<br \/>\ng&#043;&#043; shm_reader.cpp -o shm_reader -std&#061;c&#043;&#043;11<\/p>\n<p># \u7ec8\u7aef1&#xff1a;\u542f\u52a8\u5199\u8fdb\u7a0b&#xff08;\u963b\u585e\u7b49\u5f85\u56de\u8f66&#xff09;<br \/>\n.\/shm_writer<\/p>\n<p># \u7ec8\u7aef2&#xff1a;\u542f\u52a8\u8bfb\u8fdb\u7a0b&#xff08;\u8bfb\u53d6\u6570\u636e&#xff09;<br \/>\n.\/shm_reader<\/p>\n<p># \u7ec8\u7aef1&#xff1a;\u6309\u56de\u8f66&#xff0c;\u5199\u8fdb\u7a0b\u5220\u9664\u5171\u4eab\u5185\u5b58<\/p>\n<h4>\u4e03\u3001\u5171\u4eab\u5185\u5b58\u4f7f\u7528\u6ce8\u610f\u4e8b\u9879<\/h4>\n<h5>1. \u540c\u6b65\u95ee\u9898&#xff08;\u6838\u5fc3\u5751\u70b9&#xff09;<\/h5>\n<ul>\n<li>\u5171\u4eab\u5185\u5b58\u65e0\u5185\u7f6e\u540c\u6b65\u673a\u5236&#xff0c;\u591a\u4e2a\u8fdb\u7a0b\u540c\u65f6\u8bfb\u5199\u4f1a\u5bfc\u81f4\u6570\u636e\u9519\u4e71&#xff1b;<\/li>\n<li>\u2705 \u89e3\u51b3&#xff1a;\u914d\u5408\u4fe1\u53f7\u91cf&#xff08;Semaphore&#xff09;\/ \u4e92\u65a5\u9501&#xff08;Mutex&#xff09;\u5b9e\u73b0\u540c\u6b65\u3002<\/li>\n<\/ul>\n<h5>2. \u8d44\u6e90\u6cc4\u6f0f<\/h5>\n<ul>\n<li>shmctl(IPC_RMID) \u4ec5\u6807\u8bb0\u5220\u9664&#xff0c;\u9700\u6240\u6709\u8fdb\u7a0b\u89e3\u9664\u6620\u5c04&#xff08;shm_nattch&#061;0&#xff09;\u540e&#xff0c;\u5185\u6838\u624d\u91ca\u653e\u5185\u5b58&#xff1b;<\/li>\n<li>\u2705 \u907f\u5751&#xff1a;\u786e\u4fdd\u6700\u540e\u4e00\u4e2a\u8fdb\u7a0b\u8c03\u7528 IPC_RMID&#xff0c;\u6216\u7528 ipcrm -m shmid \u624b\u52a8\u5220\u9664\u3002<\/li>\n<\/ul>\n<h5>3. \u67e5\u770b \/ \u5220\u9664\u5171\u4eab\u5185\u5b58&#xff08;\u547d\u4ee4\u884c&#xff09;<\/h5>\n<p># \u67e5\u770b\u6240\u6709\u5171\u4eab\u5185\u5b58<br \/>\nipcs -m<\/p>\n<p># \u5220\u9664\u6307\u5b9ashmid\u7684\u5171\u4eab\u5185\u5b58<br \/>\nipcrm -m shmid<\/p>\n<h5>4. \u6743\u9650\u95ee\u9898<\/h5>\n<ul>\n<li>\u521b\u5efa\u65f6\u6743\u9650\u4f4d\u9700\u5408\u7406&#xff08;\u5982 0666&#xff09;&#xff0c;\u5426\u5219\u5176\u4ed6\u8fdb\u7a0b\u65e0\u6cd5\u8bbf\u95ee&#xff1b;<\/li>\n<li>\u2705 \u8c03\u6574\u6743\u9650&#xff1a;shmctl(shmid, IPC_SET, &amp;shm_info)&#xff08;\u4fee\u6539 shm_perm.mode&#xff09;\u3002<\/li>\n<\/ul>\n<h4>\u603b\u7ed3<\/h4>\n<li>\u6838\u5fc3\u51fd\u6570\u6d41\u7a0b&#xff1a;ftok() \u751f\u6210\u952e\u503c \u2192 shmget() \u521b\u5efa \/ \u83b7\u53d6\u6bb5 \u2192 shmat() \u6620\u5c04\u5230\u8fdb\u7a0b\u5730\u5740 \u2192 \u8bfb\u5199\u5185\u5b58 \u2192 shmdt() \u89e3\u9664\u6620\u5c04 \u2192 shmctl(IPC_RMID) \u5220\u9664\u6bb5&#xff1b;<\/li>\n<li>\u6838\u5fc3\u4f18\u52bf&#xff1a;\u65e0\u6570\u636e\u62f7\u8d1d&#xff0c;\u662f\u6700\u5feb\u7684 IPC \u65b9\u5f0f&#xff1b;<\/li>\n<li>\u6838\u5fc3\u6ce8\u610f&#xff1a;\u9700\u624b\u52a8\u5904\u7406\u540c\u6b65\u548c\u8d44\u6e90\u6e05\u7406&#xff0c;\u907f\u514d\u6570\u636e\u9519\u4e71\u548c\u5185\u5b58\u6cc4\u6f0f&#xff1b;<\/li>\n<li>\u9002\u7528\u573a\u666f&#xff1a;\u9ad8\u9891\u7387\u3001\u5927\u6570\u636e\u91cf\u7684\u8fdb\u7a0b\u95f4\u901a\u4fe1&#xff08;\u5982\u5b9e\u65f6\u6570\u636e\u4f20\u8f93&#xff09;\u3002<\/li>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u8282\u91cd\u70b9&#xff1a;  \u8fdb\u7a0b\u95f4\u901a\u4fe1\u4ecb\u7ecd \u7ba1\u9053 \u6d88\u606f\u961f\u5217 \u5171\u4eab\u5185\u5b58 \u4fe1\u53f7\u91cf\u4e00.\u8fdb\u7a0b\u95f4\u901a\u4fe1\u4ecb\u7ecd 1.\u8fdb\u7a0b\u95f4\u901a\u4fe1\u76ee\u7684 \u6570\u636e\u4f20\u8f93&#xff1a;\u4e00\u4e2a\u8fdb\u7a0b\u9700\u8981\u5c06\u5b83\u7684\u6570\u636e\u53d1\u9001\u7ed9\u53e6\u4e00\u4e2a\u8fdb\u7a0b \u8d44\u6e90\u5171\u4eab&#xff1a;\u591a\u4e2a\u8fdb\u7a0b\u4e4b\u95f4\u5171\u4eab\u540c\u6837\u7684\u8d44\u6e90\u3002 \u901a\u77e5\u4e8b\u4ef6&#xff1a;\u4e00\u4e2a\u8fdb\u7a0b\u9700\u8981\u5411\u53e6\u4e00\u4e2a\u6216\u4e00\u7ec4\u8fdb\u7a0b\u53d1\u9001\u6d88\u606f&#xff0c;\u901a\u77e5\u5b83&#xff08;\u5b83\u4eec&#xff09;\u53d1\u751f\u4e86\u67d0\u79cd\u4e8b\u4ef6&#xff08;\u5982\u8fdb\u7a0b\u7ec8\u6b62\u65f6\u8981\u901a\u77e5\u7236\u8fdb\u7a0b&#xff09;\u3002 \u8fdb\u7a0b\u63a7\u5236&#xff1a;\u6709\u4e9b\u8fdb\u7a0b\u5e0c\u671b\u5b8c\u5168\u63a7\u5236\u53e6\u4e00\u4e2a\u8fdb<\/p>\n","protected":false},"author":2,"featured_media":68672,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[58,43,44],"topic":[],"class_list":["post-68678","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-linux","tag-43","tag-44"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Linux_\u8fdb\u7a0b\u95f4\u901a\u4fe1 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/68678.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Linux_\u8fdb\u7a0b\u95f4\u901a\u4fe1 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u672c\u8282\u91cd\u70b9&#xff1a; \u8fdb\u7a0b\u95f4\u901a\u4fe1\u4ecb\u7ecd \u7ba1\u9053 \u6d88\u606f\u961f\u5217 \u5171\u4eab\u5185\u5b58 \u4fe1\u53f7\u91cf\u4e00.\u8fdb\u7a0b\u95f4\u901a\u4fe1\u4ecb\u7ecd 1.\u8fdb\u7a0b\u95f4\u901a\u4fe1\u76ee\u7684 \u6570\u636e\u4f20\u8f93&#xff1a;\u4e00\u4e2a\u8fdb\u7a0b\u9700\u8981\u5c06\u5b83\u7684\u6570\u636e\u53d1\u9001\u7ed9\u53e6\u4e00\u4e2a\u8fdb\u7a0b \u8d44\u6e90\u5171\u4eab&#xff1a;\u591a\u4e2a\u8fdb\u7a0b\u4e4b\u95f4\u5171\u4eab\u540c\u6837\u7684\u8d44\u6e90\u3002 \u901a\u77e5\u4e8b\u4ef6&#xff1a;\u4e00\u4e2a\u8fdb\u7a0b\u9700\u8981\u5411\u53e6\u4e00\u4e2a\u6216\u4e00\u7ec4\u8fdb\u7a0b\u53d1\u9001\u6d88\u606f&#xff0c;\u901a\u77e5\u5b83&#xff08;\u5b83\u4eec&#xff09;\u53d1\u751f\u4e86\u67d0\u79cd\u4e8b\u4ef6&#xff08;\u5982\u8fdb\u7a0b\u7ec8\u6b62\u65f6\u8981\u901a\u77e5\u7236\u8fdb\u7a0b&#xff09;\u3002 \u8fdb\u7a0b\u63a7\u5236&#xff1a;\u6709\u4e9b\u8fdb\u7a0b\u5e0c\u671b\u5b8c\u5168\u63a7\u5236\u53e6\u4e00\u4e2a\u8fdb\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/68678.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-01-30T04:57:21+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260130045718-697c3a2ed215b.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"28 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/68678.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/68678.html\",\"name\":\"Linux_\u8fdb\u7a0b\u95f4\u901a\u4fe1 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-01-30T04:57:21+00:00\",\"dateModified\":\"2026-01-30T04:57:21+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/68678.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/68678.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/68678.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Linux_\u8fdb\u7a0b\u95f4\u901a\u4fe1\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Linux_\u8fdb\u7a0b\u95f4\u901a\u4fe1 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/68678.html","og_locale":"zh_CN","og_type":"article","og_title":"Linux_\u8fdb\u7a0b\u95f4\u901a\u4fe1 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u672c\u8282\u91cd\u70b9&#xff1a; \u8fdb\u7a0b\u95f4\u901a\u4fe1\u4ecb\u7ecd \u7ba1\u9053 \u6d88\u606f\u961f\u5217 \u5171\u4eab\u5185\u5b58 \u4fe1\u53f7\u91cf\u4e00.\u8fdb\u7a0b\u95f4\u901a\u4fe1\u4ecb\u7ecd 1.\u8fdb\u7a0b\u95f4\u901a\u4fe1\u76ee\u7684 \u6570\u636e\u4f20\u8f93&#xff1a;\u4e00\u4e2a\u8fdb\u7a0b\u9700\u8981\u5c06\u5b83\u7684\u6570\u636e\u53d1\u9001\u7ed9\u53e6\u4e00\u4e2a\u8fdb\u7a0b \u8d44\u6e90\u5171\u4eab&#xff1a;\u591a\u4e2a\u8fdb\u7a0b\u4e4b\u95f4\u5171\u4eab\u540c\u6837\u7684\u8d44\u6e90\u3002 \u901a\u77e5\u4e8b\u4ef6&#xff1a;\u4e00\u4e2a\u8fdb\u7a0b\u9700\u8981\u5411\u53e6\u4e00\u4e2a\u6216\u4e00\u7ec4\u8fdb\u7a0b\u53d1\u9001\u6d88\u606f&#xff0c;\u901a\u77e5\u5b83&#xff08;\u5b83\u4eec&#xff09;\u53d1\u751f\u4e86\u67d0\u79cd\u4e8b\u4ef6&#xff08;\u5982\u8fdb\u7a0b\u7ec8\u6b62\u65f6\u8981\u901a\u77e5\u7236\u8fdb\u7a0b&#xff09;\u3002 \u8fdb\u7a0b\u63a7\u5236&#xff1a;\u6709\u4e9b\u8fdb\u7a0b\u5e0c\u671b\u5b8c\u5168\u63a7\u5236\u53e6\u4e00\u4e2a\u8fdb","og_url":"https:\/\/www.wsisp.com\/helps\/68678.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-01-30T04:57:21+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260130045718-697c3a2ed215b.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"28 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/68678.html","url":"https:\/\/www.wsisp.com\/helps\/68678.html","name":"Linux_\u8fdb\u7a0b\u95f4\u901a\u4fe1 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-01-30T04:57:21+00:00","dateModified":"2026-01-30T04:57:21+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/68678.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/68678.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/68678.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Linux_\u8fdb\u7a0b\u95f4\u901a\u4fe1"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/68678","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=68678"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/68678\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/68672"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=68678"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=68678"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=68678"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=68678"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}