{"id":116377,"date":"2018-08-21T08:55:16","date_gmt":"2018-08-21T00:55:16","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=116377"},"modified":"2018-08-20T08:59:20","modified_gmt":"2018-08-20T00:59:20","slug":"unix-odd-thing","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/unix-odd-thing.html","title":{"rendered":"UNIX \u4e0b\u5947\u602a\u7684\u4e8b\u60c5"},"content":{"rendered":"
\u6700\u8fd1\u6211\u5728\u7528\u6211\u7f16\u5199\u7684\u5404\u79cd\u5de5\u5177\u505a\u66f4\u591a UNIX \u4e0b\u7684\u4e8b\u60c5\uff0c\u6211\u9047\u5230\u4e86\u4e24\u4e2a\u6709\u8da3\u7684\u95ee\u9898\u3002\u8fd9\u4e9b\u90fd\u4e0d\u662f \u201cbug\u201d\uff0c\u800c\u662f\u6211\u6ca1\u60f3\u5230\u7684\u884c\u4e3a\u3002<\/p>\n
<\/p>\n
\u6211\u6709\u4e00\u4e2a C \u7a0b\u5e8f\u4ece\u78c1\u76d8\u8bfb\u53d6\u4e00\u4e9b\u56fe\u50cf\uff0c\u8fdb\u884c\u4e00\u4e9b\u5904\u7406\uff0c\u5e76\u5c06\u6709\u5173\u8fd9\u4e9b\u56fe\u50cf\u7684\u8f93\u51fa\u5199\u5165 STDOUT\u3002\u4f2a\u4ee3\u7801\uff1a<\/p>\n
\r\nfor(imagefilename in images)\r\n{\r\n results = process(imagefilename);\r\n printf(results);\r\n}<\/pre>\n\u5bf9\u4e8e\u6bcf\u4e2a\u56fe\u50cf\u90fd\u662f\u72ec\u7acb\u5904\u7406\u7684\uff0c\u56e0\u6b64\u6211\u81ea\u7136\u5e0c\u671b\u5c06\u5904\u7406\u4efb\u52a1\u5206\u914d\u5728\u5404\u4e2a CPU \u4e4b\u95f4\u4ee5\u52a0\u5feb\u901f\u5ea6\u3002\u6211\u901a\u5e38\u4f7f\u7528 fork()\uff0c\u6240\u4ee5\u6211\u5199\u4e86\u8fd9\u4e2a\uff1a<\/p>\n
\r\nfor(child in children)\r\n{\r\n pipe = create_pipe();\r\n worker(pipe);\r\n}\r\n\/\/ main parent process\r\nfor(imagefilename in images)\r\n{\r\n write(pipe[i_image % N_children], imagefilename)\r\n}\r\nworker()\r\n{\r\n while(1)\r\n {\r\n imagefilename = read(pipe);\r\n results = process(imagefilename);\r\n printf(results);\r\n }\r\n}<\/pre>\n\u8fd9\u662f\u6b63\u5e38\u7684\u505a\u6cd5\uff1a\u6211\u4e3a IPC \u521b\u5efa\u7ba1\u9053\uff0c\u5e76\u901a\u8fc7\u8fd9\u4e9b\u7ba1\u9053\u7ed9\u5b50\u8fdb\u7a0b worker \u53d1\u9001\u56fe\u50cf\u540d\u3002\u6bcf\u4e2a worker \u80fd\u591f\u901a\u8fc7\u53e6\u4e00\u7ec4\u7ba1\u9053\u5c06\u5176\u7ed3\u679c\u5199\u56de\u4e3b\u8fdb\u7a0b\uff0c\u4f46\u8fd9\u5f88\u75db\u82e6\uff0c\u6240\u4ee5\u6bcf\u4e2a worker \u90fd\u76f4\u63a5\u5199\u5165\u5171\u4eab STDOUT\u3002\u8fd9\u5de5\u4f5c\u6b63\u5e38\uff0c\u4f46\u6b63\u5982\u4eba\u4eec\u6240\u9884\u6599\u7684\u90a3\u6837\uff0c\u5bf9 STDOUT \u7684\u5199\u5165\u53d1\u751f\u51b2\u7a81\uff0c\u56e0\u6b64\u5404\u79cd\u56fe\u50cf\u7684\u7ed3\u679c\u6700\u7ec8\u4f1a\u6df7\u6742\u5728\u4e00\u8d77\u3002\u90a3\u5f88\u7cdf\u7cd5\u3002\u6211\u4e0d\u60f3\u81ea\u5df1\u8bbe\u7f6e\u4e2a\u9501\uff0c\u4f46\u5e78\u8fd0\u7684\u662f GNU libc \u4e3a\u5b83\u63d0\u4f9b\u4e86\u51fd\u6570\uff1aflockfile()\u3002\u6211\u628a\u5b83\u4eec\u653e\u8fdb\u53bb\u4e86\u2026\u2026\u4f46\u662f\u6ca1\u6709\u7528\uff01\u4e3a\u4ec0\u4e48\uff1f\u56e0\u4e3a flockfile() \u6700\u7ec8\u56e0\u4e3a fork() \u7684\u5199\u65f6\u590d\u5236\u884c\u4e3a\u800c\u88ab\u9650\u5236\u5728\u5355\u4e2a\u5b50\u8fdb\u7a0b\u4e2d\u3002\u5373 fork()\u63d0\u4f9b\u7684\u989d\u5916\u5b89\u5168\u6027\uff08\u4e0e\u7ebf\u7a0b\u76f8\u6bd4\uff09\uff0c\u8fd9\u5b9e\u9645\u4e0a\u6700\u7ec8\u7834\u574f\u4e86\u9501\u3002<\/p>\n
\u6211\u6ca1\u6709\u5c1d\u8bd5\u4f7f\u7528\u5176\u4ed6\u9501\u673a\u5236\uff08\u4f8b\u5982 pthread \u4e92\u65a5\u9501\uff09\uff0c\u4f46\u6211\u53ef\u4ee5\u60f3\u8c61\u5b83\u4eec\u4f1a\u9047\u5230\u7c7b\u4f3c\u7684\u95ee\u9898\u3002\u6211\u60f3\u4fdd\u6301\u7b80\u5355\uff0c\u6240\u4ee5\u5c06\u8f93\u51fa\u53d1\u9001\u56de\u7236\u8f93\u51fa\u662f\u4e0d\u53ef\u80fd\u7684\uff1a\u8fd9\u7ed9\u7a0b\u5e8f\u5458\u548c\u8fd0\u884c\u7a0b\u5e8f\u7684\u8ba1\u7b97\u673a\u5236\u9020\u4e86\u66f4\u591a\u7684\u5de5\u4f5c\u3002<\/p>\n
\u89e3\u51b3\u65b9\u6848\uff1a\u4f7f\u7528\u7ebf\u7a0b\u800c\u4e0d\u662f fork()\u3002\u8fd9\u6709\u5236\u9020\u5197\u4f59\u7ba1\u9053\u7684\u597d\u7684\u526f\u4f5c\u7528\u3002\u6700\u7ec8\u7684\u4f2a\u4ee3\u7801\uff1a<\/p>\n
\r\nfor(children)\r\n{\r\n pthread_create(worker, child_index);\r\n}\r\nfor(children)\r\n{\r\n pthread_join(child);\r\n}\r\nworker(child_index)\r\n{\r\n for(i_image = child_index; i_image < N_images; i_image += N_children)\r\n {\r\n results = process(images[i_image]);\r\n flockfile(stdout);\r\n printf(results);\r\n funlockfile(stdout);\r\n }\r\n}<\/pre>\n\u8fd9\u66f4\u7b80\u5355\uff0c\u5982\u9884\u671f\u7684\u90a3\u6837\u5de5\u4f5c\u3002\u6211\u731c\u6709\u65f6\u7ebf\u7a0b\u66f4\u597d\u3002<\/p>\n
\u5c06\u90e8\u5206\u8bfb\u53d6\u7684\u6587\u4ef6\u4f20\u9012\u7ed9\u5b50\u8fdb\u7a0b<\/strong><\/div>\n\u5bf9\u4e8e\u5404\u79cd vnlog \u5de5\u5177\uff0c\u6211\u9700\u8981\u5b9e\u73b0\u8fd9\u4e2a\u64cd\u4f5c\u5e8f\u5217\uff1a<\/p>\n
\n
- \u8fdb\u7a0b\u6253\u5f00\u4e00\u4e2a\u5173\u95ed O_CLOEXEC \u6807\u5fd7\u7684\u6587\u4ef6<\/li>\n
- \u8fdb\u7a0b\u8bfb\u53d6\u6b64\u6587\u4ef6\u7684\u4e00\u90e8\u5206\uff08\u5728 vnlog \u7684\u60c5\u51b5\u4e0b\u76f4\u5230\u56fe\u4f8b\u7684\u672b\u5c3e\uff09<\/li>\n
- \u8fdb\u7a0b\u8c03\u7528 exec() \u4ee5\u8c03\u7528\u53e6\u4e00\u4e2a\u7a0b\u5e8f\u6765\u5904\u7406\u5df2\u7ecf\u6253\u5f00\u7684\u6587\u4ef6\u7684\u5176\u4f59\u90e8\u5206<\/li>\n<\/ol>\n
\u7b2c\u4e8c\u4e2a\u7a0b\u5e8f\u53ef\u80fd\u9700\u8981\u547d\u4ee4\u884c\u4e2d\u7684\u6587\u4ef6\u540d\u800c\u4e0d\u662f\u5df2\u6253\u5f00\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\uff0c\u56e0\u4e3a\u7b2c\u4e8c\u4e2a\u7a0b\u5e8f\u53ef\u80fd\u81ea\u5df1\u8c03\u7528 \u200b\u200bopen()\u3002\u5982\u679c\u6211\u4f20\u9012\u6587\u4ef6\u540d\uff0c\u8fd9\u4e2a\u65b0\u7a0b\u5e8f\u5c06\u91cd\u65b0\u6253\u5f00\u6587\u4ef6\uff0c\u7136\u540e\u4ece\u5934\u5f00\u59cb\u8bfb\u53d6\u6587\u4ef6\uff0c\u800c\u4e0d\u662f\u4ece\u539f\u59cb\u7a0b\u5e8f\u505c\u6b62\u7684\u4f4d\u7f6e\u5f00\u59cb\u8bfb\u53d6\u3002\u5728\u6211\u7684\u7a0b\u5e8f\u4e0a\u4e0d\u53ef\u4ee5\u8fd9\u6837\u505a\uff0c\u56e0\u6b64\u5c06\u6587\u4ef6\u540d\u4f20\u9012\u7ed9\u7b2c\u4e8c\u4e2a\u7a0b\u5e8f\u662f\u884c\u4e0d\u901a\u7684\u3002<\/p>\n
\u6240\u4ee5\u6211\u771f\u7684\u9700\u8981\u4ee5\u67d0\u79cd\u65b9\u5f0f\u4f20\u9012\u5df2\u7ecf\u6253\u5f00\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u3002\u6211\u5728\u4f7f\u7528 Linux\uff08\u5176\u4ed6\u64cd\u4f5c\u7cfb\u7edf\u53ef\u80fd\u5728\u8fd9\u91cc\u8868\u73b0\u4e0d\u540c\uff09\uff0c\u6240\u4ee5\u6211\u7406\u8bba\u4e0a\u53ef\u4ee5\u901a\u8fc7\u4f20\u9012 \/dev\/fd\/N \u800c\u4e0d\u662f\u6587\u4ef6\u540d\u6765\u5b9e\u73b0\u3002\u4f46\u4e8b\u5b9e\u8bc1\u660e\u8fd9\u4e5f\u4e0d\u8d77\u4f5c\u7528\u3002\u5728 Linux\u4e0a\uff08\u518d\u8bf4\u4e00\u6b21\uff0c\u4e5f\u8bb8\u662f\u7279\u5b9a\u4e8e Linux\uff09\u5bf9\u4e8e\u666e\u901a\u6587\u4ef6 \/dev\/fd\/N \u662f\u539f\u59cb\u6587\u4ef6\u7684\u7b26\u53f7\u94fe\u63a5\u3002\u6240\u4ee5\u8fd9\u6700\u7ec8\u505a\u7684\u662f\u4e0e\u4f20\u9012\u6587\u4ef6\u540d\u5b8c\u5168\u76f8\u540c\u7684\u4e8b\u60c5\u3002<\/p>\n
\u4f46\u6709\u4e00\u4e2a\u4e34\u65f6\u65b9\u6848\uff01\u5982\u679c\u6211\u4eec\u6b63\u5728\u8bfb\u53d6\u7ba1\u9053\u800c\u4e0d\u662f\u6587\u4ef6\uff0c\u90a3\u4e48\u6ca1\u6709\u4ec0\u4e48\u53ef\u4ee5\u7b26\u53f7\u94fe\u63a5\uff0c\u5e76\u4e14 \/dev\/fd\/N \u6700\u7ec8\u5c06\u539f\u59cb\u7ba1\u9053\u4f20\u9012\u7ed9\u7b2c\u4e8c\u4e2a\u8fdb\u7a0b\uff0c\u7136\u540e\u7a0b\u5e8f\u6b63\u5e38\u5de5\u4f5c\u3002\u6211\u53ef\u4ee5\u901a\u8fc7\u5c06\u4e0a\u9762\u7684 open(\"filename\") \u66f4\u6539\u4e3a popen(\"cat filename\") \u4e4b\u7c7b\u7684\u4e1c\u897f\u6765\u4f2a\u88c5\u3002\u5478\uff01\u8fd9\u771f\u7684\u662f\u6211\u4eec\u6240\u80fd\u505a\u5230\u6700\u597d\u7684\u5417\uff1f\u8fd9\u5728 BSD \u4e0a\u770b\u4e0a\u53bb\u4f1a\u600e\u4e48\u6837\uff1f<\/p>\n
via: http:\/\/notes.secretsauce.net\/notes\/2018\/08\/03_unix-curiosities.html<\/p>\n
\u4f5c\u8005\uff1aDima Kogan \u9009\u9898\uff1alujun9972 \u8bd1\u8005\uff1ageekpi \u6821\u5bf9\uff1awxy<\/p>\n
\u672c\u6587\u7531 LCTT \u539f\u521b\u7f16\u8bd1\uff0cLinux\u4e2d\u56fd \u8363\u8a89\u63a8\u51fa<\/p>\n
\n\u539f\u6587\u6765\u81ea\uff1ahttps:\/\/linux.cn\/article-9928-1.html<\/a><\/p>\n