{"id":464,"date":"2019-08-07T17:28:04","date_gmt":"2019-08-07T09:28:04","guid":{"rendered":"https:\/\/www.linuxdevops.cn\/?p=464"},"modified":"2023-03-28T13:38:26","modified_gmt":"2023-03-28T05:38:26","slug":"shelljuebenbingxingzhixingrenwu","status":"publish","type":"post","link":"https:\/\/www.linuxdevops.cn\/2019\/08\/shelljuebenbingxingzhixingrenwu\/","title":{"rendered":"Shell\u811a\u672c\u5e76\u884c\u6267\u884c\u4efb\u52a1"},"content":{"rendered":"\n
\u9ed8\u8ba4\u7684\u60c5\u51b5\u4e0b\uff0cShell\u811a\u672c\u4e2d\u7684\u547d\u4ee4\u662f\u4e32\u884c\u6267\u884c\u7684\uff0c\u5fc5\u987b\u7b49\u5230\u524d\u4e00\u6761\u547d\u4ee4\u6267\u884c\u5b8c\u540e\u624d\u6267\u884c\u63a5\u4e0b\u6765\u7684\u547d\u4ee4\uff0c\u4f46\u662f\u5982\u679c\u6211\u6709\u4e00\u5927\u6279\u7684\u7684\u547d\u4ee4\u9700\u8981\u6267\u884c\uff0c\u800c\u4e14\u4e92\u76f8\u53c8\u6ca1\u6709\u5f71\u54cd\u7684\u60c5\u51b5\u4e0b\uff08\u6709\u5f71\u54cd\u7684\u8bdd\u5c31\u6bd4\u8f83\u590d\u6742\u4e86\uff09\uff0c\u90a3\u4e48\u5c31\u8981\u4f7f\u7528\u547d\u4ee4\u7684\u5e76\u53d1\u6267\u884c\u4e86\u3002<\/p><\/blockquote>\n\n\n
\u5229\u7528\u547d\u540d\u7ba1\u9053\u5b9e\u73b0\u5e76\u884c\u3002
mkfifo \u521b\u5efaFIFO(\u4e5f\u79f0\u4e3a\"\u547d\u540d\u7ba1\u9053\").\"FIFO\"\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u6587\u4ef6\u7c7b\u578b\uff0c\u5b83\u5141\u8bb8\u72ec\u7acb\u7684\u8fdb\u7a0b\u901a\u8baf. \u4e00\u4e2a\u8fdb\u7a0b\u6253\u5f00FIFO\u6587\u4ef6\u8fdb\u884c\u5199\u64cd\u4f5c,\u800c\u53e6\u4e00\u4e2a\u8fdb\u7a0b\u5bf9\u4e4b\u8fdb\u884c\u8bfb\u64cd\u4f5c,\u7136\u540e\u6570\u636e\u4fbf\u53ef\u4ee5\u5982\u540c\u5728shell\u6216\u8005\u5176\u5b83\u5730\u65b9\u5e38\u89c1\u7684\u7684\u533f\u540d\u7ba1\u9053\u4e00\u6837\u6d41\u7ebf\u6267\u884c.<\/p><\/blockquote>\n\n\n\u5b9e\u9a8c\uff1a\u6a21\u62df\u4efb\u52a1\u6c89\u7761\u4e00\u79d2\u8f93\u51fa\u4e00\u6b21success\uff0c\u5f0020\u5e76\u884c\u7edf\u8ba1\u7528\u65f6\u3002<\/p><\/blockquote>\n\n\n
!\/bin\/bash\n #set -x\n #\u5f00\u59cb\u65f6\u95f4\n start_time=date +%s<\/code> \n #\u58f0\u660e\u5e76\u53d1\u7ebf\u7a0b\u5e76\u53d1\u4e2a\u6570\uff0c\u8fd9\u4e2a\u662f\u6b64\u5e94\u7528\u7684\u5173\u952e\uff0c\u4e5f\u5c31\u662f\u8bbe\u7f6e\u7ba1\u9053\u7684\u6700\u5927\u4efb\u52a1\u6570,\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u8c03\u8282\u4efb\u52a1\u6570\uff0c\u907f\u514d\u5e76\u884c\u4efb\u52a1\u8fc7\u591a\u670d\u52a1\u5668\u5b95\u673a\u3002\n thread=20 \n #\u521b\u5efa\u547d\u540d\u7ba1\u9053\uff0c'$$'\u8868\u793a\u811a\u672c\u5f53\u524d\u8fd0\u884c\u7684\u8fdb\u7a0bPID\n [ -e .\/tmp\/$$.fifo ] || mkfifo .\/tmp\/$$.fifo\n #\u521b\u5efa\u6587\u4ef6\u6807\u793a\u7b26\u201c3\u201d\uff0c\u8fd9\u4e2a\u6570\u5b57\u53ef\u4ee5\u4e3a\u9664\u201c0\u201d\u3001\u201c1\u201d\u3001\u201c2\u201d\u4e4b\u5916\u7684\u6240\u6709\u672a\u58f0\u660e\u8fc7\u7684\u5b57\u7b26\uff0c\u4ee5\u8bfb\u5199\u6a21\u5f0f\u64cd\u4f5c\u7ba1\u9053\u6587\u4ef6\uff1b\u7cfb\u7edf\u8c03\u7528exec\u662f\u4ee5\u65b0\u7684\u8fdb\u7a0b\u53bb\u4ee3\u66ff\u539f\u6765\u7684\u8fdb\u7a0b\uff0c\u4f46\u8fdb\u7a0b\u7684PID\u4fdd\u6301\u4e0d\u53d8\uff0c\u6362\u53e5\u8bdd\u8bf4\u5c31\u662f\u5728\u8c03\u7528\u8fdb\u7a0b\u5185\u90e8\u6267\u884c\u4e00\u4e2a\u53ef\u6267\u884c\u6587\u4ef6\n exec 3<> .\/tmp\/$$.fifo\n#\u6e05\u9664\u521b\u5efa\u7684\u7ba1\u9053\u6587\u4ef6\n rm -rf .\/tmp\/$$.fifo\n# \u4e3a\u5e76\u53d1\u7ebf\u7a0b\u521b\u5efa\u540c\u6837\u4e2a\u6570\u7684\u5360\u4f4d\n for ((i=1;i<=20;i++)) do echo >&3 #\u5c06\u5360\u4f4d\u4fe1\u606f\u5199\u5165\u7ba1\u9053\n done\n for ((i=1;i<=100;i++))\n do\n #\u4ece\u6587\u4ef6\u63cf\u8ff0\u7b26\u7ba1\u9053\u4e2d\uff0c\u83b7\u53d6\u4e00\u4e2a\u7ba1\u9053\u7684\u7ebf\u7a0b\u5360\u4f4d\u7136\u540e\u5f00\u59cb\u6267\u884c\u64cd\u4f5c\uff1bread\u4e2d -u \u540e\u9762\u8ddffd\uff0c\u8868\u793a\u4ece\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e2d\u8bfb\u5165\uff0c\u8be5\u6587\u4ef6\u63cf\u8ff0\u7b26\u53ef\u4ee5\u662fexec\u65b0\u5f00\u542f\u7684\u3002\n read -u3\n# \u6a21\u62df\u4efb\u52a1\n {\n sleep 1\n echo \"success\" $i\n #\u4efb\u52a1\u6267\u884c\u5b8c\u540e\u5728fd3\u4e2d\u91cd\u65b0\u5199\u5165\u4e00\u4e2a\u5360\u4f4d\u7b26\uff0c\u4ee5\u4fdd\u8bc1\u8fd9\u4e2a\u7ebf\u7a0b\u6267\u884c\u5b8c\u540e\uff0c\u7ebf\u7a0b\u7ee7\u7eed\u4fdd\u6301\u5360\u4f4d\uff0c\u7ee7\u800c\u7ef4\u6301\u7ba1\u9053\u4e2d\u6c38\u8fdc\u662f20\u4e2a\u7ebf\u7a0b\u6570\uff0c&\u8868\u793a\u8be5\u90e8\u5206\u547d\u4ee4\/\u4efb\u52a1\u653e\u5165\u540e\u53f0\u4e0d\u5360\u5f53\u524d\u7684bash\uff0c\u5b9e\u73b0\u5e76\u884c\u5904\u7406\n echo >&3\n }&\n done\n wait #\u7b49\u5f85\u7236\u8fdb\u7a0b\u7684\u5b50\u8fdb\u7a0b\u90fd\u6267\u884c\u7ed3\u675f\u540e\u518d\u7ed3\u675f\u7236\u8fdb\u7a0b \n stop_time=
date +%s<\/code>\n echo \" RUN TIME:
expr $stop_time - $start_time<\/code>\"\n exec 3<&- #\u5173\u95edfd3\u7684\u7ba1\u9053 \n exec 3>&- #\u5173\u95edfd3\u7684\u7ba1\u9053 \n\n<\/pre>\n\n\n
\u5b8c\u6574shell\u4ee3\u7801\uff0c<\/p>\n\n\n
\n#!\/bin\/bash\n#set -x\nstart_time=
date +%s<\/code>\nthread=20\n[ -e .\/tmp\/$$.fifo ] || mkfifo .\/tmp\/$$.fifo\nexec 3<> .\/tmp\/$$.fifo\nrm -rf .\/tmp\/$$.fifo\n\nfor ((i=1;i<=$thread;i++))\ndo\n echo >&3\ndone\n\nfor ((i=1;i<=100;i++))\ndo\nread -u3\n{\n sleep 1\n echo \"success\" $i\n echo >&3\n}&\ndone\nwait\n\nstop_time=
date +%s<\/code>\n\necho \" RUN TIME:
expr $stop_time - $start_time<\/code>\"\n\nexec 3<&-\nexec 3>&-\n\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"
\u9ed8\u8ba4\u7684\u60c5\u51b5\u4e0b\uff0cShell\u811a\u672c\u4e2d\u7684\u547d\u4ee4\u662f\u4e32\u884c\u6267\u884c\u7684\uff0c\u5fc5\u987b\u7b49\u5230\u524d\u4e00\u6761\u547d\u4ee4\u6267\u884c\u5b8c\u540e\u624d\u6267\u884c\u63a5\u4e0b\u6765\u7684\u547d\u4ee4\uff0c\u4f46\u662f\u5982\u679c\u6211\u6709\u4e00<\/p>\n","protected":false},"author":1,"featured_media":465,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17,14],"tags":[],"yoast_head":"\n
Shell\u811a\u672c\u5e76\u884c\u6267\u884c\u4efb\u52a1<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n\t\n\t\n\n\n