{"id":3111,"date":"2017-01-06T19:28:01","date_gmt":"2017-01-06T11:28:01","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=3111"},"modified":"2017-01-06T19:28:01","modified_gmt":"2017-01-06T11:28:01","slug":"linux%e4%b8%8b%e5%ae%88%e6%8a%a4%e8%bf%9b%e7%a8%8b%e7%9a%84c%e8%af%ad%e8%a8%80%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/3111.html","title":{"rendered":"Linux\u4e0b\u5b88\u62a4\u8fdb\u7a0b\u7684C\u8bed\u8a00\u5b9e\u73b0"},"content":{"rendered":"<p>=Start=<\/p>\n<h4>\u7f18\u7531\uff1a<\/h4>\n<p>\u4ee5\u540e\u53ef\u80fd\u8981\u7528C\u8bed\u8a00\u5199\u4e00\u4e2adaemon\u7a0b\u5e8f\uff08\u5b88\u62a4\u8fdb\u7a0b\uff09\u8fd0\u884c\u5728\u670d\u52a1\u5668\u4e0a\uff0c\u4ee5\u8fdb\u884c\u4e00\u4e9b\u4fe1\u606f\u6536\u96c6\u7684\u5de5\u4f5c\u3002\u4e4b\u524d\u53ea\u662f\u7b80\u5355\u77e5\u9053\u4ec0\u4e48\u662f\u5b88\u62a4\u8fdb\u7a0b\uff0c\u4f46\u5bf9\u4e8e\u4e00\u4e9b\u7ec6\u8282\u4ee5\u53ca\u8be5\u5982\u4f55\u7f16\u5199\u90fd\u4e0d\u592a\u6e05\u695a\uff0c\u6240\u4ee5\u9700\u8981\u5148\u5b66\u4e60\u3001\u51c6\u5907\u4e00\u4e0b\u3002<\/p>\n<h4>\u6b63\u6587\uff1a<\/h4>\n<h5>\u53c2\u8003\u89e3\u7b54\uff1a<\/h5>\n<h6>\u5b88\u62a4\u8fdb\u7a0b(Daemon)\u662f\u4ec0\u4e48\uff1f<\/h6>\n<p>\u5b88\u62a4\u8fdb\u7a0b\uff0c\u4e5f\u5373\u901a\u5e38\u6240\u8bf4\u7684 Daemon \u8fdb\u7a0b\uff0c<span style=\"color: #ff0000;\">\u662f Linux \u4e0b\u4e00\u79cd\u7279\u6b8a\u7684\u540e\u53f0\u670d\u52a1\u8fdb\u7a0b\uff0c\u5b83\u72ec\u7acb\u4e8e\u63a7\u5236\u7ec8\u7aef\u5e76\u4e14\u5468\u671f\u6027\u7684\u6267\u884c\u67d0\u79cd\u4efb\u52a1\u6216\u8005\u7b49\u5f85\u5904\u7406\u67d0\u4e9b\u53d1\u751f\u7684\u4e8b\u4ef6<\/span>\u3002\u5b88\u62a4\u8fdb\u7a0b\u901a\u5e38\u5728\u7cfb\u7edf\u5f15\u5bfc\u88c5\u5165\u65f6\u542f\u52a8\uff0c\u5728\u7cfb\u7edf\u5173\u95ed\u65f6\u7ec8\u6b62\u3002Linux \u7cfb\u7edf\u4e0b\u5927\u591a\u6570\u670d\u52a1\u90fd\u662f\u901a\u8fc7\u5b88\u62a4\u8fdb\u7a0b\u5b9e\u73b0\u7684\u3002\u5b88\u62a4\u8fdb\u7a0b\u7684\u540d\u79f0\u901a\u5e38\u4ee5 \u201cd\u201d \u7ed3\u5c3e\uff0c\u6bd4\u5982 \u201chttpd\u201d\u3001\u201ccrond\u201d\u3001\u201cmysqld\u201d \u7b49\u3002<\/p>\n<h6>\u63a7\u5236\u7ec8\u7aef\u662f\u4ec0\u4e48\uff1f<\/h6>\n<p>\u7ec8\u7aef\u662f\u7528\u6237\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u8fdb\u884c\u4ea4\u6d41\u7684\u754c\u9762\u3002<span style=\"color: #ff0000;\">\u5728 Linux \u7cfb\u7edf\u4e2d\uff0c\u7528\u6237\u7531\u7ec8\u7aef\u767b\u5f55\u7cfb\u7edf\u767b\u5165\u7cfb\u7edf\u540e\u4f1a\u5f97\u5230\u4e00\u4e2a shell \u8fdb\u7a0b\uff0c\u8fd9\u4e2a\u7ec8\u7aef\u4fbf\u6210\u4e3a\u8fd9\u4e2a shell \u8fdb\u7a0b\u7684\u63a7\u5236\u7ec8\u7aef\uff08Controlling Terminal\uff09\u3002<\/span><span style=\"color: #0000ff;\">shell \u8fdb\u7a0b\u542f\u52a8\u7684\u5176\u4ed6\u8fdb\u7a0b\uff0c\u7531\u4e8e\u590d\u5236\u4e86\u7236\u8fdb\u7a0b\u7684\u4fe1\u606f\uff0c\u56e0\u6b64\u4e5f\u90fd\u540c\u4f9d\u9644\u4e8e\u8fd9\u4e2a\u63a7\u5236\u7ec8\u7aef\u3002<\/span><\/p>\n<p><span style=\"color: #ff0000;\">\u4ece\u7ec8\u7aef\u542f\u52a8\u7684\u8fdb\u7a0b\u90fd\u4f9d\u9644\u4e8e\u8be5\u7ec8\u7aef\uff0c\u5e76\u53d7\u7ec8\u7aef\u63a7\u5236\u548c\u5f71\u54cd\u3002\u7ec8\u7aef\u5173\u95ed\uff0c\u76f8\u5e94\u7684\u8fdb\u7a0b\u90fd\u4f1a\u81ea\u52a8\u5173\u95ed\u3002<\/span>\u5b88\u62a4\u8fdb\u7a0b\u8131\u79bb\u7ec8\u7aef\u7684\u76ee\u7684\uff0c\u4e5f\u5373\u662f\u4e0d\u53d7\u7ec8\u7aef\u53d8\u5316\u7684\u5f71\u54cd\u4e0d\u88ab\u7ec8\u7aef\u6253\u65ad\uff0c\u5f53\u7136\u4e5f\u4e0d\u60f3\u5728\u7ec8\u7aef\u663e\u793a\u6267\u884c\u8fc7\u7a0b\u4e2d\u7684\u4fe1\u606f\u3002\u5982\u679c\u4e0d\u60f3\u8fdb\u7a0b\u53d7\u5230\u7528\u6237\u3001\u7ec8\u7aef\u6216\u5176\u4ed6\u53d8\u5316\u7684\u5f71\u54cd\uff0c\u5c31\u5fc5\u987b\u628a\u5b83\u53d8\u6210\u5b88\u62a4\u8fdb\u7a0b\u3002<\/p>\n<h6>\u5982\u4f55\u5b9e\u73b0\u5b88\u62a4\u8fdb\u7a0b\uff1f<\/h6>\n<p>\u5b88\u62a4\u8fdb\u7a0b\u5c5e\u4e8e Linux \u8fdb\u7a0b\u7ba1\u7406\u7684\u8303\u7574\u3002\u5176\u9996\u8981\u7684\u7279\u6027\u662f\u540e\u53f0\u8fd0\u884c\uff1b\u5176\u6b21\uff0c\u8981\u4e0e\u4ece\u542f\u52a8\u5b83\u7684\u7236\u8fdb\u7a0b\u7684\u8fd0\u884c\u73af\u5883\u9694\u79bb\u5f00\u6765\uff0c\u9700\u8981\u5904\u7406\u7684\u5185\u5bb9\u5927\u81f4\u5305\u62ec\u4f1a\u8bdd\u3001\u63a7\u5236\u7ec8\u7aef\u3001\u8fdb\u7a0b\u7ec4\u3001\u6587\u4ef6\u63cf\u8ff0\u7b26\u3001\u6587\u4ef6\u6743\u9650\u63a9\u7801\u4ee5\u53ca\u5de5\u4f5c\u76ee\u5f55\u7b49\u3002<\/p>\n<p>\u5b88\u62a4\u8fdb\u7a0b\u53ef\u4ee5\u5728 Linux \u542f\u52a8\u65f6\u4ece\u811a\u672c \/etc\/rc.d \u542f\u52a8\uff0c\u4e5f\u53ef\u4ee5\u7531\u4f5c\u4e1a\u89c4\u5212\u8fdb\u7a0b crond \u542f\u52a8\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u7528\u6237\u7ec8\u7aef\uff08\u4e00\u822c\u662f Shell\uff09\u542f\u52a8\u3002<\/p>\n<p>\u5b9e\u73b0\u4e00\u4e2a\u5b88\u62a4\u8fdb\u7a0b\uff0c\u5176\u5b9e\u5c31\u662f\u5c06\u666e\u901a\u8fdb\u7a0b\u6309\u7167\u4e0a\u8ff0\u7279\u6027\u6539\u9020\u4e3a\u5b88\u62a4\u8fdb\u7a0b\u7684\u8fc7\u7a0b\u3002\u9700\u8981\u6ce8\u610f\u7684\u4e00\u70b9\u662f\uff0c\u4e0d\u540c\u7248\u672c\u7684 Unix \u7cfb\u7edf\u5176\u5b9e\u73b0\u673a\u5236\u4e0d\u540c\uff0cBSD \u548c Linux \u4e0b\u7684\u5b9e\u73b0\u7ec6\u8282\u5c31\u4e0d\u540c\u3002<\/p>\n<p>\u6839\u636e\u4e0a\u8ff0\u7684\u7279\u6027\uff0c\u6211\u4eec\u4fbf\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u5b88\u62a4\u8fdb\u7a0b\uff0c\u8fd9\u91cc\u4ee5 Linux \u7cfb\u7edf\u4e0b\u4ece\u7ec8\u7aef Shell \u6765\u542f\u52a8\u4e3a\u4f8b\u3002<\/p>\n<p><span style=\"color: #0000ff;\"><strong>1\u3001\u521b\u5efa\u5b50\u8fdb\u7a0b\uff0c\u7236\u8fdb\u7a0b\u9000\u51fa<\/strong><\/span><\/p>\n<p>\u7f16\u5199\u5b88\u62a4\u8fdb\u7a0b\u7b2c\u4e00\u6b65\uff0c\u5c31\u662f\u8981\u4f7f\u5f97\u8fdb\u7a0b\u72ec\u7acb\u4e8e\u7ec8\u7aef\u540e\u53f0\u8fd0\u884c\u3002\u4e3a\u907f\u514d\u7ec8\u7aef\u6302\u8d77\uff0c\u5c06\u7236\u8fdb\u7a0b\u9000\u51fa\uff0c\u9020\u6210\u7a0b\u5e8f\u5df2\u7ecf\u9000\u51fa\u7684\u5047\u8c61\uff0c\u6240\u6709\u540e\u9762\u7684\u5de5\u4f5c\u90fd\u5728\u5b50\u8fdb\u7a0b\u5b8c\u6210\uff0c\u8fd9\u6837\u63a7\u5236\u7ec8\u7aef\u4e5f\u53ef\u4ee5\u7ee7\u7eed\u6267\u884c\u5176\u4ed6\u547d\u4ee4\uff0c\u4ece\u800c\u5728\u5f62\u5f0f\u4e0a\u8131\u79bb\u63a7\u5236\u7ec8\u7aef\u7684\u63a7\u5236\u3002<\/p>\n<p>\u7531\u4e8e\u7236\u8fdb\u7a0b\u5148\u4e8e\u5b50\u8fdb\u7a0b\u9000\u51fa\uff0c\u5b50\u8fdb\u7a0b\u5c31\u53d8\u4e3a\u5b64\u513f\u8fdb\u7a0b\uff0c\u5e76\u7531 init \u8fdb\u7a0b\u4f5c\u4e3a\u5176\u7236\u8fdb\u7a0b\u6536\u517b\u3002<\/p>\n<p><span style=\"color: #0000ff;\"><strong>2\u3001\u5b50\u8fdb\u7a0b\u521b\u5efa\u65b0\u4f1a\u8bdd<\/strong><\/span><\/p>\n<p>\u7ecf\u8fc7\u4e0a\u4e00\u6b65\uff0c\u5b50\u8fdb\u7a0b\u5df2\u7ecf\u540e\u53f0\u8fd0\u884c\uff0c\u7136\u800c\u7cfb\u7edf\u8c03\u7528 fork \u521b\u5efa\u5b50\u8fdb\u7a0b\uff0c\u5b50\u8fdb\u7a0b\u4fbf\u590d\u5236\u4e86\u539f\u7236\u8fdb\u7a0b\u7684\u8fdb\u7a0b\u63a7\u5236\u5757\uff08PCB\uff09\uff0c\u76f8\u5e94\u5730\u7ee7\u627f\u4e86\u4e00\u4e9b\u4fe1\u606f\uff0c\u5305\u62ec\u4f1a\u8bdd\u3001\u8fdb\u7a0b\u7ec4\u3001\u63a7\u5236\u7ec8\u7aef\u7b49\u4fe1\u606f\u3002<span style=\"color: #0000ff;\">\u5c3d\u7ba1\u7236\u8fdb\u7a0b\u5df2\u7ecf\u9000\u51fa\uff0c\u4f46\u5b50\u8fdb\u7a0b\u7684\u4f1a\u8bdd\u3001\u8fdb\u7a0b\u7ec4\u3001\u63a7\u5236\u7ec8\u7aef\u7684\u4fe1\u606f\u6ca1\u6709\u6539\u53d8\u3002\u4e3a\u4f7f\u5b50\u8fdb\u7a0b\u5b8c\u5168\u6446\u8131\u7236\u8fdb\u7a0b\u7684\u73af\u5883\uff0c\u9700\u8981\u8c03\u7528 setsid \u51fd\u6570\u3002<\/span><\/p>\n<p>\u8fd9\u91cc\u6709\u5fc5\u8981\u8bf4\u4e00\u4e0b\u4e24\u4e2a\u6982\u5ff5\uff1a\u4f1a\u8bdd\u548c\u8fdb\u7a0b\u7ec4\u3002<\/p>\n<p><strong>\u8fdb\u7a0b\u7ec4\uff1a\u4e00\u4e2a\u6216\u591a\u4e2a\u8fdb\u7a0b\u7684\u96c6\u5408\u3002<\/strong>\u62e5\u6709\u552f\u4e00\u7684\u6807\u8bc6\u8fdb\u7a0b\u7ec4 ID\uff0c<span style=\"color: #ff0000;\">\u6bcf\u4e2a\u8fdb\u7a0b\u7ec4\u90fd\u6709\u4e00\u4e2a\u7ec4\u957f\u8fdb\u7a0b\uff0c\u8be5\u8fdb\u7a0b\u7684\u8fdb\u7a0b\u53f7\u7b49\u4e8e\u5176\u8fdb\u7a0b\u7ec4\u7684 ID\u3002\u8fdb\u7a0b\u7ec4 ID \u4e0d\u4f1a\u56e0\u7ec4\u957f\u8fdb\u7a0b\u9000\u51fa\u800c\u53d7\u5230\u5f71\u54cd\uff0cfork \u8c03\u7528\u4e5f\u4e0d\u4f1a\u6539\u53d8\u8fdb\u7a0b\u7ec4 ID\u3002<\/span><\/p>\n<p><strong>\u4f1a\u8bdd\uff1a\u4e00\u4e2a\u6216\u591a\u4e2a\u8fdb\u7a0b\u7ec4\u7684\u96c6\u5408\u3002<\/strong><span style=\"color: #ff0000;\">\u65b0\u5efa\u4f1a\u8bdd\u65f6\uff0c\u5f53\u524d\u8fdb\u7a0b\uff08\u4f1a\u8bdd\u4e2d\u552f\u4e00\u7684\u8fdb\u7a0b\uff09\u6210\u4e3a\u4f1a\u8bdd\u9996\u8fdb\u7a0b\uff0c\u4e5f\u662f\u5f53\u524d\u8fdb\u7a0b\u7ec4\u7684\u7ec4\u957f\u8fdb\u7a0b\uff0c\u5176\u8fdb\u7a0b\u53f7\u4e3a\u4f1a\u8bdd ID\uff0c\u540c\u6837\u4e5f\u662f\u8be5\u8fdb\u7a0b\u7ec4\u7684 ID\u3002<\/span>\u5b83\u901a\u5e38\u662f\u767b\u5f55 shell\uff0c\u4e5f\u53ef\u4ee5\u662f\u8c03\u7528 setsid \u65b0\u5efa\u4f1a\u8bdd\u7684\u5b64\u513f\u8fdb\u7a0b\u3002<strong>\u6ce8\u610f\uff1a\u7ec4\u957f\u8fdb\u7a0b\u8c03\u7528 setsid \uff0c\u5219\u51fa\u9519\u8fd4\u56de\uff0c\u65e0\u6cd5\u65b0\u5efa\u4f1a\u8bdd\u3002<\/strong><\/p>\n<p><span style=\"color: #ff0000;\">\u901a\u5e38\uff0c\u4f1a\u8bdd\u5f00\u59cb\u4e8e\u7528\u6237\u767b\u5f55\uff0c\u7ec8\u6b62\u4e8e\u7528\u6237\u9000\u51fa\uff0c\u671f\u95f4\u7684\u6240\u6709\u8fdb\u7a0b\u90fd\u5c5e\u4e8e\u8fd9\u4e2a\u4f1a\u8bdd\u3002<\/span>\u4e00\u4e2a\u4f1a\u8bdd\u4e00\u822c\u5305\u542b\u4e00\u4e2a\u4f1a\u8bdd\u9996\u8fdb\u7a0b\u3001\u4e00\u4e2a\u524d\u53f0\u8fdb\u7a0b\u7ec4\u548c\u4e00\u4e2a\u540e\u53f0\u8fdb\u7a0b\u7ec4\uff0c\u63a7\u5236\u7ec8\u7aef\u53ef\u6709\u53ef\u65e0\uff1b\u6b64\u5916\uff0c\u524d\u53f0\u8fdb\u7a0b\u7ec4\u53ea\u6709\u4e00\u4e2a\uff0c\u540e\u53f0\u8fdb\u7a0b\u7ec4\u53ef\u4ee5\u6709\u591a\u4e2a\uff0c\u8fd9\u4e9b\u8fdb\u7a0b\u7ec4\u5171\u4eab\u4e00\u4e2a\u63a7\u5236\u7ec8\u7aef\u3002<\/p>\n<p><strong>\u524d\u53f0\u8fdb\u7a0b\u7ec4<\/strong>\uff1a\u8be5\u8fdb\u7a0b\u7ec4\u4e2d\u7684\u8fdb\u7a0b\u53ef\u4ee5\u5411\u7ec8\u7aef\u8bbe\u5907\u8fdb\u884c\u8bfb\u3001\u5199\u64cd\u4f5c\uff08\u5c5e\u4e8e\u8be5\u7ec4\u7684\u8fdb\u7a0b\u53ef\u4ee5\u4ece\u7ec8\u7aef\u83b7\u5f97\u8f93\u5165\uff09\u3002\u8be5\u8fdb\u7a0b\u7ec4\u7684 ID \u7b49\u4e8e\u63a7\u5236\u7ec8\u7aef\u8fdb\u7a0b\u7ec4 ID\uff0c\u901a\u5e38\u636e\u6b64\u6765\u5224\u65ad\u524d\u53f0\u8fdb\u7a0b\u7ec4\u3002<\/p>\n<p><strong>\u540e\u53f0\u8fdb\u7a0b\u7ec4<\/strong>\uff1a\u4f1a\u8bdd\u4e2d\u9664\u4e86\u4f1a\u8bdd\u9996\u8fdb\u7a0b\u548c\u524d\u53f0\u8fdb\u7a0b\u7ec4\u4ee5\u5916\u7684\u6240\u6709\u8fdb\u7a0b\uff0c\u90fd\u5c5e\u4e8e\u540e\u53f0\u8fdb\u7a0b\u7ec4\u3002\u8be5\u8fdb\u7a0b\u7ec4\u4e2d\u7684\u8fdb\u7a0b\u53ea\u80fd\u5411\u7ec8\u7aef\u8bbe\u5907\u8fdb\u884c\u5199\u64cd\u4f5c\u3002<\/p>\n<p><strong>\u4f1a\u8bdd(session)<\/strong>\uff1a\u5982\u679c\u60f3\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u4f1a\u8bdd(session)\u7684\u5185\u5bb9\uff0c\u53ef\u4ee5\u597d\u597d\u8bfb\u4e00\u4e0b APUE \u8fd9\u672c\u4e66\u3002<\/p>\n<p><span style=\"color: #ff0000;\">\u5982\u679c\u8c03\u7528\u8fdb\u7a0b\u975e\u7ec4\u957f\u8fdb\u7a0b\uff0c\u90a3\u4e48\u5c31\u80fd\u521b\u5efa\u4e00\u4e2a\u65b0\u4f1a\u8bdd<\/span>\uff1a<\/p>\n<ul>\n<li>\u8be5\u8fdb\u7a0b\u53d8\u6210\u65b0\u4f1a\u8bdd\u7684\u9996\u8fdb\u7a0b<\/li>\n<li>\u8be5\u8fdb\u7a0b\u6210\u4e3a\u4e00\u4e2a\u65b0\u8fdb\u7a0b\u7ec4\u7684\u7ec4\u957f\u8fdb\u7a0b<\/li>\n<li>\u8be5\u8fdb\u7a0b\u6ca1\u6709\u63a7\u5236\u7ec8\u7aef\uff0c\u5982\u679c\u4e4b\u524d\u6709\uff0c\u5219\u4f1a\u88ab\u4e2d\u65ad\uff08\u4f1a\u8bdd\u8fc7\u7a0b\u5bf9\u63a7\u5236\u7ec8\u7aef\u7684\u72ec\u5360\u6027\uff09<\/li>\n<\/ul>\n<p>\u4e5f\u5c31\u662f\u8bf4\uff1a<span style=\"color: #ff0000;\"><strong>\u7ec4\u957f\u8fdb\u7a0b\u4e0d\u80fd\u6210\u4e3a\u65b0\u4f1a\u8bdd\u9996\u8fdb\u7a0b\uff0c\u65b0\u4f1a\u8bdd\u9996\u8fdb\u7a0b\u5fc5\u5b9a\u6210\u4e3a\u7ec4\u957f\u8fdb\u7a0b<\/strong><\/span>\u3002<\/p>\n<p>\u5230\u6b64\u4e3a\u6b62\uff0c\u6211\u4eec\u719f\u6089\u4e86\u4f1a\u8bdd\u4e0e\u8fdb\u7a0b\u95f4\u7684\u5173\u7cfb\uff0c\u90a3\u4e48\u5982\u4f55\u65b0\u5efa\u4e00\u4e2a\u4f1a\u8bdd\u5462\uff1f<\/p>\n<p><span style=\"color: #0000ff;\">\u901a\u8fc7\u8c03\u7528 setsid \u51fd\u6570\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u65b0\u4f1a\u8bdd\uff0c\u8c03\u7528\u8fdb\u7a0b\u62c5\u4efb\u65b0\u4f1a\u8bdd\u7684\u9996\u8fdb\u7a0b\uff0c\u5176\u4f5c\u7528\u6709<\/span>\uff1a<\/p>\n<ul>\n<li>\u4f7f\u5f53\u524d\u8fdb\u7a0b<span style=\"color: #0000ff;\">\u8131\u79bb\u539f\u4f1a\u8bdd\u7684\u63a7\u5236<\/span><\/li>\n<li>\u4f7f\u5f53\u524d\u8fdb\u7a0b<span style=\"color: #0000ff;\">\u8131\u79bb\u539f\u8fdb\u7a0b\u7ec4\u7684\u63a7\u5236<\/span><\/li>\n<li>\u4f7f\u5f53\u524d\u8fdb\u7a0b<span style=\"color: #0000ff;\">\u8131\u79bb\u539f\u63a7\u5236\u7ec8\u7aef\u7684\u63a7\u5236<\/span><\/li>\n<\/ul>\n<p><span style=\"color: #0000ff;\">\u8fd9\u6837\uff0c\u5f53\u524d\u8fdb\u7a0b\u624d\u80fd\u5b9e\u73b0\u771f\u6b63\u610f\u4e49\u4e0a\u5b8c\u5168\u72ec\u7acb\u51fa\u6765\uff0c\u6446\u8131\u5176\u4ed6\u8fdb\u7a0b\u7684\u63a7\u5236\u3002<\/span><\/p>\n<p>\u53e6\u5916\uff0c\u8981\u63d0\u4e00\u4e0b\uff0c\u5c3d\u7ba1\u8fdb\u7a0b\u53d8\u6210\u65e0\u7ec8\u7aef\u7684\u4f1a\u8bdd\u9996\u8fdb\u7a0b\uff0c\u4f46\u662f\u5b83\u4ecd\u7136\u53ef\u4ee5\u91cd\u65b0\u7533\u8bf7\u6253\u5f00\u4e00\u4e2a\u63a7\u5236\u7ec8\u7aef\u3002\u53ef\u4ee5\u901a\u8fc7\u518d\u6b21\u521b\u5efa\u5b50\u8fdb\u7a0b\u7ed3\u675f\u5f53\u524d\u8fdb\u7a0b\uff0c\u4f7f\u8fdb\u7a0b\u4e0d\u518d\u662f\u4f1a\u8bdd\u9996\u8fdb\u7a0b\u6765\u7981\u6b62\u8fdb\u7a0b\u91cd\u65b0\u6253\u5f00\u63a7\u5236\u7ec8\u7aef\u3002<\/p>\n<p><strong>3\u3001\u6539\u53d8\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55<\/strong><\/p>\n<p>\u76f4\u63a5\u8c03\u7528 chdir \u51fd\u6570\u5c06\u5207\u6362\u5230\u6839\u76ee\u5f55\u4e0b\u3002<br \/>\n\u7531\u4e8e\u8fdb\u7a0b\u8fd0\u884c\u8fc7\u7a0b\u4e2d\uff0c\u5f53\u524d\u76ee\u5f55\u6240\u5728\u7684\u6587\u4ef6\u7cfb\u7edf\uff08\u5982\uff1a\u201c\/mnt\/usb\u201d\uff09\u662f\u4e0d\u80fd\u5378\u8f7d\u7684\uff0c<span style=\"color: #ff0000;\">\u4e3a\u907f\u514d\u5bf9\u4ee5\u540e\u7684\u4f7f\u7528\u9020\u6210\u9ebb\u70e6\uff0c\u6539\u53d8\u5de5\u4f5c\u76ee\u5f55\u4e3a\u6839\u76ee\u5f55\u662f\u5fc5\u8981\u7684\u3002\u5982\u6709\u7279\u6b8a\u9700\u8981\uff0c\u4e5f\u53ef\u4ee5\u6539\u53d8\u5230\u7279\u5b9a\u76ee\u5f55\uff0c\u5982\u201c\/tmp\u201d\u3002<\/span><\/p>\n<p><strong>4\u3001\u91cd\u8bbe\u6587\u4ef6\u6743\u9650\u63a9\u7801<\/strong><\/p>\n<p>fork \u51fd\u6570\u521b\u5efa\u7684\u5b50\u8fdb\u7a0b\uff0c\u7ee7\u627f\u4e86\u7236\u8fdb\u7a0b\u7684\u6587\u4ef6\u64cd\u4f5c\u6743\u9650\uff0c<span style=\"color: #ff0000;\">\u4e3a\u9632\u6b62\u5bf9\u4ee5\u540e\u4f7f\u7528\u6587\u4ef6\u5e26\u6765\u95ee\u9898\uff0c\u9700\u8981\u91cd\u8bbe\u6587\u4ef6\u6743\u9650\u63a9\u7801\u3002<\/span><\/p>\n<p>\u6587\u4ef6\u6743\u9650\u63a9\u7801\uff0c\u8bbe\u5b9a\u4e86\u6587\u4ef6\u6743\u9650\u4e2d\u8981\u5c4f\u853d\u6389\u7684\u5bf9\u5e94\u4f4d\u3002\u8fd9\u4e2a\u8ddf\u6587\u4ef6\u6743\u9650\u7684\u516b\u8fdb\u5236\u6570\u5b57\u6a21\u5f0f\u8868\u793a\u5dee\u4e0d\u591a\uff0c\u5c06\u73b0\u6709\u5b58\u53d6\u6743\u9650\u51cf\u53bb\u6743\u9650\u63a9\u7801\uff08\u6216\u505a\u5f02\u6216\u8fd0\u7b97\uff09\uff0c\u5c31\u53ef\u4ea7\u751f\u65b0\u5efa\u6587\u4ef6\u65f6\u7684\u9884\u8bbe\u6743\u9650\u3002<\/p>\n<p>\u8c03\u7528 umask \u8bbe\u7f6e\u6587\u4ef6\u6743\u9650\u63a9\u7801\uff0c\u901a\u5e38\u662f\u91cd\u8bbe\u4e3a 0\uff0c\u6e05\u9664\u63a9\u7801\uff0c\u8fd9\u6837\u53ef\u4ee5\u5927\u5927\u589e\u5f3a\u5b88\u62a4\u8fdb\u7a0b\u7684\u7075\u6d3b\u6027\u3002<\/p>\n<p><strong>5\u3001\u5173\u95ed\u65e0\u5173\u6587\u4ef6\u63cf\u8ff0\u7b26<\/strong><\/p>\n<p>\u540c\u6587\u4ef6\u6743\u9650\u63a9\u7801\u4e00\u6837\uff0c\u5b50\u8fdb\u7a0b\u53ef\u80fd\u7ee7\u627f\u4e86\u7236\u8fdb\u7a0b\u6253\u5f00\u7684\u6587\u4ef6\uff0c\u800c\u8fd9\u4e9b\u6587\u4ef6\u53ef\u80fd\u6c38\u8fdc\u4e0d\u4f1a\u88ab\u7528\u5230\uff0c\u4f46\u5b83\u4eec\u4e00\u6837\u6d88\u8017\u7cfb\u7edf\u8d44\u6e90\uff0c\u800c\u4e14\u53ef\u80fd\u5bfc\u81f4\u6240\u5728\u7684\u6587\u4ef6\u7cfb\u7edf\u65e0\u6cd5\u5378\u4e0b\uff0c\u56e0\u6b64\u9700\u8981\u4e00\u4e00\u5173\u95ed\u5b83\u4eec\u3002\u7531\u4e8e\u5b88\u62a4\u8fdb\u7a0b\u8131\u79bb\u4e86\u7ec8\u7aef\u8fd0\u884c\uff0c\u56e0\u6b64\u6807\u51c6\u8f93\u5165\u3001\u6807\u51c6\u8f93\u51fa\u3001\u6807\u51c6\u9519\u8bef\u8f93\u51fa\u8fd93\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e5f\u8981\u5173\u95ed\u3002\u901a\u5e38\u6309\u5982\u4e0b\u65b9\u5f0f\u6765\u5173\u95ed\uff1a<\/p>\n<pre class=\"lang:default decode:true\">for (i=0; i &lt; MAXFILE; i++)\n    close(i);<\/pre>\n<p>\u8fd9\u91cc\u8981\u6ce8\u610f\u4e0b\uff0cparam.h \u5934\u6587\u4ef6\u4e2d\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5e38\u91cf NOFILE\uff0c\u8868\u793a\u6700\u5927\u5141\u8bb8\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\uff0c\u4f46\u662f\u6211\u4eec\u5c3d\u91cf\u4e0d\u8981\u7528\u5b83\uff0c\u800c\u662f\u901a\u8fc7\u8c03\u7528\u51fd\u6570 getdtablesize \u8fd4\u56de\u8fdb\u7a0b\u6587\u4ef6\u63cf\u8ff0\u7b26\u8868\u4e2d\u7684\u9879\u6570\uff08\u5373\u6253\u5f00\u7684\u6587\u4ef6\u6570\u76ee\uff09\uff1a<\/p>\n<blockquote>\n<p style=\"padding-left: 30px;\">\/* The following are not really correct but it is a value we used for a long time<br \/>\nand which seems to be usable. People should not use\u00a0<strong>NOFILE<\/strong>\u00a0and\u00a0<strong>NCARGS<\/strong>\u00a0anyway. *\/<br \/>\n<strong>#define NOFILE 256<\/strong><br \/>\n<strong>#define NCARGS 131072<\/strong><\/p>\n<\/blockquote>\n<p>\u81f3\u6b64\u4e3a\u6b62\uff0c\u4e00\u4e2a\u7b80\u5355\u7684\u5b88\u62a4\u8fdb\u7a0b\u5c31\u5efa\u7acb\u8d77\u6765\u4e86\u3002<\/p>\n<p>\u53e6\u5916\uff0c\u6709\u4e9b Unix \u63d0\u4f9b\u4e00\u4e2a daemon \u7684 C \u5e93\u51fd\u6570\uff0c\u5b9e\u73b0\u5b88\u62a4\u8fdb\u7a0b\u3002\uff08BSD \u548c Linux \u5747\u63d0\u4f9b\u8fd9\u4e2a\u51fd\u6570\uff09\uff1a<\/p>\n<pre class=\"lang:default decode:true\">NAME\n     daemon - run in the background\nSYNOPSIS\n     #include &lt;unistd.h&gt;\n     int daemon(int nochdir, int noclose);\nDESCRIPTION\n     The daemon() function is for programs wishing to detach themselves from the controlling terminal and run in the background as system daemons.<\/pre>\n<h6>\u5b88\u62a4\u8fdb\u7a0b\u7684\u793a\u4f8b\u4ee3\u7801<\/h6>\n<pre class=\"lang:default decode:true \">#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;string.h&gt;\n#include &lt;time.h&gt;\n#include &lt;unistd.h&gt;\n#include &lt;sys\/param.h&gt;\n#include &lt;sys\/types.h&gt;\n#include &lt;sys\/stat.h&gt;\n#include &lt;fcntl.h&gt;\n\n\/\/ \u5b88\u62a4\u8fdb\u7a0b\u521d\u59cb\u5316\u51fd\u6570\nvoid init_daemon()\n{\n    pid_t pid;\n    int i = 0;\n\n    if ((pid = fork()) == -1) {\n        printf(\"Fork error !\\n\");\n        exit(1);\n    }\n    if (pid != 0) {\n        exit(0);        \/\/ \u7236\u8fdb\u7a0b\u9000\u51fa\n    }\n\n    setsid();           \/\/ \u5b50\u8fdb\u7a0b\u5f00\u542f\u65b0\u4f1a\u8bdd\uff0c\u5e76\u6210\u4e3a\u4f1a\u8bdd\u9996\u8fdb\u7a0b\u548c\u7ec4\u957f\u8fdb\u7a0b\n    if ((pid = fork()) == -1) {\n        printf(\"Fork error !\\n\");\n        exit(-1);\n    }\n    if (pid != 0) {\n        exit(0);        \/\/ \u7ed3\u675f\u7b2c\u4e00\u5b50\u8fdb\u7a0b\uff0c\u7b2c\u4e8c\u5b50\u8fdb\u7a0b\u4e0d\u518d\u662f\u4f1a\u8bdd\u9996\u8fdb\u7a0b\n    }\n    chdir(\"\/tmp\");      \/\/ \u6539\u53d8\u5de5\u4f5c\u76ee\u5f55\n    umask(0);           \/\/ \u91cd\u8bbe\u6587\u4ef6\u63a9\u7801\n    for (; i &lt; getdtablesize(); ++i) {\n       close(i);        \/\/ \u5173\u95ed\u6253\u5f00\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\n    }\n\n    return;\n}\n\nint main(int argc, char *argv[])\n{\n    int fp;\n    time_t t;\n    char buf[] = {\"This is a daemon:  \"};\n    char *datetime;\n    int len = 0;\n    \/\/printf(\"The NOFILE is: %d\\n\", NOFILE);\n    \/\/printf(\"The tablesize is: %d\\n\", getdtablesize());\n    \/\/printf(\"The pid is: %d\\n\", getpid());\n\n    \/\/ \u521d\u59cb\u5316 Daemon \u8fdb\u7a0b\n    init_daemon();\n\n    \/\/ \u6bcf\u9694\u4e00\u5206\u949f\u8bb0\u5f55\u8fd0\u884c\u72b6\u6001\n    while (1) {\n        if (-1 == (fp = open(\"\/tmp\/daemon.log\", O_CREAT|O_WRONLY|O_APPEND, 0600))) {\n          printf(\"Open file error !\\n\");\n          exit(1);\n        }\n        len = strlen(buf);\n        write(fp, buf, len);\n        t = time(0);\n        datetime = asctime(localtime(&amp;t));\n        len = strlen(datetime);\n        write(fp, datetime, len);\n        close(fp);\n        sleep(60);\n    }\n\n    return 0;\n}<\/pre>\n<h6>\u5b88\u62a4\u8fdb\u7a0b\u7684\u6b63\u786e\u6027\u9a8c\u8bc1<\/h6>\n<p>\u7f16\u8bd1\u4ee3\u7801\uff1a<br \/>\n<code>gcc -o firstdaemon daemonize.c<\/code><\/p>\n<p>\u542f\u52a8\u5b88\u62a4\u8fdb\u7a0b\uff1a<br \/>\n<code>.\/firstdaemon<\/code><\/p>\n<p>\u68c0\u67e5\u662f\u5426\u5de5\u4f5c\u6b63\u5e38\uff1a<br \/>\n<code>ps -xj | grep firstdaemon<\/code><\/p>\n<p>\u8f93\u51fa\u5e94\u8be5\u7c7b\u4f3c\u4e8e\uff1a<\/p>\n<pre class=\"lang:default decode:true\">+------+------+------+------+-----+-------+------+------+------+-----+\n| PPID | PID  | PGID | SID  | TTY | TPGID | STAT | UID  | TIME | CMD |\n+------+------+------+------+-----+-------+------+------+------+-----+\n|    1 | 3387 | 3386 | 3386 | ?   |    -1 | S    | 1000 | 0:00 | .\/  |\n+------+------+------+------+-----+-------+------+------+------+-----+<\/pre>\n<p>\u89e3\u91ca\u5982\u4e0b\uff1a<\/p>\n<ul>\n<li>\u5b88\u62a4\u8fdb\u7a0b\u4e0d\u5e94\u8be5\u6709\u63a7\u5236\u7ec8\u7aef\uff0c\u6240\u4ee5(TTY = ?)<\/li>\n<li>\u5b88\u62a4\u8fdb\u7a0b\u7684\u7236\u8fdb\u7a0bID\u4e3a1\uff0c\u5373init\u8fdb\u7a0b<\/li>\n<li>\u5176\u4e2d PID != SID \u8868\u660e\u8be5\u8fdb\u7a0b\u4e0d\u662f\u4f1a\u8bdd\u7684leader\u8fdb\u7a0b\uff0c\u56e0\u4e3a\u7b2c\u4e8c\u4e2afork()\u7684\u4f5c\u7528<\/li>\n<li>\u56e0\u4e3a PID != SID \u6240\u4ee5\u8be5\u5b88\u62a4\u8fdb\u7a0b\u65e0\u6cd5\u91cd\u65b0\u6253\u5f00\/\u63a7\u5236\u4e00\u4e2aTTY<\/li>\n<\/ul>\n<h5>\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n<ul>\n<li><a href=\"http:\/\/alfred-sun.github.io\/blog\/2015\/06\/18\/daemon-implementation\/\">Linux \u5b88\u62a4\u8fdb\u7a0b\u7684\u5b9e\u73b0<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/17954432\/creating-a-daemon-in-linux\">Creating a daemon in Linux<\/a><\/li>\n<li><a href=\"http:\/\/www.nenew.net\/linux-c-program-daemon-example.html\">linux\u4e0bdaemon\u5b88\u62a4\u8fdb\u7a0b\u7684\u7f16\u5199\u5b9e\u4f8b<\/a><\/li>\n<li><a href=\"http:\/\/blog.csdn.net\/Jammg\/article\/details\/51069502\">Linux C\u521b\u5efa\u5b88\u62a4\u8fdb\u7a0b(daemon)\u53ca\u7ec8\u7aef\u76f8\u5173\u6982\u5ff5<\/a><\/li>\n<li>=<\/li>\n<li>APUE<\/li>\n<\/ul>\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u4ee5\u540e\u53ef\u80fd\u8981\u7528C\u8bed\u8a00\u5199\u4e00\u4e2adaemon\u7a0b\u5e8f\uff08\u5b88\u62a4\u8fdb\u7a0b\uff09\u8fd0\u884c\u5728\u670d\u52a1\u5668\u4e0a\uff0c\u4ee5\u8fdb\u884c\u4e00\u4e9b\u4fe1\u606f\u6536\u96c6 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,11,7],"tags":[717,30,718],"class_list":["post-3111","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-linux","category-programing","tag-daemon","tag-linux","tag-718"],"views":7812,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/3111","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=3111"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/3111\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=3111"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=3111"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=3111"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}