{"id":118337,"date":"2026-10-27T00:01:44","date_gmt":"2026-10-26T16:01:44","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/118337.html"},"modified":"2026-10-27T00:01:44","modified_gmt":"2026-10-26T16:01:44","slug":"ubuntu%e9%9b%b6%e5%9f%ba%e7%a1%80%e5%85%a5%e9%97%a8%e5%88%b0%e7%b2%be%e9%80%9a%e3%80%9010-9%e8%ae%b2%e3%80%91%ef%bc%9a%e5%83%b5%e5%b0%b8%e8%bf%9b%e7%a8%8b%e4%b8%8e%e5%ad%a4%e5%84%bf%e8%bf%9b%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/118337.html","title":{"rendered":"Ubuntu\u96f6\u57fa\u7840\u5165\u95e8\u5230\u7cbe\u901a\u301010.9\u8bb2\u3011\uff1a\u50f5\u5c38\u8fdb\u7a0b\u4e0e\u5b64\u513f\u8fdb\u7a0b\u2014\u2014Linux \u8fdb\u7a0b\u751f\u547d\u5468\u671f\u7684\u201c\u7070\u8272\u5730\u5e26\u201c"},"content":{"rendered":"<p> <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261026160132-6adf795c2eb04.png\" \/> <\/p>\n<p>&#x1f3c6; \u672c\u6587\u6536\u5f55\u4e8e \u300a\u6eda\u96ea\u7403\u5b66 Ubuntu\u300b \u4e13\u680f\u3002 \u672c\u4e13\u680f\u9762\u5411\u6709\u4e00\u5b9a\u8ba1\u7b97\u673a\u57fa\u7840&#xff0c;\u4f46\u5c1a\u672a\u7cfb\u7edf\u5b66\u4e60 Linux \/ Ubuntu \u7684\u8bfb\u8005&#xff0c;\u91c7\u7528\u201c\u6eda\u96ea\u7403\u5f0f\u5b66\u4e60\u6cd5\u201d&#xff1a;\u5148\u88c5\u597d\u3001\u518d\u4f1a\u7528\u3001\u518d\u7406\u89e3\u3001\u518d\u4f18\u5316\u3001\u518d\u5b9e\u6218&#xff0c;\u5e26\u4f60\u4ece\u7b2c\u4e00\u6b21\u8fdb\u5165 Ubuntu \u684c\u9762 \/ \u7ec8\u7aef\u5f00\u59cb&#xff0c;\u9010\u6b65\u638c\u63e1 Ubuntu \u7684\u65e5\u5e38\u4f7f\u7528\u3001\u547d\u4ee4\u64cd\u4f5c\u3001\u8f6f\u4ef6\u5b89\u88c5\u3001\u7cfb\u7edf\u914d\u7f6e\u3001\u5f00\u53d1\u73af\u5883\u642d\u5efa\u4e0e\u670d\u52a1\u5668\u90e8\u7f72\u80fd\u529b\u3002 \u2003 &#x1f3af; \u9002\u5408\u4eba\u7fa4&#xff1a; Linux \u521d\u5b66\u8005\u3001Ubuntu \u65b0\u624b\u3001\u8ba1\u7b97\u673a\u4e13\u4e1a\u5b66\u751f\u3001\u540e\u7aef \/ \u8fd0\u7ef4 \/ \u6d4b\u8bd5\u5165\u95e8\u540c\u5b66\u3001\u60f3\u642d\u5efa Linux \u5f00\u53d1\u73af\u5883\u7684\u5f00\u53d1\u8005\u3001\u51c6\u5907\u5b66\u4e60\u670d\u52a1\u5668\u90e8\u7f72\u7684\u540c\u5b66&#xff0c;\u4ee5\u53ca\u60f3\u4ece Windows \u8fc7\u6e21\u5230 Ubuntu \u5de5\u4f5c\u6d41\u7684\u7528\u6237\u3002<\/p>\n<p>\u4ece\u201c\u4f1a\u6253\u5f00\u7ec8\u7aef\u201d\u5230\u201c\u80fd\u7ba1\u7406\u7cfb\u7edf\u201d&#xff0c;\u4ece\u201c\u590d\u5236\u547d\u4ee4\u201d\u5230\u201c\u7406\u89e3 Linux \u601d\u7ef4\u201d&#xff0c;\u8fd9\u4e00\u6b21&#xff0c;\u6211\u4eec\u4e0d\u96f6\u6563\u5b66&#xff0c;\u800c\u662f\u4e00\u8def\u6eda\u96ea\u7403&#xff01; \u2003 &#x1f389; <font color=\"red\">\u9650\u65f6\u798f\u5229&#xff1a;\u5f53\u524d\u4e13\u680f\u6d3b\u52a8\u4e2d&#xff0c;\u4e00\u6b21\u8ba2\u9605&#xff0c;\u7ec8\u8eab\u9605\u8bfb&#xff0c;\u540e\u7eed\u66f4\u65b0\u7ae0\u8282\u5168\u90e8\u514d\u8d39\u89e3\u9501 &#x1f449; \u7acb\u5373\u67e5\u770b<\/font> &#x1f448;\ufe0f<\/p>\n<p>\u6f14\u793a\u73af\u5883\u8bf4\u660e&#xff1a;<\/p>\n<ul>\n<li>\u5f00\u53d1\u5de5\u5177&#xff1a;Visual Studio Code \/ JetBrains \u7cfb\u5217 IDE \/ Vim \/ Nano<\/li>\n<li>Ubuntu\u7248\u672c&#xff1a;Ubuntu 22.04 LTS \/ Ubuntu 24.04 LTS&#xff0c;\u63a8\u8350 Ubuntu 24.04 LTS<\/li>\n<li>Linux\u5185\u6838\u7248\u672c&#xff1a;Linux Kernel 6.x \u6216\u66f4\u9ad8<\/li>\n<li>\u7ec8\u7aef\u73af\u5883&#xff1a;Bash \/ Zsh<\/li>\n<li>\u5305\u7ba1\u7406\u5de5\u5177&#xff1a;APT \/ Snap \/ Flatpak<\/li>\n<li>\u8fdc\u7a0b\u8fde\u63a5\u5de5\u5177&#xff1a;OpenSSH \/ Windows Terminal \/ MobaXterm \/ Termius<\/li>\n<li>\u865a\u62df\u5316\u73af\u5883&#xff1a;VMware Workstation \/ VirtualBox \/ WSL2 \/ \u4e91\u670d\u52a1\u5668<\/li>\n<li>\u5bb9\u5668\u73af\u5883&#xff1a;Docker \/ Docker Compose<\/li>\n<li>\u64cd\u4f5c\u7cfb\u7edf&#xff1a;Ubuntu Desktop \/ Ubuntu Server<\/li>\n<\/ul>\n<p> <img decoding=\"async\" width=\"1000\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261026160134-6adf795e4d637.png\" \/> <\/p>\n<h4>\u5168\u6587\u76ee\u5f55&#xff1a;<\/h4>\n<ul>\n<li>\n<ul>\n<li>\u4e00\u3001\u524d\u8a00&#xff1a;\u90a3\u4e9b&#034;\u6b7b\u800c\u4e0d\u50f5&#034;\u7684\u8fdb\u7a0b<\/li>\n<li>\u4e8c\u3001\u672c\u6587\u8981\u89e3\u51b3\u4ec0\u4e48\u95ee\u9898<\/li>\n<li>\u4e09\u3001Linux \u8fdb\u7a0b\u751f\u547d\u5468\u671f\u57fa\u7840\u56de\u987e<\/li>\n<li>\n<ul>\n<li>3.1 \u8fdb\u7a0b\u662f\u600e\u4e48\u8bde\u751f\u7684<\/li>\n<li>3.2 \u8fdb\u7a0b\u72b6\u6001\u673a&#xff1a;\u4ece\u521b\u5efa\u5230\u6d88\u4ea1<\/li>\n<li>3.3 \u7236\u5b50\u8fdb\u7a0b\u5173\u7cfb\u4e0e wait() \u673a\u5236<\/li>\n<\/ul>\n<\/li>\n<li>\u56db\u3001\u50f5\u5c38\u8fdb\u7a0b\u6df1\u5ea6\u89e3\u6790<\/li>\n<li>\n<ul>\n<li>4.1 \u4ec0\u4e48\u662f\u50f5\u5c38\u8fdb\u7a0b<\/li>\n<li>4.2 \u50f5\u5c38\u8fdb\u7a0b\u662f\u600e\u4e48\u4ea7\u751f\u7684<\/li>\n<li>4.3 \u50f5\u5c38\u8fdb\u7a0b\u5360\u7528\u4ec0\u4e48\u8d44\u6e90<\/li>\n<li>4.4 \u50f5\u5c38\u8fdb\u7a0b\u6709\u591a\u5371\u9669<\/li>\n<li>4.5 \u5982\u4f55\u8bc6\u522b\u50f5\u5c38\u8fdb\u7a0b<\/li>\n<\/ul>\n<\/li>\n<li>\u4e94\u3001\u5b64\u513f\u8fdb\u7a0b\u6df1\u5ea6\u89e3\u6790<\/li>\n<li>\n<ul>\n<li>5.1 \u4ec0\u4e48\u662f\u5b64\u513f\u8fdb\u7a0b<\/li>\n<li>5.2 \u5b64\u513f\u8fdb\u7a0b\u7684\u4ea7\u751f\u573a\u666f<\/li>\n<li>5.3 init\/systemd \u5982\u4f55\u6536\u517b\u5b64\u513f\u8fdb\u7a0b<\/li>\n<li>5.4 \u5b64\u513f\u8fdb\u7a0b\u662f\u5426\u6709\u5bb3<\/li>\n<\/ul>\n<\/li>\n<li>\u516d\u3001\u50f5\u5c38\u8fdb\u7a0b\u4e0e\u5b64\u513f\u8fdb\u7a0b\u7684\u672c\u8d28\u533a\u522b<\/li>\n<li>\u4e03\u3001\u5b9e\u9a8c\u73af\u5883\u51c6\u5907<\/li>\n<li>\u516b\u3001\u6848\u4f8b\u4e00&#xff1a;\u4eb2\u624b\u5236\u9020\u4e00\u4e2a\u50f5\u5c38\u8fdb\u7a0b<\/li>\n<li>\n<ul>\n<li>8.1 \u7f16\u5199\u4ea7\u751f\u50f5\u5c38\u8fdb\u7a0b\u7684 C \u7a0b\u5e8f<\/li>\n<li>8.2 \u89c2\u5bdf\u50f5\u5c38\u8fdb\u7a0b<\/li>\n<li>8.3 \u547d\u4ee4\u89e3\u6790<\/li>\n<\/ul>\n<\/li>\n<li>\u4e5d\u3001\u6848\u4f8b\u4e8c&#xff1a;\u4eb2\u624b\u5236\u9020\u4e00\u4e2a\u5b64\u513f\u8fdb\u7a0b<\/li>\n<li>\n<ul>\n<li>9.1 \u7f16\u5199\u4ea7\u751f\u5b64\u513f\u8fdb\u7a0b\u7684 C \u7a0b\u5e8f<\/li>\n<li>9.2 \u89c2\u5bdf\u5b64\u513f\u8fdb\u7a0b\u7684 init \u63a5\u7ba1\u8fc7\u7a0b<\/li>\n<li>9.3 \u547d\u4ee4\u89e3\u6790<\/li>\n<\/ul>\n<\/li>\n<li>\u5341\u3001\u6848\u4f8b\u4e09&#xff1a;\u771f\u5b9e\u670d\u52a1\u5668\u4e2d\u7684\u50f5\u5c38\u8fdb\u7a0b\u6392\u67e5\u4e0e\u5904\u7406<\/li>\n<li>\n<ul>\n<li>10.1 \u7528 ps \u548c top \u5b9a\u4f4d\u50f5\u5c38\u8fdb\u7a0b<\/li>\n<li>10.2 \u627e\u5230\u50f5\u5c38\u8fdb\u7a0b\u7684\u7236\u8fdb\u7a0b<\/li>\n<li>10.3 \u901a\u8fc7 SIGCHLD \u4fe1\u53f7\u89e6\u53d1\u7236\u8fdb\u7a0b\u56de\u6536<\/li>\n<li>10.4 \u7ec8\u6781\u624b\u6bb5&#xff1a;\u7ec8\u6b62\u7236\u8fdb\u7a0b<\/li>\n<li>10.5 \u7f16\u5199\u53ef\u4ee5\u6b63\u786e\u56de\u6536\u5b50\u8fdb\u7a0b\u7684 Python \u670d\u52a1<\/li>\n<li>10.6 \u547d\u4ee4\u89e3\u6790<\/li>\n<\/ul>\n<\/li>\n<li>\u5341\u4e00\u3001\u8fdb\u7a0b\u72b6\u6001\u7801\u5168\u89e3\u6790<\/li>\n<li>\u5341\u4e8c\u3001\u6d41\u7a0b\u56fe<\/li>\n<li>\n<ul>\n<li>12.1 \u8fdb\u7a0b\u5b8c\u6574\u751f\u547d\u5468\u671f\u72b6\u6001\u56fe<\/li>\n<li>12.2 \u50f5\u5c38\u8fdb\u7a0b\u4ea7\u751f\u4e0e\u6d88\u4ea1\u6d41\u7a0b<\/li>\n<li>12.3 \u5b64\u513f\u8fdb\u7a0b\u88ab init \u63a5\u7ba1\u6d41\u7a0b<\/li>\n<li>12.4 \u751f\u4ea7\u73af\u5883\u50f5\u5c38\u8fdb\u7a0b\u6392\u67e5\u6d41\u7a0b<\/li>\n<\/ul>\n<\/li>\n<li>\u5341\u4e09\u3001\u6df1\u5165\u539f\u7406&#xff1a;\u5185\u6838\u89c6\u89d2\u4e0b\u7684\u8fdb\u7a0b\u56de\u6536<\/li>\n<li>\n<ul>\n<li>13.1 \u8fdb\u7a0b\u63cf\u8ff0\u7b26\u4e0e task_struct<\/li>\n<li>13.2 wait() \u7cfb\u5217\u7cfb\u7edf\u8c03\u7528<\/li>\n<li>13.3 SIGCHLD \u4fe1\u53f7\u7684\u4f5c\u7528<\/li>\n<li>13.4 \u4e3a\u4ec0\u4e48 PID 1 \u6c38\u8fdc\u4e0d\u4f1a\u4ea7\u751f\u50f5\u5c38\u8fdb\u7a0b<\/li>\n<\/ul>\n<\/li>\n<li>\u5341\u56db\u3001Docker \u5bb9\u5668\u4e2d\u7684\u50f5\u5c38\u8fdb\u7a0b\u95ee\u9898<\/li>\n<li>\n<ul>\n<li>14.1 \u5bb9\u5668\u5185\u7684 PID 1 \u9677\u9631<\/li>\n<li>14.2 \u4f7f\u7528 tini \u89e3\u51b3\u5bb9\u5668\u50f5\u5c38\u8fdb\u7a0b\u95ee\u9898<\/li>\n<li>14.3 Docker \u5b98\u65b9\u63a8\u8350\u7684 init \u65b9\u6848<\/li>\n<\/ul>\n<\/li>\n<li>\u5341\u4e94\u3001systemd \u4e0e\u8fdb\u7a0b\u751f\u547d\u5468\u671f\u7ba1\u7406<\/li>\n<li>\n<ul>\n<li>15.1 systemd \u5982\u4f55\u7ba1\u7406\u5b50\u8fdb\u7a0b<\/li>\n<li>15.2 KillMode \u914d\u7f6e\u7684\u91cd\u8981\u6027<\/li>\n<li>15.3 \u5b9e\u9645\u670d\u52a1\u914d\u7f6e\u793a\u4f8b<\/li>\n<\/ul>\n<\/li>\n<li>\u5341\u516d\u3001\u5e38\u89c1\u95ee\u9898\u4e0e\u8e29\u5751\u603b\u7ed3<\/li>\n<li>\u5341\u4e03\u3001\u6700\u4f73\u5b9e\u8df5\u5efa\u8bae<\/li>\n<li>\u5341\u516b\u3001\u6269\u5c55\u77e5\u8bc6\u70b9<\/li>\n<li>\n<ul>\n<li>\u8fdb\u7a0b\u7ec4\u4e0e\u4f1a\u8bdd<\/li>\n<li>subreaper \u673a\u5236<\/li>\n<li>\/proc \u6587\u4ef6\u7cfb\u7edf\u6df1\u5ea6\u4f7f\u7528<\/li>\n<li>\u4f7f\u7528 strace \u8ddf\u8e2a\u8fdb\u7a0b\u7cfb\u7edf\u8c03\u7528<\/li>\n<\/ul>\n<\/li>\n<li>\u5341\u4e5d\u3001\u5b8c\u6574\u64cd\u4f5c\u6d41\u7a0b\u56de\u987e<\/li>\n<li>\u4e8c\u5341\u3001\u603b\u7ed3<\/li>\n<li>&#x1f9e7; \u5b66\u4e60\u798f\u5229 \u00b7 \u9650\u65f6\u5f00\u653e &#x1f9e7;<\/li>\n<li>&#x1faf5; Who am I&#xff1f;<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3>\u4e00\u3001\u524d\u8a00&#xff1a;\u90a3\u4e9b&#034;\u6b7b\u800c\u4e0d\u50f5&#034;\u7684\u8fdb\u7a0b<\/h3>\n<p>\u6211\u7b2c\u4e00\u6b21\u5728\u751f\u4ea7\u670d\u52a1\u5668\u4e0a\u9047\u5230\u50f5\u5c38\u8fdb\u7a0b&#xff08;Zombie Process&#xff09;&#xff0c;\u662f\u5728\u4e00\u6b21\u6df1\u591c\u7684\u6027\u80fd\u6545\u969c\u6392\u67e5\u4e2d\u3002\u5f53\u65f6\u7cfb\u7edf\u8d1f\u8f7d\u770b\u8d77\u6765\u4e0d\u9ad8&#xff0c;\u5185\u5b58\u4e5f\u6ca1\u6709\u660e\u663e\u538b\u529b&#xff0c;\u4f46\u670d\u52a1\u5c31\u662f\u83ab\u540d\u5176\u5999\u5730\u5f00\u59cb\u62d2\u7edd\u65b0\u8fde\u63a5\u3002\u7528 ps aux \u4e00\u770b&#xff0c;\u8fdb\u7a0b\u5217\u8868\u91cc\u8d6b\u7136\u51fa\u73b0\u4e86\u5341\u51e0\u4e2a\u72b6\u6001\u4e3a Z \u7684\u8fdb\u7a0b&#xff0c;\u540e\u9762\u8ddf\u7740 &lt;defunct&gt; \u7684\u6807\u8bb0\u2014\u2014\u201c\u5df2\u7ecf\u5931\u6548\u201d\u3002<\/p>\n<p>\u6211\u5f53\u65f6\u7b2c\u4e00\u53cd\u5e94\u662f&#xff1a;\u8fd9\u4e9b\u8fdb\u7a0b\u6b7b\u4e86\u5417&#xff1f;\u90a3\u4e3a\u4ec0\u4e48\u5b83\u4eec\u8fd8\u5728\u8fdb\u7a0b\u5217\u8868\u91cc&#xff1f;<\/p>\n<p>\u540e\u6765\u624d\u660e\u767d&#xff0c;\u50f5\u5c38\u8fdb\u7a0b\u5c31\u662f Linux \u8fdb\u7a0b\u751f\u547d\u5468\u671f\u91cc\u7684\u4e00\u4e2a&#034;\u7070\u8272\u5730\u5e26&#034;&#xff1a;\u8fdb\u7a0b\u7684\u4ee3\u7801\u5df2\u7ecf\u6267\u884c\u5b8c\u6bd5&#xff0c;\u8d44\u6e90\u4e5f\u5927\u591a\u91ca\u653e\u4e86&#xff0c;\u4f46\u5b83\u7684\u8fdb\u7a0b\u63cf\u8ff0\u7b26\u8fd8\u5728\u5185\u6838\u91cc\u6302\u7740&#xff0c;\u56e0\u4e3a\u6ca1\u6709\u4eba\u6765\u4e3a\u5b83&#034;\u6536\u5c38&#034;\u2014\u2014\u4e5f\u5c31\u662f\u7236\u8fdb\u7a0b\u6ca1\u6709\u8c03\u7528 wait() \u8bfb\u53d6\u5b83\u7684\u9000\u51fa\u72b6\u6001\u3002<\/p>\n<p>\u8fd9\u4e2a\u573a\u666f\u5728\u6211\u7b2c\u4e00\u6b21\u9047\u5230\u65f6\u89c9\u5f97\u5f88\u8be1\u5f02&#xff0c;\u540e\u6765\u7406\u89e3\u4e86 Linux \u8fdb\u7a0b\u6a21\u578b\u4e4b\u540e&#xff0c;\u53cd\u800c\u89c9\u5f97\u8fd9\u662f\u4e00\u4e2a\u8bbe\u8ba1\u975e\u5e38\u7cbe\u5999\u7684\u673a\u5236&#xff1a;\u5b83\u5728\u5b50\u8fdb\u7a0b\u548c\u7236\u8fdb\u7a0b\u4e4b\u95f4&#xff0c;\u5efa\u7acb\u4e86\u4e00\u4e2a&#034;\u9000\u51fa\u4fe1\u606f\u4f20\u9012\u7684\u4e34\u65f6\u7f13\u51b2\u533a&#034;\u3002\u53ea\u662f\u5982\u679c\u7236\u8fdb\u7a0b\u4e0d\u8d1f\u8d23\u4efb\u5730\u4e0d\u53bb\u8bfb&#xff0c;\u8fd9\u4e2a\u7f13\u51b2\u533a\u5c31\u4f1a\u8d8a\u5806\u8d8a\u591a&#xff0c;\u6700\u7ec8\u6210\u4e3a\u7cfb\u7edf\u7684\u9690\u60a3\u3002<\/p>\n<p>\u5b64\u513f\u8fdb\u7a0b&#xff08;Orphan Process&#xff09;\u5219\u662f\u53e6\u4e00\u79cd\u573a\u666f&#xff1a;\u7236\u8fdb\u7a0b\u5148\u6b7b\u4e86&#xff0c;\u5b50\u8fdb\u7a0b\u8fd8\u6d3b\u7740\u3002\u8fd9\u65f6\u5019 Linux \u4f1a\u628a\u5b64\u513f\u8fdb\u7a0b&#034;\u8fc7\u7ee7&#034;\u7ed9 init&#xff08;\u4e5f\u5c31\u662f PID \u4e3a 1 \u7684\u8fdb\u7a0b&#xff0c;\u5728 Ubuntu 22.04\/24.04 \u4e2d\u662f systemd&#xff09;&#xff0c;\u7531 systemd \u6765\u627f\u62c5\u7236\u8fdb\u7a0b\u7684\u8d23\u4efb\u3002\u6240\u4ee5\u5b64\u513f\u8fdb\u7a0b\u5176\u5b9e\u6ca1\u4ec0\u4e48\u53ef\u6015\u7684&#xff0c;\u7cfb\u7edf\u81ea\u5df1\u4f1a\u5904\u7406\u3002<\/p>\n<p>\u8fd9\u4e24\u4e2a\u6982\u5ff5&#xff0c;\u662f Linux \u8fdb\u7a0b\u7ba1\u7406\u4e2d\u4e00\u4e2a\u5bb9\u6613\u88ab\u521d\u5b66\u8005\u5ffd\u89c6\u7684\u89d2\u843d\u3002\u4f46\u5f53\u4f60\u5728\u670d\u52a1\u5668\u4e0a\u53d1\u73b0\u8d44\u6e90\u5f02\u5e38\u3001PID \u8017\u5c3d\u6216\u8005\u67d0\u4e9b\u670d\u52a1\u65e0\u6cd5\u542f\u52a8\u65b0\u8fdb\u7a0b\u65f6&#xff0c;\u641e\u61c2\u8fd9\u4e24\u4e2a\u6982\u5ff5\u5f80\u5f80\u80fd\u8ba9\u4f60\u627e\u5230\u95ee\u9898\u7684\u6839\u6e90\u3002<\/p>\n<p>\u8fd9\u7bc7\u6587\u7ae0&#xff0c;\u6211\u4eec\u5c31\u4ece\u5934\u5230\u5c3e\u628a\u50f5\u5c38\u8fdb\u7a0b\u548c\u5b64\u513f\u8fdb\u7a0b\u8bb2\u900f\u3002<\/p>\n<h3>\u4e8c\u3001\u672c\u6587\u8981\u89e3\u51b3\u4ec0\u4e48\u95ee\u9898<\/h3>\n<p>\u8bfb\u5b8c\u8fd9\u7bc7\u6587\u7ae0&#xff0c;\u4f60\u5e94\u8be5\u80fd\u591f\u56de\u7b54\u4ee5\u4e0b\u95ee\u9898&#xff0c;\u5e76\u4e14\u80fd\u5728\u771f\u5b9e\u7684 Ubuntu \u7cfb\u7edf\u4e0a\u52a8\u624b\u64cd\u4f5c&#xff1a;<\/p>\n<li>\u8fdb\u7a0b\u662f\u600e\u4e48&#034;\u6b7b&#034;\u7684&#xff1f;Linux \u5185\u6838\u5728\u8fdb\u7a0b\u9000\u51fa\u65f6\u505a\u4e86\u4ec0\u4e48&#xff1f;<\/li>\n<li>\u50f5\u5c38\u8fdb\u7a0b\u7684\u6280\u672f\u5b9a\u4e49\u662f\u4ec0\u4e48&#xff1f;\u5b83\u5230\u5e95\u5360\u7528\u4e86\u54ea\u4e9b\u8d44\u6e90&#xff0c;\u53c8\u91ca\u653e\u4e86\u54ea\u4e9b&#xff1f;<\/li>\n<li>\u5b64\u513f\u8fdb\u7a0b\u548c\u50f5\u5c38\u8fdb\u7a0b\u6709\u4ec0\u4e48\u672c\u8d28\u533a\u522b&#xff1f;\u54ea\u4e2a\u66f4\u5371\u9669&#xff1f;<\/li>\n<li>\u5982\u4f55\u5728 Ubuntu \u4e2d\u8bc6\u522b\u3001\u5b9a\u4f4d\u50f5\u5c38\u8fdb\u7a0b&#xff1f;<\/li>\n<li>\u50f5\u5c38\u8fdb\u7a0b\u80fd\u88ab\u76f4\u63a5 kill \u6389\u5417&#xff1f;\u771f\u6b63\u6709\u6548\u7684\u5904\u7406\u65b9\u5f0f\u662f\u4ec0\u4e48&#xff1f;<\/li>\n<li>Docker \u5bb9\u5668\u91cc\u7684 PID 1 \u95ee\u9898\u548c\u50f5\u5c38\u8fdb\u7a0b\u6709\u4ec0\u4e48\u5173\u7cfb&#xff1f;<\/li>\n<li>\u5728 Python\/Java\/Shell \u7b49\u8bed\u8a00\u5199\u670d\u52a1\u65f6&#xff0c;\u5982\u4f55\u4ece\u4ee3\u7801\u5c42\u9762\u907f\u514d\u4ea7\u751f\u50f5\u5c38\u8fdb\u7a0b&#xff1f;<\/li>\n<li>systemd \u662f\u5982\u4f55\u5e2e\u52a9\u6211\u4eec\u7ba1\u7406\u5b50\u8fdb\u7a0b\u751f\u547d\u5468\u671f\u7684&#xff1f;<\/li>\n<p>\u8fd9\u4e9b\u95ee\u9898\u7684\u7b54\u6848&#xff0c;\u6db5\u76d6\u4e86\u4ece\u5165\u95e8\u6982\u5ff5\u5230\u5de5\u7a0b\u5b9e\u8df5\u7684\u5b8c\u6574\u5c42\u6b21\u3002\u5982\u679c\u4f60\u662f Linux \u521d\u5b66\u8005&#xff0c;\u5efa\u8bae\u4ece\u7b2c\u4e09\u8282\u5f00\u59cb\u987a\u5e8f\u9605\u8bfb&#xff1b;\u5982\u679c\u4f60\u5df2\u7ecf\u5bf9\u8fdb\u7a0b\u57fa\u7840\u6709\u4e00\u5b9a\u4e86\u89e3&#xff0c;\u53ef\u4ee5\u76f4\u63a5\u8df3\u5230\u7b2c\u56db\u8282\u4ece\u50f5\u5c38\u8fdb\u7a0b\u5f00\u59cb\u6df1\u5165\u3002<\/p>\n<h3>\u4e09\u3001Linux \u8fdb\u7a0b\u751f\u547d\u5468\u671f\u57fa\u7840\u56de\u987e<\/h3>\n<p>\u5728\u8fdb\u5165\u6b63\u9898\u4e4b\u524d&#xff0c;\u6211\u4eec\u9700\u8981\u628a Linux \u8fdb\u7a0b\u751f\u547d\u5468\u671f\u7684\u57fa\u7840\u6846\u67b6\u642d\u8d77\u6765\u3002\u5f88\u591a\u8bfb\u8005\u5728\u5b66 Linux \u7684\u65f6\u5019&#xff0c;\u547d\u4ee4\u7528\u5f97\u633a\u987a&#xff0c;\u4f46\u5bf9&#034;\u8fdb\u7a0b\u7a76\u7adf\u662f\u600e\u4e48\u8dd1\u8d77\u6765\u7684\u3001\u600e\u4e48\u7ed3\u675f\u7684&#034;\u8fd9\u6761\u7ebf&#xff0c;\u59cb\u7ec8\u662f\u6a21\u7cca\u7684\u3002\u8fd9\u4e2a\u6a21\u7cca\u4f1a\u8ba9\u4f60\u5728\u9047\u5230\u50f5\u5c38\u8fdb\u7a0b\u8fd9\u7c7b\u95ee\u9898\u65f6\u65e0\u4ece\u4e0b\u624b\u3002<\/p>\n<h4>3.1 \u8fdb\u7a0b\u662f\u600e\u4e48\u8bde\u751f\u7684<\/h4>\n<p>\u5728 Linux \u4e2d&#xff0c;\u51e0\u4e4e\u6240\u6709\u7684\u8fdb\u7a0b\u90fd\u662f\u901a\u8fc7 fork() \u7cfb\u7edf\u8c03\u7528\u8bde\u751f\u7684\u3002fork() \u4f1a\u521b\u5efa\u4e00\u4e2a\u5f53\u524d\u8fdb\u7a0b&#xff08;\u7236\u8fdb\u7a0b&#xff09;\u7684\u526f\u672c&#xff0c;\u8fd9\u4e2a\u526f\u672c\u5c31\u662f\u5b50\u8fdb\u7a0b\u3002\u5b50\u8fdb\u7a0b\u7ee7\u627f\u4e86\u7236\u8fdb\u7a0b\u7684\u5185\u5b58\u6620\u50cf\u3001\u6587\u4ef6\u63cf\u8ff0\u7b26\u3001\u4fe1\u53f7\u5904\u7406\u7b49\u5927\u91cf\u5c5e\u6027&#xff0c;\u4f46\u62e5\u6709\u72ec\u7acb\u7684 PID\u3002<\/p>\n<p>\u5f88\u591a\u4eba\u4f1a\u95ee&#xff1a;\u90a3\u6700\u521d\u7684\u90a3\u4e2a\u8fdb\u7a0b\u662f\u4ece\u54ea\u6765\u7684&#xff1f;\u7b54\u6848\u662f PID 1&#xff0c;\u4e5f\u5c31\u662f init \u8fdb\u7a0b&#xff08;\u5728 Ubuntu 22.04\/24.04 \u4e2d\u662f systemd&#xff09;\u3002\u5b83\u662f Linux \u5185\u6838\u5728\u5b8c\u6210\u521d\u59cb\u5316\u4e4b\u540e\u542f\u52a8\u7684\u7b2c\u4e00\u4e2a\u7528\u6237\u7a7a\u95f4\u8fdb\u7a0b&#xff0c;\u662f\u6240\u6709\u8fdb\u7a0b\u7684&#034;\u7956\u5b97&#034;\u3002<\/p>\n<p>\u7cfb\u7edf\u4e0a\u6240\u6709\u7684\u8fdb\u7a0b&#xff0c;\u90fd\u662f\u4ece PID 1 \u4e00\u4ee3\u4e00\u4ee3 fork \u51fa\u6765\u7684\u3002\u4f60\u6253\u5f00\u4e00\u4e2a\u7ec8\u7aef&#xff0c;\u7ec8\u7aef\u6a21\u62df\u5668&#xff08;gnome-terminal \u6216 bash&#xff09;\u662f\u4e00\u4e2a\u8fdb\u7a0b&#xff1b;\u4f60\u5728\u7ec8\u7aef\u91cc\u8fd0\u884c\u4e00\u4e2a\u547d\u4ee4&#xff08;\u6bd4\u5982 ls&#xff09;&#xff0c;shell \u4f1a fork \u51fa\u4e00\u4e2a\u5b50\u8fdb\u7a0b\u6765\u6267\u884c\u8fd9\u4e2a\u547d\u4ee4\u3002\u6574\u4e2a\u7cfb\u7edf\u5c31\u662f\u4e00\u68f5\u4ee5 PID 1 \u4e3a\u6839\u7684\u8fdb\u7a0b\u6811\u3002<\/p>\n<p>\u7528\u4e0b\u9762\u8fd9\u4e2a\u547d\u4ee4\u53ef\u4ee5\u76f4\u89c2\u5730\u770b\u5230\u8fd9\u68f5\u8fdb\u7a0b\u6811&#xff1a;<\/p>\n<p><span class=\"token comment\"># \u4ee5\u6811\u5f62\u7ed3\u6784\u663e\u793a\u5f53\u524d\u6240\u6709\u8fdb\u7a0b\u53ca\u5176\u7236\u5b50\u5173\u7cfb<\/span><br \/>\npstree <span class=\"token parameter variable\">-p<\/span><\/p>\n<p><span class=\"token comment\"># \u5982\u679c pstree \u6ca1\u6709\u5b89\u88c5&#xff0c;\u5148\u5b89\u88c5 psmisc \u5de5\u5177\u5305<\/span><br \/>\n<span class=\"token function\">sudo<\/span> <span class=\"token function\">apt<\/span> <span class=\"token function\">install<\/span> <span class=\"token parameter variable\">-y<\/span> psmisc<\/p>\n<p>\u8fd0\u884c\u4e4b\u540e\u4f60\u4f1a\u770b\u5230\u7c7b\u4f3c\u8fd9\u6837\u7684\u8f93\u51fa&#xff1a;<\/p>\n<p>systemd(1)\u2500\u252c\u2500ModemManager(845)\u2500\u252c\u2500{ModemManager}(893)<br \/>\n           \u2502                   \u2514\u2500{ModemManager}(894)<br \/>\n           \u251c\u2500NetworkManager(832)\u2500\u252c\u2500{NetworkManager}(881)<br \/>\n           \u2502                    \u2514\u2500{NetworkManager}(882)<br \/>\n           \u251c\u2500sshd(1123)\u2500\u2500\u2500sshd(2456)\u2500\u2500\u2500bash(2458)\u2500\u2500\u2500pstree(3201)<br \/>\n           \u251c\u2500nginx(1234)\u2500\u252c\u2500nginx(1235)<br \/>\n           \u2502             \u2514\u2500nginx(1236)<br \/>\n           \u2514\u2500&#8230;<\/p>\n<p>\u8fd9\u5f20\u56fe\u91cc&#xff0c;\u62ec\u53f7\u91cc\u7684\u6570\u5b57\u662f PID\u3002\u4f60\u80fd\u6e05\u695a\u5730\u770b\u5230\u8fdb\u7a0b\u4e4b\u95f4\u7684\u7236\u5b50\u5173\u7cfb&#xff1a;systemd \u662f\u6839&#xff0c;sshd \u76d1\u542c SSH \u8fde\u63a5&#xff0c;\u5f53\u4f60\u901a\u8fc7 SSH \u767b\u5f55\u65f6\u5b83 fork \u51fa\u4e00\u4e2a\u65b0\u7684 sshd \u5b50\u8fdb\u7a0b&#xff0c;\u8fd9\u4e2a\u5b50\u8fdb\u7a0b\u518d fork \u51fa bash&#xff0c;bash \u518d fork \u51fa\u4f60\u6267\u884c\u7684\u547d\u4ee4\u3002<\/p>\n<h4>3.2 \u8fdb\u7a0b\u72b6\u6001\u673a&#xff1a;\u4ece\u521b\u5efa\u5230\u6d88\u4ea1<\/h4>\n<p>\u4e00\u4e2a\u8fdb\u7a0b\u5728\u5176\u4e00\u751f\u4e2d&#xff0c;\u4f1a\u7ecf\u5386\u591a\u79cd\u72b6\u6001\u3002\u7406\u89e3\u8fd9\u4e9b\u72b6\u6001&#xff0c;\u662f\u7406\u89e3\u50f5\u5c38\u8fdb\u7a0b\u7684\u57fa\u7840\u3002<\/p>\n<p>Linux \u8fdb\u7a0b\u7684\u4e3b\u8981\u72b6\u6001\u5982\u4e0b&#xff1a;<\/p>\n<table>\n<tr>\u72b6\u6001\u7801\u5168\u79f0\u542b\u4e49<\/tr>\n<tbody>\n<tr>\n<td>R<\/td>\n<td>Running<\/td>\n<td>\u6b63\u5728 CPU \u4e0a\u8fd0\u884c&#xff0c;\u6216\u5728\u8fd0\u884c\u961f\u5217\u4e2d\u7b49\u5f85\u8fd0\u884c<\/td>\n<\/tr>\n<tr>\n<td>S<\/td>\n<td>Sleeping<\/td>\n<td>\u53ef\u4e2d\u65ad\u7684\u7761\u7720\u72b6\u6001&#xff0c;\u7b49\u5f85\u67d0\u4e2a\u4e8b\u4ef6&#xff08;\u5982 I\/O \u5b8c\u6210\u3001\u4fe1\u53f7\u5230\u8fbe&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>D<\/td>\n<td>Disk sleep<\/td>\n<td>\u4e0d\u53ef\u4e2d\u65ad\u7684\u7761\u7720&#xff0c;\u901a\u5e38\u5728\u7b49\u5f85\u78c1\u76d8 I\/O&#xff0c;\u4e0d\u53ef\u88ab kill<\/td>\n<\/tr>\n<tr>\n<td>T<\/td>\n<td>Stopped<\/td>\n<td>\u8fdb\u7a0b\u88ab\u4fe1\u53f7\u6682\u505c&#xff08;\u5982 Ctrl&#043;Z&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>Z<\/td>\n<td>Zombie<\/td>\n<td>\u50f5\u5c38\u72b6\u6001&#xff0c;\u8fdb\u7a0b\u5df2\u9000\u51fa\u4f46\u5c1a\u672a\u88ab\u7236\u8fdb\u7a0b\u56de\u6536<\/td>\n<\/tr>\n<tr>\n<td>X<\/td>\n<td>Dead<\/td>\n<td>\u8fdb\u7a0b\u5373\u5c06\u88ab\u5220\u9664&#xff0c;\u6781\u5c11\u5728 ps \u4e2d\u770b\u5230<\/td>\n<\/tr>\n<tr>\n<td>I<\/td>\n<td>Idle<\/td>\n<td>\u5185\u6838\u7ebf\u7a0b\u7a7a\u95f2\u72b6\u6001&#xff08;Linux 4.14&#043; \u65b0\u589e&#xff09;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5176\u4e2d Z \u72b6\u6001\u5c31\u662f\u6211\u4eec\u4eca\u5929\u7684\u4e3b\u89d2\u2014\u2014\u50f5\u5c38\u8fdb\u7a0b\u3002<\/p>\n<h4>3.3 \u7236\u5b50\u8fdb\u7a0b\u5173\u7cfb\u4e0e wait() \u673a\u5236<\/h4>\n<p>\u8fd9\u662f\u6574\u7bc7\u6587\u7ae0\u6700\u6838\u5fc3\u7684\u5e95\u5c42\u673a\u5236&#xff0c;\u5fc5\u987b\u641e\u6e05\u695a\u3002<\/p>\n<p>\u5f53\u4e00\u4e2a\u8fdb\u7a0b\u8c03\u7528 exit() \u9000\u51fa\u65f6&#xff0c;Linux \u5185\u6838\u4f1a&#xff1a;<\/p>\n<li>\u91ca\u653e\u8fdb\u7a0b\u5360\u7528\u7684\u5927\u90e8\u5206\u8d44\u6e90&#xff1a;\u5185\u5b58\u3001\u6253\u5f00\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u3001\u9501\u7b49&#xff1b;<\/li>\n<li>\u4f46\u4e0d\u4f1a\u7acb\u5373\u5220\u9664\u8fdb\u7a0b\u63cf\u8ff0\u7b26&#xff08;\u5373 task_struct \u7ed3\u6784\u4f53&#xff09;&#xff1b;<\/li>\n<li>\u5185\u6838\u5c06\u8fdb\u7a0b\u72b6\u6001\u7f6e\u4e3a Z&#xff08;\u50f5\u5c38\u6001&#xff09;&#xff1b;<\/li>\n<li>\u5185\u6838\u5411\u7236\u8fdb\u7a0b\u53d1\u9001 SIGCHLD \u4fe1\u53f7&#xff0c;\u901a\u77e5\u7236\u8fdb\u7a0b&#034;\u4f60\u6709\u4e00\u4e2a\u5b50\u8fdb\u7a0b\u9000\u51fa\u4e86&#034;&#xff1b;<\/li>\n<li>\u7236\u8fdb\u7a0b\u901a\u8fc7\u8c03\u7528 wait() \u6216 waitpid() \u7cfb\u7edf\u8c03\u7528\u6765\u8bfb\u53d6\u5b50\u8fdb\u7a0b\u7684\u9000\u51fa\u72b6\u6001&#xff1b;<\/li>\n<li>\u7236\u8fdb\u7a0b\u8bfb\u53d6\u4e4b\u540e&#xff0c;\u5185\u6838\u624d\u4f1a\u5f7b\u5e95\u5220\u9664\u8be5\u5b50\u8fdb\u7a0b\u7684\u8fdb\u7a0b\u63cf\u8ff0\u7b26&#xff0c;PID \u88ab\u91ca\u653e&#xff0c;\u8fdb\u7a0b\u624d\u771f\u6b63\u6d88\u4ea1\u3002<\/li>\n<p>\u8fd9\u4e2a\u673a\u5236\u7684\u8bbe\u8ba1\u610f\u56fe\u5f88\u6e05\u6670&#xff1a;\u7236\u8fdb\u7a0b\u9700\u8981\u77e5\u9053\u5b50\u8fdb\u7a0b\u662f\u600e\u4e48\u9000\u51fa\u7684&#xff08;\u6b63\u5e38\u9000\u51fa\u8fd8\u662f\u88ab\u4fe1\u53f7\u6740\u6b7b&#xff0c;\u9000\u51fa\u7801\u662f\u591a\u5c11&#xff09;&#xff0c;\u5185\u6838\u9700\u8981\u4e00\u4e2a\u5730\u65b9\u4fdd\u5b58\u8fd9\u4e9b\u4fe1\u606f&#xff0c;\u76f4\u5230\u7236\u8fdb\u7a0b\u6765\u8bfb\u53d6\u3002\u8fd9\u4e2a&#034;\u6682\u5b58\u5904&#034;&#xff0c;\u5c31\u662f\u5904\u4e8e\u50f5\u5c38\u72b6\u6001\u7684\u8fdb\u7a0b\u63cf\u8ff0\u7b26\u3002<\/p>\n<p>\u95ee\u9898\u5c31\u51fa\u5728\u7b2c5\u6b65&#xff1a;\u5982\u679c\u7236\u8fdb\u7a0b\u6ca1\u6709\u8c03\u7528 wait()\u2014\u2014\u53ef\u80fd\u662f\u7236\u8fdb\u7a0b\u4ee3\u7801\u5199\u5f97\u6709\u95ee\u9898&#xff0c;\u5fd8\u8bb0\u5904\u7406 SIGCHLD&#xff0c;\u6216\u8005\u7236\u8fdb\u7a0b\u81ea\u5df1\u9677\u5165\u4e86\u6b7b\u5faa\u73af\u2014\u2014\u90a3\u8fd9\u4e2a\u50f5\u5c38\u8fdb\u7a0b\u5c31\u4f1a\u4e00\u76f4\u6302\u5728\u90a3\u91cc&#xff0c;\u7b49\u5f85\u4e00\u4e2a\u6c38\u8fdc\u4e0d\u4f1a\u6765\u7684&#034;\u6536\u5c38\u4eba&#034;\u3002<\/p>\n<p>\u7406\u89e3\u4e86\u8fd9\u4e2a\u673a\u5236&#xff0c;\u50f5\u5c38\u8fdb\u7a0b\u548c\u5b64\u513f\u8fdb\u7a0b\u7684\u6982\u5ff5\u5c31\u8fce\u5203\u800c\u89e3\u4e86\u3002<\/p>\n<h3>\u56db\u3001\u50f5\u5c38\u8fdb\u7a0b\u6df1\u5ea6\u89e3\u6790<\/h3>\n<h4>4.1 \u4ec0\u4e48\u662f\u50f5\u5c38\u8fdb\u7a0b<\/h4>\n<p>\u50f5\u5c38\u8fdb\u7a0b&#xff08;Zombie Process&#xff09;\u662f\u6307\u5df2\u7ecf\u6267\u884c\u5b8c\u6bd5\u3001\u8c03\u7528\u4e86 exit() \u9000\u51fa&#xff0c;\u4f46\u5176\u8fdb\u7a0b\u63cf\u8ff0\u7b26&#xff08;task_struct&#xff09;\u4ecd\u7136\u5b58\u5728\u4e8e\u5185\u6838\u8fdb\u7a0b\u8868\u4e2d\u7684\u8fdb\u7a0b\u3002\u8fd9\u79cd\u8fdb\u7a0b\u7684\u72b6\u6001\u7801\u5728 ps \u8f93\u51fa\u4e2d\u663e\u793a\u4e3a Z&#xff0c;\u540c\u65f6\u901a\u5e38\u8fd8\u9644\u6709 &lt;defunct&gt;&#xff08;\u5931\u6548&#xff09;\u7684\u6807\u8bb0\u3002<\/p>\n<p>\u7528\u4e00\u4e2a\u751f\u6d3b\u5316\u7684\u6bd4\u55bb\u6765\u7406\u89e3&#xff1a;\u4f60\u96c7\u4e86\u4e00\u4e2a\u4e34\u65f6\u5de5&#xff08;\u5b50\u8fdb\u7a0b&#xff09;\u5b8c\u6210\u67d0\u9879\u4efb\u52a1&#xff0c;\u4efb\u52a1\u5b8c\u6210\u540e\u4e34\u65f6\u5de5\u7ad9\u5728\u95e8\u53e3\u7b49\u4f60\u6765\u7b7e\u6536&#xff08;\u7236\u8fdb\u7a0b\u8c03\u7528 wait() \u8bfb\u53d6\u9000\u51fa\u72b6\u6001&#xff09;&#xff0c;\u7b7e\u6536\u5b8c\u6210\u540e\u4e34\u65f6\u5de5\u79bb\u5f00&#xff08;\u8fdb\u7a0b\u63cf\u8ff0\u7b26\u88ab\u5220\u9664&#xff09;\u3002\u4f46\u5982\u679c\u4f60&#xff08;\u7236\u8fdb\u7a0b&#xff09;\u4e00\u76f4\u6ca1\u6765\u7b7e\u6536&#xff0c;\u4e34\u65f6\u5de5\u5c31\u8fd9\u6837\u7ad9\u5728\u95e8\u53e3&#xff0c;\u65e2\u6ca1\u6709\u5728\u5de5\u4f5c&#xff08;\u8d44\u6e90\u5df2\u7ecf\u91ca\u653e&#xff09;&#xff0c;\u4e5f\u8d70\u4e0d\u4e86&#xff08;\u8fdb\u7a0b\u63cf\u8ff0\u7b26\u8fd8\u5728&#xff09;\u3002\u8fd9\u4e2a\u72b6\u6001&#xff0c;\u5c31\u662f\u50f5\u5c38\u3002<\/p>\n<h4>4.2 \u50f5\u5c38\u8fdb\u7a0b\u662f\u600e\u4e48\u4ea7\u751f\u7684<\/h4>\n<p>\u50f5\u5c38\u8fdb\u7a0b\u7684\u4ea7\u751f&#xff0c;\u4ece\u6280\u672f\u5c42\u9762\u8bf4&#xff0c;\u53ea\u6709\u4e00\u4e2a\u539f\u56e0&#xff1a;\u5b50\u8fdb\u7a0b\u5df2\u9000\u51fa&#xff0c;\u4f46\u7236\u8fdb\u7a0b\u5c1a\u672a\u8c03\u7528 wait() \u6216 waitpid() \u6765\u8bfb\u53d6\u5b50\u8fdb\u7a0b\u7684\u9000\u51fa\u72b6\u6001\u3002<\/p>\n<p>\u5728\u73b0\u5b9e\u4e2d&#xff0c;\u8fd9\u901a\u5e38\u6765\u81ea\u4ee5\u4e0b\u51e0\u79cd\u60c5\u51b5&#xff1a;<\/p>\n<p>\u60c5\u51b5\u4e00&#xff1a;\u7236\u8fdb\u7a0b\u4ee3\u7801\u6709\u7f3a\u9677<\/p>\n<p>\u8fd9\u662f\u6700\u5e38\u89c1\u7684\u539f\u56e0\u3002\u7236\u8fdb\u7a0b\u9891\u7e41 fork \u5b50\u8fdb\u7a0b\u6765\u5904\u7406\u4efb\u52a1&#xff0c;\u4f46\u5728\u5b50\u8fdb\u7a0b\u9000\u51fa\u65f6\u6ca1\u6709\u6b63\u786e\u5904\u7406 SIGCHLD \u4fe1\u53f7&#xff0c;\u5bfc\u81f4\u9000\u51fa\u7684\u5b50\u8fdb\u7a0b\u7d2f\u79ef\u6210\u5927\u91cf\u50f5\u5c38\u8fdb\u7a0b\u3002\u8fd9\u79cd\u60c5\u51b5\u5728\u9ad8\u5e76\u53d1\u7684 Web \u670d\u52a1\u3001\u6d88\u606f\u961f\u5217\u6d88\u8d39\u8005\u7b49\u573a\u666f\u4e0b\u5f88\u5bb9\u6613\u51fa\u73b0\u3002<\/p>\n<p>\u60c5\u51b5\u4e8c&#xff1a;\u7236\u8fdb\u7a0b\u5fd9\u4e8e\u5904\u7406\u5176\u4ed6\u903b\u8f91<\/p>\n<p>\u7236\u8fdb\u7a0b\u521b\u5efa\u5b50\u8fdb\u7a0b\u540e&#xff0c;\u81ea\u5df1\u9677\u5165\u4e86\u4e00\u4e2a\u957f\u65f6\u95f4\u7684\u963b\u585e\u64cd\u4f5c&#xff08;\u6bd4\u5982\u7b49\u5f85\u7f51\u7edc\u54cd\u5e94&#xff09;&#xff0c;\u5728\u8fd9\u6bb5\u65f6\u95f4\u5185\u5b50\u8fdb\u7a0b\u9000\u51fa\u4e86&#xff0c;\u4f46\u7236\u8fdb\u7a0b\u6682\u65f6\u6ca1\u80fd\u8c03\u7528 wait()\u3002\u8fd9\u79cd\u60c5\u51b5\u4ea7\u751f\u7684\u662f\u77ed\u6682\u7684\u50f5\u5c38\u8fdb\u7a0b&#xff0c;\u7236\u8fdb\u7a0b\u5fd9\u5b8c\u4e4b\u540e\u901a\u5e38\u4f1a\u81ea\u884c\u5904\u7406&#xff0c;\u5371\u5bb3\u4e0d\u5927\u3002<\/p>\n<p>\u60c5\u51b5\u4e09&#xff1a;\u4f7f\u7528\u4e86\u53cc\u91cd fork \u6280\u5de7\u4f46\u5b9e\u73b0\u6709\u8bef<\/p>\n<p>\u5728\u5b88\u62a4\u8fdb\u7a0b&#xff08;Daemon&#xff09;\u7f16\u7a0b\u4e2d&#xff0c;\u6709\u4e00\u79cd\u7ecf\u5178\u7684&#034;\u53cc\u91cd fork&#034;\u6280\u5de7&#xff0c;\u76ee\u7684\u662f\u8ba9\u6700\u7ec8\u7684\u5de5\u4f5c\u8fdb\u7a0b\u6210\u4e3a\u5b64\u513f\u8fdb\u7a0b&#xff08;\u88ab init \u63a5\u7ba1&#xff09;&#xff0c;\u4ece\u800c\u907f\u514d\u50f5\u5c38\u95ee\u9898\u3002\u4f46\u5982\u679c\u5b9e\u73b0\u4e0d\u5f53&#xff0c;\u4e5f\u53ef\u80fd\u7559\u4e0b\u50f5\u5c38\u3002<\/p>\n<p>\u60c5\u51b5\u56db&#xff1a;\u811a\u672c\u4e2d\u7684\u540e\u53f0\u8fdb\u7a0b\u7ba1\u7406\u4e0d\u5f53<\/p>\n<p>Shell \u811a\u672c\u91cc\u7528 &amp; \u628a\u8fdb\u7a0b\u653e\u5230\u540e\u53f0\u8fd0\u884c&#xff0c;\u5982\u679c\u811a\u672c\u63d0\u524d\u9000\u51fa\u4f46\u5b50\u8fdb\u7a0b\u8fd8\u6ca1\u7ed3\u675f&#xff0c;\u6216\u8005\u53cd\u8fc7\u6765\u5b50\u8fdb\u7a0b\u7ed3\u675f\u4e86\u811a\u672c\u8fd8\u5728\u8dd1\u4f46\u6ca1\u6709 wait&#xff0c;\u90fd\u53ef\u80fd\u4ea7\u751f\u50f5\u5c38\u8fdb\u7a0b\u3002<\/p>\n<h4>4.3 \u50f5\u5c38\u8fdb\u7a0b\u5360\u7528\u4ec0\u4e48\u8d44\u6e90<\/h4>\n<p>\u8fd9\u662f\u5f88\u591a\u4eba\u6df7\u6dc6\u7684\u4e00\u70b9\u3002\u6211\u5f53\u65f6\u7b2c\u4e00\u6b21\u9047\u5230\u50f5\u5c38\u8fdb\u7a0b&#xff0c;\u4e5f\u8bef\u4ee5\u4e3a\u5b83\u4f1a\u5360\u7528\u5927\u91cf\u5185\u5b58\u548c CPU\u3002\u5b9e\u9645\u4e0a\u5e76\u975e\u5982\u6b64\u3002<\/p>\n<p>\u5f53\u8fdb\u7a0b\u8fdb\u5165\u50f5\u5c38\u72b6\u6001\u65f6&#xff0c;\u4ee5\u4e0b\u8d44\u6e90\u5df2\u7ecf\u88ab\u91ca\u653e&#xff1a;<\/p>\n<ul>\n<li>\u8fdb\u7a0b\u5360\u7528\u7684\u7269\u7406\u5185\u5b58\u548c\u865a\u62df\u5185\u5b58\u6620\u5c04<\/li>\n<li>\u6253\u5f00\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26<\/li>\n<li>\u5360\u7528\u7684 I\/O \u8d44\u6e90<\/li>\n<li>\u4fe1\u53f7\u5904\u7406\u5668<\/li>\n<li>\u5b50\u8fdb\u7a0b\u521b\u5efa\u7684\u5185\u5b58\u6620\u5c04&#xff08;mmap&#xff09;<\/li>\n<\/ul>\n<p>\u4ecd\u7136\u4fdd\u7559\u7684\u8d44\u6e90\u53ea\u6709\u4e00\u4e2a&#xff1a;<\/p>\n<ul>\n<li>\u8fdb\u7a0b\u63cf\u8ff0\u7b26\u672c\u8eab&#xff08;task_struct&#xff09;&#xff1a;\u8fd9\u662f\u4e00\u4e2a\u5185\u6838\u6570\u636e\u7ed3\u6784&#xff0c;\u4fdd\u5b58\u7740\u8fdb\u7a0b\u7684\u9000\u51fa\u72b6\u6001\u3001PID\u3001PPID\u3001\u8d44\u6e90\u4f7f\u7528\u7edf\u8ba1&#xff08;\u7528\u4e8e getrusage()&#xff09;\u7b49\u5c11\u91cf\u4fe1\u606f\u3002\u5355\u4e2a\u50f5\u5c38\u8fdb\u7a0b\u5360\u7528\u7684\u5185\u5b58\u6781\u5c11&#xff0c;\u5927\u6982\u53ea\u6709\u51e0 KB\u3002<\/li>\n<\/ul>\n<p>\u4f46\u6700\u5173\u952e\u7684\u8d44\u6e90\u662f PID&#xff08;\u8fdb\u7a0b ID&#xff09;\u3002<\/p>\n<p>Linux \u7cfb\u7edf\u7684 PID \u662f\u6709\u4e0a\u9650\u7684\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b&#xff0c;Ubuntu \u7cfb\u7edf\u7684 PID \u6700\u5927\u503c\u662f 32768&#xff08;\u53ef\u4ee5\u901a\u8fc7 \/proc\/sys\/kernel\/pid_max \u67e5\u770b\u548c\u4fee\u6539&#xff0c;\u6700\u9ad8\u53ef\u4ee5\u8bbe\u7f6e\u4e3a 4194304&#xff09;\u3002\u6bcf\u4e00\u4e2a\u50f5\u5c38\u8fdb\u7a0b\u5360\u7528\u4e00\u4e2a PID&#xff0c;\u5982\u679c\u50f5\u5c38\u8fdb\u7a0b\u79ef\u7d2f\u5230\u6570\u91cf\u4e0e PID \u4e0a\u9650\u76f8\u8fd1&#xff0c;\u7cfb\u7edf\u5c31\u65e0\u6cd5\u521b\u5efa\u65b0\u8fdb\u7a0b\u2014\u2014\u65e0\u8bba\u662f fork \u8fd8\u662f exec&#xff0c;\u90fd\u4f1a\u5931\u8d25&#xff0c;\u8fd4\u56de \u201cResource temporarily unavailable\u201d \u7684\u9519\u8bef\u3002<\/p>\n<p>\u8fd9\u624d\u662f\u50f5\u5c38\u8fdb\u7a0b\u771f\u6b63\u7684\u5371\u5bb3\u6240\u5728&#xff1a;\u4e0d\u662f\u5185\u5b58&#xff0c;\u800c\u662f PID \u8017\u5c3d\u3002<\/p>\n<p><span class=\"token comment\"># \u67e5\u770b\u5f53\u524d\u7cfb\u7edf\u7684 PID \u6700\u5927\u503c<\/span><br \/>\n<span class=\"token function\">cat<\/span> \/proc\/sys\/kernel\/pid_max<\/p>\n<p><span class=\"token comment\"># \u67e5\u770b\u5f53\u524d\u7cfb\u7edf\u4e2d\u7684\u50f5\u5c38\u8fdb\u7a0b\u6570\u91cf<\/span><br \/>\n<span class=\"token function\">ps<\/span> aux <span class=\"token operator\">|<\/span> <span class=\"token function\">awk<\/span> <span class=\"token string\">&#039;{print $8}&#039;<\/span> <span class=\"token operator\">|<\/span> <span class=\"token function\">grep<\/span> <span class=\"token parameter variable\">-c<\/span> <span class=\"token string\">&#039;^Z&#039;<\/span><\/p>\n<h4>4.4 \u50f5\u5c38\u8fdb\u7a0b\u6709\u591a\u5371\u9669<\/h4>\n<p>\u5355\u4e2a\u50f5\u5c38\u8fdb\u7a0b\u51e0\u4e4e\u6ca1\u6709\u5371\u9669\u3002\u95ee\u9898\u5728\u4e8e\u50f5\u5c38\u8fdb\u7a0b\u7684\u7d2f\u79ef\u3002<\/p>\n<p>\u5982\u679c\u4e00\u4e2a\u670d\u52a1\u6301\u7eed\u4ea7\u751f\u50f5\u5c38\u8fdb\u7a0b&#xff0c;\u968f\u7740\u65f6\u95f4\u63a8\u79fb&#xff0c;\u50f5\u5c38\u8fdb\u7a0b\u7684\u6570\u91cf\u4f1a\u4e0d\u65ad\u589e\u52a0&#xff0c;\u6700\u7ec8\u53ef\u80fd&#xff1a;<\/p>\n<li>\u8017\u5c3d PID \u7a7a\u95f4&#xff1a;\u7cfb\u7edf\u65e0\u6cd5 fork \u65b0\u8fdb\u7a0b&#xff0c;\u670d\u52a1\u65e0\u6cd5\u63a5\u53d7\u65b0\u8bf7\u6c42&#xff0c;\u751a\u81f3 SSH \u65b0\u8fde\u63a5\u4e5f\u65e0\u6cd5\u5efa\u7acb\u3002<\/li>\n<li>\u5f71\u54cd\u76d1\u63a7\u7cfb\u7edf&#xff1a;\u5f88\u591a\u76d1\u63a7\u5de5\u5177\u4f1a\u5bf9\u50f5\u5c38\u8fdb\u7a0b\u6570\u91cf\u8bbe\u7f6e\u544a\u8b66\u9608\u503c&#xff0c;\u5927\u91cf\u50f5\u5c38\u8fdb\u7a0b\u4f1a\u89e6\u53d1\u544a\u8b66\u3002<\/li>\n<li>\u63a9\u76d6\u771f\u5b9e\u95ee\u9898&#xff1a;\u5927\u91cf\u50f5\u5c38\u8fdb\u7a0b\u901a\u5e38\u610f\u5473\u7740\u7236\u8fdb\u7a0b\u7684\u4ee3\u7801\u5b58\u5728\u7f3a\u9677&#xff0c;\u8fd9\u4e2a\u7f3a\u9677\u672c\u8eab\u53ef\u80fd\u8fd8\u4f34\u968f\u7740\u5176\u4ed6\u66f4\u4e25\u91cd\u7684\u95ee\u9898&#xff08;\u5982\u5185\u5b58\u6cc4\u6f0f\u3001\u6587\u4ef6\u63cf\u8ff0\u7b26\u6cc4\u6f0f\u7b49&#xff09;\u3002<\/li>\n<p>\u5728\u6211\u7ecf\u5386\u8fc7\u7684\u4e00\u6b21\u7ebf\u4e0a\u4e8b\u6545\u4e2d&#xff0c;\u67d0\u4e2a Java \u670d\u52a1\u4f7f\u7528 Runtime.getRuntime().exec() \u9891\u7e41\u8c03\u7528\u5916\u90e8\u547d\u4ee4&#xff0c;\u4f46\u6ca1\u6709\u6b63\u786e\u8c03\u7528 Process.waitFor()&#xff0c;\u5bfc\u81f4\u5728\u9ad8\u5cf0\u671f\u6bcf\u5206\u949f\u4ea7\u751f\u6570\u5341\u4e2a\u50f5\u5c38\u8fdb\u7a0b&#xff0c;\u5927\u7ea6\u4e24\u4e2a\u5c0f\u65f6\u540e&#xff0c;PID \u7a7a\u95f4\u8017\u5c3d&#xff0c;\u6574\u53f0\u673a\u5668\u65e0\u6cd5\u521b\u5efa\u4efb\u4f55\u65b0\u8fdb\u7a0b&#xff0c;\u6700\u7ec8\u53ea\u80fd\u5f3a\u5236\u91cd\u542f\u670d\u52a1\u5668\u3002<\/p>\n<h4>4.5 \u5982\u4f55\u8bc6\u522b\u50f5\u5c38\u8fdb\u7a0b<\/h4>\n<p>\u8bc6\u522b\u50f5\u5c38\u8fdb\u7a0b\u4e3b\u8981\u4f9d\u9760 ps \u548c top \u547d\u4ee4&#xff0c;\u5177\u4f53\u65b9\u6cd5\u6211\u4eec\u653e\u5728\u6848\u4f8b\u4e09\u4e2d\u8be6\u7ec6\u6f14\u793a\u3002\u8fd9\u91cc\u5148\u7ed9\u51fa\u6700\u5feb\u901f\u7684\u4e24\u79cd\u65b9\u5f0f&#xff1a;<\/p>\n<p><span class=\"token comment\"># \u65b9\u5f0f\u4e00&#xff1a;\u7528 ps \u8fc7\u6ee4\u72b6\u6001\u4e3a Z \u7684\u8fdb\u7a0b<\/span><br \/>\n<span class=\"token comment\"># STAT \u5217\u4e2d\u4e3a Z \u7684&#xff0c;\u5c31\u662f\u50f5\u5c38\u8fdb\u7a0b<\/span><br \/>\n<span class=\"token function\">ps<\/span> aux <span class=\"token operator\">|<\/span> <span class=\"token function\">grep<\/span> <span class=\"token string\">&#039; Z &#039;<\/span><\/p>\n<p><span class=\"token comment\"># \u65b9\u5f0f\u4e8c&#xff1a;\u4f7f\u7528 top \u67e5\u770b\u50f5\u5c38\u8fdb\u7a0b\u603b\u6570<\/span><br \/>\n<span class=\"token comment\"># top \u754c\u9762\u7b2c\u4e00\u884c Tasks \u4fe1\u606f\u4e2d\u4f1a\u663e\u793a zombie \u6570\u91cf<\/span><br \/>\n<span class=\"token function\">top<\/span><\/p>\n<p>\u5728 top \u7684\u7b2c\u4e8c\u884c&#xff0c;\u4f60\u4f1a\u770b\u5230\u7c7b\u4f3c\u8fd9\u6837\u7684\u4fe1\u606f&#xff1a;<\/p>\n<p>Tasks: 214 total,   1 running, 213 sleeping,   0 stopped,   2 zombie<\/p>\n<p>\u6700\u540e\u7684 2 zombie \u5c31\u662f\u5f53\u524d\u7cfb\u7edf\u4e2d\u7684\u50f5\u5c38\u8fdb\u7a0b\u6570\u91cf\u3002<\/p>\n<h3>\u4e94\u3001\u5b64\u513f\u8fdb\u7a0b\u6df1\u5ea6\u89e3\u6790<\/h3>\n<h4>5.1 \u4ec0\u4e48\u662f\u5b64\u513f\u8fdb\u7a0b<\/h4>\n<p>\u5b64\u513f\u8fdb\u7a0b&#xff08;Orphan Process&#xff09;\u662f\u6307\u7236\u8fdb\u7a0b\u5df2\u7ecf\u9000\u51fa&#xff0c;\u4f46\u5176\u5b50\u8fdb\u7a0b\u4ecd\u7136\u5728\u8fd0\u884c\u7684\u8fdb\u7a0b\u3002<\/p>\n<p>\u8fd9\u4e2a\u540d\u5b57\u53d6\u5f97\u975e\u5e38\u5f62\u8c61\u2014\u2014\u7236\u8fdb\u7a0b\u6b7b\u4e86&#xff0c;\u5b50\u8fdb\u7a0b\u5c31\u6210\u4e86&#034;\u5b64\u513f&#034;\u3002<\/p>\n<p>\u5b64\u513f\u8fdb\u7a0b\u5728\u6982\u5ff5\u4e0a\u548c\u50f5\u5c38\u8fdb\u7a0b\u51e0\u4e4e\u662f&#034;\u955c\u50cf\u5173\u7cfb&#034;&#xff1a;<\/p>\n<ul>\n<li>\u50f5\u5c38\u8fdb\u7a0b&#xff1a;\u5b50\u8fdb\u7a0b\u5148\u6b7b&#xff0c;\u7236\u8fdb\u7a0b\u8fd8\u6d3b\u7740&#xff0c;\u4f46\u7236\u8fdb\u7a0b\u4e0d\u6765&#034;\u6536\u5c38&#034;<\/li>\n<li>\u5b64\u513f\u8fdb\u7a0b&#xff1a;\u7236\u8fdb\u7a0b\u5148\u6b7b&#xff0c;\u5b50\u8fdb\u7a0b\u8fd8\u6d3b\u7740<\/li>\n<\/ul>\n<p>\u8fd9\u4e24\u4e2a\u6982\u5ff5\u653e\u5728\u4e00\u8d77\u5bf9\u6bd4&#xff0c;\u5f88\u591a\u4eba\u624d\u80fd\u771f\u6b63\u7406\u89e3\u6e05\u695a\u3002<\/p>\n<h4>5.2 \u5b64\u513f\u8fdb\u7a0b\u7684\u4ea7\u751f\u573a\u666f<\/h4>\n<p>\u5b64\u513f\u8fdb\u7a0b\u7684\u4ea7\u751f\u975e\u5e38\u666e\u904d&#xff0c;\u5728\u65e5\u5e38 Linux \u4f7f\u7528\u4e2d\u968f\u65f6\u90fd\u5728\u53d1\u751f&#xff1a;<\/p>\n<p>\u573a\u666f\u4e00&#xff1a;\u7ec8\u7aef\u5173\u95ed&#xff0c;\u7a0b\u5e8f\u8fd8\u5728\u8fd0\u884c<\/p>\n<p>\u4f60\u5728\u7ec8\u7aef\u91cc\u8fd0\u884c\u4e86\u4e00\u4e2a\u9700\u8981\u957f\u65f6\u95f4\u6267\u884c\u7684\u547d\u4ee4&#xff08;\u6bd4\u5982 python train.py&#xff09;&#xff0c;\u7136\u540e\u76f4\u63a5\u5173\u95ed\u4e86\u7ec8\u7aef\u7a97\u53e3\u3002\u7ec8\u7aef\u8fdb\u7a0b\u9000\u51fa&#xff0c;\u4f46 Python \u8fdb\u7a0b\u53ef\u80fd\u4ecd\u7136\u5728\u8fd0\u884c&#xff08;\u53d6\u51b3\u4e8e\u4fe1\u53f7\u5904\u7406&#xff09;\u2014\u2014\u5982\u679c\u5b83\u6ca1\u6709\u88ab SIGHUP \u4fe1\u53f7\u6740\u6b7b&#xff0c;\u5c31\u4f1a\u6210\u4e3a\u5b64\u513f\u8fdb\u7a0b\u3002<\/p>\n<p>\u573a\u666f\u4e8c&#xff1a;\u811a\u672c\u4e2d\u7684\u540e\u53f0\u4efb\u52a1<\/p>\n<p>\u5728 Shell \u811a\u672c\u4e2d&#xff0c;\u7528 &amp; \u628a\u4e00\u4e2a\u8fdb\u7a0b\u653e\u5230\u540e\u53f0&#xff0c;\u7136\u540e\u811a\u672c\u672c\u8eab\u9000\u51fa&#xff0c;\u90a3\u4e2a\u540e\u53f0\u8fdb\u7a0b\u5c31\u6210\u4e86\u5b64\u513f\u8fdb\u7a0b\u3002<\/p>\n<p>\u573a\u666f\u4e09&#xff1a;\u5b88\u62a4\u8fdb\u7a0b\u7684\u6807\u51c6\u521b\u5efa\u65b9\u5f0f<\/p>\n<p>\u4f20\u7edf\u7684\u5b88\u62a4\u8fdb\u7a0b&#xff08;Daemon&#xff09;\u521b\u5efa\u65b9\u5f0f&#xff0c;\u5c31\u662f\u4e3b\u52a8\u5229\u7528\u5b64\u513f\u8fdb\u7a0b\u673a\u5236&#xff1a;\u7236\u8fdb\u7a0b fork \u51fa\u5b50\u8fdb\u7a0b\u540e\u7acb\u5373\u9000\u51fa&#xff0c;\u5b50\u8fdb\u7a0b\u6210\u4e3a\u5b64\u513f&#xff0c;\u88ab init \u63a5\u7ba1&#xff0c;\u4ece\u6b64\u5728\u540e\u53f0\u8fd0\u884c&#xff0c;\u4e0d\u53d7\u7ec8\u7aef\u5f71\u54cd\u3002\u8fd9\u662f\u4e00\u79cd\u6709\u610f\u4e3a\u4e4b\u7684\u3001\u6b63\u5e38\u7684\u884c\u4e3a\u3002<\/p>\n<p>\u573a\u666f\u56db&#xff1a;\u7236\u8fdb\u7a0b\u5d29\u6e83<\/p>\n<p>\u7236\u8fdb\u7a0b\u56e0\u4e3a BUG\u3001\u6bb5\u9519\u8bef\u6216\u88ab SIGKILL \u6740\u6b7b\u800c\u610f\u5916\u9000\u51fa&#xff0c;\u5176\u4ecd\u5728\u8fd0\u884c\u7684\u5b50\u8fdb\u7a0b\u6210\u4e3a\u5b64\u513f\u8fdb\u7a0b\u3002<\/p>\n<h4>5.3 init\/systemd \u5982\u4f55\u6536\u517b\u5b64\u513f\u8fdb\u7a0b<\/h4>\n<p>Linux \u5185\u6838\u5bf9\u5b64\u513f\u8fdb\u7a0b\u7684\u5904\u7406\u65b9\u5f0f\u975e\u5e38\u4f18\u96c5&#xff1a;\u5185\u6838\u4f1a\u81ea\u52a8\u5c06\u5b64\u513f\u8fdb\u7a0b\u7684\u7236\u8fdb\u7a0b\u91cd\u65b0\u8bbe\u7f6e\u4e3a PID 1&#xff08;init\/systemd&#xff09;&#xff0c;\u7531 systemd \u6765\u627f\u62c5\u7236\u8fdb\u7a0b\u7684\u804c\u8d23\u3002<\/p>\n<p>\u8fd9\u4e2a\u673a\u5236\u53eb\u505a re-parenting&#xff08;\u91cd\u65b0\u8ba4\u4eb2&#xff09;\u3002<\/p>\n<p>systemd \u4f5c\u4e3a\u6240\u6709\u5b64\u513f\u8fdb\u7a0b\u7684&#034;\u7ec8\u6781\u76d1\u62a4\u4eba&#034;&#xff0c;\u4f1a\u5728\u5b64\u513f\u8fdb\u7a0b\u9000\u51fa\u65f6\u6b63\u786e\u5730\u8c03\u7528 wait() \u6765\u56de\u6536\u5b83\u4eec\u3002\u56e0\u6b64&#xff0c;\u5b64\u513f\u8fdb\u7a0b\u672c\u8eab\u4e0d\u4f1a\u53d8\u6210\u50f5\u5c38\u8fdb\u7a0b\u2014\u2014\u5b83\u4eec\u6700\u7ec8\u90fd\u4f1a\u88ab systemd \u5e72\u51c0\u5730\u56de\u6536\u3002<\/p>\n<p>\u53ef\u4ee5\u7528\u4ee5\u4e0b\u547d\u4ee4\u9a8c\u8bc1&#xff1a;<\/p>\n<p><span class=\"token comment\"># \u67e5\u770b\u67d0\u4e2a\u8fdb\u7a0b\u7684\u7236\u8fdb\u7a0b ID&#xff08;PPID&#xff09;<\/span><br \/>\n<span class=\"token comment\"># \u5982\u679c PPID \u4e3a 1&#xff0c;\u8bf4\u660e\u8be5\u8fdb\u7a0b\u7684\u7236\u8fdb\u7a0b\u662f systemd<\/span><br \/>\n<span class=\"token function\">ps<\/span> <span class=\"token parameter variable\">-o<\/span> pid,ppid,comm <span class=\"token parameter variable\">-p<\/span> <span class=\"token operator\">&lt;<\/span>\u8fdb\u7a0bPID<span class=\"token operator\">&gt;<\/span><\/p>\n<p>\u5728 Ubuntu 22.04\/24.04 \u4e2d&#xff0c;PID 1 \u662f systemd&#xff0c;\u4f60\u53ef\u4ee5\u8fd9\u6837\u786e\u8ba4&#xff1a;<\/p>\n<p><span class=\"token comment\"># \u67e5\u770b PID 1 \u662f\u4ec0\u4e48\u8fdb\u7a0b<\/span><br \/>\n<span class=\"token function\">ps<\/span> <span class=\"token parameter variable\">-p<\/span> <span class=\"token number\">1<\/span> <span class=\"token parameter variable\">-o<\/span> pid,comm<\/p>\n<p>\u8f93\u51fa&#xff1a;<\/p>\n<p>  PID COMMAND<br \/>\n    1 systemd<\/p>\n<p>\u503c\u5f97\u4e00\u63d0\u7684\u662f&#xff0c;\u5728\u67d0\u4e9b\u7279\u6b8a\u60c5\u51b5\u4e0b&#xff08;\u4e3b\u8981\u662f\u5bb9\u5668\u73af\u5883&#xff09;&#xff0c;\u5b64\u513f\u8fdb\u7a0b\u53ef\u80fd\u4f1a\u88ab\u6258\u7ba1\u7ed9\u5b50\u5b50\u8fdb\u7a0b\u6536\u5272\u8005&#xff08;subreaper&#xff0c;\u901a\u8fc7 prctl(PR_SET_CHILD_SUBREAPER, &#8230;) \u8bbe\u7f6e\u7684\u8fdb\u7a0b&#xff09;&#xff0c;\u800c\u4e0d\u662f PID 1\u3002\u4f46\u5728\u5e38\u89c4 Ubuntu \u7cfb\u7edf\u4e2d&#xff0c;\u603b\u662f\u7531 systemd \u6765\u6536\u517b\u5b64\u513f\u8fdb\u7a0b\u3002<\/p>\n<h4>5.4 \u5b64\u513f\u8fdb\u7a0b\u662f\u5426\u6709\u5bb3<\/h4>\n<p>\u901a\u5e38\u60c5\u51b5\u4e0b&#xff0c;\u5b64\u513f\u8fdb\u7a0b\u662f\u65e0\u5bb3\u7684\u3002<\/p>\n<p>\u539f\u56e0\u5f88\u7b80\u5355&#xff1a;\u5b64\u513f\u8fdb\u7a0b\u88ab systemd \u63a5\u7ba1\u4e4b\u540e&#xff0c;\u5b83\u7684\u751f\u547d\u5468\u671f\u7ba1\u7406\u5c31\u7531 systemd \u8d1f\u8d23\u4e86\u3002systemd \u662f\u4e00\u4e2a\u8bbe\u8ba1\u4e25\u8c28\u3001\u5145\u5206\u6d4b\u8bd5\u8fc7\u7684 init \u7cfb\u7edf&#xff0c;\u5b83\u4e00\u5b9a\u4f1a\u5728\u5b64\u513f\u8fdb\u7a0b\u9000\u51fa\u65f6\u6b63\u786e\u56de\u6536\u5b83&#xff0c;\u4e0d\u4f1a\u4ea7\u751f\u50f5\u5c38\u8fdb\u7a0b\u3002<\/p>\n<p>\u5b64\u513f\u8fdb\u7a0b\u7ee7\u7eed\u6b63\u5e38\u8fd0\u884c&#xff0c;\u6d88\u8017\u5b83\u8be5\u6d88\u8017\u7684\u8d44\u6e90&#xff08;CPU\u3001\u5185\u5b58\u7b49&#xff09;&#xff0c;\u8fd9\u4e9b\u662f\u6b63\u5e38\u7684\u8d44\u6e90\u6d88\u8017\u3002\u5b64\u513f\u8fdb\u7a0b\u672c\u8eab\u4e0d\u4f1a&#034;\u6cc4\u6f0f&#034;\u8d44\u6e90&#xff0c;\u4e0d\u4f1a\u79ef\u7d2f\u5783\u573e\u2014\u2014\u5b83\u548c\u666e\u901a\u8fdb\u7a0b\u7684\u552f\u4e00\u533a\u522b\u5c31\u662f\u5b83\u7684\u7236\u8fdb\u7a0b\u662f systemd\u3002<\/p>\n<p>\u4f46\u5b64\u513f\u8fdb\u7a0b\u5728\u67d0\u4e9b\u573a\u666f\u4e0b\u9700\u8981\u8b66\u60d5&#xff1a;<\/p>\n<li>\u610f\u5916\u5b64\u513f&#xff1a;\u672c\u6765\u5e94\u8be5\u968f\u7236\u8fdb\u7a0b\u4e00\u8d77\u9000\u51fa\u7684\u5b50\u8fdb\u7a0b&#xff0c;\u56e0\u4e3a\u7236\u8fdb\u7a0b\u5d29\u6e83\u800c\u53d8\u6210\u4e86\u5b64\u513f&#xff0c;\u7ee7\u7eed\u5728\u540e\u53f0\u6d88\u8017\u8d44\u6e90\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b&#xff0c;\u5b64\u513f\u8fdb\u7a0b\u7684\u5b58\u5728\u672c\u8eab\u4e0d\u662f\u95ee\u9898&#xff0c;\u4f46\u5b83\u7684\u5b58\u5728\u610f\u5473\u7740\u4e4b\u524d\u6709\u5f02\u5e38\u5d29\u6e83\u53d1\u751f\u3002<\/li>\n<li>\u670d\u52a1\u964d\u7ea7&#xff1a;\u67d0\u4e2a\u670d\u52a1\u7684\u4e3b\u8fdb\u7a0b\u9000\u51fa\u540e&#xff0c;\u5176\u76d1\u63a7\u5b50\u8fdb\u7a0b\u6210\u4e3a\u5b64\u513f&#xff0c;\u7ee7\u7eed\u5411\u5916\u6c47\u62a5\u5065\u5eb7\u72b6\u6001&#xff0c;\u63a9\u76d6\u4e86\u4e3b\u670d\u52a1\u5df2\u7ecf\u5931\u8d25\u7684\u4e8b\u5b9e\u3002<\/li>\n<li>\u6743\u9650\u9057\u7559&#xff1a;\u7236\u8fdb\u7a0b\u4ee5\u9ad8\u6743\u9650\u8fd0\u884c\u4e86\u5b50\u8fdb\u7a0b&#xff0c;\u7236\u8fdb\u7a0b\u9000\u51fa\u540e&#xff0c;\u9ad8\u6743\u9650\u5b50\u8fdb\u7a0b\u6210\u4e3a\u5b64\u513f\u7ee7\u7eed\u8fd0\u884c&#xff0c;\u53ef\u80fd\u6210\u4e3a\u5b89\u5168\u9690\u60a3\u3002<\/li>\n<h3>\u516d\u3001\u50f5\u5c38\u8fdb\u7a0b\u4e0e\u5b64\u513f\u8fdb\u7a0b\u7684\u672c\u8d28\u533a\u522b<\/h3>\n<p>\u628a\u8fd9\u4e24\u4e2a\u6982\u5ff5\u5e76\u6392\u5bf9\u6bd4&#xff0c;\u4f1a\u66f4\u6e05\u6670&#xff1a;<\/p>\n<table>\n<tr>\u7279\u5f81\u50f5\u5c38\u8fdb\u7a0b\u5b64\u513f\u8fdb\u7a0b<\/tr>\n<tbody>\n<tr>\n<td>\u5b9a\u4e49<\/td>\n<td>\u5b50\u8fdb\u7a0b\u5df2\u9000\u51fa&#xff0c;\u7236\u8fdb\u7a0b\u672a\u56de\u6536<\/td>\n<td>\u7236\u8fdb\u7a0b\u5df2\u9000\u51fa&#xff0c;\u5b50\u8fdb\u7a0b\u4ecd\u8fd0\u884c<\/td>\n<\/tr>\n<tr>\n<td>\u8fdb\u7a0b\u72b6\u6001<\/td>\n<td>Z&#xff08;Zombie&#xff09;&#xff0c;\u5df2\u7ecf\u6b7b\u4ea1<\/td>\n<td>\u6b63\u5e38\u8fd0\u884c\u72b6\u6001&#xff08;R\/S\/D\u7b49&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u662f\u5426\u5360\u7528 CPU\/\u5185\u5b58<\/td>\n<td>\u5426&#xff08;\u8d44\u6e90\u5df2\u91ca\u653e&#xff09;<\/td>\n<td>\u662f&#xff08;\u6b63\u5e38\u8fd0\u884c\u4e2d&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u5360\u7528 PID<\/td>\n<td>\u662f&#xff08;PID \u672a\u91ca\u653e&#xff09;<\/td>\n<td>\u662f&#xff08;PID \u6b63\u5e38\u5360\u7528&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u5371\u9669\u6027<\/td>\n<td>\u79ef\u7d2f\u540e\u53ef\u80fd\u8017\u5c3d PID<\/td>\n<td>\u901a\u5e38\u65e0\u5bb3<\/td>\n<\/tr>\n<tr>\n<td>\u5185\u6838\u5904\u7406<\/td>\n<td>\u7b49\u5f85\u7236\u8fdb\u7a0b\u8c03\u7528 wait()<\/td>\n<td>\u88ab systemd \u63a5\u7ba1&#xff0c;\u6b63\u5e38\u56de\u6536<\/td>\n<\/tr>\n<tr>\n<td>\u80fd\u5426\u88ab kill \u6740\u6b7b<\/td>\n<td>\u4e0d\u80fd&#xff08;\u5df2\u7ecf\u6b7b\u4e86&#xff09;<\/td>\n<td>\u53ef\u4ee5\u6b63\u5e38 kill<\/td>\n<\/tr>\n<tr>\n<td>\u539f\u56e0<\/td>\n<td>\u7236\u8fdb\u7a0b\u4ee3\u7801\u7f3a\u9677<\/td>\n<td>\u7236\u8fdb\u7a0b\u5148\u9000\u51fa&#xff0c;\u5b50\u8fdb\u7a0b\u7ee7\u7eed\u8fd0\u884c<\/td>\n<\/tr>\n<tr>\n<td>\u53ef\u80fd\u4e92\u76f8\u8f6c\u5316\u5417<\/td>\n<td>\u5b64\u513f\u8fdb\u7a0b\u9000\u51fa\u540e\u4f1a\u6210\u4e3a\u50f5\u5c38\u5417&#xff1f;\u4e0d\u4f1a&#xff0c;systemd \u4f1a\u6b63\u786e\u56de\u6536<\/td>\n<td>\u50f5\u5c38\u8fdb\u7a0b\u7684\u7236\u8fdb\u7a0b\u9000\u51fa\u540e&#xff0c;\u50f5\u5c38\u4f1a\u88ab systemd \u6b63\u786e\u56de\u6536<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u6700\u540e\u4e00\u884c\u7279\u522b\u91cd\u8981&#xff1a;\u5982\u679c\u50f5\u5c38\u8fdb\u7a0b\u7684\u7236\u8fdb\u7a0b\u9000\u51fa\u4e86&#xff0c;\u50f5\u5c38\u8fdb\u7a0b\u4f1a\u88ab systemd \u63a5\u7ba1\u5e76\u7acb\u5373\u56de\u6536\u3002\u8fd9\u662f\u5904\u7406\u50f5\u5c38\u8fdb\u7a0b\u7684\u4e00\u4e2a\u5e38\u7528\u624b\u6bb5&#xff0c;\u6211\u4eec\u5728\u6848\u4f8b\u4e09\u4e2d\u4f1a\u8be6\u7ec6\u8bb2\u3002<\/p>\n<h3>\u4e03\u3001\u5b9e\u9a8c\u73af\u5883\u51c6\u5907<\/h3>\n<p>\u672c\u6587\u7684\u5b9e\u9a8c\u90fd\u53ef\u4ee5\u5728 Ubuntu 22.04 LTS \u6216 Ubuntu 24.04 LTS \u4e0a\u8fd0\u884c&#xff0c;\u684c\u9762\u7248\u548c\u670d\u52a1\u5668\u7248\u5747\u53ef\u3002\u6211\u4eec\u9700\u8981\u7528\u5230 C \u8bed\u8a00\u7f16\u8bd1\u5668\u548c Python&#xff0c;\u8fd9\u4e9b\u5728 Ubuntu \u4e0a\u5f88\u5bb9\u6613\u5b89\u88c5\u3002<\/p>\n<p><span class=\"token comment\"># \u67e5\u770b\u5f53\u524d\u7cfb\u7edf\u7248\u672c&#xff0c;\u786e\u8ba4\u662f Ubuntu 22.04 \u6216 24.04<\/span><br \/>\nlsb_release <span class=\"token parameter variable\">-a<\/span><\/p>\n<p>\u9884\u671f\u8f93\u51fa&#xff08;22.04 \u4e3a\u4f8b&#xff09;&#xff1a;<\/p>\n<p>No LSB modules are available.<br \/>\nDistributor ID: Ubuntu<br \/>\nDescription:    Ubuntu 22.04.4 LTS<br \/>\nRelease:        22.04<br \/>\nCodename:       jammy<\/p>\n<p><span class=\"token comment\"># \u66f4\u65b0\u8f6f\u4ef6\u6e90\u7d22\u5f15&#xff0c;\u786e\u4fdd\u5b89\u88c5\u7684\u662f\u6700\u65b0\u7248\u672c<\/span><br \/>\n<span class=\"token function\">sudo<\/span> <span class=\"token function\">apt<\/span> update<\/p>\n<p><span class=\"token comment\"># \u5b89\u88c5 C \u8bed\u8a00\u7f16\u8bd1\u5668&#xff08;gcc&#xff09;\u3001Python 3\u3001\u4ee5\u53ca\u5e38\u7528\u5de5\u5177<\/span><br \/>\n<span class=\"token function\">sudo<\/span> <span class=\"token function\">apt<\/span> <span class=\"token function\">install<\/span> <span class=\"token parameter variable\">-y<\/span> gcc python3 psmisc procps <span class=\"token function\">htop<\/span><\/p>\n<p>\u5b89\u88c5\u5b8c\u6210\u540e\u9a8c\u8bc1&#xff1a;<\/p>\n<p><span class=\"token comment\"># \u9a8c\u8bc1 gcc \u7248\u672c<\/span><br \/>\ngcc <span class=\"token parameter variable\">&#8211;version<\/span><br \/>\n<span class=\"token comment\"># \u9884\u671f\u8f93\u51fa\u7c7b\u4f3c&#xff1a;gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0<\/span><\/p>\n<p><span class=\"token comment\"># \u9a8c\u8bc1 Python3 \u7248\u672c<\/span><br \/>\npython3 <span class=\"token parameter variable\">&#8211;version<\/span><br \/>\n<span class=\"token comment\"># \u9884\u671f\u8f93\u51fa\u7c7b\u4f3c&#xff1a;Python 3.10.12<\/span><\/p>\n<p><span class=\"token comment\"># \u9a8c\u8bc1 pstree \u53ef\u7528&#xff08;\u6765\u81ea psmisc \u5305&#xff09;<\/span><br \/>\npstree <span class=\"token parameter variable\">&#8211;version<\/span><br \/>\n<span class=\"token comment\"># \u9884\u671f\u8f93\u51fa\u7c7b\u4f3c&#xff1a;pstree (PSmisc) 23.4<\/span><\/p>\n<p>\u6211\u4eec\u8fd8\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u5de5\u4f5c\u76ee\u5f55\u6765\u5b58\u653e\u5b9e\u9a8c\u4ee3\u7801&#xff1a;<\/p>\n<p><span class=\"token comment\"># \u521b\u5efa\u5b9e\u9a8c\u76ee\u5f55<\/span><br \/>\n<span class=\"token function\">mkdir<\/span> <span class=\"token parameter variable\">-p<\/span> ~\/zombie_orphan_lab<br \/>\n<span class=\"token builtin class-name\">cd<\/span> ~\/zombie_orphan_lab<\/p>\n<h3>\u516b\u3001\u6848\u4f8b\u4e00&#xff1a;\u4eb2\u624b\u5236\u9020\u4e00\u4e2a\u50f5\u5c38\u8fdb\u7a0b<\/h3>\n<p>\u7406\u8bba\u8bb2\u4e86\u8fd9\u4e48\u591a&#xff0c;\u4e0d\u5982\u4eb2\u624b\u9020\u4e00\u4e2a\u50f5\u5c38\u8fdb\u7a0b\u6765\u770b\u770b&#xff0c;\u8fd9\u6bd4\u4efb\u4f55\u6587\u5b57\u63cf\u8ff0\u90fd\u8981\u76f4\u89c2\u3002<\/p>\n<h4>8.1 \u7f16\u5199\u4ea7\u751f\u50f5\u5c38\u8fdb\u7a0b\u7684 C \u7a0b\u5e8f<\/h4>\n<p>\u6211\u4eec\u5199\u4e00\u4e2a\u7b80\u5355\u7684 C \u7a0b\u5e8f&#xff1a;\u7236\u8fdb\u7a0b fork \u51fa\u4e00\u4e2a\u5b50\u8fdb\u7a0b&#xff0c;\u5b50\u8fdb\u7a0b\u7acb\u5373\u9000\u51fa&#xff0c;\u800c\u7236\u8fdb\u7a0b\u6545\u610f\u7761\u7720 60 \u79d2\u4e0d\u8c03\u7528 wait()&#xff0c;\u4ece\u800c\u8ba9\u5b50\u8fdb\u7a0b\u7684\u9000\u51fa\u72b6\u6001\u65e0\u4eba\u56de\u6536&#xff0c;\u4ea7\u751f\u4e00\u4e2a\u50f5\u5c38\u8fdb\u7a0b\u3002<\/p>\n<p><span class=\"token comment\"># \u8fdb\u5165\u5b9e\u9a8c\u76ee\u5f55<\/span><br \/>\n<span class=\"token builtin class-name\">cd<\/span> ~\/zombie_orphan_lab<\/p>\n<p><span class=\"token comment\"># \u7528 nano \u521b\u5efa\u6e90\u6587\u4ef6&#xff08;\u4f60\u4e5f\u53ef\u4ee5\u7528 vim&#xff09;<\/span><br \/>\n<span class=\"token function\">nano<\/span> zombie_demo.c<\/p>\n<p>\u5728 zombie_demo.c \u4e2d\u8f93\u5165\u4ee5\u4e0b\u5185\u5bb9&#xff1a;<\/p>\n<p><span class=\"token comment\">\/*<br \/>\n * zombie_demo.c<br \/>\n * \u6f14\u793a\u50f5\u5c38\u8fdb\u7a0b\u7684\u4ea7\u751f&#xff1a;\u7236\u8fdb\u7a0b\u4e0d\u8c03\u7528 wait()&#xff0c;\u5b50\u8fdb\u7a0b\u9000\u51fa\u540e\u53d8\u6210\u50f5\u5c38<br \/>\n *\/<\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;stdio.h&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;stdlib.h&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;unistd.h&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;sys\/types.h&gt;<\/span><\/span><\/p>\n<p><span class=\"token keyword\">int<\/span> <span class=\"token function\">main<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token class-name\">pid_t<\/span> child_pid<span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token function\">printf<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;[\u7236\u8fdb\u7a0b] PID: %d&#xff0c;\u5373\u5c06 fork \u5b50\u8fdb\u7a0b\\\\n&#034;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token function\">getpid<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    child_pid <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">fork<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>  <span class=\"token comment\">\/\/ \u521b\u5efa\u5b50\u8fdb\u7a0b<\/span><\/p>\n<p>    <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>child_pid <span class=\"token operator\">&lt;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token comment\">\/\/ fork \u5931\u8d25<\/span><br \/>\n        <span class=\"token function\">perror<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;fork \u5931\u8d25&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token function\">exit<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>child_pid <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u5b50\u8fdb\u7a0b\u5206\u652f<\/span><br \/>\n        <span class=\"token function\">printf<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;[\u5b50\u8fdb\u7a0b] PID: %d&#xff0c;PPID: %d&#xff0c;\u5373\u5c06\u9000\u51fa\\\\n&#034;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token function\">getpid<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token function\">getppid<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u5b50\u8fdb\u7a0b\u7acb\u5373\u9000\u51fa&#xff0c;\u9000\u51fa\u7801\u4e3a 0<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u6b64\u65f6\u5b50\u8fdb\u7a0b\u7684\u8fdb\u7a0b\u63cf\u8ff0\u7b26\u8fd8\u5728&#xff0c;\u7b49\u5f85\u7236\u8fdb\u7a0b\u8c03\u7528 wait() \u56de\u6536<\/span><br \/>\n        <span class=\"token function\">exit<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">else<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u7236\u8fdb\u7a0b\u5206\u652f<\/span><br \/>\n        <span class=\"token function\">printf<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;[\u7236\u8fdb\u7a0b] fork \u6210\u529f&#xff0c;\u5b50\u8fdb\u7a0b PID: %d\\\\n&#034;<\/span><span class=\"token punctuation\">,<\/span> child_pid<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token function\">printf<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;[\u7236\u8fdb\u7a0b] \u6545\u610f\u4e0d\u8c03\u7528 wait()&#xff0c;\u7761\u7720 60 \u79d2&#8230;\\\\n&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token function\">printf<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;[\u7236\u8fdb\u7a0b] \u8bf7\u5728\u53e6\u4e00\u4e2a\u7ec8\u7aef\u8fd0\u884c: ps aux | grep Z\\\\n&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u6545\u610f\u7761\u7720 60 \u79d2&#xff0c;\u671f\u95f4\u4e0d\u8c03\u7528 wait()<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u5b50\u8fdb\u7a0b\u5728\u8fd9\u6bb5\u65f6\u95f4\u5185\u4f1a\u4ee5\u50f5\u5c38\u72b6\u6001\u5b58\u5728<\/span><br \/>\n        <span class=\"token function\">sleep<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">60<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token function\">printf<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;[\u7236\u8fdb\u7a0b] 60 \u79d2\u7ed3\u675f&#xff0c;\u9000\u51fa\u3002\u7236\u8fdb\u7a0b\u9000\u51fa\u540e\u50f5\u5c38\u8fdb\u7a0b\u5c06\u88ab systemd \u63a5\u7ba1\u5e76\u56de\u6536\\\\n&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    <span class=\"token keyword\">return<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>\u4fdd\u5b58\u9000\u51fa&#xff08;nano \u4e2d\u6309 Ctrl&#043;O \u4fdd\u5b58&#xff0c;Ctrl&#043;X \u9000\u51fa&#xff09;\u3002<\/p>\n<p><span class=\"token comment\"># \u7f16\u8bd1 C \u7a0b\u5e8f<\/span><br \/>\ngcc <span class=\"token parameter variable\">-o<\/span> zombie_demo zombie_demo.c<\/p>\n<p><span class=\"token comment\"># \u786e\u8ba4\u7f16\u8bd1\u6210\u529f<\/span><br \/>\n<span class=\"token function\">ls<\/span> <span class=\"token parameter variable\">-la<\/span> zombie_demo<\/p>\n<h4>8.2 \u89c2\u5bdf\u50f5\u5c38\u8fdb\u7a0b<\/h4>\n<p>\u63a5\u4e0b\u6765\u6211\u4eec\u9700\u8981\u4e24\u4e2a\u7ec8\u7aef\u7a97\u53e3&#xff1a;\u4e00\u4e2a\u8fd0\u884c\u7a0b\u5e8f&#xff0c;\u4e00\u4e2a\u89c2\u5bdf\u3002<\/p>\n<p>\u5728\u7ec8\u7aef 1 \u4e2d\u8fd0\u884c\u7a0b\u5e8f&#xff1a;<\/p>\n<p><span class=\"token builtin class-name\">cd<\/span> ~\/zombie_orphan_lab<br \/>\n.\/zombie_demo<\/p>\n<p>\u4f60\u4f1a\u770b\u5230\u8f93\u51fa\u7c7b\u4f3c&#xff1a;<\/p>\n<p>[\u7236\u8fdb\u7a0b] PID: 12345&#xff0c;\u5373\u5c06 fork \u5b50\u8fdb\u7a0b<br \/>\n[\u7236\u8fdb\u7a0b] fork \u6210\u529f&#xff0c;\u5b50\u8fdb\u7a0b PID: 12346<br \/>\n[\u7236\u8fdb\u7a0b] \u6545\u610f\u4e0d\u8c03\u7528 wait()&#xff0c;\u7761\u7720 60 \u79d2&#8230;<br \/>\n[\u7236\u8fdb\u7a0b] \u8bf7\u5728\u53e6\u4e00\u4e2a\u7ec8\u7aef\u8fd0\u884c: ps aux | grep Z<br \/>\n[\u5b50\u8fdb\u7a0b] PID: 12346&#xff0c;PPID: 12345&#xff0c;\u5373\u5c06\u9000\u51fa<\/p>\n<p>\u5728\u7ec8\u7aef 2 \u4e2d\u89c2\u5bdf\u50f5\u5c38\u8fdb\u7a0b&#xff1a;<\/p>\n<p><span class=\"token comment\"># \u65b9\u6cd5\u4e00&#xff1a;\u7528 ps \u67e5\u627e\u72b6\u6001\u4e3a Z \u7684\u8fdb\u7a0b<\/span><br \/>\n<span class=\"token comment\"># STAT \u5217\u663e\u793a Z \u6216 Zs \u7684\u5c31\u662f\u50f5\u5c38\u8fdb\u7a0b<\/span><br \/>\n<span class=\"token function\">ps<\/span> aux <span class=\"token operator\">|<\/span> <span class=\"token function\">grep<\/span> <span class=\"token string\">&#039;Z&#039;<\/span><\/p>\n<p>\u4f60\u4f1a\u770b\u5230\u7c7b\u4f3c&#xff1a;<\/p>\n<p>user     12346  0.0  0.0      0     0 pts\/0    Z&#043;   10:32   0:00 [zombie_demo] &lt;defunct&gt;<\/p>\n<p>\u6ce8\u610f\u51e0\u4e2a\u5173\u952e\u70b9&#xff1a;<\/p>\n<ul>\n<li>Z&#043; \u5c31\u662f\u50f5\u5c38\u72b6\u6001&#xff08;Z \u52a0\u4e0a &#043; \u8868\u793a\u5728\u524d\u53f0\u8fdb\u7a0b\u7ec4\u4e2d&#xff09;<\/li>\n<li>&lt;defunct&gt; \u8868\u793a\u8be5\u8fdb\u7a0b\u5df2\u7ecf\u5931\u6548&#xff08;defunct \u7684\u82f1\u6587\u610f\u601d\u5c31\u662f&#034;\u5df2\u6545\u7684\u3001\u4e0d\u518d\u5b58\u5728\u7684&#034;&#xff09;<\/li>\n<li>CPU \u65f6\u95f4\u4e3a 0:00&#xff0c;\u8bf4\u660e\u5b83\u4e0d\u6d88\u8017 CPU<\/li>\n<li>\u5185\u5b58\u5b57\u6bb5&#xff08;RSS&#xff09;\u4e3a 0&#xff0c;\u8bf4\u660e\u4e0d\u5360\u7528\u7269\u7406\u5185\u5b58<\/li>\n<\/ul>\n<p><span class=\"token comment\"># \u65b9\u6cd5\u4e8c&#xff1a;\u7528 ps \u4ee5\u66f4\u53cb\u597d\u7684\u683c\u5f0f\u67e5\u770b&#xff0c;\u663e\u793a PID \u548c PPID<\/span><br \/>\n<span class=\"token function\">ps<\/span> <span class=\"token parameter variable\">-eo<\/span> pid,ppid,stat,comm <span class=\"token operator\">|<\/span> <span class=\"token function\">grep<\/span> Z<\/p>\n<p>\u8f93\u51fa&#xff1a;<\/p>\n<p>  PID   PPID STAT COMMAND<br \/>\n12346  12345 Z&#043;   zombie_demo<\/p>\n<p>\u4ece\u8fd9\u91cc\u4f60\u80fd\u6e05\u695a\u770b\u5230&#xff1a;\u50f5\u5c38\u8fdb\u7a0b&#xff08;12346&#xff09;\u7684\u7236\u8fdb\u7a0b\u662f&#xff08;12345&#xff09;&#xff0c;\u4e5f\u5c31\u662f\u6211\u4eec\u90a3\u4e2a\u6545\u610f\u7761\u7720\u7684\u7236\u8fdb\u7a0b\u3002<\/p>\n<p><span class=\"token comment\"># \u65b9\u6cd5\u4e09&#xff1a;\u7528 top \u67e5\u770b\u50f5\u5c38\u8fdb\u7a0b\u6570\u91cf<\/span><br \/>\n<span class=\"token comment\"># top \u754c\u9762\u7684 Tasks \u884c\u4f1a\u663e\u793a zombie \u6570\u91cf<\/span><br \/>\n<span class=\"token function\">top<\/span> <span class=\"token parameter variable\">-b<\/span> <span class=\"token parameter variable\">-n<\/span> <span class=\"token number\">1<\/span> <span class=\"token operator\">|<\/span> <span class=\"token function\">head<\/span> <span class=\"token parameter variable\">-5<\/span><\/p>\n<p>\u8f93\u51fa&#xff08;\u5173\u6ce8 zombie \u5b57\u6bb5&#xff09;&#xff1a;<\/p>\n<p>Tasks: 215 total,   1 running, 214 sleeping,   0 stopped,   1 zombie<\/p>\n<p><span class=\"token comment\"># \u65b9\u6cd5\u56db&#xff1a;\u67e5\u770b \/proc \u6587\u4ef6\u7cfb\u7edf\u4e2d\u50f5\u5c38\u8fdb\u7a0b\u7684\u4fe1\u606f<\/span><br \/>\n<span class=\"token comment\"># \/proc\/&lt;PID&gt;\/status \u5305\u542b\u8fdb\u7a0b\u7684\u8be6\u7ec6\u72b6\u6001<\/span><br \/>\n<span class=\"token function\">cat<\/span> \/proc\/12346\/status<\/p>\n<p>\u8f93\u51fa\u4e2d\u4f60\u4f1a\u770b\u5230&#xff1a;<\/p>\n<p>Name:   zombie_demo<br \/>\nUmask:  0022<br \/>\nState:  Z (zombie)<br \/>\nTgid:   12346<br \/>\nNgid:   0<br \/>\nPid:    12346<br \/>\nPPid:   12345<br \/>\n&#8230;<br \/>\nVmPeak: (\u7a7a\u767d&#xff0c;\u56e0\u4e3a\u8d44\u6e90\u5df2\u91ca\u653e)<\/p>\n<p>\u73b0\u5728\u7b49\u5f85 60 \u79d2&#xff0c;\u7236\u8fdb\u7a0b\u9000\u51fa\u540e&#xff0c;\u5728\u7ec8\u7aef 2 \u4e2d\u518d\u6b21\u8fd0\u884c&#xff1a;<\/p>\n<p><span class=\"token comment\"># \u786e\u8ba4\u50f5\u5c38\u8fdb\u7a0b\u5df2\u7ecf\u88ab\u56de\u6536<\/span><br \/>\n<span class=\"token function\">ps<\/span> aux <span class=\"token operator\">|<\/span> <span class=\"token function\">grep<\/span> <span class=\"token string\">&#039;Z&#039;<\/span><br \/>\n<span class=\"token comment\"># \u6b64\u65f6\u5e94\u8be5\u6ca1\u6709\u8f93\u51fa&#xff0c;\u8bf4\u660e\u7236\u8fdb\u7a0b\u9000\u51fa\u540e\u50f5\u5c38\u8fdb\u7a0b\u88ab systemd \u63a5\u7ba1\u5e76\u56de\u6536\u4e86<\/span><\/p>\n<p>\u8fd9\u4e2a\u5b9e\u9a8c\u5b8c\u6574\u5730\u5c55\u793a\u4e86\u50f5\u5c38\u8fdb\u7a0b\u7684\u5b8c\u6574\u751f\u547d\u5468\u671f&#xff1a;\u521b\u5efa \u2192 \u5b50\u8fdb\u7a0b\u9000\u51fa \u2192 \u53d8\u4e3a\u50f5\u5c38 \u2192 \u7236\u8fdb\u7a0b\u9000\u51fa \u2192 \u88ab systemd \u56de\u6536\u3002<\/p>\n<h4>8.3 \u547d\u4ee4\u89e3\u6790<\/h4>\n<p>ps aux | grep &#039;Z&#039;<\/p>\n<p>\u8fd9\u662f\u6700\u76f4\u63a5\u7684\u50f5\u5c38\u8fdb\u7a0b\u67e5\u627e\u547d\u4ee4\u3002ps aux \u5217\u51fa\u7cfb\u7edf\u4e2d\u6240\u6709\u7528\u6237\u7684\u6240\u6709\u8fdb\u7a0b&#xff0c;aux \u4e2d\u7684 a \u8868\u793a\u663e\u793a\u6240\u6709\u7528\u6237\u7684\u8fdb\u7a0b&#xff0c;u \u8868\u793a\u4ee5\u7528\u6237\u53cb\u597d\u683c\u5f0f\u663e\u793a&#xff0c;x \u8868\u793a\u5305\u542b\u6ca1\u6709\u63a7\u5236\u7ec8\u7aef\u7684\u8fdb\u7a0b\u3002\u901a\u8fc7 grep &#039;Z&#039; \u8fc7\u6ee4&#xff0c;\u6211\u4eec\u627e\u5230 STAT \u5217\u5305\u542b Z \u7684\u8fdb\u7a0b\u3002<\/p>\n<p>\u4f46\u8fd9\u4e2a\u547d\u4ee4\u6709\u4e00\u4e2a\u5c0f\u95ee\u9898&#xff1a;grep &#039;Z&#039; \u8fd9\u4e2a\u5339\u914d\u6a21\u5f0f\u592a\u5bbd\u677e\u4e86&#xff0c;\u53ef\u80fd\u4f1a\u5339\u914d\u5230\u5f88\u591a\u4e0d\u76f8\u5173\u7684\u884c&#xff08;\u6bd4\u5982\u8fdb\u7a0b\u540d\u4e2d\u5305\u542b Z \u7684\u8fdb\u7a0b&#xff09;\u3002\u66f4\u7cbe\u786e\u7684\u7528\u6cd5\u662f&#xff1a;<\/p>\n<p><span class=\"token comment\"># \u66f4\u7cbe\u786e\u5730\u5339\u914d&#xff1a;STAT \u5b57\u6bb5\u7b2c\u4e00\u4e2a\u5b57\u7b26\u4e3a Z<\/span><br \/>\n<span class=\"token function\">ps<\/span> aux <span class=\"token operator\">|<\/span> <span class=\"token function\">awk<\/span> <span class=\"token string\">&#039;$8 ~ \/^Z\/&#039;<\/span><\/p>\n<p>cat \/proc\/&lt;PID&gt;\/status<\/p>\n<p>\/proc \u662f Linux \u7684&#034;\u4f2a\u6587\u4ef6\u7cfb\u7edf&#034;&#xff0c;\u5b83\u4e0d\u662f\u771f\u6b63\u5b58\u50a8\u5728\u78c1\u76d8\u4e0a\u7684\u6587\u4ef6\u7cfb\u7edf&#xff0c;\u800c\u662f\u5185\u6838\u5728\u5185\u5b58\u4e2d\u7ef4\u62a4\u7684\u4e00\u4e2a\u63a5\u53e3&#xff0c;\u901a\u8fc7\u5b83\u53ef\u4ee5\u8bfb\u53d6\u5185\u6838\u7684\u5b9e\u65f6\u6570\u636e\u3002\u6bcf\u4e2a\u8fdb\u7a0b\u5728 \/proc \u4e0b\u90fd\u6709\u4e00\u4e2a\u4ee5\u5176 PID \u547d\u540d\u7684\u76ee\u5f55&#xff0c;\u5176\u4e2d status \u6587\u4ef6\u5305\u542b\u4e86\u8be5\u8fdb\u7a0b\u7684\u8be6\u7ec6\u72b6\u6001\u4fe1\u606f\u3002\u8fd9\u5bf9\u4e8e\u6df1\u5165\u4e86\u89e3\u50f5\u5c38\u8fdb\u7a0b\u7684\u6b8b\u7559\u4fe1\u606f\u975e\u5e38\u6709\u7528\u3002<\/p>\n<p>top -b -n 1<\/p>\n<p>-b \u8868\u793a\u4ee5\u6279\u5904\u7406\u6a21\u5f0f\u8fd0\u884c&#xff08;\u4e0d\u662f\u4ea4\u4e92\u5f0f&#xff09;&#xff0c;-n 1 \u8868\u793a\u53ea\u5237\u65b0\u4e00\u6b21\u5c31\u9000\u51fa\u3002\u8fd9\u79cd\u7528\u6cd5\u5e38\u89c1\u4e8e\u811a\u672c\u4e2d&#xff0c;\u7528\u4e8e\u6355\u83b7\u67d0\u4e00\u65f6\u523b\u7684 top \u5feb\u7167\u3002<\/p>\n<h3>\u4e5d\u3001\u6848\u4f8b\u4e8c&#xff1a;\u4eb2\u624b\u5236\u9020\u4e00\u4e2a\u5b64\u513f\u8fdb\u7a0b<\/h3>\n<h4>9.1 \u7f16\u5199\u4ea7\u751f\u5b64\u513f\u8fdb\u7a0b\u7684 C \u7a0b\u5e8f<\/h4>\n<p>\u8fd9\u6b21\u6211\u4eec\u53cd\u8fc7\u6765&#xff1a;\u5b50\u8fdb\u7a0b\u6301\u7eed\u8fd0\u884c&#xff0c;\u7236\u8fdb\u7a0b\u7acb\u5373\u9000\u51fa\u3002<\/p>\n<p><span class=\"token builtin class-name\">cd<\/span> ~\/zombie_orphan_lab<br \/>\n<span class=\"token function\">nano<\/span> orphan_demo.c<\/p>\n<p><span class=\"token comment\">\/*<br \/>\n * orphan_demo.c<br \/>\n * \u6f14\u793a\u5b64\u513f\u8fdb\u7a0b&#xff1a;\u7236\u8fdb\u7a0b\u5148\u9000\u51fa&#xff0c;\u5b50\u8fdb\u7a0b\u7ee7\u7eed\u8fd0\u884c<br \/>\n * \u5b50\u8fdb\u7a0b\u6210\u4e3a\u5b64\u513f\u540e&#xff0c;\u4f1a\u88ab systemd&#xff08;PID 1&#xff09;\u63a5\u7ba1<br \/>\n *\/<\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;stdio.h&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;stdlib.h&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;unistd.h&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;sys\/types.h&gt;<\/span><\/span><\/p>\n<p><span class=\"token keyword\">int<\/span> <span class=\"token function\">main<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token class-name\">pid_t<\/span> child_pid<span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token function\">printf<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;[\u7236\u8fdb\u7a0b] PID: %d&#xff0c;\u5373\u5c06 fork \u5b50\u8fdb\u7a0b\\\\n&#034;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token function\">getpid<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    child_pid <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">fork<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>child_pid <span class=\"token operator\">&lt;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token function\">perror<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;fork \u5931\u8d25&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token function\">exit<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>child_pid <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u5b50\u8fdb\u7a0b\u5206\u652f&#xff1a;\u7236\u8fdb\u7a0b\u9000\u51fa\u540e&#xff0c;\u6211\u4eec\u89c2\u5bdf PPID \u7684\u53d8\u5316<\/span><br \/>\n        <span class=\"token function\">printf<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;[\u5b50\u8fdb\u7a0b] PID: %d&#xff0c;\u521d\u59cb PPID: %d\\\\n&#034;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token function\">getpid<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token function\">getppid<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token function\">printf<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;[\u5b50\u8fdb\u7a0b] \u6211\u5c06\u6bcf\u9694 5 \u79d2\u6253\u5370\u4e00\u6b21\u81ea\u5df1\u7684 PPID&#xff0c;\u89c2\u5bdf\u53d8\u5316\\\\n&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>        <span class=\"token comment\">\/\/ \u5b50\u8fdb\u7a0b\u6301\u7eed\u8fd0\u884c&#xff0c;\u6bcf5\u79d2\u6253\u5370\u4e00\u6b21 PPID<\/span><br \/>\n        <span class=\"token keyword\">for<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span> i <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span> i <span class=\"token operator\">&lt;<\/span> <span class=\"token number\">6<\/span><span class=\"token punctuation\">;<\/span> i<span class=\"token operator\">&#043;&#043;<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n            <span class=\"token function\">sleep<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">5<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n            <span class=\"token comment\">\/\/ \u968f\u7740\u65f6\u95f4\u63a8\u79fb&#xff0c;getppid() \u7684\u8fd4\u56de\u503c\u4f1a\u4ece\u7236\u8fdb\u7a0b\u7684 PID \u53d8\u6210 1&#xff08;systemd \u7684 PID&#xff09;<\/span><br \/>\n            <span class=\"token function\">printf<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;[\u5b50\u8fdb\u7a0b] \u5f53\u524d PPID: %d&#xff08;\u5982\u679c\u7236\u8fdb\u7a0b\u5df2\u9000\u51fa&#xff0c;\u8fd9\u91cc\u5e94\u8be5\u662f 1&#xff09;\\\\n&#034;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token function\">getppid<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><\/p>\n<p>        <span class=\"token function\">printf<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;[\u5b50\u8fdb\u7a0b] \u8fd0\u884c\u7ed3\u675f&#xff0c;\u9000\u51fa\\\\n&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token function\">exit<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">else<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u7236\u8fdb\u7a0b\u5206\u652f&#xff1a;\u7acb\u5373\u9000\u51fa&#xff0c;\u8ba9\u5b50\u8fdb\u7a0b\u6210\u4e3a\u5b64\u513f<\/span><br \/>\n        <span class=\"token function\">printf<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;[\u7236\u8fdb\u7a0b] fork \u6210\u529f&#xff0c;\u5b50\u8fdb\u7a0b PID: %d\\\\n&#034;<\/span><span class=\"token punctuation\">,<\/span> child_pid<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token function\">printf<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;[\u7236\u8fdb\u7a0b] \u7236\u8fdb\u7a0b\u5373\u5c06\u9000\u51fa&#xff0c;\u5b50\u8fdb\u7a0b\u5c06\u6210\u4e3a\u5b64\u513f&#xff0c;\u88ab systemd \u63a5\u7ba1\\\\n&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token function\">printf<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;[\u7236\u8fdb\u7a0b] \u8bf7\u7528: ps -o pid,ppid,comm -p %d \u89c2\u5bdf\u5b50\u8fdb\u7a0b\u7684 PPID \u53d8\u5316\\\\n&#034;<\/span><span class=\"token punctuation\">,<\/span> child_pid<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u7236\u8fdb\u7a0b\u7acb\u5373\u9000\u51fa<\/span><br \/>\n        <span class=\"token function\">exit<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token comment\"># \u7f16\u8bd1<\/span><br \/>\ngcc <span class=\"token parameter variable\">-o<\/span> orphan_demo orphan_demo.c<\/p>\n<p><span class=\"token comment\"># \u8fd0\u884c<\/span><br \/>\n.\/orphan_demo<\/p>\n<h4>9.2 \u89c2\u5bdf\u5b64\u513f\u8fdb\u7a0b\u7684 init \u63a5\u7ba1\u8fc7\u7a0b<\/h4>\n<p>\u7236\u8fdb\u7a0b\u9000\u51fa\u975e\u5e38\u5feb&#xff0c;\u6240\u4ee5\u4f60\u7684\u7ec8\u7aef\u4f1a\u7acb\u523b\u8fd4\u56de\u63d0\u793a\u7b26\u3002\u5b50\u8fdb\u7a0b\u5728\u540e\u53f0\u7ee7\u7eed\u8fd0\u884c&#xff0c;\u4f60\u53ef\u80fd\u770b\u5230\u5b50\u8fdb\u7a0b\u7684\u8f93\u51fa\u5076\u5c14\u51fa\u73b0\u5728\u7ec8\u7aef\u4e0a&#xff08;\u56e0\u4e3a\u5b50\u8fdb\u7a0b\u7ee7\u627f\u4e86\u7236\u8fdb\u7a0b\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u5305\u62ec stdout&#xff09;\u3002<\/p>\n<p><span class=\"token comment\"># \u8bb0\u4e0b\u5b50\u8fdb\u7a0b\u7684 PID&#xff08;\u4ece\u7a0b\u5e8f\u8f93\u51fa\u4e2d\u83b7\u5f97&#xff09;&#xff0c;\u7136\u540e\u89c2\u5bdf\u5176\u7236\u8fdb\u7a0b<\/span><br \/>\n<span class=\"token comment\"># \u8fd9\u91cc\u5047\u8bbe\u5b50\u8fdb\u7a0b PID \u662f 12350<\/span><br \/>\n<span class=\"token function\">ps<\/span> <span class=\"token parameter variable\">-o<\/span> pid,ppid,stat,comm <span class=\"token parameter variable\">-p<\/span> <span class=\"token number\">12350<\/span><\/p>\n<p>\u5982\u679c\u5728\u7236\u8fdb\u7a0b\u9000\u51fa\u540e\u6267\u884c\u6b64\u547d\u4ee4&#xff0c;\u4f60\u4f1a\u770b\u5230 PPID \u5df2\u7ecf\u53d8\u6210\u4e86 1&#xff1a;<\/p>\n<p>  PID  PPID STAT COMMAND<br \/>\n12350     1 S    orphan_demo<\/p>\n<p>PPID \u4ece\u539f\u6765\u7684\u7236\u8fdb\u7a0b PID&#xff08;\u6bd4\u5982 12345&#xff09;\u53d8\u6210\u4e86 1&#xff08;systemd&#xff09;&#xff0c;\u8fd9\u5c31\u662f\u5185\u6838\u5b8c\u6210 re-parenting \u7684\u7ed3\u679c\u3002<\/p>\n<p><span class=\"token comment\"># \u7528 pstree \u4e5f\u80fd\u770b\u5230\u8fd9\u4e2a\u8fdb\u7a0b\u73b0\u5728\u6302\u5728 systemd \u4e0b\u9762<\/span><br \/>\npstree <span class=\"token parameter variable\">-p<\/span> <span class=\"token operator\">|<\/span> <span class=\"token function\">grep<\/span> orphan<br \/>\n<span class=\"token comment\"># \u8f93\u51fa\u7c7b\u4f3c&#xff1a;systemd(1)\u2500&#8230;\u2500orphan_demo(12350)<\/span><\/p>\n<p>\u7b49\u5b50\u8fdb\u7a0b\u81ea\u7136\u9000\u51fa&#xff08;\u7ea6 30 \u79d2&#xff09;\u540e&#xff0c;\u53ef\u4ee5\u786e\u8ba4\u5b83\u88ab\u5e72\u51c0\u5730\u56de\u6536\u4e86&#xff1a;<\/p>\n<p><span class=\"token comment\"># \u786e\u8ba4\u5b64\u513f\u8fdb\u7a0b\u9000\u51fa\u540e\u6ca1\u6709\u7559\u4e0b\u50f5\u5c38<\/span><br \/>\n<span class=\"token function\">ps<\/span> aux <span class=\"token operator\">|<\/span> <span class=\"token function\">grep<\/span> orphan<br \/>\n<span class=\"token comment\"># \u5e94\u8be5\u6ca1\u6709\u8f93\u51fa&#xff0c;\u8bf4\u660e systemd \u6b63\u786e\u56de\u6536\u4e86\u5b64\u513f\u8fdb\u7a0b<\/span><\/p>\n<p>\u8fd9\u8bc1\u660e\u4e86\u5b64\u513f\u8fdb\u7a0b\u7684\u56de\u6536\u662f\u7531 systemd \u81ea\u52a8\u5904\u7406\u7684&#xff0c;\u4e0d\u9700\u8981\u6211\u4eec\u505a\u4efb\u4f55\u5e72\u9884\u3002<\/p>\n<h4>9.3 \u547d\u4ee4\u89e3\u6790<\/h4>\n<p>ps -o pid,ppid,stat,comm -p &lt;PID&gt;<\/p>\n<p>\u8fd9\u4e2a\u547d\u4ee4\u4f7f\u7528\u4e86 ps \u7684\u81ea\u5b9a\u4e49\u8f93\u51fa\u683c\u5f0f&#xff08;-o&#xff09;&#xff1a;\u53ea\u663e\u793a\u6211\u4eec\u5173\u5fc3\u7684\u51e0\u4e2a\u5b57\u6bb5\u2014\u2014PID&#xff08;\u8fdb\u7a0bID&#xff09;\u3001PPID&#xff08;\u7236\u8fdb\u7a0bID&#xff09;\u3001STAT&#xff08;\u72b6\u6001&#xff09;\u3001COMM&#xff08;\u547d\u4ee4\u540d&#xff09;\u3002-p &lt;PID&gt; \u5219\u662f\u53ea\u663e\u793a\u6307\u5b9a PID \u7684\u8fdb\u7a0b\u4fe1\u606f\u3002\u8fd9\u6bd4 ps aux \u8f93\u51fa\u7684\u4fe1\u606f\u66f4\u805a\u7126&#xff0c;\u5728\u8c03\u8bd5\u7279\u5b9a\u8fdb\u7a0b\u65f6\u975e\u5e38\u65b9\u4fbf\u3002<\/p>\n<p>\u4e3a\u4ec0\u4e48 PPID \u4f1a\u53d8\u6210 1<\/p>\n<p>\u8fd9\u662f Linux \u5185\u6838\u5728\u7236\u8fdb\u7a0b\u9000\u51fa\u65f6\u81ea\u52a8\u6267\u884c\u7684\u884c\u4e3a\u3002\u5f53\u7236\u8fdb\u7a0b\u9000\u51fa\u65f6&#xff0c;\u5185\u6838\u904d\u5386\u7236\u8fdb\u7a0b\u7684\u6240\u6709\u5b50\u8fdb\u7a0b&#xff0c;\u5c06\u5176 PPID \u4fee\u6539\u4e3a 1&#xff08;PID subreaper&#xff0c;\u901a\u5e38\u662f systemd&#xff09;\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u5b8c\u5168\u7531\u5185\u6838\u81ea\u52a8\u5b8c\u6210&#xff0c;\u4e0d\u9700\u8981\u4efb\u4f55\u7528\u6237\u6001\u7a0b\u5e8f\u7684\u53c2\u4e0e\u3002<\/p>\n<h3>\u5341\u3001\u6848\u4f8b\u4e09&#xff1a;\u771f\u5b9e\u670d\u52a1\u5668\u4e2d\u7684\u50f5\u5c38\u8fdb\u7a0b\u6392\u67e5\u4e0e\u5904\u7406<\/h3>\n<p>\u524d\u4e24\u4e2a\u6848\u4f8b\u5e2e\u6211\u4eec\u5efa\u7acb\u4e86\u76f4\u89c2\u8ba4\u8bc6\u3002\u73b0\u5728\u6765\u5230\u6700\u91cd\u8981\u7684\u5de5\u7a0b\u5b9e\u8df5\u90e8\u5206&#xff1a;\u5f53\u4f60\u5728\u771f\u5b9e\u7684\u670d\u52a1\u5668\u4e0a\u9047\u5230\u50f5\u5c38\u8fdb\u7a0b&#xff0c;\u5e94\u8be5\u600e\u4e48\u5206\u6790\u548c\u5904\u7406&#xff1f;<\/p>\n<h4>10.1 \u7528 ps \u548c top \u5b9a\u4f4d\u50f5\u5c38\u8fdb\u7a0b<\/h4>\n<p><span class=\"token comment\"># \u7b2c\u4e00\u6b65&#xff1a;\u5feb\u901f\u786e\u8ba4\u7cfb\u7edf\u4e2d\u662f\u5426\u5b58\u5728\u50f5\u5c38\u8fdb\u7a0b\u53ca\u5176\u6570\u91cf<\/span><br \/>\n<span class=\"token comment\"># \u65b9\u6cd5\u4e00&#xff1a;top \u6982\u89c8<\/span><br \/>\n<span class=\"token function\">top<\/span> <span class=\"token parameter variable\">-b<\/span> <span class=\"token parameter variable\">-n<\/span> <span class=\"token number\">1<\/span> <span class=\"token operator\">|<\/span> <span class=\"token function\">grep<\/span> <span class=\"token parameter variable\">-E<\/span> <span class=\"token string\">&#034;^Tasks&#034;<\/span><\/p>\n<p>\u8f93\u51fa\u793a\u4f8b&#xff1a;<\/p>\n<p>Tasks: 214 total,   1 running, 212 sleeping,   0 stopped,   3 zombie<\/p>\n<p><span class=\"token comment\"># \u65b9\u6cd5\u4e8c&#xff1a;ps \u5217\u51fa\u6240\u6709\u50f5\u5c38\u8fdb\u7a0b\u7684\u8be6\u7ec6\u4fe1\u606f<\/span><br \/>\n<span class=\"token comment\"># -e \u5217\u51fa\u6240\u6709\u8fdb\u7a0b&#xff0c;-o \u81ea\u5b9a\u4e49\u8f93\u51fa\u683c\u5f0f<\/span><br \/>\n<span class=\"token function\">ps<\/span> <span class=\"token parameter variable\">-eo<\/span> pid,ppid,stat,user,comm,args <span class=\"token parameter variable\">&#8211;sort<\/span><span class=\"token operator\">&#061;<\/span>stat <span class=\"token operator\">|<\/span> <span class=\"token function\">grep<\/span> <span class=\"token string\">&#039;^[[:space:]]*[0-9].*Z&#039;<\/span><\/p>\n<p><span class=\"token comment\"># \u66f4\u7b80\u6d01\u7684\u65b9\u5f0f&#xff1a;\u76f4\u63a5\u8fc7\u6ee4 STAT \u5217<\/span><br \/>\n<span class=\"token function\">ps<\/span> aux <span class=\"token operator\">|<\/span> <span class=\"token function\">awk<\/span> <span class=\"token string\">&#039;NR&#061;&#061;1 || $8 ~ \/^Z\/&#039;<\/span><\/p>\n<p>\u8fd9\u4f1a\u8f93\u51fa\u8868\u5934\u548c\u6240\u6709\u50f5\u5c38\u8fdb\u7a0b\u7684\u4fe1\u606f&#xff0c;\u4fbf\u4e8e\u5206\u6790\u3002<\/p>\n<p><span class=\"token comment\"># \u65b9\u6cd5\u4e09&#xff1a;\u4f7f\u7528 \/proc \u6587\u4ef6\u7cfb\u7edf\u7edf\u8ba1\u50f5\u5c38\u8fdb\u7a0b\u6570\u91cf<\/span><br \/>\n<span class=\"token comment\"># \u904d\u5386 \/proc\/[0-9]*\/status&#xff0c;\u627e\u51fa\u72b6\u6001\u4e3a zombie \u7684<\/span><br \/>\n<span class=\"token function\">grep<\/span> <span class=\"token parameter variable\">-r<\/span> <span class=\"token string\">&#034;^State:.*zombie&#034;<\/span> \/proc\/<span class=\"token punctuation\">[<\/span><span class=\"token number\">0<\/span>-9<span class=\"token punctuation\">]<\/span>*\/status <span class=\"token operator\"><span class=\"token file-descriptor important\">2<\/span>&gt;<\/span>\/dev\/null<\/p>\n<h4>10.2 \u627e\u5230\u50f5\u5c38\u8fdb\u7a0b\u7684\u7236\u8fdb\u7a0b<\/h4>\n<p>\u627e\u5230\u50f5\u5c38\u8fdb\u7a0b\u53ea\u662f\u7b2c\u4e00\u6b65&#xff0c;\u771f\u6b63\u8981\u5904\u7406\u7684\u662f\u5b83\u7684\u7236\u8fdb\u7a0b\u3002\u50f5\u5c38\u8fdb\u7a0b\u672c\u8eab\u65e0\u6cd5\u88ab kill&#xff08;\u56e0\u4e3a\u5b83\u5df2\u7ecf\u6b7b\u4e86&#xff09;&#xff0c;\u5fc5\u987b\u901a\u8fc7\u7236\u8fdb\u7a0b\u6765\u89e3\u51b3\u95ee\u9898\u3002<\/p>\n<p><span class=\"token comment\"># \u5047\u8bbe\u901a\u8fc7\u4e0a\u9762\u7684\u547d\u4ee4\u627e\u5230\u4e86\u50f5\u5c38\u8fdb\u7a0b&#xff0c;PID \u4e3a 12346&#xff0c;PPID \u4e3a 12345<\/span><br \/>\n<span class=\"token comment\"># \u73b0\u5728\u67e5\u770b\u7236\u8fdb\u7a0b&#xff08;12345&#xff09;\u7684\u8be6\u7ec6\u4fe1\u606f<\/span><\/p>\n<p><span class=\"token comment\"># \u67e5\u770b\u7236\u8fdb\u7a0b\u7684\u8fdb\u7a0b\u4fe1\u606f<\/span><br \/>\n<span class=\"token function\">ps<\/span> <span class=\"token parameter variable\">-p<\/span> <span class=\"token number\">12345<\/span> <span class=\"token parameter variable\">-o<\/span> pid,ppid,stat,comm,args<\/p>\n<p><span class=\"token comment\"># \u67e5\u770b\u7236\u8fdb\u7a0b\u6253\u5f00\u4e86\u54ea\u4e9b\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff08;\u5224\u65ad\u8d44\u6e90\u6cc4\u6f0f\u60c5\u51b5&#xff09;<\/span><br \/>\n<span class=\"token function\">ls<\/span> <span class=\"token parameter variable\">-la<\/span> \/proc\/12345\/fd <span class=\"token operator\"><span class=\"token file-descriptor important\">2<\/span>&gt;<\/span>\/dev\/null <span class=\"token operator\">|<\/span> <span class=\"token function\">head<\/span> <span class=\"token parameter variable\">-20<\/span><\/p>\n<p><span class=\"token comment\"># \u67e5\u770b\u7236\u8fdb\u7a0b\u7684\u5b8c\u6574\u547d\u4ee4\u884c&#xff08;\u6709\u65f6 comm \u5b57\u6bb5\u88ab\u622a\u65ad&#xff09;<\/span><br \/>\n<span class=\"token function\">cat<\/span> \/proc\/12345\/cmdline <span class=\"token operator\">|<\/span> <span class=\"token function\">tr<\/span> <span class=\"token string\">&#039;\\\\0&#039;<\/span> <span class=\"token string\">&#039; &#039;<\/span><br \/>\n<span class=\"token builtin class-name\">echo<\/span>  <span class=\"token comment\"># \u6362\u884c<\/span><\/p>\n<p><span class=\"token comment\"># \u67e5\u770b\u7236\u8fdb\u7a0b\u521b\u5efa\u4e86\u591a\u5c11\u4e2a\u50f5\u5c38\u5b50\u8fdb\u7a0b&#xff08;\u6279\u91cf\u50f5\u5c38\u7684\u60c5\u51b5&#xff09;<\/span><br \/>\n<span class=\"token function\">ps<\/span> <span class=\"token parameter variable\">-eo<\/span> ppid,stat <span class=\"token operator\">|<\/span> <span class=\"token function\">awk<\/span> <span class=\"token string\">&#039;$2 ~ \/^Z\/ {count[$1]&#043;&#043;} END {for (ppid in count) print ppid, count[ppid]}&#039;<\/span> <span class=\"token operator\">|<\/span> <span class=\"token function\">sort<\/span> <span class=\"token parameter variable\">-k2<\/span> <span class=\"token parameter variable\">-rn<\/span><\/p>\n<p>\u6700\u540e\u8fd9\u6761\u547d\u4ee4\u4f1a\u7edf\u8ba1\u6bcf\u4e2a\u7236\u8fdb\u7a0b\u4e0b\u6709\u591a\u5c11\u4e2a\u50f5\u5c38\u5b50\u8fdb\u7a0b&#xff0c;\u8f93\u51fa\u683c\u5f0f\u4e3a PPID \u50f5\u5c38\u6570\u91cf&#xff0c;\u6309\u6570\u91cf\u4ece\u5927\u5230\u5c0f\u6392\u5e8f\u3002\u8fd9\u5728\u6392\u67e5\u6279\u91cf\u50f5\u5c38\u8fdb\u7a0b\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<h4>10.3 \u901a\u8fc7 SIGCHLD \u4fe1\u53f7\u89e6\u53d1\u7236\u8fdb\u7a0b\u56de\u6536<\/h4>\n<p>\u627e\u5230\u7236\u8fdb\u7a0b\u4e4b\u540e&#xff0c;\u7b2c\u4e00\u6b65\u4e0d\u662f\u7acb\u5373\u6740\u6b7b\u7236\u8fdb\u7a0b&#xff08;\u90a3\u6837\u4f1a\u5f71\u54cd\u6b63\u5728\u8fd0\u884c\u7684\u670d\u52a1&#xff09;&#xff0c;\u800c\u662f\u5c1d\u8bd5\u53d1\u9001 SIGCHLD \u4fe1\u53f7&#xff0c;\u63d0\u9192\u7236\u8fdb\u7a0b&#034;\u4f60\u6709\u5b50\u8fdb\u7a0b\u9000\u51fa\u4e86&#xff0c;\u5feb\u6765\u5904\u7406&#034;&#xff1a;<\/p>\n<p><span class=\"token comment\"># \u5411\u7236\u8fdb\u7a0b\u53d1\u9001 SIGCHLD \u4fe1\u53f7&#xff0c;\u63d0\u793a\u5b83\u53bb\u56de\u6536\u5b50\u8fdb\u7a0b<\/span><br \/>\n<span class=\"token comment\"># \u6ce8\u610f&#xff1a;\u8fd9\u79cd\u65b9\u5f0f\u53ea\u5bf9\u6b63\u786e\u5b9e\u73b0\u4e86 SIGCHLD \u5904\u7406\u5668\u7684\u7236\u8fdb\u7a0b\u6709\u6548<\/span><br \/>\n<span class=\"token function\">kill<\/span> <span class=\"token parameter variable\">-SIGCHLD<\/span> <span class=\"token number\">12345<\/span><\/p>\n<p><span class=\"token comment\"># \u53d1\u9001\u4fe1\u53f7\u540e&#xff0c;\u7b49\u5f85\u51e0\u79d2&#xff0c;\u518d\u6b21\u68c0\u67e5\u50f5\u5c38\u8fdb\u7a0b\u662f\u5426\u51cf\u5c11<\/span><br \/>\n<span class=\"token function\">sleep<\/span> <span class=\"token number\">3<\/span><br \/>\n<span class=\"token function\">ps<\/span> aux <span class=\"token operator\">|<\/span> <span class=\"token function\">awk<\/span> <span class=\"token string\">&#039;$8 ~ \/^Z\/&#039;<\/span><\/p>\n<p>\u5982\u679c\u7236\u8fdb\u7a0b\u7684\u4ee3\u7801\u6b63\u786e\u5b9e\u73b0\u4e86 SIGCHLD \u4fe1\u53f7\u5904\u7406&#xff08;\u5373\u5728\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u4e2d\u8c03\u7528\u4e86 waitpid()&#xff09;&#xff0c;\u90a3\u4e48\u53d1\u9001 SIGCHLD \u4e4b\u540e&#xff0c;\u50f5\u5c38\u8fdb\u7a0b\u5e94\u8be5\u4f1a\u88ab\u56de\u6536\u3002<\/p>\n<p>\u4f46\u5982\u679c\u7236\u8fdb\u7a0b\u7684\u4ee3\u7801\u6839\u672c\u6ca1\u6709\u5904\u7406 SIGCHLD\u2014\u2014\u8fd9\u624d\u662f\u4ea7\u751f\u5927\u91cf\u50f5\u5c38\u8fdb\u7a0b\u7684\u6839\u672c\u539f\u56e0\u2014\u2014\u90a3\u4e48\u53d1\u9001\u4fe1\u53f7\u4e5f\u6ca1\u6709\u7528\u3002<\/p>\n<h4>10.4 \u7ec8\u6781\u624b\u6bb5&#xff1a;\u7ec8\u6b62\u7236\u8fdb\u7a0b<\/h4>\n<p>\u5982\u679c SIGCHLD \u6ca1\u6709\u6548\u679c&#xff0c;\u800c\u4e14\u50f5\u5c38\u8fdb\u7a0b\u6570\u91cf\u8fd8\u5728\u589e\u957f&#xff0c;\u90a3\u4e48\u5c31\u9700\u8981\u8003\u8651\u7ec8\u6b62\u7236\u8fdb\u7a0b\u4e86\u3002\u7236\u8fdb\u7a0b\u9000\u51fa\u540e&#xff0c;\u5b83\u4e0b\u9762\u7684\u50f5\u5c38\u8fdb\u7a0b\u4f1a\u88ab systemd \u63a5\u7ba1\u5e76\u7acb\u5373\u56de\u6536\u3002<\/p>\n<p><span class=\"token comment\"># \u7b2c\u4e00\u6b65&#xff1a;\u5148\u5c1d\u8bd5\u4f18\u96c5\u5730\u7ec8\u6b62\u7236\u8fdb\u7a0b&#xff08;SIGTERM \u4fe1\u53f7&#xff09;<\/span><br \/>\n<span class=\"token comment\"># SIGTERM \u662f\u793c\u8c8c\u7684\u7ec8\u6b62\u8bf7\u6c42&#xff0c;\u7ed9\u8fdb\u7a0b\u673a\u4f1a\u505a\u6e05\u7406\u5de5\u4f5c<\/span><br \/>\n<span class=\"token function\">sudo<\/span> <span class=\"token function\">kill<\/span> <span class=\"token parameter variable\">-SIGTERM<\/span> <span class=\"token number\">12345<\/span><\/p>\n<p><span class=\"token comment\"># \u7b49\u5f85\u51e0\u79d2&#xff0c;\u786e\u8ba4\u7236\u8fdb\u7a0b\u662f\u5426\u6b63\u5e38\u9000\u51fa<\/span><br \/>\n<span class=\"token function\">sleep<\/span> <span class=\"token number\">5<\/span><br \/>\n<span class=\"token function\">ps<\/span> <span class=\"token parameter variable\">-p<\/span> <span class=\"token number\">12345<\/span><\/p>\n<p><span class=\"token comment\"># \u5982\u679c SIGTERM \u6ca1\u6709\u6548\u679c&#xff08;\u8fdb\u7a0b\u5ffd\u7565\u4e86\u8be5\u4fe1\u53f7&#xff09;&#xff0c;\u518d\u4f7f\u7528 SIGKILL<\/span><br \/>\n<span class=\"token comment\"># \u6ce8\u610f&#xff1a;SIGKILL \u662f\u5f3a\u5236\u7ec8\u6b62&#xff0c;\u8fdb\u7a0b\u65e0\u6cd5\u62e6\u622a&#xff0c;\u4f1a\u7acb\u5373\u7ec8\u6b62&#xff0c;\u6ca1\u6709\u673a\u4f1a\u505a\u6e05\u7406<\/span><br \/>\n<span class=\"token comment\"># \u4f7f\u7528\u524d\u8bf7\u786e\u8ba4\u8fd9\u4e2a\u8fdb\u7a0b\u53ef\u4ee5\u88ab\u5b89\u5168\u5730\u5f3a\u5236\u7ec8\u6b62<\/span><br \/>\n<span class=\"token function\">sudo<\/span> <span class=\"token function\">kill<\/span> <span class=\"token parameter variable\">-SIGKILL<\/span> <span class=\"token number\">12345<\/span><\/p>\n<p><span class=\"token comment\"># \u7ec8\u6b62\u7236\u8fdb\u7a0b\u540e&#xff0c;\u786e\u8ba4\u50f5\u5c38\u8fdb\u7a0b\u5df2\u88ab\u56de\u6536<\/span><br \/>\n<span class=\"token function\">sleep<\/span> <span class=\"token number\">2<\/span><br \/>\n<span class=\"token function\">ps<\/span> aux <span class=\"token operator\">|<\/span> <span class=\"token function\">awk<\/span> <span class=\"token string\">&#039;$8 ~ \/^Z\/&#039;<\/span><br \/>\n<span class=\"token comment\"># \u50f5\u5c38\u8fdb\u7a0b\u5e94\u8be5\u5df2\u7ecf\u6d88\u5931<\/span><\/p>\n<p>\u26a0\ufe0f \u91cd\u8981\u63d0\u9192&#xff1a;\u5728\u751f\u4ea7\u73af\u5883\u4e2d\u7ec8\u6b62\u670d\u52a1\u8fdb\u7a0b\u4e4b\u524d&#xff0c;\u5fc5\u987b\u8bc4\u4f30\u5bf9\u4e1a\u52a1\u7684\u5f71\u54cd\u3002\u5982\u679c\u8fd9\u4e2a\u7236\u8fdb\u7a0b\u662f\u6838\u5fc3\u670d\u52a1&#xff08;\u5982 Nginx worker manager\u3001Java \u5e94\u7528\u4e3b\u8fdb\u7a0b&#xff09;&#xff0c;\u7ec8\u6b62\u5b83\u53ef\u80fd\u4f1a\u4e2d\u65ad\u6b63\u5728\u5904\u7406\u7684\u8bf7\u6c42&#xff0c;\u751a\u81f3\u5bfc\u81f4\u6570\u636e\u4e0d\u4e00\u81f4\u3002\u5efa\u8bae\u5728\u7ef4\u62a4\u7a97\u53e3\u671f\u95f4\u64cd\u4f5c&#xff0c;\u5e76\u63d0\u524d\u901a\u77e5\u76f8\u5173\u56e2\u961f\u3002<\/p>\n<h4>10.5 \u7f16\u5199\u53ef\u4ee5\u6b63\u786e\u56de\u6536\u5b50\u8fdb\u7a0b\u7684 Python \u670d\u52a1<\/h4>\n<p>\u771f\u6b63\u89e3\u51b3\u50f5\u5c38\u8fdb\u7a0b\u95ee\u9898&#xff0c;\u6cbb\u6807\u5728\u4e8e\u7ec8\u6b62\u6709\u95ee\u9898\u7684\u7236\u8fdb\u7a0b&#xff0c;\u6cbb\u672c\u5728\u4e8e\u4fee\u590d\u7236\u8fdb\u7a0b\u7684\u4ee3\u7801&#xff0c;\u8ba9\u5b83\u6b63\u786e\u5904\u7406\u5b50\u8fdb\u7a0b\u7684\u9000\u51fa\u3002<\/p>\n<p>\u4e0b\u9762\u6211\u4eec\u7528 Python \u6f14\u793a\u4e24\u4e2a\u7248\u672c&#xff1a;\u4e00\u4e2a\u4f1a\u4ea7\u751f\u50f5\u5c38\u8fdb\u7a0b&#xff08;\u9519\u8bef\u793a\u8303&#xff09;&#xff0c;\u4e00\u4e2a\u4e0d\u4f1a&#xff08;\u6b63\u786e\u793a\u8303&#xff09;\u3002<\/p>\n<p>\u9519\u8bef\u7248\u672c&#xff1a;\u4f1a\u4ea7\u751f\u50f5\u5c38\u8fdb\u7a0b<\/p>\n<p><span class=\"token function\">nano<\/span> ~\/zombie_orphan_lab\/bad_worker.py<\/p>\n<p><span class=\"token comment\">#!\/usr\/bin\/env python3<\/span><br \/>\n<span class=\"token triple-quoted-string string\">&#034;&#034;&#034;<br \/>\nbad_worker.py &#8211; \u4f1a\u4ea7\u751f\u50f5\u5c38\u8fdb\u7a0b\u7684\u9519\u8bef\u793a\u8303<br \/>\n\u6a21\u62df\u4e00\u4e2a\u7236\u8fdb\u7a0b\u9891\u7e41 fork \u5b50\u8fdb\u7a0b\u4f46\u4e0d\u56de\u6536\u7684\u670d\u52a1<br \/>\n&#034;&#034;&#034;<\/span><br \/>\n<span class=\"token keyword\">import<\/span> os<br \/>\n<span class=\"token keyword\">import<\/span> time<br \/>\n<span class=\"token keyword\">import<\/span> signal<\/p>\n<p><span class=\"token keyword\">def<\/span> <span class=\"token function\">handle_request<\/span><span class=\"token punctuation\">(<\/span>request_id<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token triple-quoted-string string\">&#034;&#034;&#034;\u6a21\u62df\u5904\u7406\u4e00\u4e2a\u8bf7\u6c42&#034;&#034;&#034;<\/span><br \/>\n    <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string-interpolation\"><span class=\"token string\">f&#034;[\u5b50\u8fdb\u7a0b <\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>os<span class=\"token punctuation\">.<\/span>getpid<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">] \u5904\u7406\u8bf7\u6c42 #<\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>request_id<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">&#xff0c;\u7761\u7720 1 \u79d2&#034;<\/span><\/span><span class=\"token punctuation\">)<\/span><br \/>\n    time<span class=\"token punctuation\">.<\/span>sleep<span class=\"token punctuation\">(<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string-interpolation\"><span class=\"token string\">f&#034;[\u5b50\u8fdb\u7a0b <\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>os<span class=\"token punctuation\">.<\/span>getpid<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">] \u8bf7\u6c42 #<\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>request_id<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\"> \u5904\u7406\u5b8c\u6210&#xff0c;\u9000\u51fa&#034;<\/span><\/span><span class=\"token punctuation\">)<\/span><br \/>\n    os<span class=\"token punctuation\">.<\/span>_exit<span class=\"token punctuation\">(<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u5b50\u8fdb\u7a0b\u9000\u51fa<\/span><\/p>\n<p><span class=\"token keyword\">def<\/span> <span class=\"token function\">bad_server<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token triple-quoted-string string\">&#034;&#034;&#034;\u9519\u8bef\u7684\u670d\u52a1&#xff1a;\u4e0d\u56de\u6536\u5b50\u8fdb\u7a0b&#xff0c;\u4f1a\u4ea7\u751f\u50f5\u5c38\u8fdb\u7a0b&#034;&#034;&#034;<\/span><br \/>\n    <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string-interpolation\"><span class=\"token string\">f&#034;[\u7236\u8fdb\u7a0b <\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>os<span class=\"token punctuation\">.<\/span>getpid<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">] \u670d\u52a1\u542f\u52a8&#xff0c;\u5c06\u5904\u7406 5 \u4e2a\u8bf7\u6c42&#034;<\/span><\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p>    <span class=\"token keyword\">for<\/span> i <span class=\"token keyword\">in<\/span> <span class=\"token builtin\">range<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">5<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n        pid <span class=\"token operator\">&#061;<\/span> os<span class=\"token punctuation\">.<\/span>fork<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u521b\u5efa\u5b50\u8fdb\u7a0b\u5904\u7406\u8bf7\u6c42<\/span><br \/>\n        <span class=\"token keyword\">if<\/span> pid <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">:<\/span><br \/>\n            <span class=\"token comment\"># \u5b50\u8fdb\u7a0b&#xff1a;\u5904\u7406\u8bf7\u6c42<\/span><br \/>\n            handle_request<span class=\"token punctuation\">(<\/span>i<span class=\"token punctuation\">)<\/span><br \/>\n        <span class=\"token keyword\">else<\/span><span class=\"token punctuation\">:<\/span><br \/>\n            <span class=\"token comment\"># \u7236\u8fdb\u7a0b&#xff1a;\u4e0d\u8c03\u7528 wait()&#xff0c;\u76f4\u63a5\u7ee7\u7eed\u5faa\u73af<\/span><br \/>\n            <span class=\"token comment\"># \u8fd9\u5c31\u662f BUG \u6240\u5728&#xff01;\u5b50\u8fdb\u7a0b\u9000\u51fa\u540e\u4f1a\u6210\u4e3a\u50f5\u5c38\u8fdb\u7a0b<\/span><br \/>\n            <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string-interpolation\"><span class=\"token string\">f&#034;[\u7236\u8fdb\u7a0b] fork \u4e86\u5b50\u8fdb\u7a0b <\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>pid<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\"> \u6765\u5904\u7406\u8bf7\u6c42 #<\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>i<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">&#xff0c;\u4f46\u4e0d\u7b49\u5f85\u5b83\u9000\u51fa&#034;<\/span><\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p>        time<span class=\"token punctuation\">.<\/span>sleep<span class=\"token punctuation\">(<\/span><span class=\"token number\">0.5<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u7b80\u77ed\u6682\u505c&#xff0c;\u65b9\u4fbf\u89c2\u5bdf<\/span><\/p>\n<p>    <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string-interpolation\"><span class=\"token string\">f&#034;[\u7236\u8fdb\u7a0b] \u6240\u6709\u8bf7\u6c42\u5df2\u6d3e\u53d1&#xff0c;\u7761\u7720 30 \u79d2&#xff08;\u89c2\u5bdf\u50f5\u5c38\u8fdb\u7a0b&#xff09;&#034;<\/span><\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string-interpolation\"><span class=\"token string\">f&#034;[\u7236\u8fdb\u7a0b] \u8bf7\u8fd0\u884c: ps aux | awk &#039;$8 ~ \/^Z\/&#039;&#034;<\/span><\/span><span class=\"token punctuation\">)<\/span><br \/>\n    time<span class=\"token punctuation\">.<\/span>sleep<span class=\"token punctuation\">(<\/span><span class=\"token number\">30<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;[\u7236\u8fdb\u7a0b] \u9000\u51fa&#034;<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p><span class=\"token keyword\">if<\/span> __name__ <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token string\">&#034;__main__&#034;<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    bad_server<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p><span class=\"token comment\"># \u8fd0\u884c\u9519\u8bef\u7248\u672c&#xff0c;\u5728\u53e6\u4e00\u4e2a\u7ec8\u7aef\u89c2\u5bdf\u50f5\u5c38\u8fdb\u7a0b<\/span><br \/>\npython3 ~\/zombie_orphan_lab\/bad_worker.py <span class=\"token operator\">&amp;<\/span><br \/>\n<span class=\"token function\">sleep<\/span> <span class=\"token number\">5<\/span><br \/>\n<span class=\"token function\">ps<\/span> aux <span class=\"token operator\">|<\/span> <span class=\"token function\">awk<\/span> <span class=\"token string\">&#039;NR&#061;&#061;1 || $8 ~ \/^Z\/&#039;<\/span><\/p>\n<p>\u4f60\u4f1a\u770b\u5230\u591a\u4e2a python3 &lt;defunct&gt; \u7684\u50f5\u5c38\u8fdb\u7a0b\u3002<\/p>\n<p>\u6b63\u786e\u7248\u672c\u4e00&#xff1a;\u4f7f\u7528 signal.signal \u5904\u7406 SIGCHLD<\/p>\n<p><span class=\"token function\">nano<\/span> ~\/zombie_orphan_lab\/good_worker_signal.py<\/p>\n<p><span class=\"token comment\">#!\/usr\/bin\/env python3<\/span><br \/>\n<span class=\"token triple-quoted-string string\">&#034;&#034;&#034;<br \/>\ngood_worker_signal.py &#8211; \u901a\u8fc7\u6ce8\u518c SIGCHLD \u4fe1\u53f7\u5904\u7406\u5668\u6765\u6b63\u786e\u56de\u6536\u5b50\u8fdb\u7a0b<br \/>\n&#034;&#034;&#034;<\/span><br \/>\n<span class=\"token keyword\">import<\/span> os<br \/>\n<span class=\"token keyword\">import<\/span> time<br \/>\n<span class=\"token keyword\">import<\/span> signal<\/p>\n<p><span class=\"token keyword\">def<\/span> <span class=\"token function\">sigchld_handler<\/span><span class=\"token punctuation\">(<\/span>signum<span class=\"token punctuation\">,<\/span> frame<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token triple-quoted-string string\">&#034;&#034;&#034;<br \/>\n    SIGCHLD \u4fe1\u53f7\u5904\u7406\u5668&#xff1a;\u5f53\u6709\u5b50\u8fdb\u7a0b\u9000\u51fa\u65f6&#xff0c;\u5185\u6838\u4f1a\u53d1\u9001 SIGCHLD \u7ed9\u7236\u8fdb\u7a0b<br \/>\n    \u6211\u4eec\u5728\u8fd9\u91cc\u8c03\u7528 waitpid \u6765\u56de\u6536\u6240\u6709\u5df2\u9000\u51fa\u7684\u5b50\u8fdb\u7a0b<br \/>\n    &#034;&#034;&#034;<\/span><br \/>\n    <span class=\"token keyword\">while<\/span> <span class=\"token boolean\">True<\/span><span class=\"token punctuation\">:<\/span><br \/>\n        <span class=\"token keyword\">try<\/span><span class=\"token punctuation\">:<\/span><br \/>\n            <span class=\"token comment\"># waitpid(-1, &#8230;) \u7b49\u5f85\u4efb\u610f\u5b50\u8fdb\u7a0b<\/span><br \/>\n            <span class=\"token comment\"># os.WNOHANG \u8868\u793a\u975e\u963b\u585e&#xff1a;\u5982\u679c\u6ca1\u6709\u5b50\u8fdb\u7a0b\u9000\u51fa&#xff0c;\u7acb\u5373\u8fd4\u56de\u800c\u4e0d\u662f\u963b\u585e<\/span><br \/>\n            pid<span class=\"token punctuation\">,<\/span> status <span class=\"token operator\">&#061;<\/span> os<span class=\"token punctuation\">.<\/span>waitpid<span class=\"token punctuation\">(<\/span><span class=\"token operator\">&#8211;<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">,<\/span> os<span class=\"token punctuation\">.<\/span>WNOHANG<span class=\"token punctuation\">)<\/span><br \/>\n            <span class=\"token keyword\">if<\/span> pid <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">:<\/span><br \/>\n                <span class=\"token comment\"># \u6ca1\u6709\u66f4\u591a\u5df2\u9000\u51fa\u7684\u5b50\u8fdb\u7a0b\u4e86&#xff0c;\u9000\u51fa\u5faa\u73af<\/span><br \/>\n                <span class=\"token keyword\">break<\/span><br \/>\n            <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string-interpolation\"><span class=\"token string\">f&#034;[\u7236\u8fdb\u7a0b] \u6210\u529f\u56de\u6536\u5b50\u8fdb\u7a0b <\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>pid<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">&#xff0c;\u9000\u51fa\u72b6\u6001: <\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>os<span class=\"token punctuation\">.<\/span>WEXITSTATUS<span class=\"token punctuation\">(<\/span>status<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">&#034;<\/span><\/span><span class=\"token punctuation\">)<\/span><br \/>\n        <span class=\"token keyword\">except<\/span> ChildProcessError<span class=\"token punctuation\">:<\/span><br \/>\n            <span class=\"token comment\"># \u6ca1\u6709\u5b50\u8fdb\u7a0b\u4e86<\/span><br \/>\n            <span class=\"token keyword\">break<\/span><br \/>\n        <span class=\"token keyword\">except<\/span> Exception <span class=\"token keyword\">as<\/span> e<span class=\"token punctuation\">:<\/span><br \/>\n            <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string-interpolation\"><span class=\"token string\">f&#034;[\u7236\u8fdb\u7a0b] waitpid \u5f02\u5e38: <\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>e<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">&#034;<\/span><\/span><span class=\"token punctuation\">)<\/span><br \/>\n            <span class=\"token keyword\">break<\/span><\/p>\n<p><span class=\"token keyword\">def<\/span> <span class=\"token function\">handle_request<\/span><span class=\"token punctuation\">(<\/span>request_id<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token triple-quoted-string string\">&#034;&#034;&#034;\u5b50\u8fdb\u7a0b\u5904\u7406\u8bf7\u6c42&#034;&#034;&#034;<\/span><br \/>\n    <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string-interpolation\"><span class=\"token string\">f&#034;[\u5b50\u8fdb\u7a0b <\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>os<span class=\"token punctuation\">.<\/span>getpid<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">] \u5904\u7406\u8bf7\u6c42 #<\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>request_id<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">&#034;<\/span><\/span><span class=\"token punctuation\">)<\/span><br \/>\n    time<span class=\"token punctuation\">.<\/span>sleep<span class=\"token punctuation\">(<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string-interpolation\"><span class=\"token string\">f&#034;[\u5b50\u8fdb\u7a0b <\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>os<span class=\"token punctuation\">.<\/span>getpid<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">] \u8bf7\u6c42 #<\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>request_id<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\"> \u5b8c\u6210&#034;<\/span><\/span><span class=\"token punctuation\">)<\/span><br \/>\n    os<span class=\"token punctuation\">.<\/span>_exit<span class=\"token punctuation\">(<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p><span class=\"token keyword\">def<\/span> <span class=\"token function\">good_server_with_signal<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token triple-quoted-string string\">&#034;&#034;&#034;\u6b63\u786e\u7684\u670d\u52a1&#xff1a;\u6ce8\u518c SIGCHLD \u4fe1\u53f7\u5904\u7406\u5668&#034;&#034;&#034;<\/span><br \/>\n    <span class=\"token comment\"># \u6ce8\u518c SIGCHLD \u4fe1\u53f7\u5904\u7406\u5668<\/span><br \/>\n    <span class=\"token comment\"># \u5f53\u4efb\u4f55\u5b50\u8fdb\u7a0b\u9000\u51fa\u65f6&#xff0c;\u5185\u6838\u4f1a\u5411\u6211\u4eec\u53d1\u9001 SIGCHLD&#xff0c;\u6211\u4eec\u5728\u56de\u8c03\u4e2d\u56de\u6536\u5b83<\/span><br \/>\n    signal<span class=\"token punctuation\">.<\/span>signal<span class=\"token punctuation\">(<\/span>signal<span class=\"token punctuation\">.<\/span>SIGCHLD<span class=\"token punctuation\">,<\/span> sigchld_handler<span class=\"token punctuation\">)<\/span><\/p>\n<p>    <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string-interpolation\"><span class=\"token string\">f&#034;[\u7236\u8fdb\u7a0b <\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>os<span class=\"token punctuation\">.<\/span>getpid<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">] \u670d\u52a1\u542f\u52a8&#xff08;\u5df2\u6ce8\u518c SIGCHLD \u5904\u7406\u5668&#xff09;&#034;<\/span><\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p>    <span class=\"token keyword\">for<\/span> i <span class=\"token keyword\">in<\/span> <span class=\"token builtin\">range<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">5<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n        pid <span class=\"token operator\">&#061;<\/span> os<span class=\"token punctuation\">.<\/span>fork<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n        <span class=\"token keyword\">if<\/span> pid <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">:<\/span><br \/>\n            handle_request<span class=\"token punctuation\">(<\/span>i<span class=\"token punctuation\">)<\/span><br \/>\n        <span class=\"token keyword\">else<\/span><span class=\"token punctuation\">:<\/span><br \/>\n            <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string-interpolation\"><span class=\"token string\">f&#034;[\u7236\u8fdb\u7a0b] fork \u4e86\u5b50\u8fdb\u7a0b <\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>pid<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\"> \u5904\u7406\u8bf7\u6c42 #<\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>i<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">&#034;<\/span><\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p>        time<span class=\"token punctuation\">.<\/span>sleep<span class=\"token punctuation\">(<\/span><span class=\"token number\">0.5<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p>    <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;[\u7236\u8fdb\u7a0b] \u6240\u6709\u8bf7\u6c42\u5df2\u6d3e\u53d1&#xff0c;\u7761\u7720 10 \u79d2&#034;<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;[\u7236\u8fdb\u7a0b] \u8bf7\u8fd0\u884c: ps aux | awk &#039;$8 ~ \/^Z\/&#039; \u786e\u8ba4\u6ca1\u6709\u50f5\u5c38\u8fdb\u7a0b&#034;<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    time<span class=\"token punctuation\">.<\/span>sleep<span class=\"token punctuation\">(<\/span><span class=\"token number\">10<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;[\u7236\u8fdb\u7a0b] \u9000\u51fa&#034;<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p><span class=\"token keyword\">if<\/span> __name__ <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token string\">&#034;__main__&#034;<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    good_server_with_signal<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p>\u6b63\u786e\u7248\u672c\u4e8c&#xff1a;\u4f7f\u7528 subprocess \u6a21\u5757&#xff08;Python \u63a8\u8350\u65b9\u5f0f&#xff09;<\/p>\n<p>\u5728\u5b9e\u9645\u7684 Python \u9879\u76ee\u4e2d&#xff0c;\u6211\u66f4\u63a8\u8350\u4f7f\u7528 subprocess \u6a21\u5757\u800c\u4e0d\u662f\u76f4\u63a5\u8c03\u7528 os.fork()\u3002subprocess \u6a21\u5757\u5c01\u88c5\u4e86\u8fdb\u7a0b\u7ba1\u7406\u7684\u7ec6\u8282&#xff0c;\u7528\u8d77\u6765\u66f4\u5b89\u5168&#xff1a;<\/p>\n<p><span class=\"token function\">nano<\/span> ~\/zombie_orphan_lab\/good_worker_subprocess.py<\/p>\n<p><span class=\"token comment\">#!\/usr\/bin\/env python3<\/span><br \/>\n<span class=\"token triple-quoted-string string\">&#034;&#034;&#034;<br \/>\ngood_worker_subprocess.py<br \/>\n\u4f7f\u7528 subprocess \u6a21\u5757&#xff0c;\u8fd9\u662f Python \u4e2d\u7ba1\u7406\u5b50\u8fdb\u7a0b\u7684\u63a8\u8350\u65b9\u5f0f<br \/>\nsubprocess \u6a21\u5757\u4f1a\u5728\u9002\u5f53\u65f6\u673a\u56de\u6536\u5b50\u8fdb\u7a0b&#xff0c;\u4e0d\u4f1a\u4ea7\u751f\u50f5\u5c38\u8fdb\u7a0b<br \/>\n&#034;&#034;&#034;<\/span><br \/>\n<span class=\"token keyword\">import<\/span> subprocess<br \/>\n<span class=\"token keyword\">import<\/span> time<\/p>\n<p><span class=\"token keyword\">def<\/span> <span class=\"token function\">run_task<\/span><span class=\"token punctuation\">(<\/span>task_id<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token triple-quoted-string string\">&#034;&#034;&#034;\u4f7f\u7528 subprocess \u8fd0\u884c\u4e00\u4e2a\u5b50\u4efb\u52a1&#034;&#034;&#034;<\/span><br \/>\n    <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string-interpolation\"><span class=\"token string\">f&#034;[\u4e3b\u8fdb\u7a0b] \u542f\u52a8\u4efb\u52a1 #<\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>task_id<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">&#034;<\/span><\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p>    <span class=\"token comment\"># subprocess.run \u4f1a\u7b49\u5f85\u5b50\u8fdb\u7a0b\u5b8c\u6210\u5e76\u56de\u6536\u5b83<\/span><br \/>\n    <span class=\"token comment\"># \u8fd9\u662f\u6700\u7b80\u5355\u7684\u65b9\u5f0f&#xff0c;\u9002\u5408\u4e32\u884c\u4efb\u52a1<\/span><br \/>\n    result <span class=\"token operator\">&#061;<\/span> subprocess<span class=\"token punctuation\">.<\/span>run<span class=\"token punctuation\">(<\/span><br \/>\n        <span class=\"token punctuation\">[<\/span><span class=\"token string\">&#034;python3&#034;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#034;-c&#034;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string-interpolation\"><span class=\"token string\">f&#034;import time; print(&#039;\u4efb\u52a1 <\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>task_id<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\"> \u8fd0\u884c\u4e2d&#039;); time.sleep(1); print(&#039;\u4efb\u52a1 <\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>task_id<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\"> \u5b8c\u6210&#039;)&#034;<\/span><\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">,<\/span><br \/>\n        capture_output<span class=\"token operator\">&#061;<\/span><span class=\"token boolean\">True<\/span><span class=\"token punctuation\">,<\/span><br \/>\n        text<span class=\"token operator\">&#061;<\/span><span class=\"token boolean\">True<\/span><br \/>\n    <span class=\"token punctuation\">)<\/span><\/p>\n<p>    <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string-interpolation\"><span class=\"token string\">f&#034;[\u4e3b\u8fdb\u7a0b] \u4efb\u52a1 #<\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>task_id<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\"> \u5df2\u5b8c\u6210&#xff0c;\u8fd4\u56de\u7801: <\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>result<span class=\"token punctuation\">.<\/span>returncode<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">&#034;<\/span><\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string-interpolation\"><span class=\"token string\">f&#034;[\u4e3b\u8fdb\u7a0b] \u4efb\u52a1\u8f93\u51fa: <\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>result<span class=\"token punctuation\">.<\/span>stdout<span class=\"token punctuation\">.<\/span>strip<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">&#034;<\/span><\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p><span class=\"token keyword\">def<\/span> <span class=\"token function\">good_server_subprocess<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token triple-quoted-string string\">&#034;&#034;&#034;\u4f7f\u7528 subprocess \u7684\u6b63\u786e\u670d\u52a1&#xff0c;\u81ea\u52a8\u7ba1\u7406\u5b50\u8fdb\u7a0b\u751f\u547d\u5468\u671f&#034;&#034;&#034;<\/span><br \/>\n    <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;[\u4e3b\u8fdb\u7a0b] \u670d\u52a1\u542f\u52a8&#xff08;\u4f7f\u7528 subprocess \u6a21\u5757&#xff09;&#034;<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p>    <span class=\"token keyword\">for<\/span> i <span class=\"token keyword\">in<\/span> <span class=\"token builtin\">range<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">3<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n        run_task<span class=\"token punctuation\">(<\/span>i<span class=\"token punctuation\">)<\/span><\/p>\n<p>    <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;[\u4e3b\u8fdb\u7a0b] \u6240\u6709\u4efb\u52a1\u5b8c\u6210&#xff0c;\u65e0\u50f5\u5c38\u8fdb\u7a0b&#034;<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p><span class=\"token keyword\">if<\/span> __name__ <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token string\">&#034;__main__&#034;<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    good_server_subprocess<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p><span class=\"token comment\"># \u8fd0\u884c\u6b63\u786e\u7248\u672c&#xff0c;\u786e\u8ba4\u6ca1\u6709\u50f5\u5c38\u8fdb\u7a0b\u4ea7\u751f<\/span><br \/>\npython3 ~\/zombie_orphan_lab\/good_worker_signal.py <span class=\"token operator\">&amp;<\/span><br \/>\n<span class=\"token function\">sleep<\/span> <span class=\"token number\">8<\/span><br \/>\n<span class=\"token builtin class-name\">echo<\/span> <span class=\"token string\">&#034;&#061;&#061;&#061; \u68c0\u67e5\u50f5\u5c38\u8fdb\u7a0b &#061;&#061;&#061;&#034;<\/span><br \/>\n<span class=\"token function\">ps<\/span> aux <span class=\"token operator\">|<\/span> <span class=\"token function\">awk<\/span> <span class=\"token string\">&#039;NR&#061;&#061;1 || $8 ~ \/^Z\/&#039;<\/span><br \/>\n<span class=\"token builtin class-name\">echo<\/span> <span class=\"token string\">&#034;&#061;&#061;&#061; \u5982\u679c\u4e0a\u9762\u53ea\u6709\u8868\u5934&#xff0c;\u8bf4\u660e\u6ca1\u6709\u50f5\u5c38\u8fdb\u7a0b &#061;&#061;&#061;&#034;<\/span><\/p>\n<h4>10.6 \u547d\u4ee4\u89e3\u6790<\/h4>\n<p>kill -SIGTERM \u548c kill -SIGKILL \u7684\u533a\u522b<\/p>\n<p>\u8fd9\u662f\u4e00\u4e2a\u5f88\u591a\u521d\u5b66\u8005\u5bb9\u6613\u6df7\u6dc6\u7684\u70b9\u3002SIGTERM&#xff08;\u4fe1\u53f7\u7f16\u53f7 15&#xff09;\u662f&#034;\u8bf7\u6c42\u7ec8\u6b62&#034;\u4fe1\u53f7&#xff0c;\u8fdb\u7a0b\u53ef\u4ee5\u6355\u83b7\u5e76\u5904\u7406\u5b83\u2014\u2014\u901a\u5e38\u7528\u4e8e\u505a\u6e05\u7406\u5de5\u4f5c&#xff08;\u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5\u3001\u5237\u65b0\u7f13\u51b2\u533a\u3001\u4fdd\u5b58\u72b6\u6001\u7b49&#xff09;&#xff0c;\u7136\u540e\u518d\u9000\u51fa\u3002SIGKILL&#xff08;\u4fe1\u53f7\u7f16\u53f7 9&#xff09;\u662f&#034;\u5f3a\u5236\u7ec8\u6b62&#034;\u4fe1\u53f7&#xff0c;\u8fdb\u7a0b\u65e0\u6cd5\u6355\u83b7\u3001\u5ffd\u7565\u6216\u62e6\u622a\u5b83&#xff0c;\u5185\u6838\u4f1a\u76f4\u63a5\u5f3a\u5236\u7ec8\u6b62\u8fdb\u7a0b\u3002<\/p>\n<p>\u539f\u5219\u662f&#xff1a;\u4f18\u5148\u4f7f\u7528 SIGTERM&#xff0c;\u53ea\u6709\u5728 SIGTERM \u65e0\u6548\u65f6\u624d\u4f7f\u7528 SIGKILL\u3002<\/p>\n<p>\u5f3a\u5236 SIGKILL \u7684\u95ee\u9898\u5728\u4e8e&#xff1a;\u8fdb\u7a0b\u6ca1\u6709\u673a\u4f1a\u505a\u6e05\u7406&#xff0c;\u53ef\u80fd\u5bfc\u81f4&#xff1a;<\/p>\n<ul>\n<li>\u4e34\u65f6\u6587\u4ef6\u6ca1\u6709\u5220\u9664<\/li>\n<li>\u6570\u636e\u5e93\u8fde\u63a5\u6ca1\u6709\u6b63\u5e38\u5173\u95ed<\/li>\n<li>\u6b63\u5728\u5199\u5165\u7684\u6587\u4ef6\u635f\u574f<\/li>\n<li>\u6301\u6709\u7684\u9501\u6ca1\u6709\u91ca\u653e<\/li>\n<\/ul>\n<p>os.waitpid(-1, os.WNOHANG) \u7684\u542b\u4e49<\/p>\n<p>waitpid \u662f Python \u5bf9\u7cfb\u7edf\u8c03\u7528 waitpid() \u7684\u76f4\u63a5\u5c01\u88c5\u3002\u7b2c\u4e00\u4e2a\u53c2\u6570 -1 \u8868\u793a\u7b49\u5f85\u4efb\u610f\u5b50\u8fdb\u7a0b&#xff08;\u800c\u4e0d\u662f\u6307\u5b9a PID \u7684\u5b50\u8fdb\u7a0b&#xff09;\u3002os.WNOHANG \u662f\u4e00\u4e2a\u6807\u5fd7\u4f4d&#xff0c;\u8868\u793a&#034;\u975e\u963b\u585e\u6a21\u5f0f&#034;&#xff1a;\u5982\u679c\u5f53\u524d\u6ca1\u6709\u5df2\u9000\u51fa\u7684\u5b50\u8fdb\u7a0b&#xff0c;\u7acb\u5373\u8fd4\u56de (0, 0) \u800c\u4e0d\u662f\u963b\u585e\u7b49\u5f85\u3002\u5728 SIGCHLD \u5904\u7406\u5668\u4e2d&#xff0c;\u6211\u4eec\u7528 while True \u5faa\u73af\u914d\u5408 WNOHANG&#xff0c;\u4e00\u6b21\u6027\u56de\u6536\u6240\u6709\u5df2\u9000\u51fa\u7684\u5b50\u8fdb\u7a0b\u2014\u2014\u8fd9\u5f88\u91cd\u8981&#xff0c;\u56e0\u4e3a\u591a\u4e2a\u5b50\u8fdb\u7a0b\u53ef\u80fd\u540c\u65f6\u9000\u51fa&#xff0c;\u4f46 SIGCHLD \u4fe1\u53f7\u53ef\u80fd\u53ea\u53d1\u9001\u4e00\u6b21&#xff08;\u4fe1\u53f7\u4e0d\u4f1a\u6392\u961f&#xff09;\u3002<\/p>\n<h3>\u5341\u4e00\u3001\u8fdb\u7a0b\u72b6\u6001\u7801\u5168\u89e3\u6790<\/h3>\n<p>\u5728\u6392\u67e5\u50f5\u5c38\u8fdb\u7a0b\u548c\u5b64\u513f\u8fdb\u7a0b\u65f6&#xff0c;\u6211\u4eec\u4f1a\u9891\u7e41\u770b\u5230\u8fdb\u7a0b\u7684\u72b6\u6001\u7801\u3002\u8fd9\u91cc\u5bf9 ps \u8f93\u51fa\u4e2d\u7684 STAT \u5b57\u6bb5\u505a\u4e00\u4e2a\u5b8c\u6574\u89e3\u6790&#xff0c;\u8fd9\u5728\u5b9e\u9645\u6392\u67e5\u4e2d\u975e\u5e38\u5b9e\u7528\u3002<\/p>\n<p>STAT \u5b57\u6bb5\u53ef\u4ee5\u5305\u542b\u591a\u4e2a\u5b57\u7b26&#xff0c;\u7b2c\u4e00\u4e2a\u5b57\u7b26\u662f\u4e3b\u72b6\u6001&#xff0c;\u540e\u9762\u7684\u5b57\u7b26\u662f\u9644\u52a0\u6807\u8bb0&#xff1a;<\/p>\n<p>\u4e3b\u72b6\u6001\u5b57\u7b26&#xff1a;<\/p>\n<table>\n<tr>\u5b57\u7b26\u542b\u4e49\u5178\u578b\u573a\u666f<\/tr>\n<tbody>\n<tr>\n<td>R<\/td>\n<td>Running&#xff08;\u8fd0\u884c\u4e2d\u6216\u53ef\u8fd0\u884c&#xff09;<\/td>\n<td>\u8fdb\u7a0b\u6b63\u5728 CPU \u4e0a\u6267\u884c&#xff0c;\u6216\u5728\u8fd0\u884c\u961f\u5217\u4e2d\u7b49\u5f85<\/td>\n<\/tr>\n<tr>\n<td>S<\/td>\n<td>Sleeping&#xff08;\u53ef\u4e2d\u65ad\u7761\u7720&#xff09;<\/td>\n<td>\u7b49\u5f85\u67d0\u4e2a\u4e8b\u4ef6&#xff0c;\u5982 I\/O \u5b8c\u6210\u3001\u5b9a\u65f6\u5668\u89e6\u53d1\u3001\u4fe1\u53f7\u5230\u8fbe<\/td>\n<\/tr>\n<tr>\n<td>D<\/td>\n<td>Disk sleep&#xff08;\u4e0d\u53ef\u4e2d\u65ad\u7761\u7720&#xff09;<\/td>\n<td>\u7b49\u5f85\u78c1\u76d8 I\/O&#xff0c;\u6b64\u72b6\u6001\u7684\u8fdb\u7a0b\u4e0d\u80fd\u88ab kill<\/td>\n<\/tr>\n<tr>\n<td>T<\/td>\n<td>Stopped&#xff08;\u5df2\u505c\u6b62&#xff09;<\/td>\n<td>\u88ab SIGSTOP \u6216 Ctrl&#043;Z \u6682\u505c<\/td>\n<\/tr>\n<tr>\n<td>t<\/td>\n<td>Tracing stop&#xff08;\u8ddf\u8e2a\u505c\u6b62&#xff09;<\/td>\n<td>\u88ab\u8c03\u8bd5\u5668&#xff08;\u5982 gdb\/strace&#xff09;\u6682\u505c<\/td>\n<\/tr>\n<tr>\n<td>Z<\/td>\n<td>Zombie&#xff08;\u50f5\u5c38&#xff09;<\/td>\n<td>\u5df2\u9000\u51fa\u4f46\u672a\u88ab\u7236\u8fdb\u7a0b\u56de\u6536<\/td>\n<\/tr>\n<tr>\n<td>X<\/td>\n<td>Dead&#xff08;\u6b7b\u4ea1&#xff09;<\/td>\n<td>\u5373\u5c06\u88ab\u5220\u9664&#xff0c;\u51e0\u4e4e\u4e0d\u4f1a\u5728 ps \u8f93\u51fa\u4e2d\u770b\u5230<\/td>\n<\/tr>\n<tr>\n<td>I<\/td>\n<td>Idle&#xff08;\u7a7a\u95f2&#xff09;<\/td>\n<td>\u5185\u6838\u7ebf\u7a0b&#xff0c;\u5904\u4e8e\u7a7a\u95f2\u72b6\u6001<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u9644\u52a0\u6807\u8bb0\u5b57\u7b26&#xff1a;<\/p>\n<table>\n<tr>\u5b57\u7b26\u542b\u4e49<\/tr>\n<tbody>\n<tr>\n<td>&lt;<\/td>\n<td>\u9ad8\u4f18\u5148\u7ea7\u8fdb\u7a0b&#xff08;nice \u503c\u4e3a\u8d1f&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>N<\/td>\n<td>\u4f4e\u4f18\u5148\u7ea7\u8fdb\u7a0b&#xff08;nice \u503c\u4e3a\u6b63&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>L<\/td>\n<td>\u6709\u5185\u5b58\u9875\u88ab\u9501\u5b9a\u5728\u5185\u5b58\u4e2d&#xff08;\u5e38\u89c1\u4e8e\u5b9e\u65f6\u8fdb\u7a0b&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>s<\/td>\n<td>\u4f1a\u8bdd\u9886\u5bfc\u8005&#xff08;Session Leader&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>&#043;<\/td>\n<td>\u5c5e\u4e8e\u524d\u53f0\u8fdb\u7a0b\u7ec4<\/td>\n<\/tr>\n<tr>\n<td>l<\/td>\n<td>\u591a\u7ebf\u7a0b\u8fdb\u7a0b<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4e3e\u51e0\u4e2a\u5e38\u89c1\u7ec4\u5408&#xff1a;<\/p>\n<ul>\n<li>Ss &#8211; \u5904\u4e8e\u7761\u7720\u72b6\u6001&#xff0c;\u4e14\u662f\u4f1a\u8bdd\u9886\u5bfc\u8005&#xff08;\u5e38\u89c1\u4e8e sshd\u3001bash \u7b49&#xff09;<\/li>\n<li>Sl &#8211; \u7761\u7720\u4e2d\u7684\u591a\u7ebf\u7a0b\u8fdb\u7a0b&#xff08;\u5e38\u89c1\u4e8e Java\u3001Node.js \u8fdb\u7a0b&#xff09;<\/li>\n<li>R&#043; &#8211; \u5728\u524d\u53f0\u8fd0\u884c\u4e2d<\/li>\n<li>Z&#043; &#8211; \u5728\u524d\u53f0\u7684\u50f5\u5c38\u8fdb\u7a0b&#xff08;\u4f60\u4e3b\u52a8\u8fd0\u884c\u7684\u7a0b\u5e8f\u4ea7\u751f\u7684&#xff09;<\/li>\n<li>Zs &#8211; \u4f1a\u8bdd\u9886\u5bfc\u8005\u7684\u50f5\u5c38&#xff08;\u6bd4\u8f83\u5c11\u89c1&#xff09;<\/li>\n<li>D &#8211; \u4e0d\u53ef\u4e2d\u65ad\u7761\u7720&#xff08;\u51fa\u73b0\u5927\u91cf D \u72b6\u6001\u901a\u5e38\u610f\u5473\u7740\u78c1\u76d8 I\/O \u95ee\u9898&#xff09;<\/li>\n<\/ul>\n<h3>\u5341\u4e8c\u3001\u6d41\u7a0b\u56fe<\/h3>\n<h4>12.1 \u8fdb\u7a0b\u5b8c\u6574\u751f\u547d\u5468\u671f\u72b6\u6001\u56fe<\/h4>\n<p>\u76f8\u5173\u793a\u610f\u56fe\u7ed8\u5236\u5982\u4e0b&#xff0c;\u4ec5\u4f9b\u5927\u5bb6\u4fdd\u5b58\u67e5\u9605&#xff1a;<\/p>\n<p> <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261026160135-6adf795f2906a.png\" \/> <\/p>\n<p>\u8fd9\u5f20\u72b6\u6001\u56fe\u5c55\u793a\u4e86 Linux \u8fdb\u7a0b\u5b8c\u6574\u7684\u751f\u547d\u5468\u671f\u3002\u6ce8\u610f Zombie \u662f\u4e00\u4e2a&#034;\u672b\u6001&#034;&#xff0c;\u8fdb\u7a0b\u8fdb\u5165 Zombie \u72b6\u6001\u540e\u53ea\u80fd\u901a\u8fc7\u7236\u8fdb\u7a0b\u8c03\u7528 wait() \u6765\u5f7b\u5e95\u6d88\u4ea1&#xff0c;\u6ca1\u6709\u5176\u4ed6\u8def\u5f84\u3002\u8fd9\u4e5f\u89e3\u91ca\u4e86\u4e3a\u4ec0\u4e48\u5bf9\u50f5\u5c38\u8fdb\u7a0b\u4f7f\u7528 kill \u65e0\u6548\u2014\u2014\u5b83\u5df2\u7ecf\u6b7b\u4e86&#xff0c;\u53ea\u662f&#034;\u5c38\u4f53&#034;\u8fd8\u6ca1\u6709\u88ab\u6e05\u7406\u3002<\/p>\n<h4>12.2 \u50f5\u5c38\u8fdb\u7a0b\u4ea7\u751f\u4e0e\u6d88\u4ea1\u6d41\u7a0b<\/h4>\n<p>\u76f8\u5173\u793a\u610f\u56fe\u7ed8\u5236\u5982\u4e0b&#xff0c;\u4ec5\u4f9b\u5927\u5bb6\u4fdd\u5b58\u67e5\u9605&#xff1a;<\/p>\n<p> <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261026160138-6adf7962ccf06.png\" \/> <\/p>\n<p>\u8fd9\u5f20\u56fe\u6e05\u695a\u5730\u5c55\u793a\u4e86\u50f5\u5c38\u8fdb\u7a0b\u7684\u4e24\u6761\u6d88\u4ea1\u8def\u5f84&#xff1a;\u4e00\u662f\u7236\u8fdb\u7a0b\u53ca\u65f6\u8c03\u7528 wait() \u6b63\u5e38\u56de\u6536&#xff1b;\u4e8c\u662f\u7236\u8fdb\u7a0b\u9000\u51fa\u540e\u7531 systemd \u4ee3\u4e3a\u56de\u6536\u3002\u53ea\u6709\u7236\u8fdb\u7a0b\u957f\u671f\u4e0d\u9000\u51fa\u4e14\u6301\u7eed\u4ea7\u751f\u50f5\u5c38&#xff0c;\u624d\u4f1a\u8d70\u5411\u6700\u574f\u7684\u7ed3\u5c40\u2014\u2014PID \u8017\u5c3d\u3002<\/p>\n<h4>12.3 \u5b64\u513f\u8fdb\u7a0b\u88ab init \u63a5\u7ba1\u6d41\u7a0b<\/h4>\n<p>\u76f8\u5173\u793a\u610f\u56fe\u7ed8\u5236\u5982\u4e0b&#xff0c;\u4ec5\u4f9b\u5927\u5bb6\u4fdd\u5b58\u67e5\u9605&#xff1a;<\/p>\n<p> <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261026160139-6adf7963e6d5a.png\" \/> <\/p>\n<p>\u8fd9\u5f20\u65f6\u5e8f\u56fe\u5c55\u793a\u4e86\u5b64\u513f\u8fdb\u7a0b\u88ab systemd \u63a5\u7ba1\u7684\u5b8c\u6574\u8fc7\u7a0b\u3002\u5173\u952e\u6b65\u9aa4\u662f\u5185\u6838\u5728\u7236\u8fdb\u7a0b\u9000\u51fa\u65f6\u81ea\u52a8\u6267\u884c\u7684 re-parenting \u64cd\u4f5c\u2014\u2014\u8fd9\u5b8c\u5168\u662f\u5185\u6838\u884c\u4e3a&#xff0c;systemd \u4e0d\u9700\u8981\u505a\u4efb\u4f55\u4e3b\u52a8\u64cd\u4f5c&#xff0c;\u53ea\u9700\u8981\u5728\u5b64\u513f\u8fdb\u7a0b\u9000\u51fa\u65f6\u6b63\u5e38\u5904\u7406 SIGCHLD \u4fe1\u53f7&#xff08;systemd \u4f5c\u4e3a init \u7cfb\u7edf&#xff0c;\u81ea\u7136\u4f1a\u8fd9\u4e48\u505a&#xff09;\u3002<\/p>\n<h4>12.4 \u751f\u4ea7\u73af\u5883\u50f5\u5c38\u8fdb\u7a0b\u6392\u67e5\u6d41\u7a0b<\/h4>\n<p>\u76f8\u5173\u793a\u610f\u56fe\u7ed8\u5236\u5982\u4e0b&#xff0c;\u4ec5\u4f9b\u5927\u5bb6\u4fdd\u5b58\u67e5\u9605&#xff1a;<\/p>\n<p> <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261026160141-6adf796518a60.png\" \/> <\/p>\n<p>\u8fd9\u5f20\u6392\u67e5\u6d41\u7a0b\u56fe\u662f\u6211\u5728\u5b9e\u9645\u5de5\u4f5c\u4e2d\u603b\u7ed3\u51fa\u6765\u7684\u6392\u67e5\u8def\u5f84\u3002\u6838\u5fc3\u539f\u5219\u662f&#xff1a;\u5148\u627e\u7236\u8fdb\u7a0b&#xff0c;\u518d\u8bc4\u4f30\u5f71\u54cd&#xff0c;\u6700\u540e\u9009\u62e9\u5408\u9002\u624b\u6bb5\u5904\u7406\u3002\u4e0d\u8981\u4e00\u4e0a\u6765\u5c31 kill -9&#xff0c;\u90a3\u4f1a\u5e26\u6765\u66f4\u591a\u95ee\u9898\u3002<\/p>\n<h3>\u5341\u4e09\u3001\u6df1\u5165\u539f\u7406&#xff1a;\u5185\u6838\u89c6\u89d2\u4e0b\u7684\u8fdb\u7a0b\u56de\u6536<\/h3>\n<p>\u5982\u679c\u4f60\u53ea\u662f\u8fd0\u7ef4\u89d2\u8272&#xff0c;\u524d\u9762\u7684\u5185\u5bb9\u5df2\u7ecf\u8db3\u591f\u4f7f\u7528\u4e86\u3002\u8fd9\u4e00\u8282\u6df1\u5165\u4e00\u4e9b&#xff0c;\u4ece\u5185\u6838\u89d2\u5ea6\u8bb2\u6e05\u695a\u5e95\u5c42\u673a\u5236&#xff0c;\u9002\u5408\u5bf9\u539f\u7406\u611f\u5174\u8da3\u7684\u8bfb\u8005&#xff0c;\u6216\u8005\u9700\u8981\u5199\u7cfb\u7edf\u7ea7\u7a0b\u5e8f\u7684\u5f00\u53d1\u8005\u3002<\/p>\n<h4>13.1 \u8fdb\u7a0b\u63cf\u8ff0\u7b26\u4e0e task_struct<\/h4>\n<p>\u5728 Linux \u5185\u6838\u4e2d&#xff0c;\u6bcf\u4e2a\u8fdb\u7a0b\u90fd\u7531\u4e00\u4e2a task_struct \u7ed3\u6784\u4f53\u6765\u63cf\u8ff0\u3002\u8fd9\u4e2a\u7ed3\u6784\u4f53\u662f\u8fdb\u7a0b\u5728\u5185\u6838\u4e2d\u7684&#034;\u8eab\u4efd\u8bc1&#034;&#xff0c;\u5305\u542b\u4e86\u8fdb\u7a0b\u8fd0\u884c\u6240\u9700\u7684\u6240\u6709\u4fe1\u606f\u3002<\/p>\n<p>\u5f53\u8fdb\u7a0b\u8c03\u7528 exit() \u65f6&#xff0c;\u5185\u6838\u4f1a&#xff1a;<\/p>\n<li>\u901a\u8fc7 do_exit() \u51fd\u6570\u5f00\u59cb\u8fdb\u7a0b\u9000\u51fa\u6d41\u7a0b<\/li>\n<li>\u8c03\u7528 exit_mm() \u91ca\u653e\u8fdb\u7a0b\u7684\u5185\u5b58\u7a7a\u95f4<\/li>\n<li>\u8c03\u7528 exit_sem()\u3001exit_files()\u3001exit_fs() \u7b49\u51fd\u6570\u91ca\u653e\u5404\u79cd\u8d44\u6e90<\/li>\n<li>\u4fee\u6539\u8fdb\u7a0b\u72b6\u6001\u4e3a EXIT_ZOMBIE<\/li>\n<li>\u8c03\u7528 do_notify_parent() \u5411\u7236\u8fdb\u7a0b\u53d1\u9001 SIGCHLD \u4fe1\u53f7<\/li>\n<li>\u8c03\u7528 schedule() \u8ba9\u51fa CPU&#xff0c;\u6c38\u4e0d\u8fd4\u56de<\/li>\n<p>\u6b64\u65f6 task_struct \u4e2d\u4fdd\u7559\u7684\u5173\u952e\u5b57\u6bb5\u5305\u62ec&#xff1a;<\/p>\n<p>exit_code    &#8211; \u8fdb\u7a0b\u9000\u51fa\u7801&#xff08;\u4f9b\u7236\u8fdb\u7a0b\u8bfb\u53d6&#xff09;<br \/>\nexit_signal  &#8211; \u5bfc\u81f4\u9000\u51fa\u7684\u4fe1\u53f7\u7f16\u53f7<br \/>\nru_utime     &#8211; \u7528\u6237\u6001 CPU \u65f6\u95f4&#xff08;\u4f9b getrusage \u4f7f\u7528&#xff09;<br \/>\nru_stime     &#8211; \u5185\u6838\u6001 CPU \u65f6\u95f4<br \/>\n&#8230;<\/p>\n<p>\u8fd9\u4e9b\u4fe1\u606f\u975e\u5e38\u5c11&#xff08;\u51e0\u5341\u5230\u51e0\u767e\u5b57\u8282&#xff09;&#xff0c;\u4f46\u5728\u7236\u8fdb\u7a0b\u8c03\u7528 wait() \u4e4b\u524d&#xff0c;\u5fc5\u987b\u4fdd\u7559\u3002<\/p>\n<h4>13.2 wait() \u7cfb\u5217\u7cfb\u7edf\u8c03\u7528<\/h4>\n<p>\u7236\u8fdb\u7a0b\u901a\u8fc7\u4ee5\u4e0b\u51e0\u4e2a\u7cfb\u7edf\u8c03\u7528\u6765\u56de\u6536\u5b50\u8fdb\u7a0b&#xff1a;<\/p>\n<table>\n<tr>\u8c03\u7528\u7279\u70b9<\/tr>\n<tbody>\n<tr>\n<td>wait(status)<\/td>\n<td>\u7b49\u5f85\u4efb\u610f\u5b50\u8fdb\u7a0b\u9000\u51fa&#xff08;\u963b\u585e&#xff09;&#xff0c;\u53ea\u80fd\u56de\u6536\u4e00\u4e2a<\/td>\n<\/tr>\n<tr>\n<td>waitpid(pid, status, options)<\/td>\n<td>\u7b49\u5f85\u6307\u5b9a PID \u7684\u5b50\u8fdb\u7a0b&#xff0c;\u53ef\u8bbe\u7f6e\u975e\u963b\u585e\u9009\u9879<\/td>\n<\/tr>\n<tr>\n<td>waitid(idtype, id, infop, options)<\/td>\n<td>\u66f4\u7075\u6d3b\u7684\u7b49\u5f85&#xff0c;\u63d0\u4f9b\u66f4\u591a\u9000\u51fa\u4fe1\u606f<\/td>\n<\/tr>\n<tr>\n<td>wait3(status, options, rusage)<\/td>\n<td>\u5e26\u8d44\u6e90\u4f7f\u7528\u7edf\u8ba1\u7684\u7b49\u5f85<\/td>\n<\/tr>\n<tr>\n<td>wait4(pid, status, options, rusage)<\/td>\n<td>\u7ed3\u5408 waitpid \u548c wait3<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5728 C \u8bed\u8a00\u4e2d&#xff0c;\u6b63\u786e\u7684\u975e\u963b\u585e\u56de\u6536\u5b50\u8fdb\u7a0b\u7684\u60ef\u7528\u6cd5\u662f&#xff1a;<\/p>\n<p><span class=\"token comment\">\/*<br \/>\n * \u5728 SIGCHLD \u4fe1\u53f7\u5904\u7406\u5668\u4e2d\u4f7f\u7528\u6b64\u60ef\u7528\u6cd5<br \/>\n * \u5faa\u73af\u56de\u6536\u6240\u6709\u5df2\u9000\u51fa\u7684\u5b50\u8fdb\u7a0b<br \/>\n * \u5fc5\u987b\u4f7f\u7528 WNOHANG&#xff0c;\u5426\u5219\u5728\u6ca1\u6709\u5df2\u9000\u51fa\u5b50\u8fdb\u7a0b\u65f6\u4f1a\u963b\u585e<br \/>\n *\/<\/span><br \/>\n<span class=\"token keyword\">void<\/span> <span class=\"token function\">sigchld_handler<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span> signum<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token keyword\">int<\/span> saved_errno <span class=\"token operator\">&#061;<\/span> errno<span class=\"token punctuation\">;<\/span>  <span class=\"token comment\">\/\/ \u4fdd\u5b58 errno&#xff0c;\u4fe1\u53f7\u5904\u7406\u5668\u53ef\u80fd\u88ab\u4e2d\u65ad<\/span><br \/>\n    <span class=\"token keyword\">int<\/span> status<span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token class-name\">pid_t<\/span> pid<span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/* \u5faa\u73af\u76f4\u5230\u6ca1\u6709\u66f4\u591a\u5df2\u9000\u51fa\u7684\u5b50\u8fdb\u7a0b *\/<\/span><br \/>\n    <span class=\"token keyword\">while<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">(<\/span>pid <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">waitpid<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">&#8211;<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token operator\">&amp;<\/span>status<span class=\"token punctuation\">,<\/span> WNOHANG<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&gt;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token function\">WIFEXITED<\/span><span class=\"token punctuation\">(<\/span>status<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n            <span class=\"token comment\">\/* \u5b50\u8fdb\u7a0b\u6b63\u5e38\u9000\u51fa *\/<\/span><br \/>\n            <span class=\"token function\">printf<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\u5b50\u8fdb\u7a0b %d \u4ee5\u9000\u51fa\u7801 %d \u6b63\u5e38\u9000\u51fa\\\\n&#034;<\/span><span class=\"token punctuation\">,<\/span> pid<span class=\"token punctuation\">,<\/span> <span class=\"token function\">WEXITSTATUS<\/span><span class=\"token punctuation\">(<\/span>status<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">else<\/span> <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token function\">WIFSIGNALED<\/span><span class=\"token punctuation\">(<\/span>status<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n            <span class=\"token comment\">\/* \u5b50\u8fdb\u7a0b\u88ab\u4fe1\u53f7\u6740\u6b7b *\/<\/span><br \/>\n            <span class=\"token function\">printf<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\u5b50\u8fdb\u7a0b %d \u88ab\u4fe1\u53f7 %d \u6740\u6b7b\\\\n&#034;<\/span><span class=\"token punctuation\">,<\/span> pid<span class=\"token punctuation\">,<\/span> <span class=\"token function\">WTERMSIG<\/span><span class=\"token punctuation\">(<\/span>status<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    errno <span class=\"token operator\">&#061;<\/span> saved_errno<span class=\"token punctuation\">;<\/span>  <span class=\"token comment\">\/\/ \u6062\u590d errno<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<h4>13.3 SIGCHLD \u4fe1\u53f7\u7684\u4f5c\u7528<\/h4>\n<p>SIGCHLD \u4fe1\u53f7\u662f\u8fdb\u7a0b\u7ba1\u7406\u4e2d\u7684\u4e00\u4e2a\u5173\u952e\u4fe1\u53f7&#xff0c;\u5b83\u5728\u4ee5\u4e0b\u60c5\u51b5\u4e0b\u7531\u5185\u6838\u53d1\u9001\u7ed9\u7236\u8fdb\u7a0b&#xff1a;<\/p>\n<li>\u5b50\u8fdb\u7a0b\u7ec8\u6b62&#xff08;\u6b63\u5e38\u9000\u51fa\u6216\u88ab\u4fe1\u53f7\u6740\u6b7b&#xff09;<\/li>\n<li>\u5b50\u8fdb\u7a0b\u88ab SIGSTOP \u505c\u6b62&#xff08;\u5982\u679c\u8bbe\u7f6e\u4e86 SA_NOCLDSTOP \u6807\u5fd7\u5219\u4e0d\u53d1\u9001&#xff09;<\/li>\n<li>\u5b50\u8fdb\u7a0b\u88ab SIGCONT \u6062\u590d&#xff08;\u5728 Linux 2.6&#043; \u4e2d&#xff09;<\/li>\n<p>SIGCHLD \u7684\u9ed8\u8ba4\u5904\u7406\u65b9\u5f0f\u662f\u5ffd\u7565&#xff08;SIG_DFL \u5b9e\u9645\u4e0a\u662f\u5ffd\u7565\u6b64\u4fe1\u53f7\u7684&#xff09;\u3002\u4f46\u6709\u4e00\u4e2a\u91cd\u8981\u7684\u7ec6\u8282&#xff1a;\u5373\u4f7f\u7236\u8fdb\u7a0b&#034;\u5ffd\u7565&#034;\u4e86 SIGCHLD&#xff0c;\u5728\u67d0\u4e9b\u5b9e\u73b0\u4e2d\u5185\u6838\u4e5f\u4f1a\u81ea\u52a8\u56de\u6536\u5b50\u8fdb\u7a0b&#xff08;\u8fd9\u53d6\u51b3\u4e8e\u5177\u4f53\u7684 SA_NOCLDWAIT \u6807\u5fd7\u8bbe\u7f6e&#xff09;\u3002<\/p>\n<p>\u5728 Ubuntu&#xff08;Linux \u5185\u6838&#xff09;\u4e2d&#xff0c;\u5982\u679c\u4f60\u663e\u5f0f\u5730\u5c06 SIGCHLD \u8bbe\u7f6e\u4e3a SIG_IGN&#xff08;\u800c\u4e0d\u662f\u9ed8\u8ba4\u7684\u5ffd\u7565&#xff09;&#xff0c;\u5185\u6838\u4f1a\u81ea\u52a8\u56de\u6536\u6240\u6709\u5b50\u8fdb\u7a0b&#xff0c;\u4e0d\u4f1a\u4ea7\u751f\u50f5\u5c38\u8fdb\u7a0b\u3002\u8fd9\u662f\u4e00\u4e2a\u5f88\u6709\u7528\u7684\u7279\u6027&#xff1a;<\/p>\n<p><span class=\"token comment\">\/*<br \/>\n * \u901a\u8fc7\u663e\u5f0f\u8bbe\u7f6e SIGCHLD \u4e3a SIG_IGN&#xff0c;<br \/>\n * \u544a\u8bc9\u5185\u6838\u81ea\u52a8\u56de\u6536\u5b50\u8fdb\u7a0b&#xff08;\u4e0d\u4ea7\u751f\u50f5\u5c38&#xff09;<br \/>\n * \u6ce8\u610f&#xff1a;\u8fd9\u6837\u505a\u4e4b\u540e&#xff0c;\u4f60\u4e5f\u65e0\u6cd5\u901a\u8fc7 wait() \u83b7\u53d6\u5b50\u8fdb\u7a0b\u7684\u9000\u51fa\u72b6\u6001\u4e86<br \/>\n *\/<\/span><br \/>\n<span class=\"token function\">signal<\/span><span class=\"token punctuation\">(<\/span>SIGCHLD<span class=\"token punctuation\">,<\/span> SIG_IGN<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>\u4f46\u8981\u6ce8\u610f&#xff0c;\u8fd9\u79cd\u65b9\u5f0f\u4f60\u4e22\u5931\u4e86\u83b7\u53d6\u5b50\u8fdb\u7a0b\u9000\u51fa\u72b6\u6001\u7684\u80fd\u529b\u3002\u5982\u679c\u4f60\u9700\u8981\u77e5\u9053\u5b50\u8fdb\u7a0b\u662f\u5426\u6210\u529f\u5b8c\u6210\u4e86\u4efb\u52a1&#xff0c;\u8fd8\u662f\u9700\u8981\u663e\u5f0f\u8c03\u7528 wait()\u3002<\/p>\n<h4>13.4 \u4e3a\u4ec0\u4e48 PID 1 \u6c38\u8fdc\u4e0d\u4f1a\u4ea7\u751f\u50f5\u5c38\u8fdb\u7a0b<\/h4>\n<p>\u8fd9\u662f\u4e00\u4e2a\u6709\u8da3\u7684\u8bbe\u8ba1\u95ee\u9898&#xff1a;systemd&#xff08;PID 1&#xff09;\u7ba1\u7406\u7740\u7cfb\u7edf\u4e2d\u6240\u6709\u7684\u5b64\u513f\u8fdb\u7a0b&#xff0c;\u90a3 systemd \u81ea\u5df1\u4f1a\u4e0d\u4f1a\u4ea7\u751f\u50f5\u5c38\u8fdb\u7a0b&#xff1f;<\/p>\n<p>\u7b54\u6848\u662f&#xff1a;\u7406\u8bba\u4e0a\u53ef\u4ee5&#xff0c;\u4f46 systemd \u7684\u5b9e\u73b0\u4fdd\u8bc1\u4e86\u4e0d\u4f1a\u3002<\/p>\n<p>systemd \u6709\u4e00\u4e2a\u4e13\u95e8\u7684\u4e8b\u4ef6\u5faa\u73af\u6765\u5904\u7406 SIGCHLD \u4fe1\u53f7\u3002\u5f53\u4efb\u4f55\u5b50\u8fdb\u7a0b&#xff08;\u5305\u62ec\u901a\u8fc7 re-parenting \u6258\u7ba1\u7ed9\u5b83\u7684\u5b64\u513f\u8fdb\u7a0b&#xff09;\u9000\u51fa\u65f6&#xff0c;systemd \u90fd\u4f1a\u53ca\u65f6\u8c03\u7528 waitpid() \u6765\u56de\u6536\u5b83\u3002\u8fd9\u662f systemd \u4f5c\u4e3a init \u7cfb\u7edf\u7684\u6838\u5fc3\u804c\u8d23\u4e4b\u4e00\u3002<\/p>\n<p>\u4e8b\u5b9e\u4e0a&#xff0c;Linux \u5185\u6838\u4e5f\u7ed9\u4e88\u4e86 PID 1 \u7279\u6b8a\u5f85\u9047&#xff1a;\u5982\u679c PID 1 \u9000\u51fa&#xff0c;\u5185\u6838\u4f1a\u89e6\u53d1 kernel panic&#xff08;\u5185\u6838\u6050\u614c&#xff09;&#xff0c;\u56e0\u4e3a init \u8fdb\u7a0b\u7684\u9000\u51fa\u610f\u5473\u7740\u6ca1\u6709\u4efb\u4f55\u8fdb\u7a0b\u80fd\u591f\u7ee7\u7eed\u7ba1\u7406\u7cfb\u7edf\u4e86\u3002<\/p>\n<h3>\u5341\u56db\u3001Docker \u5bb9\u5668\u4e2d\u7684\u50f5\u5c38\u8fdb\u7a0b\u95ee\u9898<\/h3>\n<p>\u8fd9\u4e00\u8282\u975e\u5e38\u5b9e\u7528\u3002\u5728 Docker \u5bb9\u5668\u53d8\u5f97\u666e\u53ca\u4e4b\u540e&#xff0c;\u50f5\u5c38\u8fdb\u7a0b\u95ee\u9898\u6709\u4e86\u4e00\u4e2a\u65b0\u7684\u9ad8\u53d1\u573a\u666f&#xff0c;\u5f88\u591a\u5f00\u53d1\u8005\u5728\u5bb9\u5668\u91cc\u9047\u5230\u50f5\u5c38\u8fdb\u7a0b\u65f6\u6478\u4e0d\u7740\u5934\u8111\u3002<\/p>\n<h4>14.1 \u5bb9\u5668\u5185\u7684 PID 1 \u9677\u9631<\/h4>\n<p>\u5728 Docker \u5bb9\u5668\u4e2d&#xff0c;\u4f60\u7684\u5e94\u7528\u8fdb\u7a0b\u901a\u5e38\u5c31\u662f PID 1\u3002\u8fd9\u770b\u8d77\u6765\u6ca1\u4ec0\u4e48\u95ee\u9898&#xff0c;\u4f46\u6709\u4e00\u4e2a\u91cd\u8981\u7684\u5dee\u522b&#xff1a;<\/p>\n<p>\u666e\u901a\u5e94\u7528\u8fdb\u7a0b\u5e76\u6ca1\u6709\u5b9e\u73b0 init \u7684\u804c\u8d23\u3002<\/p>\n<p>\u4e5f\u5c31\u662f\u8bf4&#xff0c;\u5f53\u4f60\u7684 Node.js \u5e94\u7528&#xff08;PID 1&#xff09;fork \u4e86\u5b50\u8fdb\u7a0b&#xff0c;\u6216\u8005\u901a\u8fc7 exec \u8c03\u7528\u4e86\u5916\u90e8\u547d\u4ee4&#xff0c;\u8fd9\u4e9b\u5b50\u8fdb\u7a0b\u9000\u51fa\u540e&#xff0c;\u5982\u679c\u4f60\u7684 Node.js \u5e94\u7528\u6ca1\u6709\u6b63\u786e\u56de\u6536\u5b83\u4eec&#xff0c;\u5c31\u4f1a\u5728\u5bb9\u5668\u5185\u79ef\u7d2f\u50f5\u5c38\u8fdb\u7a0b\u3002<\/p>\n<p>\u66f4\u7cdf\u7cd5\u7684\u662f&#xff0c;\u5f53\u5bb9\u5668\u5185\u6709\u5b64\u513f\u8fdb\u7a0b&#xff08;\u6bd4\u5982\u4f60\u7684 Node.js \u5e94\u7528 fork \u4e86\u5b50\u8fdb\u7a0b\u540e\u5d29\u6e83\u4e86&#xff09;\u65f6&#xff0c;\u672c\u6765\u5e94\u8be5\u88ab systemd \u63a5\u7ba1\u7684\u5b64\u513f\u8fdb\u7a0b&#xff0c;\u5728\u5bb9\u5668\u5185\u53ea\u80fd\u88ab\u6258\u7ba1\u7ed9 PID 1\u2014\u2014\u4e5f\u5c31\u662f\u4f60\u81ea\u5df1\u7684\u5e94\u7528\u3002\u5982\u679c\u4f60\u7684\u5e94\u7528&#xff08;PID 1&#xff09;\u5df2\u7ecf\u5d29\u6e83\u4e86&#xff0c;\u90a3\u5b64\u513f\u8fdb\u7a0b\u5c31\u771f\u7684\u65e0\u5bb6\u53ef\u5f52\u4e86\u3002<\/p>\n<p>\u5728\u5bb9\u5668\u5185\u67e5\u770b\u662f\u5426\u6709\u50f5\u5c38\u8fdb\u7a0b&#xff1a;<\/p>\n<p><span class=\"token comment\"># \u8fdb\u5165\u5bb9\u5668\u5185\u90e8\u68c0\u67e5<\/span><br \/>\n<span class=\"token function\">docker<\/span> <span class=\"token builtin class-name\">exec<\/span> <span class=\"token parameter variable\">-it<\/span> <span class=\"token operator\">&lt;<\/span>container_name<span class=\"token operator\">&gt;<\/span> <span class=\"token function\">ps<\/span> aux<br \/>\n<span class=\"token comment\"># \u67e5\u770b\u662f\u5426\u6709 Z \u72b6\u6001\u7684\u8fdb\u7a0b<\/span><\/p>\n<p><span class=\"token comment\"># \u6216\u8005\u4ece\u5bbf\u4e3b\u673a\u89d2\u5ea6\u67e5\u770b\u5bb9\u5668\u5185\u7684\u6240\u6709\u8fdb\u7a0b<\/span><br \/>\n<span class=\"token comment\"># &#8211;pid \u662f\u5bb9\u5668\u4f7f\u7528\u7684 PID namespace<\/span><br \/>\n<span class=\"token function\">ps<\/span> aux <span class=\"token operator\">|<\/span> <span class=\"token function\">grep<\/span> defunct<\/p>\n<h4>14.2 \u4f7f\u7528 tini \u89e3\u51b3\u5bb9\u5668\u50f5\u5c38\u8fdb\u7a0b\u95ee\u9898<\/h4>\n<p>tini \u662f\u4e00\u4e2a\u4e13\u4e3a\u5bb9\u5668\u8bbe\u8ba1\u7684\u8f7b\u91cf\u7ea7 init \u7cfb\u7edf&#xff0c;\u5b83\u7684\u6838\u5fc3\u529f\u80fd\u53ea\u6709\u4e24\u4e2a&#xff1a;<\/p>\n<li>\u6b63\u786e\u5904\u7406\u4fe1\u53f7\u8f6c\u53d1&#xff08;\u628a SIGTERM \u7b49\u4fe1\u53f7\u8f6c\u53d1\u7ed9\u5b50\u8fdb\u7a0b&#xff09;<\/li>\n<li>\u56de\u6536\u50f5\u5c38\u8fdb\u7a0b&#xff08;\u4f5c\u4e3a PID 1&#xff0c;\u5904\u7406\u597d\u6240\u6709\u5b64\u513f\u8fdb\u7a0b\u7684\u56de\u6536&#xff09;<\/li>\n<p>\u5728 Dockerfile \u4e2d\u4f7f\u7528 tini&#xff1a;<\/p>\n<p># \u65b9\u5f0f\u4e00&#xff1a;\u624b\u52a8\u5b89\u88c5 tini<br \/>\nFROM ubuntu:22.04<\/p>\n<p># \u5b89\u88c5 tini<br \/>\nRUN apt-get update &amp;&amp; apt-get install -y tini<\/p>\n<p># \u4f7f\u7528 tini \u4f5c\u4e3a ENTRYPOINT&#xff0c;\u5b83\u4f1a\u4f5c\u4e3a PID 1 \u8fd0\u884c<br \/>\n# \u7136\u540e tini \u4f1a exec \u4f60\u7684\u5b9e\u9645\u5e94\u7528\u8fdb\u7a0b<br \/>\nENTRYPOINT [&#034;\/usr\/bin\/tini&#034;, &#034;&#8211;&#034;]<\/p>\n<p># \u4f60\u7684\u5b9e\u9645\u5e94\u7528<br \/>\nCMD [&#034;python3&#034;, &#034;your_app.py&#034;]<\/p>\n<p># \u65b9\u5f0f\u4e8c&#xff1a;\u4f7f\u7528\u591a\u9636\u6bb5\u6784\u5efa\u4ece tini \u5b98\u65b9\u955c\u50cf\u590d\u5236<br \/>\nFROM tini AS tini-base<br \/>\nFROM ubuntu:22.04<br \/>\nCOPY &#8211;from&#061;tini-base \/tini \/tini<br \/>\nENTRYPOINT [&#034;\/tini&#034;, &#034;&#8211;&#034;]<br \/>\nCMD [&#034;your_app&#034;]<\/p>\n<p>tini \u7684\u5de5\u4f5c\u539f\u7406\u975e\u5e38\u6e05\u6670&#xff1a;\u5b83\u4f5c\u4e3a PID 1 \u8fd0\u884c&#xff0c;\u8d1f\u8d23&#xff1a;<\/p>\n<ul>\n<li>\u8c03\u7528 exec \u542f\u52a8\u4f60\u7684\u5b9e\u9645\u5e94\u7528&#xff08;\u6240\u4ee5\u4f60\u7684\u5e94\u7528\u4e0d\u4f1a\u662f PID 1&#xff09;<\/li>\n<li>\u6ce8\u518c SIGCHLD \u5904\u7406\u5668&#xff0c;\u53ca\u65f6\u56de\u6536\u6240\u6709\u5b64\u513f\u548c\u50f5\u5c38\u8fdb\u7a0b<\/li>\n<li>\u5c06 SIGTERM \u7b49\u4fe1\u53f7\u8f6c\u53d1\u7ed9\u4f60\u7684\u5e94\u7528\u8fdb\u7a0b<\/li>\n<\/ul>\n<h4>14.3 Docker \u5b98\u65b9\u63a8\u8350\u7684 init \u65b9\u6848<\/h4>\n<p>Docker \u5728 1.13 \u7248\u672c\u4e4b\u540e\u5185\u7f6e\u4e86\u5bf9 &#8211;init \u53c2\u6570\u7684\u652f\u6301&#xff08;\u5e95\u5c42\u4e5f\u662f\u57fa\u4e8e tini&#xff09;&#xff1a;<\/p>\n<p><span class=\"token comment\"># \u8fd0\u884c\u5bb9\u5668\u65f6\u4f7f\u7528 &#8211;init \u53c2\u6570<\/span><br \/>\n<span class=\"token comment\"># Docker \u4f1a\u81ea\u52a8\u5728\u5bb9\u5668\u5185\u6ce8\u5165\u4e00\u4e2a\u8f7b\u91cf\u7ea7 init \u8fdb\u7a0b<\/span><br \/>\n<span class=\"token function\">docker<\/span> run <span class=\"token parameter variable\">&#8211;init<\/span> my_image<\/p>\n<p><span class=\"token comment\"># \u5bf9\u4e8e Docker Compose&#xff0c;\u5728\u670d\u52a1\u914d\u7f6e\u4e2d\u6dfb\u52a0&#xff1a;<\/span><br \/>\n<span class=\"token comment\"># &#xff08;\u8fd9\u662f docker-compose.yml \u7684\u914d\u7f6e\u65b9\u5f0f&#xff09;<\/span><\/p>\n<p><span class=\"token comment\"># docker-compose.yml \u793a\u4f8b<\/span><br \/>\n<span class=\"token key atrule\">version<\/span><span class=\"token punctuation\">:<\/span> <span class=\"token string\">&#039;3.8&#039;<\/span><br \/>\n<span class=\"token key atrule\">services<\/span><span class=\"token punctuation\">:<\/span><br \/>\n  <span class=\"token key atrule\">app<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token key atrule\">image<\/span><span class=\"token punctuation\">:<\/span> my_app<span class=\"token punctuation\">:<\/span>latest<br \/>\n    <span class=\"token key atrule\">init<\/span><span class=\"token punctuation\">:<\/span> <span class=\"token boolean important\">true<\/span>  <span class=\"token comment\"># \u542f\u7528 init&#xff0c;\u89e3\u51b3\u5bb9\u5668\u5185\u50f5\u5c38\u8fdb\u7a0b\u95ee\u9898<\/span><br \/>\n    <span class=\"token key atrule\">ports<\/span><span class=\"token punctuation\">:<\/span><br \/>\n      <span class=\"token punctuation\">&#8211;<\/span> <span class=\"token string\">&#034;8080:8080&#034;<\/span><\/p>\n<p><span class=\"token comment\"># \u9a8c\u8bc1&#xff1a;\u8fdb\u5165\u5bb9\u5668\u540e&#xff0c;PID 1 \u5e94\u8be5\u662f tini \u800c\u4e0d\u662f\u4f60\u7684\u5e94\u7528<\/span><br \/>\n<span class=\"token function\">docker<\/span> <span class=\"token builtin class-name\">exec<\/span> <span class=\"token parameter variable\">-it<\/span> my_container <span class=\"token function\">ps<\/span> aux <span class=\"token operator\">|<\/span> <span class=\"token function\">head<\/span> <span class=\"token parameter variable\">-5<\/span><\/p>\n<p>\u9a8c\u8bc1\u8f93\u51fa\u5e94\u7c7b\u4f3c&#xff1a;<\/p>\n<p>PID   USER     TIME  COMMAND<br \/>\n    1 root      0:00 \/sbin\/docker-init &#8212; python3 your_app.py<br \/>\n    7 root      0:00 python3 your_app.py<\/p>\n<h3>\u5341\u4e94\u3001systemd \u4e0e\u8fdb\u7a0b\u751f\u547d\u5468\u671f\u7ba1\u7406<\/h3>\n<p>systemd \u4e0d\u53ea\u662f\u4e00\u4e2a&#034;\u670d\u52a1\u542f\u52a8\u5668&#034;&#xff0c;\u5b83\u5bf9\u8fdb\u7a0b\u751f\u547d\u5468\u671f\u7684\u7ba1\u7406\u975e\u5e38\u7cbe\u7ec6\u3002\u5728\u6392\u67e5\u548c\u5904\u7406\u50f5\u5c38\u8fdb\u7a0b\u76f8\u5173\u95ee\u9898\u65f6&#xff0c;\u4e86\u89e3 systemd \u7684\u884c\u4e3a\u5f88\u91cd\u8981\u3002<\/p>\n<h4>15.1 systemd \u5982\u4f55\u7ba1\u7406\u5b50\u8fdb\u7a0b<\/h4>\n<p>\u6bcf\u5f53 systemd \u542f\u52a8\u4e00\u4e2a\u670d\u52a1&#xff08;Unit&#xff09;&#xff0c;\u5b83\u4f1a&#xff1a;<\/p>\n<li>\u4e3a\u8be5\u670d\u52a1\u521b\u5efa\u4e00\u4e2a cgroup&#xff08;\u63a7\u5236\u7ec4&#xff09;&#xff0c;\u670d\u52a1\u7684\u6240\u6709\u5b50\u8fdb\u7a0b\u90fd\u5c5e\u4e8e\u8fd9\u4e2a cgroup<\/li>\n<li>\u8ddf\u8e2a\u8fd9\u4e2a cgroup \u4e2d\u7684\u6240\u6709\u8fdb\u7a0b<\/li>\n<li>\u5f53\u670d\u52a1\u505c\u6b62\u65f6&#xff0c;\u7ec8\u6b62 cgroup \u4e2d\u7684\u6240\u6709\u8fdb\u7a0b&#xff08;\u9ed8\u8ba4\u884c\u4e3a&#xff09;<\/li>\n<li>\u6b63\u786e\u5904\u7406 SIGCHLD&#xff0c;\u56de\u6536\u6240\u6709\u5b50\u8fdb\u7a0b<\/li>\n<p>\u8fd9\u610f\u5473\u7740&#xff0c;\u901a\u8fc7 systemd \u7ba1\u7406\u7684\u670d\u52a1&#xff0c;\u5176\u5b50\u8fdb\u7a0b\u7684\u751f\u547d\u5468\u671f\u4e5f\u7531 systemd \u63a5\u7ba1\u3002\u5373\u4f7f\u670d\u52a1\u4e3b\u8fdb\u7a0b fork \u51fa\u4e86\u5b50\u8fdb\u7a0b\u5e76\u5fd8\u8bb0\u56de\u6536&#xff0c;systemd \u4e5f\u4f1a\u901a\u8fc7 cgroup \u673a\u5236\u6765\u6e05\u7406\u3002<\/p>\n<p><span class=\"token comment\"># \u67e5\u770b\u67d0\u4e2a\u670d\u52a1\u7684 cgroup&#xff0c;\u4ee5 nginx \u4e3a\u4f8b<\/span><br \/>\nsystemctl status nginx <span class=\"token operator\">|<\/span> <span class=\"token function\">grep<\/span> CGroup<br \/>\n<span class=\"token comment\"># \u6216\u8005<\/span><br \/>\n<span class=\"token function\">cat<\/span> \/sys\/fs\/cgroup\/system.slice\/nginx.service\/cgroup.procs<\/p>\n<p><span class=\"token comment\"># \u67e5\u770b systemd \u7ba1\u7406\u4e0b\u7684\u8fdb\u7a0b\u6811<\/span><br \/>\n<span class=\"token comment\"># -a \u663e\u793a\u6240\u6709\u8fdb\u7a0b&#xff0c;-l \u663e\u793a\u5b8c\u6574\u547d\u4ee4\u884c&#xff0c;-p \u663e\u793a PID<\/span><br \/>\nsystemd-cgls<\/p>\n<h4>15.2 KillMode \u914d\u7f6e\u7684\u91cd\u8981\u6027<\/h4>\n<p>systemd \u670d\u52a1\u7684 KillMode \u53c2\u6570\u63a7\u5236\u7740\u5f53\u670d\u52a1\u505c\u6b62\u65f6&#xff0c;\u5982\u4f55\u7ec8\u6b62\u76f8\u5173\u8fdb\u7a0b\u3002\u8fd9\u4e2a\u53c2\u6570\u548c\u50f5\u5c38\u8fdb\u7a0b\u7684\u5173\u7cfb\u5f88\u5bc6\u5207&#xff1a;<\/p>\n<table>\n<tr>KillMode \u503c\u884c\u4e3a<\/tr>\n<tbody>\n<tr>\n<td>control-group&#xff08;\u9ed8\u8ba4&#xff09;<\/td>\n<td>\u7ec8\u6b62\u670d\u52a1 cgroup \u4e2d\u7684\u6240\u6709\u8fdb\u7a0b<\/td>\n<\/tr>\n<tr>\n<td>process<\/td>\n<td>\u53ea\u7ec8\u6b62\u4e3b\u8fdb\u7a0b&#xff08;MainPID&#xff09;&#xff0c;\u5b50\u8fdb\u7a0b\u4e0d\u53d7\u5f71\u54cd&#xff08;\u53ef\u80fd\u6210\u4e3a\u5b64\u513f&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>mixed<\/td>\n<td>\u4e3b\u8fdb\u7a0b\u53d1 SIGTERM&#xff0c;cgroup \u4e2d\u5176\u4ed6\u8fdb\u7a0b\u53d1 SIGKILL<\/td>\n<\/tr>\n<tr>\n<td>none<\/td>\n<td>\u4e0d\u7ec8\u6b62\u4efb\u4f55\u8fdb\u7a0b&#xff0c;\u53ea\u6267\u884c ExecStop \u547d\u4ee4<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u9ed8\u8ba4\u7684 control-group \u6a21\u5f0f\u662f\u6700\u5b89\u5168\u7684\u2014\u2014\u5b83\u4fdd\u8bc1\u670d\u52a1\u505c\u6b62\u65f6&#xff0c;\u6240\u6709\u76f8\u5173\u5b50\u8fdb\u7a0b\u90fd\u88ab\u6e05\u7406&#xff0c;\u4e0d\u4f1a\u4ea7\u751f\u5b64\u513f\u8fdb\u7a0b\u3002<\/p>\n<p><span class=\"token comment\"># \u67e5\u770b\u4e00\u4e2a\u670d\u52a1\u7684 KillMode \u914d\u7f6e<\/span><br \/>\nsystemctl show nginx <span class=\"token operator\">|<\/span> <span class=\"token function\">grep<\/span> KillMode<\/p>\n<h4>15.3 \u5b9e\u9645\u670d\u52a1\u914d\u7f6e\u793a\u4f8b<\/h4>\n<p>\u5982\u679c\u4f60\u8981\u4e3a\u81ea\u5df1\u7684\u670d\u52a1\u7f16\u5199 systemd unit \u6587\u4ef6&#xff0c;\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5904\u7406\u5b50\u8fdb\u7a0b\u751f\u547d\u5468\u671f\u7684\u63a8\u8350\u914d\u7f6e&#xff1a;<\/p>\n<p><span class=\"token comment\"># \u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u670d\u52a1\u7684 unit \u6587\u4ef6<\/span><br \/>\n<span class=\"token function\">sudo<\/span> <span class=\"token function\">nano<\/span> \/etc\/systemd\/system\/myworker.service<\/p>\n<p># \/etc\/systemd\/system\/myworker.service<br \/>\n# \u4e00\u4e2a\u6b63\u786e\u5904\u7406\u5b50\u8fdb\u7a0b\u751f\u547d\u5468\u671f\u7684 systemd \u670d\u52a1\u914d\u7f6e\u793a\u4f8b<\/p>\n<p>[Unit]<br \/>\nDescription&#061;My Worker Service<br \/>\nDocumentation&#061;https:\/\/example.com\/docs<br \/>\nAfter&#061;network.target<\/p>\n<p>[Service]<br \/>\nType&#061;forking<br \/>\n# \u6307\u5b9a\u670d\u52a1\u4e3b\u8fdb\u7a0b\u7684 PID \u6587\u4ef6&#xff08;forking \u7c7b\u578b\u5fc5\u987b&#xff09;<br \/>\nPIDFile&#061;\/run\/myworker\/myworker.pid<\/p>\n<p>User&#061;www-data<br \/>\nGroup&#061;www-data<\/p>\n<p># \u5de5\u4f5c\u76ee\u5f55<br \/>\nWorkingDirectory&#061;\/opt\/myworker<\/p>\n<p># \u542f\u52a8\u547d\u4ee4<br \/>\nExecStart&#061;\/opt\/myworker\/bin\/myworker &#8211;daemon &#8211;pidfile&#061;\/run\/myworker\/myworker.pid<\/p>\n<p># \u505c\u6b62\u547d\u4ee4&#xff08;\u4f18\u96c5\u505c\u6b62&#xff09;<br \/>\nExecStop&#061;\/bin\/kill -TERM $MAINPID<\/p>\n<p># \u91cd\u65b0\u52a0\u8f7d\u547d\u4ee4&#xff08;\u5982\u652f\u6301\u70ed\u91cd\u8f7d&#xff09;<br \/>\nExecReload&#061;\/bin\/kill -HUP $MAINPID<\/p>\n<p># \u7ec8\u6b62\u6a21\u5f0f&#xff1a;\u7ec8\u6b62\u670d\u52a1 cgroup \u4e2d\u7684\u6240\u6709\u8fdb\u7a0b<br \/>\n# \u8fd9\u662f\u9ed8\u8ba4\u503c&#xff0c;\u663e\u5f0f\u5199\u51fa\u4ee5\u660e\u786e\u610f\u56fe<br \/>\nKillMode&#061;control-group<\/p>\n<p># \u53d1\u9001 SIGTERM \u540e\u7b49\u5f85\u7684\u65f6\u95f4&#xff08;\u79d2&#xff09;&#xff0c;\u8d85\u65f6\u540e\u53d1 SIGKILL<br \/>\nTimeoutStopSec&#061;30<\/p>\n<p># \u670d\u52a1\u5d29\u6e83\u65f6\u81ea\u52a8\u91cd\u542f<br \/>\nRestart&#061;on-failure<br \/>\n# \u5931\u8d25\u91cd\u542f\u95f4\u9694<br \/>\nRestartSec&#061;5s<\/p>\n<p># \u9650\u5236\u6700\u5927\u5b50\u8fdb\u7a0b\u6570&#xff08;\u9632\u6b62 fork \u70b8\u5f39\u5bfc\u81f4\u7cfb\u7edf\u4e0d\u7a33\u5b9a&#xff09;<br \/>\nTasksMax&#061;256<\/p>\n<p>[Install]<br \/>\nWantedBy&#061;multi-user.target<\/p>\n<p><span class=\"token comment\"># \u91cd\u65b0\u52a0\u8f7d systemd \u914d\u7f6e<\/span><br \/>\n<span class=\"token function\">sudo<\/span> systemctl daemon-reload<\/p>\n<p><span class=\"token comment\"># \u542f\u52a8\u670d\u52a1<\/span><br \/>\n<span class=\"token function\">sudo<\/span> systemctl start myworker<\/p>\n<p><span class=\"token comment\"># \u67e5\u770b\u670d\u52a1\u72b6\u6001<\/span><br \/>\nsystemctl status myworker<\/p>\n<p><span class=\"token comment\"># \u67e5\u770b\u670d\u52a1\u65e5\u5fd7<\/span><br \/>\njournalctl <span class=\"token parameter variable\">-u<\/span> myworker <span class=\"token parameter variable\">-f<\/span><\/p>\n<p><span class=\"token comment\"># \u9a8c\u8bc1\u670d\u52a1\u505c\u6b62\u65f6\u5b50\u8fdb\u7a0b\u662f\u5426\u88ab\u6b63\u786e\u6e05\u7406<\/span><br \/>\n<span class=\"token function\">sudo<\/span> systemctl stop myworker<br \/>\n<span class=\"token comment\"># \u505c\u6b62\u540e&#xff0c;\u68c0\u67e5\u662f\u5426\u6709\u50f5\u5c38\u8fdb\u7a0b\u6216\u9057\u7559\u5b50\u8fdb\u7a0b<\/span><br \/>\n<span class=\"token function\">ps<\/span> aux <span class=\"token operator\">|<\/span> <span class=\"token function\">grep<\/span> myworker<\/p>\n<h3>\u5341\u516d\u3001\u5e38\u89c1\u95ee\u9898\u4e0e\u8e29\u5751\u603b\u7ed3<\/h3>\n<p>Q1&#xff1a;\u6211\u7528 kill -9 \u53ef\u4ee5\u6740\u6b7b\u50f5\u5c38\u8fdb\u7a0b\u5417&#xff1f;<\/p>\n<p>\u4e0d\u884c\u3002kill -9 \u53d1\u9001\u7684\u662f SIGKILL \u4fe1\u53f7&#xff0c;\u4f46\u50f5\u5c38\u8fdb\u7a0b\u5df2\u7ecf\u6b7b\u4e86\u2014\u2014\u5b83\u7684\u4ee3\u7801\u4e0d\u518d\u8fd0\u884c&#xff0c;\u65e0\u6cd5\u63a5\u6536\u4efb\u4f55\u4fe1\u53f7&#xff0c;\u66f4\u65e0\u6cd5\u5904\u7406\u4fe1\u53f7\u3002\u5bf9\u50f5\u5c38\u8fdb\u7a0b\u53d1\u9001\u4efb\u4f55\u4fe1\u53f7\u90fd\u6ca1\u6709\u6548\u679c\u3002\u552f\u4e00\u80fd\u6e05\u9664\u50f5\u5c38\u8fdb\u7a0b\u7684\u65b9\u6cd5\u662f\u8ba9\u5176\u7236\u8fdb\u7a0b\u8c03\u7528 wait()&#xff0c;\u6216\u8005\u7ec8\u6b62\u5176\u7236\u8fdb\u7a0b&#xff08;\u7236\u8fdb\u7a0b\u9000\u51fa\u540e&#xff0c;systemd \u4f1a\u63a5\u7ba1\u5e76\u7acb\u5373\u56de\u6536&#xff09;\u3002<\/p>\n<p>\u8fd9\u662f\u521d\u5b66\u8005\u6700\u5e38\u89c1\u7684\u8bef\u533a\u4e4b\u4e00&#xff0c;\u6211\u81ea\u5df1\u6700\u521d\u4e5f\u72af\u8fc7\u8fd9\u4e2a\u9519\u8bef\u3002\u5f53\u65f6\u5728\u670d\u52a1\u5668\u4e0a\u770b\u5230 &lt;defunct&gt; \u7684\u8fdb\u7a0b&#xff0c;\u76f4\u89c9\u53cd\u5e94\u5c31\u662f kill -9&#xff0c;\u7136\u540e\u53d1\u73b0\u6beb\u65e0\u6548\u679c&#xff0c;\u624d\u5f00\u59cb\u8ba4\u771f\u7814\u7a76\u4e3a\u4ec0\u4e48\u3002<\/p>\n<p>Q2&#xff1a;\u7cfb\u7edf\u4e2d\u6709\u51e0\u4e2a\u50f5\u5c38\u8fdb\u7a0b&#xff0c;\u9700\u8981\u7acb\u523b\u5904\u7406\u5417&#xff1f;<\/p>\n<p>\u4e0d\u4e00\u5b9a\u3002\u5076\u5c14\u51fa\u73b0\u4e00\u4e24\u4e2a\u77ed\u6682\u7684\u50f5\u5c38\u8fdb\u7a0b\u662f\u6b63\u5e38\u73b0\u8c61\u2014\u2014\u7236\u8fdb\u7a0b\u5728\u67d0\u4e2a\u64cd\u4f5c\u5b8c\u6210\u540e\u4f1a\u56de\u6536\u5b83\u4eec\u3002\u771f\u6b63\u9700\u8981\u8b66\u60d5\u7684\u662f&#xff1a;\u6570\u91cf\u6301\u7eed\u589e\u957f\u3001\u957f\u65f6\u95f4\u4e0d\u51cf\u5c11\u3001\u6570\u91cf\u8d85\u8fc7\u51e0\u5341\u4e2a\u7684\u60c5\u51b5\u3002<\/p>\n<p>\u5224\u65ad\u65b9\u5f0f&#xff1a;\u89c2\u5bdf\u4e00\u6bb5\u65f6\u95f4&#xff08;\u6bd4\u59825\u5206\u949f&#xff09;&#xff0c;\u5982\u679c\u50f5\u5c38\u8fdb\u7a0b\u6570\u91cf\u5728\u51cf\u5c11\u6216\u4fdd\u6301\u7a33\u5b9a&#xff08;\u4e14\u6570\u91cf\u5f88\u5c11&#xff09;&#xff0c;\u901a\u5e38\u4e0d\u9700\u8981\u7acb\u5373\u5904\u7406\u3002\u5982\u679c\u6570\u91cf\u5728\u6301\u7eed\u589e\u957f&#xff0c;\u5c31\u9700\u8981\u6392\u67e5\u7236\u8fdb\u7a0b\u7684\u95ee\u9898\u4e86\u3002<\/p>\n<p>Q3&#xff1a;\u5b64\u513f\u8fdb\u7a0b\u4f1a\u81ea\u52a8\u53d8\u6210\u50f5\u5c38\u8fdb\u7a0b\u5417&#xff1f;<\/p>\n<p>\u4e0d\u4f1a\u3002\u5b64\u513f\u8fdb\u7a0b\u88ab systemd \u63a5\u7ba1\u540e&#xff0c;systemd \u4f1a\u5728\u5b64\u513f\u8fdb\u7a0b\u9000\u51fa\u65f6\u6b63\u786e\u8c03\u7528 wait() \u6765\u56de\u6536\u5b83&#xff0c;\u4e0d\u4f1a\u4ea7\u751f\u50f5\u5c38\u8fdb\u7a0b\u3002\u8fd9\u662f Linux \u8fdb\u7a0b\u7ba1\u7406\u8bbe\u8ba1\u4e2d\u5f88\u4f18\u96c5\u7684\u4e00\u70b9\u3002<\/p>\n<p>Q4&#xff1a;\u5982\u679c\u50f5\u5c38\u8fdb\u7a0b\u7684\u7236\u8fdb\u7a0b\u672c\u8eab\u5c31\u662f\u4e00\u4e2a\u7cfb\u7edf\u5173\u952e\u670d\u52a1&#xff08;\u5982 Nginx&#xff09;&#xff0c;\u4e0d\u80fd\u968f\u4fbf\u7ec8\u6b62\u600e\u4e48\u529e&#xff1f;<\/p>\n<p>\u8fd9\u79cd\u60c5\u51b5\u9700\u8981\u5206\u6790\u50f5\u5c38\u8fdb\u7a0b\u662f\u5426\u5728\u6301\u7eed\u589e\u957f\u3002\u5982\u679c Nginx \u786e\u5b9e\u5728\u6301\u7eed\u4ea7\u751f\u50f5\u5c38\u8fdb\u7a0b&#xff0c;\u901a\u5e38\u8bf4\u660e\u67d0\u4e2a Nginx \u6a21\u5757\u6216\u914d\u7f6e\u6709\u95ee\u9898\u3002<\/p>\n<p>\u5904\u7406\u5efa\u8bae&#xff1a;<\/p>\n<p><span class=\"token comment\"># \u67e5\u770b Nginx \u4ea7\u751f\u4e86\u591a\u5c11\u50f5\u5c38\u8fdb\u7a0b<\/span><br \/>\n<span class=\"token function\">ps<\/span> <span class=\"token parameter variable\">-eo<\/span> ppid,stat <span class=\"token operator\">|<\/span> <span class=\"token function\">awk<\/span> <span class=\"token string\">&#039;$2 ~ \/^Z\/ {count[$1]&#043;&#043;} END {for (p in count) print p, count[p]}&#039;<\/span><\/p>\n<p><span class=\"token comment\"># \u5982\u679c\u6570\u91cf\u4e0d\u591a\u4e14\u4e0d\u5728\u589e\u957f&#xff0c;\u5148\u8bb0\u5f55\u76d1\u63a7&#xff0c;\u7b49\u7ef4\u62a4\u7a97\u53e3\u4f18\u96c5\u91cd\u542f<\/span><br \/>\n<span class=\"token function\">sudo<\/span> systemctl reload nginx  <span class=\"token comment\"># \u5148\u5c1d\u8bd5 reload&#xff08;\u4e0d\u4e2d\u65ad\u8fde\u63a5&#xff09;<\/span><\/p>\n<p><span class=\"token comment\"># \u5982\u679c\u5fc5\u987b\u91cd\u542f&#xff0c;\u5148\u786e\u8ba4\u5728\u4f4e\u5cf0\u671f<\/span><br \/>\n<span class=\"token function\">sudo<\/span> systemctl restart nginx<\/p>\n<p>Q5&#xff1a;\u4e3a\u4ec0\u4e48 ps aux \u4e2d\u6709\u65f6\u5019\u770b\u5230 Z \u72b6\u6001&#xff0c;\u4f46\u51e0\u79d2\u540e\u5c31\u6d88\u5931\u4e86&#xff1f;<\/p>\n<p>\u8fd9\u662f\u6b63\u5e38\u7684\u77ed\u6682\u50f5\u5c38\u72b6\u6001\u3002\u8fdb\u7a0b\u4ece\u9000\u51fa\u5230\u7236\u8fdb\u7a0b\u8c03\u7528 wait() \u4e4b\u95f4&#xff0c;\u5fc5\u7136\u6709\u4e00\u6bb5\u65f6\u95f4\u5904\u4e8e\u50f5\u5c38\u72b6\u6001\u3002\u5982\u679c\u7236\u8fdb\u7a0b\u53cd\u5e94\u8fc5\u901f&#xff08;\u6ce8\u518c\u4e86 SIGCHLD \u5904\u7406\u5668\u5e76\u53ca\u65f6\u8c03\u7528 wait()&#xff09;&#xff0c;\u8fd9\u6bb5\u65f6\u95f4\u53ef\u80fd\u53ea\u6709\u6beb\u79d2\u7ea7\u522b&#xff0c;\u5728 ps \u8f93\u51fa\u4e2d\u6765\u4e0d\u53ca\u663e\u793a\u5c31\u6d88\u5931\u4e86\u3002\u77ed\u6682\u7684\u50f5\u5c38\u8fdb\u7a0b\u662f\u5b8c\u5168\u6b63\u5e38\u7684&#xff0c;\u4e0d\u5fc5\u62c5\u5fc3\u3002<\/p>\n<p>Q6&#xff1a;\u5728 Shell \u811a\u672c\u4e2d&#xff0c;&amp; \u8fd0\u884c\u7684\u540e\u53f0\u8fdb\u7a0b\u4f1a\u4ea7\u751f\u50f5\u5c38\u8fdb\u7a0b\u5417&#xff1f;<\/p>\n<p>\u4f1a&#xff0c;\u5728\u811a\u672c\u9000\u51fa\u4e4b\u524d\u662f\u50f5\u5c38\u72b6\u6001\u3002\u4f46 Shell \u672c\u8eab\u5904\u7406\u4e86\u8fd9\u4e2a\u95ee\u9898&#xff1a;Bash \u5728\u811a\u672c\u7ed3\u675f\u65f6\u4f1a\u81ea\u52a8\u56de\u6536\u540e\u53f0\u8fdb\u7a0b\u3002\u5982\u679c\u4f60\u7684\u811a\u672c\u9700\u8981\u7b49\u5f85\u540e\u53f0\u8fdb\u7a0b\u5b8c\u6210\u5e76\u83b7\u53d6\u5176\u9000\u51fa\u7801&#xff0c;\u5e94\u8be5\u4f7f\u7528 wait \u547d\u4ee4&#xff1a;<\/p>\n<p><span class=\"token shebang important\">#!\/bin\/bash<\/span><\/p>\n<p><span class=\"token comment\"># \u542f\u52a8\u540e\u53f0\u8fdb\u7a0b<\/span><br \/>\n.\/long_task.sh <span class=\"token operator\">&amp;<\/span><br \/>\n<span class=\"token assign-left variable\">TASK_PID<\/span><span class=\"token operator\">&#061;<\/span><span class=\"token variable\">$!<\/span>  <span class=\"token comment\"># \u83b7\u53d6\u540e\u53f0\u8fdb\u7a0b\u7684 PID<\/span><\/p>\n<p><span class=\"token comment\"># \u505a\u4e00\u4e9b\u5176\u4ed6\u4e8b\u60c5<\/span><br \/>\n<span class=\"token builtin class-name\">echo<\/span> <span class=\"token string\">&#034;\u4efb\u52a1\u6b63\u5728\u540e\u53f0\u8fd0\u884c&#8230;&#034;<\/span><\/p>\n<p><span class=\"token comment\"># \u7b49\u5f85\u540e\u53f0\u8fdb\u7a0b\u5b8c\u6210\u5e76\u83b7\u53d6\u9000\u51fa\u7801<\/span><br \/>\n<span class=\"token function\">wait<\/span> <span class=\"token variable\">$TASK_PID<\/span><br \/>\n<span class=\"token assign-left variable\">EXIT_CODE<\/span><span class=\"token operator\">&#061;<\/span><span class=\"token variable\">$?<\/span><\/p>\n<p><span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">[<\/span> <span class=\"token variable\">$EXIT_CODE<\/span> <span class=\"token parameter variable\">-eq<\/span> <span class=\"token number\">0<\/span> <span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token keyword\">then<\/span><br \/>\n    <span class=\"token builtin class-name\">echo<\/span> <span class=\"token string\">&#034;\u4efb\u52a1\u6210\u529f\u5b8c\u6210&#034;<\/span><br \/>\n<span class=\"token keyword\">else<\/span><br \/>\n    <span class=\"token builtin class-name\">echo<\/span> <span class=\"token string\">&#034;\u4efb\u52a1\u5931\u8d25&#xff0c;\u9000\u51fa\u7801: <span class=\"token variable\">$EXIT_CODE<\/span>&#034;<\/span><br \/>\n<span class=\"token keyword\">fi<\/span><\/p>\n<p>Q7&#xff1a;Java \u7a0b\u5e8f\u8c03\u7528 Runtime.getRuntime().exec() \u540e\u4f1a\u4ea7\u751f\u50f5\u5c38\u8fdb\u7a0b\u5417&#xff1f;<\/p>\n<p>\u4f1a&#xff0c;\u5982\u679c\u4e0d\u6b63\u786e\u5904\u7406\u7684\u8bdd\u3002Java \u7684 Runtime.exec() \u548c ProcessBuilder \u90fd\u9700\u8981\u8c03\u7528 Process.waitFor() \u6216\u8005\u81f3\u5c11\u6d88\u8017\u6389\u5b50\u8fdb\u7a0b\u7684 stdout\/stderr \u6d41&#xff0c;\u5426\u5219\u5b50\u8fdb\u7a0b\u9000\u51fa\u540e\u4f1a\u6210\u4e3a\u50f5\u5c38\u8fdb\u7a0b&#xff08;\u76f4\u5230 Java JVM \u7684\u5783\u573e\u56de\u6536\u5668\u56de\u6536 Process \u5bf9\u8c61\u5e76\u8c03\u7528 finalize()&#xff0c;\u624d\u4f1a\u89e6\u53d1 wait()\u2014\u2014\u8fd9\u4e2a\u65f6\u673a\u662f\u4e0d\u786e\u5b9a\u7684&#xff09;\u3002<\/p>\n<p>\u6b63\u786e\u7684 Java \u4ee3\u7801\u793a\u4f8b&#xff1a;<\/p>\n<p><span class=\"token class-name\">ProcessBuilder<\/span> pb <span class=\"token operator\">&#061;<\/span> <span class=\"token keyword\">new<\/span> <span class=\"token class-name\">ProcessBuilder<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;ls&#034;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#034;-la&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\npb<span class=\"token punctuation\">.<\/span><span class=\"token function\">redirectErrorStream<\/span><span class=\"token punctuation\">(<\/span><span class=\"token boolean\">true<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token class-name\">Process<\/span> process <span class=\"token operator\">&#061;<\/span> pb<span class=\"token punctuation\">.<\/span><span class=\"token function\">start<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u6d88\u8017\u8f93\u51fa&#xff08;\u9632\u6b62\u7f13\u51b2\u533a\u963b\u585e&#xff09;<\/span><br \/>\n<span class=\"token keyword\">try<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token class-name\">BufferedReader<\/span> reader <span class=\"token operator\">&#061;<\/span> <span class=\"token keyword\">new<\/span> <span class=\"token class-name\">BufferedReader<\/span><span class=\"token punctuation\">(<\/span><br \/>\n        <span class=\"token keyword\">new<\/span> <span class=\"token class-name\">InputStreamReader<\/span><span class=\"token punctuation\">(<\/span>process<span class=\"token punctuation\">.<\/span><span class=\"token function\">getInputStream<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token class-name\">String<\/span> line<span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">while<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">(<\/span>line <span class=\"token operator\">&#061;<\/span> reader<span class=\"token punctuation\">.<\/span><span class=\"token function\">readLine<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">!&#061;<\/span> <span class=\"token keyword\">null<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token class-name\">System<\/span><span class=\"token punctuation\">.<\/span>out<span class=\"token punctuation\">.<\/span><span class=\"token function\">println<\/span><span class=\"token punctuation\">(<\/span>line<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u7b49\u5f85\u8fdb\u7a0b\u5b8c\u6210\u5e76\u83b7\u53d6\u9000\u51fa\u7801&#xff08;\u8fd9\u4f1a\u9690\u5f0f\u56de\u6536\u5b50\u8fdb\u7a0b&#xff09;<\/span><br \/>\n<span class=\"token keyword\">int<\/span> exitCode <span class=\"token operator\">&#061;<\/span> process<span class=\"token punctuation\">.<\/span><span class=\"token function\">waitFor<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>Q8&#xff1a;Docker \u5bb9\u5668\u91cc\u7684\u5e94\u7528\u9891\u7e41\u4ea7\u751f\u50f5\u5c38\u8fdb\u7a0b&#xff0c;\u4f46\u6211\u4e0d\u60f3\u4fee\u6539\u4ee3\u7801\u600e\u4e48\u529e&#xff1f;<\/p>\n<p>\u6700\u7b80\u5355\u7684\u65b9\u6848\u662f\u5728 docker run \u65f6\u52a0\u4e0a &#8211;init \u53c2\u6570&#xff0c;\u6216\u8005\u5728 Docker Compose \u4e2d\u8bbe\u7f6e init: true\u3002Docker \u4f1a\u5728\u5bb9\u5668\u5185\u6ce8\u5165\u4e00\u4e2a tini \u4f5c\u4e3a PID 1&#xff0c;\u7531\u5b83\u6765\u5904\u7406\u6240\u6709\u5b64\u513f\u548c\u50f5\u5c38\u8fdb\u7a0b&#xff0c;\u4f60\u7684\u5e94\u7528\u4ee3\u7801\u4e0d\u9700\u8981\u4efb\u4f55\u4fee\u6539\u3002<\/p>\n<p><span class=\"token comment\"># \u4f7f\u7528 &#8211;init \u8fd0\u884c\u5bb9\u5668<\/span><br \/>\n<span class=\"token function\">docker<\/span> run <span class=\"token parameter variable\">&#8211;init<\/span> <span class=\"token parameter variable\">-d<\/span> my_buggy_image<\/p>\n<p><span class=\"token comment\"># \u9a8c\u8bc1<\/span><br \/>\n<span class=\"token function\">docker<\/span> <span class=\"token builtin class-name\">exec<\/span> <span class=\"token operator\">&lt;<\/span>container_id<span class=\"token operator\">&gt;<\/span> <span class=\"token function\">ps<\/span> aux <span class=\"token operator\">|<\/span> <span class=\"token function\">head<\/span> <span class=\"token parameter variable\">-5<\/span><br \/>\n<span class=\"token comment\"># PID 1 \u5e94\u8be5\u662f docker-init \u6216 tini&#xff0c;\u800c\u4e0d\u662f\u4f60\u7684\u5e94\u7528<\/span><\/p>\n<p>Q9&#xff1a;\u5982\u679c\u4e00\u4e2a\u670d\u52a1\u5728 Ubuntu \u670d\u52a1\u5668\u4e0a\u6301\u7eed\u4ea7\u751f\u50f5\u5c38\u8fdb\u7a0b&#xff0c;\u5f71\u54cd\u4e86\u7cfb\u7edf\u53ef\u7528\u6027&#xff0c;\u6709\u6ca1\u6709\u4e34\u65f6\u7f13\u89e3\u63aa\u65bd&#xff1f;<\/p>\n<p>\u5982\u679c\u7ef4\u62a4\u7a97\u53e3\u8fd8\u6ca1\u5230&#xff0c;\u4f46\u50f5\u5c38\u8fdb\u7a0b\u6570\u91cf\u5df2\u7ecf\u5f88\u591a&#xff0c;\u53ef\u4ee5\u4e34\u65f6\u8c03\u9ad8 PID \u6700\u5927\u503c\u6765\u4e89\u53d6\u65f6\u95f4&#xff1a;<\/p>\n<p><span class=\"token comment\"># \u67e5\u770b\u5f53\u524d PID \u6700\u5927\u503c<\/span><br \/>\n<span class=\"token function\">cat<\/span> \/proc\/sys\/kernel\/pid_max<\/p>\n<p><span class=\"token comment\"># \u4e34\u65f6\u8c03\u9ad8&#xff08;\u8fd9\u662f\u8fd0\u884c\u65f6\u4fee\u6539&#xff0c;\u91cd\u542f\u540e\u6062\u590d\u9ed8\u8ba4\u503c&#xff09;<\/span><br \/>\n<span class=\"token comment\"># \u6700\u5927\u53ef\u4ee5\u8bbe\u7f6e\u4e3a 4194304&#xff08;\u5185\u6838\u652f\u6301\u7684\u6700\u5927\u503c&#xff09;<\/span><br \/>\n<span class=\"token function\">sudo<\/span> <span class=\"token function\">sysctl<\/span> <span class=\"token parameter variable\">-w<\/span> <span class=\"token assign-left variable\">kernel.pid_max<\/span><span class=\"token operator\">&#061;<\/span><span class=\"token number\">1000000<\/span><\/p>\n<p><span class=\"token comment\"># \u5982\u679c\u8981\u6c38\u4e45\u751f\u6548&#xff0c;\u4fee\u6539 sysctl \u914d\u7f6e\u6587\u4ef6<\/span><br \/>\n<span class=\"token function\">sudo<\/span> <span class=\"token function\">nano<\/span> \/etc\/sysctl.d\/99-pid-max.conf<br \/>\n<span class=\"token comment\"># \u5185\u5bb9&#xff1a;<\/span><br \/>\n<span class=\"token comment\"># kernel.pid_max &#061; 1000000<\/span><br \/>\n<span class=\"token function\">sudo<\/span> <span class=\"token function\">sysctl<\/span> <span class=\"token parameter variable\">-p<\/span> \/etc\/sysctl.d\/99-pid-max.conf<\/p>\n<p>\u26a0\ufe0f \u8fd9\u53ea\u662f\u4e34\u65f6\u7f13\u89e3\u63aa\u65bd&#xff0c;\u4e0d\u80fd\u4ece\u6839\u672c\u4e0a\u89e3\u51b3\u95ee\u9898\u3002\u6839\u672c\u89e3\u51b3\u65b9\u6848\u8fd8\u662f\u627e\u5230\u5e76\u4fee\u590d\u4ea7\u751f\u50f5\u5c38\u8fdb\u7a0b\u7684\u7236\u8fdb\u7a0b\u3002<\/p>\n<p>Q10&#xff1a;\/proc\/sys\/kernel\/pid_max \u548c\u8fdb\u7a0b\u6570\u91cf\u7684\u5173\u7cfb\u662f\u4ec0\u4e48&#xff1f;<\/p>\n<p>\/proc\/sys\/kernel\/pid_max \u5b9a\u4e49\u7684\u662f PID \u7684\u6700\u5927\u503c&#xff0c;\u4e0d\u662f\u7cfb\u7edf\u80fd\u540c\u65f6\u8fd0\u884c\u7684\u6700\u5927\u8fdb\u7a0b\u6570\u3002\u6bcf\u4e2a\u8fdb\u7a0b\u5728\u521b\u5efa\u65f6\u4f1a\u88ab\u5206\u914d\u4e00\u4e2a PID&#xff08;\u4ece\u5c0f\u5230\u5927\u5206\u914d&#xff0c;\u5230\u8fbe\u6700\u5927\u503c\u540e\u5faa\u73af\u56de\u6765\u4ece\u5c0f\u503c\u5f00\u59cb\u5206\u914d&#xff09;\u3002\u5982\u679c\u6240\u6709\u5c0f\u4e8e pid_max \u7684 PID \u90fd\u5df2\u7ecf\u88ab\u4f7f\u7528\u4e2d\u7684\u8fdb\u7a0b\u5360\u7528&#xff08;\u5305\u62ec\u50f5\u5c38\u8fdb\u7a0b&#xff09;&#xff0c;\u90a3\u4e48\u65b0\u7684\u8fdb\u7a0b\u5c31\u65e0\u6cd5\u521b\u5efa\u4e86\u3002<\/p>\n<h3>\u5341\u4e03\u3001\u6700\u4f73\u5b9e\u8df5\u5efa\u8bae<\/h3>\n<p>\u8fd9\u4e00\u8282\u662f\u6211\u5728\u5b9e\u9645\u5de5\u4f5c\u4e2d\u603b\u7ed3\u7684\u3001\u771f\u5b9e\u6709\u6548\u7684\u6700\u4f73\u5b9e\u8df5&#xff0c;\u4e0d\u662f\u7167\u6284\u624b\u518c\u7684\u7406\u8bba\u3002<\/p>\n<p>1. \u7a0b\u5e8f\u5f00\u53d1\u5c42\u9762&#xff1a;\u4e3b\u52a8\u5904\u7406 SIGCHLD<\/p>\n<p>\u65e0\u8bba\u4f60\u7528\u4ec0\u4e48\u8bed\u8a00\u5199\u670d\u52a1&#xff0c;\u53ea\u8981\u6d89\u53ca\u5230 fork \u5b50\u8fdb\u7a0b\u6216\u8c03\u7528\u5916\u90e8\u547d\u4ee4&#xff0c;\u90fd\u8981\u786e\u4fdd\u5b50\u8fdb\u7a0b\u7684\u9000\u51fa\u72b6\u6001\u88ab\u6b63\u786e\u8bfb\u53d6\u3002<\/p>\n<ul>\n<li>C\/C&#043;&#043;&#xff1a;\u6ce8\u518c SIGCHLD \u4fe1\u53f7\u5904\u7406\u5668&#xff0c;\u5728\u5176\u4e2d\u5faa\u73af\u8c03\u7528 waitpid(-1, &#8230;, WNOHANG)<\/li>\n<li>Python&#xff1a;\u4f7f\u7528 subprocess.Popen \u65f6&#xff0c;\u8bb0\u5f97\u8c03\u7528 wait() \u6216 communicate()&#xff1b;\u4f7f\u7528 os.fork() \u65f6&#xff0c;\u540c\u6837\u9700\u8981\u6ce8\u518c signal.SIGCHLD \u5904\u7406\u5668<\/li>\n<li>Java&#xff1a;\u8c03\u7528 Process.waitFor() \u5e76\u6d88\u8017\u8fdb\u7a0b\u7684 stdout\/stderr<\/li>\n<li>Node.js&#xff1a;child_process.spawn() \u6ce8\u518c close \u6216 exit \u4e8b\u4ef6<\/li>\n<li>Go&#xff1a;\u4f7f\u7528 cmd.Wait() \u7b49\u5f85\u5b50\u8fdb\u7a0b\u5b8c\u6210<\/li>\n<\/ul>\n<p>2. Docker \u5316\u670d\u52a1&#xff1a;\u59cb\u7ec8\u4f7f\u7528 init<\/p>\n<p>\u5728 Kubernetes\u3001Docker Compose\u3001Docker Swarm \u7b49\u5bb9\u5668\u7f16\u6392\u73af\u5883\u4e2d&#xff0c;\u4e3a\u6bcf\u4e2a\u670d\u52a1\u5bb9\u5668\u542f\u7528 init&#xff08;&#8211;init \u6216 init: true&#xff09;&#xff0c;\u8fd9\u662f\u4e00\u4e2a\u4ee3\u4ef7\u6781\u4f4e\u4f46\u6536\u76ca\u660e\u663e\u7684\u6700\u4f73\u5b9e\u8df5&#xff0c;\u53ef\u4ee5\u907f\u514d\u5927\u91cf\u96be\u4ee5\u8c03\u8bd5\u7684\u5bb9\u5668\u5185\u50f5\u5c38\u8fdb\u7a0b\u95ee\u9898\u3002<\/p>\n<p>3. systemd \u670d\u52a1&#xff1a;\u4f7f\u7528 cgroup \u7ba1\u7406\u8fdb\u7a0b\u751f\u547d\u5468\u671f<\/p>\n<p>\u5bf9\u4e8e\u901a\u8fc7 systemd \u7ba1\u7406\u7684\u670d\u52a1&#xff0c;\u4fdd\u6301 KillMode&#061;control-group&#xff08;\u9ed8\u8ba4\u503c&#xff09;&#xff0c;\u8ba9 systemd \u5728\u670d\u52a1\u505c\u6b62\u65f6\u6e05\u7406\u6240\u6709\u76f8\u5173\u8fdb\u7a0b\u3002\u4e0d\u8981\u8f7b\u6613\u4fee\u6539\u4e3a KillMode&#061;process&#xff0c;\u9664\u975e\u4f60\u975e\u5e38\u6e05\u695a\u81ea\u5df1\u5728\u505a\u4ec0\u4e48\u3002<\/p>\n<p>4. \u76d1\u63a7&#xff1a;\u8bbe\u7f6e\u50f5\u5c38\u8fdb\u7a0b\u6570\u91cf\u544a\u8b66<\/p>\n<p>\u5728\u751f\u4ea7\u670d\u52a1\u5668\u4e0a&#xff0c;\u901a\u8fc7\u76d1\u63a7\u7cfb\u7edf&#xff08;\u5982 Prometheus &#043; Alertmanager\u3001Zabbix\u3001Nagios \u7b49&#xff09;\u8bbe\u7f6e\u50f5\u5c38\u8fdb\u7a0b\u6570\u91cf\u7684\u544a\u8b66\u9608\u503c\u3002\u5efa\u8bae&#xff1a;<\/p>\n<ul>\n<li>\u8b66\u544a&#xff08;Warning&#xff09;\u9608\u503c&#xff1a;5\u4e2a\u4ee5\u4e0a<\/li>\n<li>\u4e25\u91cd&#xff08;Critical&#xff09;\u9608\u503c&#xff1a;20\u4e2a\u4ee5\u4e0a<\/li>\n<\/ul>\n<p><span class=\"token comment\"># \u7528\u4e8e\u76d1\u63a7\u811a\u672c\u7684\u50f5\u5c38\u8fdb\u7a0b\u8ba1\u6570\u547d\u4ee4<\/span><br \/>\n<span class=\"token assign-left variable\">ZOMBIE_COUNT<\/span><span class=\"token operator\">&#061;<\/span><span class=\"token variable\"><span class=\"token variable\">$(<\/span><span class=\"token function\">ps<\/span> <span class=\"token parameter variable\">-eo<\/span> <span class=\"token function\">stat<\/span> <span class=\"token operator\">|<\/span> <span class=\"token function\">grep<\/span> <span class=\"token parameter variable\">-c<\/span> <span class=\"token string\">&#039;^Z&#039;<\/span><span class=\"token variable\">)<\/span><\/span><br \/>\n<span class=\"token builtin class-name\">echo<\/span> <span class=\"token string\">&#034;\u5f53\u524d\u50f5\u5c38\u8fdb\u7a0b\u6570\u91cf: <span class=\"token variable\">$ZOMBIE_COUNT<\/span>&#034;<\/span><\/p>\n<p>5. \u6392\u67e5&#xff1a;\u7528\u7ed3\u6784\u5316\u65b9\u5f0f\u6392\u67e5&#xff0c;\u4e0d\u8981\u76f2\u76ee\u64cd\u4f5c<\/p>\n<p>\u9047\u5230\u50f5\u5c38\u8fdb\u7a0b\u95ee\u9898\u65f6&#xff0c;\u6309\u4ee5\u4e0b\u987a\u5e8f\u6392\u67e5&#xff0c;\u4e0d\u8981\u4e0a\u6765\u5c31 kill -9&#xff1a;<\/p>\n<p>\u7b2c\u4e00\u6b65&#xff1a;\u786e\u8ba4\u6570\u91cf\u548c\u8d8b\u52bf&#xff08;\u662f\u5076\u53d1\u8fd8\u662f\u6301\u7eed\u589e\u957f&#xff1f;&#xff09;<br \/>\n\u7b2c\u4e8c\u6b65&#xff1a;\u627e\u5230\u7236\u8fdb\u7a0b&#xff08;ps -eo pid,ppid,stat,comm | grep Z&#xff09;<br \/>\n\u7b2c\u4e09\u6b65&#xff1a;\u8bc4\u4f30\u7236\u8fdb\u7a0b\u662f\u5426\u53ef\u4ee5\u91cd\u542f&#xff08;\u4e1a\u52a1\u5f71\u54cd\u8bc4\u4f30&#xff09;<br \/>\n\u7b2c\u56db\u6b65&#xff1a;\u5148\u5c1d\u8bd5 SIGCHLD&#xff08;kill -SIGCHLD PPID&#xff09;<br \/>\n\u7b2c\u4e94\u6b65&#xff1a;\u5fc5\u8981\u65f6\u5728\u7ef4\u62a4\u7a97\u53e3\u91cd\u542f\u7236\u8fdb\u7a0b&#xff08;\u5148 SIGTERM&#xff0c;\u518d SIGKILL&#xff09;<br \/>\n\u7b2c\u516d\u6b65&#xff1a;\u4ece\u4ee3\u7801\u5c42\u9762\u4fee\u590d\u6839\u672c\u539f\u56e0<\/p>\n<p>6. \u7248\u672c\u9009\u62e9&#xff1a;\u4f7f\u7528 Ubuntu LTS \u7248\u672c<\/p>\n<p>\u575a\u6301\u4f7f\u7528 Ubuntu 22.04 LTS \u6216 Ubuntu 24.04 LTS\u3002LTS&#xff08;Long Term Support&#xff09;\u7248\u672c\u62e5\u67095\u5e74\u7684\u5b98\u65b9\u652f\u6301\u5468\u671f&#xff08;\u4ed8\u8d39\u53ef\u5ef6\u957f\u523010\u5e74&#xff09;&#xff0c;\u5185\u6838\u548c systemd \u7248\u672c\u7a33\u5b9a&#xff0c;\u8fdb\u7a0b\u7ba1\u7406\u673a\u5236\u6210\u719f\u3002Ubuntu 22.04 \u4f7f\u7528 Linux \u5185\u6838 5.15&#xff0c;Ubuntu 24.04 \u4f7f\u7528 Linux \u5185\u6838 6.8&#xff0c;\u4e24\u8005\u7684\u8fdb\u7a0b\u7ba1\u7406\u529f\u80fd\u90fd\u975e\u5e38\u5b8c\u5584\u3002<\/p>\n<p>\u907f\u514d\u4f7f\u7528\u975e LTS \u7248\u672c&#xff08;\u5982 22.10\u300123.04 \u7b49&#xff09;\u5728\u751f\u4ea7\u73af\u5883\u90e8\u7f72&#xff0c;\u5b83\u4eec\u7684\u652f\u6301\u5468\u671f\u53ea\u67099\u4e2a\u6708\u3002<\/p>\n<p>7. \u5907\u4efd\u4e0e\u53d8\u66f4\u7ba1\u7406<\/p>\n<p>\u5728\u4fee\u6539\u4efb\u4f55 systemd \u670d\u52a1\u914d\u7f6e\u6216\u7cfb\u7edf\u53c2\u6570\u4e4b\u524d&#xff0c;\u8bb0\u5f55\u539f\u59cb\u503c&#xff1a;<\/p>\n<p><span class=\"token comment\"># \u5907\u4efd systemd \u670d\u52a1\u914d\u7f6e<\/span><br \/>\n<span class=\"token function\">sudo<\/span> <span class=\"token function\">cp<\/span> \/etc\/systemd\/system\/myservice.service \/etc\/systemd\/system\/myservice.service.bak<\/p>\n<p><span class=\"token comment\"># \u8bb0\u5f55\u5f53\u524d sysctl \u53c2\u6570\u503c<\/span><br \/>\n<span class=\"token function\">sudo<\/span> <span class=\"token function\">sysctl<\/span> <span class=\"token parameter variable\">-a<\/span> <span class=\"token operator\">&gt;<\/span> ~\/sysctl_backup_<span class=\"token variable\"><span class=\"token variable\">$(<\/span><span class=\"token function\">date<\/span> &#043;%Y%m%d<span class=\"token variable\">)<\/span><\/span>.txt<\/p>\n<p><span class=\"token comment\"># \u4fee\u6539\u540e\u5982\u679c\u6709\u95ee\u9898&#xff0c;\u53ef\u4ee5\u5feb\u901f\u6062\u590d<\/span><br \/>\n<span class=\"token function\">sudo<\/span> <span class=\"token function\">cp<\/span> \/etc\/systemd\/system\/myservice.service.bak \/etc\/systemd\/system\/myservice.service<br \/>\n<span class=\"token function\">sudo<\/span> systemctl daemon-reload<\/p>\n<h3>\u5341\u516b\u3001\u6269\u5c55\u77e5\u8bc6\u70b9<\/h3>\n<h4>\u8fdb\u7a0b\u7ec4\u4e0e\u4f1a\u8bdd<\/h4>\n<p>\u7406\u89e3\u50f5\u5c38\u8fdb\u7a0b\u65f6&#xff0c;\u4e86\u89e3\u8fdb\u7a0b\u7ec4&#xff08;Process Group&#xff09;\u548c\u4f1a\u8bdd&#xff08;Session&#xff09;\u7684\u6982\u5ff5\u4f1a\u6709\u5e2e\u52a9\u3002<\/p>\n<p>\u8fdb\u7a0b\u7ec4&#xff1a;\u4e00\u7ec4\u76f8\u5173\u8fdb\u7a0b\u7684\u96c6\u5408&#xff0c;\u62e5\u6709\u5171\u540c\u7684 PGID&#xff08;\u8fdb\u7a0b\u7ec4ID&#xff09;\u3002\u4fe1\u53f7\u53ef\u4ee5\u53d1\u9001\u7ed9\u6574\u4e2a\u8fdb\u7a0b\u7ec4&#xff08;\u4f7f\u7528\u8d1f PID \u503c&#xff09;\u3002<\/p>\n<p>\u4f1a\u8bdd&#xff1a;\u4e00\u4e2a\u6216\u591a\u4e2a\u8fdb\u7a0b\u7ec4\u7684\u96c6\u5408&#xff0c;\u901a\u5e38\u5bf9\u5e94\u4e00\u4e2a\u767b\u5f55\u4f1a\u8bdd\u6216\u4e00\u4e2a\u7ec8\u7aef\u3002<\/p>\n<p><span class=\"token comment\"># \u67e5\u770b\u8fdb\u7a0b\u7684 PID\u3001PGID\u3001SID&#xff08;\u4f1a\u8bddID&#xff09;<\/span><br \/>\n<span class=\"token function\">ps<\/span> <span class=\"token parameter variable\">-eo<\/span> pid,pgid,sid,comm<\/p>\n<p><span class=\"token comment\"># \u5411\u6574\u4e2a\u8fdb\u7a0b\u7ec4\u53d1\u9001\u4fe1\u53f7&#xff08;\u8d1f PID \u503c\u4ee3\u8868 PGID&#xff09;<\/span><br \/>\n<span class=\"token comment\"># \u4f8b\u5982&#xff0c;\u7ec8\u6b62 PGID \u4e3a 12345 \u7684\u8fdb\u7a0b\u7ec4\u4e2d\u7684\u6240\u6709\u8fdb\u7a0b<\/span><br \/>\n<span class=\"token function\">kill<\/span> &#8212; <span class=\"token parameter variable\">-12345<\/span>  <span class=\"token comment\"># \u6ce8\u610f\u53cc\u7834\u6298\u53f7&#xff0c;\u9632\u6b62\u88ab\u89e3\u6790\u4e3a\u9009\u9879<\/span><\/p>\n<h4>subreaper \u673a\u5236<\/h4>\n<p>\u5728 Linux 3.4&#043; \u4e2d&#xff0c;\u8fdb\u7a0b\u53ef\u4ee5\u901a\u8fc7 prctl(PR_SET_CHILD_SUBREAPER, 1) \u5c06\u81ea\u5df1\u8bbe\u7f6e\u4e3a&#034;\u5b50\u5b59\u8fdb\u7a0b\u6536\u5272\u8005&#034;&#xff08;subreaper&#xff09;\u3002\u8fd9\u6837&#xff0c;\u5728 PID 1 \u4e4b\u524d&#xff0c;\u5b64\u513f\u8fdb\u7a0b\u4f1a\u5148\u88ab\u6258\u7ba1\u7ed9\u6700\u8fd1\u7684 subreaper&#xff0c;\u800c\u4e0d\u662f\u76f4\u63a5\u4ea4\u7ed9 init\u3002<\/p>\n<p>\u8fd9\u4e2a\u673a\u5236\u4e3b\u8981\u7528\u4e8e\u5bb9\u5668\u8fd0\u884c\u65f6&#xff08;\u5982 Docker \u7684 containerd&#xff09;\u548c\u4e00\u4e9b\u8fdb\u7a0b\u7ba1\u7406\u5de5\u5177&#xff08;\u5982 s6\u3001runit&#xff09;\u3002\u7406\u89e3\u8fd9\u4e2a\u673a\u5236\u6709\u52a9\u4e8e\u4f60\u7406\u89e3\u4e3a\u4ec0\u4e48 Docker \u5bb9\u5668\u5185\u7684 PID 1 \u884c\u4e3a\u4f1a\u6709\u6240\u4e0d\u540c\u3002<\/p>\n<p><span class=\"token comment\"># \u67e5\u770b\u4e00\u4e2a\u8fdb\u7a0b\u662f\u5426\u8bbe\u7f6e\u4e86 subreaper \u6807\u5fd7<\/span><br \/>\n<span class=\"token function\">cat<\/span> \/proc\/<span class=\"token operator\">&lt;<\/span>PID<span class=\"token operator\">&gt;<\/span>\/status <span class=\"token operator\">|<\/span> <span class=\"token function\">grep<\/span> NoNewPrivs<br \/>\n<span class=\"token comment\"># \u6ce8&#xff1a;subreaper \u6807\u5fd7\u6ca1\u6709\u76f4\u63a5\u5728 status \u4e2d\u663e\u793a&#xff0c;\u9700\u8981\u901a\u8fc7\u4ee3\u7801\u8bfb\u53d6<\/span><\/p>\n<h4>\/proc \u6587\u4ef6\u7cfb\u7edf\u6df1\u5ea6\u4f7f\u7528<\/h4>\n<p>\u5bf9\u4e8e\u8fdb\u7a0b\u8c03\u8bd5&#xff0c;\/proc \u6587\u4ef6\u7cfb\u7edf\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u4fe1\u606f&#xff1a;<\/p>\n<p><span class=\"token comment\"># \u67e5\u770b\u8fdb\u7a0b\u7684\u5185\u5b58\u6620\u5c04<\/span><br \/>\n<span class=\"token function\">cat<\/span> \/proc\/<span class=\"token operator\">&lt;<\/span>PID<span class=\"token operator\">&gt;<\/span>\/maps<\/p>\n<p><span class=\"token comment\"># \u67e5\u770b\u8fdb\u7a0b\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26<\/span><br \/>\n<span class=\"token function\">ls<\/span> <span class=\"token parameter variable\">-la<\/span> \/proc\/<span class=\"token operator\">&lt;<\/span>PID<span class=\"token operator\">&gt;<\/span>\/fd<\/p>\n<p><span class=\"token comment\"># \u67e5\u770b\u8fdb\u7a0b\u7684\u547d\u540d\u7a7a\u95f4\u4fe1\u606f&#xff08;\u5bb9\u5668\u76f8\u5173&#xff09;<\/span><br \/>\n<span class=\"token function\">ls<\/span> <span class=\"token parameter variable\">-la<\/span> \/proc\/<span class=\"token operator\">&lt;<\/span>PID<span class=\"token operator\">&gt;<\/span>\/ns<\/p>\n<p><span class=\"token comment\"># \u67e5\u770b\u8fdb\u7a0b\u7684 cgroup \u5f52\u5c5e<\/span><br \/>\n<span class=\"token function\">cat<\/span> \/proc\/<span class=\"token operator\">&lt;<\/span>PID<span class=\"token operator\">&gt;<\/span>\/cgroup<\/p>\n<p><span class=\"token comment\"># \u67e5\u770b\u8fdb\u7a0b\u7684\u5b8c\u6574\u4fe1\u606f&#xff08;\u5185\u6838\u89c6\u89d2&#xff09;<\/span><br \/>\n<span class=\"token function\">cat<\/span> \/proc\/<span class=\"token operator\">&lt;<\/span>PID<span class=\"token operator\">&gt;<\/span>\/stat<\/p>\n<h4>\u4f7f\u7528 strace \u8ddf\u8e2a\u8fdb\u7a0b\u7cfb\u7edf\u8c03\u7528<\/h4>\n<p>\u5f53\u4f60\u6000\u7591\u4e00\u4e2a\u670d\u52a1\u5728\u5b50\u8fdb\u7a0b\u7ba1\u7406\u4e0a\u6709\u95ee\u9898\u65f6&#xff0c;\u53ef\u4ee5\u7528 strace \u8ddf\u8e2a\u5176\u7cfb\u7edf\u8c03\u7528&#xff0c;\u89c2\u5bdf\u662f\u5426\u8c03\u7528\u4e86 wait() \u7cfb\u5217\u51fd\u6570&#xff1a;<\/p>\n<p><span class=\"token comment\"># \u5b89\u88c5 strace<\/span><br \/>\n<span class=\"token function\">sudo<\/span> <span class=\"token function\">apt<\/span> <span class=\"token function\">install<\/span> <span class=\"token parameter variable\">-y<\/span> <span class=\"token function\">strace<\/span><\/p>\n<p><span class=\"token comment\"># \u8ddf\u8e2a\u67d0\u4e2a\u8fdb\u7a0b\u7684\u7cfb\u7edf\u8c03\u7528&#xff0c;\u8fc7\u6ee4 wait \u76f8\u5173\u8c03\u7528<\/span><br \/>\n<span class=\"token function\">sudo<\/span> <span class=\"token function\">strace<\/span> <span class=\"token parameter variable\">-p<\/span> <span class=\"token operator\">&lt;<\/span>PID<span class=\"token operator\">&gt;<\/span> <span class=\"token parameter variable\">-e<\/span> <span class=\"token assign-left variable\">trace<\/span><span class=\"token operator\">&#061;<\/span>wait4,waitpid,waitid <span class=\"token operator\"><span class=\"token file-descriptor important\">2<\/span>&gt;<\/span><span class=\"token file-descriptor important\">&amp;1<\/span><\/p>\n<p>\u5982\u679c\u4f60\u770b\u5230 waitpid(-1, &#8230;) \u88ab\u5468\u671f\u6027\u8c03\u7528&#xff0c;\u8bf4\u660e\u7236\u8fdb\u7a0b\u5728\u6b63\u786e\u5730\u56de\u6536\u5b50\u8fdb\u7a0b\u3002\u5982\u679c\u5b8c\u5168\u6ca1\u6709 wait \u76f8\u5173\u8c03\u7528&#xff0c;\u8bf4\u660e\u7236\u8fdb\u7a0b\u6839\u672c\u6ca1\u6709\u5904\u7406\u5b50\u8fdb\u7a0b\u9000\u51fa&#xff0c;\u8fd9\u5c31\u662f\u50f5\u5c38\u8fdb\u7a0b\u7684\u6839\u6e90\u3002<\/p>\n<h3>\u5341\u4e5d\u3001\u5b8c\u6574\u64cd\u4f5c\u6d41\u7a0b\u56de\u987e<\/h3>\n<p>\u4e0b\u9762\u662f\u672c\u6587\u6240\u6709\u6838\u5fc3\u64cd\u4f5c\u7684\u5b8c\u6574\u6d41\u7a0b\u6c47\u603b&#xff0c;\u4f60\u53ef\u4ee5\u628a\u5b83\u4f5c\u4e3a\u4e00\u4e2a\u64cd\u4f5c\u624b\u518c\u6765\u4f7f\u7528\u3002<\/p>\n<p>\u6b65\u9aa4\u4e00&#xff1a;\u73af\u5883\u51c6\u5907<\/p>\n<p><span class=\"token comment\"># \u66f4\u65b0\u7cfb\u7edf\u8f6f\u4ef6\u6e90\u7d22\u5f15<\/span><br \/>\n<span class=\"token function\">sudo<\/span> <span class=\"token function\">apt<\/span> update<\/p>\n<p><span class=\"token comment\"># \u5b89\u88c5\u5fc5\u8981\u5de5\u5177<\/span><br \/>\n<span class=\"token function\">sudo<\/span> <span class=\"token function\">apt<\/span> <span class=\"token function\">install<\/span> <span class=\"token parameter variable\">-y<\/span> gcc python3 psmisc procps <span class=\"token function\">htop<\/span> <span class=\"token function\">strace<\/span><\/p>\n<p><span class=\"token comment\"># \u521b\u5efa\u5b9e\u9a8c\u76ee\u5f55<\/span><br \/>\n<span class=\"token function\">mkdir<\/span> <span class=\"token parameter variable\">-p<\/span> ~\/zombie_orphan_lab <span class=\"token operator\">&amp;&amp;<\/span> <span class=\"token builtin class-name\">cd<\/span> ~\/zombie_orphan_lab<\/p>\n<p>\u6b65\u9aa4\u4e8c&#xff1a;\u7406\u89e3\u8fdb\u7a0b\u72b6\u6001<\/p>\n<p><span class=\"token comment\"># \u67e5\u770b\u6240\u6709\u8fdb\u7a0b\u53ca\u5176\u72b6\u6001<\/span><br \/>\n<span class=\"token function\">ps<\/span> aux<\/p>\n<p><span class=\"token comment\"># \u4ee5\u6811\u5f62\u663e\u793a\u8fdb\u7a0b\u5c42\u7ea7\u5173\u7cfb&#xff0c;\u5e2e\u52a9\u7406\u89e3\u7236\u5b50\u5173\u7cfb<\/span><br \/>\npstree <span class=\"token parameter variable\">-p<\/span><\/p>\n<p><span class=\"token comment\"># \u5b9e\u65f6\u67e5\u770b\u8fdb\u7a0b\u72b6\u6001&#xff0c;\u5173\u6ce8 Tasks \u884c\u7684 zombie \u6570\u91cf<\/span><br \/>\n<span class=\"token function\">top<\/span><\/p>\n<p>\u6b65\u9aa4\u4e09&#xff1a;\u68c0\u67e5\u7cfb\u7edf\u4e2d\u7684\u50f5\u5c38\u8fdb\u7a0b<\/p>\n<p><span class=\"token comment\"># \u65b9\u6cd5\u4e00&#xff1a;ps \u8fc7\u6ee4<\/span><br \/>\n<span class=\"token function\">ps<\/span> aux <span class=\"token operator\">|<\/span> <span class=\"token function\">awk<\/span> <span class=\"token string\">&#039;NR&#061;&#061;1 || $8 ~ \/^Z\/&#039;<\/span><\/p>\n<p><span class=\"token comment\"># \u65b9\u6cd5\u4e8c&#xff1a;top \u7edf\u8ba1<\/span><br \/>\n<span class=\"token function\">top<\/span> <span class=\"token parameter variable\">-b<\/span> <span class=\"token parameter variable\">-n<\/span> <span class=\"token number\">1<\/span> <span class=\"token operator\">|<\/span> <span class=\"token function\">grep<\/span> <span class=\"token string\">&#034;^Tasks&#034;<\/span><\/p>\n<p><span class=\"token comment\"># \u65b9\u6cd5\u4e09&#xff1a;\u7edf\u8ba1\u5404\u7236\u8fdb\u7a0b\u7684\u50f5\u5c38\u5b50\u8fdb\u7a0b\u6570<\/span><br \/>\n<span class=\"token function\">ps<\/span> <span class=\"token parameter variable\">-eo<\/span> ppid,stat <span class=\"token operator\">|<\/span> <span class=\"token function\">awk<\/span> <span class=\"token string\">&#039;$2 ~ \/^Z\/ {count[$1]&#043;&#043;} END {for (p in count) print p, count[p]}&#039;<\/span><\/p>\n<p>\u6b65\u9aa4\u56db&#xff1a;\u5b9a\u4f4d\u95ee\u9898\u7236\u8fdb\u7a0b<\/p>\n<p><span class=\"token comment\"># \u5047\u8bbe\u53d1\u73b0\u50f5\u5c38\u8fdb\u7a0b PID \u4e3a 12346&#xff0c;PPID \u4e3a 12345<\/span><br \/>\n<span class=\"token comment\"># \u67e5\u770b\u7236\u8fdb\u7a0b\u8be6\u60c5<\/span><br \/>\n<span class=\"token function\">ps<\/span> <span class=\"token parameter variable\">-p<\/span> <span class=\"token number\">12345<\/span> <span class=\"token parameter variable\">-o<\/span> pid,ppid,stat,comm,args<\/p>\n<p><span class=\"token comment\"># \u67e5\u770b\u7236\u8fdb\u7a0b\u5b8c\u6574\u547d\u4ee4\u884c<\/span><br \/>\n<span class=\"token function\">cat<\/span> \/proc\/12345\/cmdline <span class=\"token operator\">|<\/span> <span class=\"token function\">tr<\/span> <span class=\"token string\">&#039;\\\\0&#039;<\/span> <span class=\"token string\">&#039; &#039;<\/span> <span class=\"token operator\">&amp;&amp;<\/span> <span class=\"token builtin class-name\">echo<\/span><\/p>\n<p><span class=\"token comment\"># \u67e5\u770b\u7236\u8fdb\u7a0b\u6253\u5f00\u7684\u6587\u4ef6<\/span><br \/>\n<span class=\"token function\">ls<\/span> <span class=\"token parameter variable\">-la<\/span> \/proc\/12345\/fd <span class=\"token operator\">|<\/span> <span class=\"token function\">head<\/span> <span class=\"token parameter variable\">-20<\/span><\/p>\n<p>\u6b65\u9aa4\u4e94&#xff1a;\u5c1d\u8bd5\u6062\u590d<\/p>\n<p><span class=\"token comment\"># \u5148\u53d1 SIGCHLD&#xff0c;\u770b\u662f\u5426\u6709\u6548<\/span><br \/>\n<span class=\"token function\">kill<\/span> <span class=\"token parameter variable\">-SIGCHLD<\/span> <span class=\"token number\">12345<\/span><br \/>\n<span class=\"token function\">sleep<\/span> <span class=\"token number\">3<\/span><br \/>\n<span class=\"token function\">ps<\/span> aux <span class=\"token operator\">|<\/span> <span class=\"token function\">awk<\/span> <span class=\"token string\">&#039;$8 ~ \/^Z\/&#039;<\/span><\/p>\n<p><span class=\"token comment\"># \u5982\u679c\u65e0\u6548&#xff0c;\u5728\u7ef4\u62a4\u7a97\u53e3\u4f18\u96c5\u505c\u6b62\u7236\u8fdb\u7a0b<\/span><br \/>\n<span class=\"token function\">sudo<\/span> <span class=\"token function\">kill<\/span> <span class=\"token parameter variable\">-SIGTERM<\/span> <span class=\"token number\">12345<\/span><br \/>\n<span class=\"token function\">sleep<\/span> <span class=\"token number\">10<\/span><\/p>\n<p><span class=\"token comment\"># \u5982\u679c SIGTERM \u65e0\u6548&#xff0c;\u5f3a\u5236\u7ec8\u6b62<\/span><br \/>\n<span class=\"token function\">sudo<\/span> <span class=\"token function\">kill<\/span> <span class=\"token parameter variable\">-SIGKILL<\/span> <span class=\"token number\">12345<\/span><\/p>\n<p>\u6b65\u9aa4\u516d&#xff1a;\u786e\u8ba4\u6062\u590d\u6548\u679c<\/p>\n<p><span class=\"token comment\"># \u786e\u8ba4\u50f5\u5c38\u8fdb\u7a0b\u5df2\u6e05\u9664<\/span><br \/>\n<span class=\"token function\">ps<\/span> aux <span class=\"token operator\">|<\/span> <span class=\"token function\">awk<\/span> <span class=\"token string\">&#039;$8 ~ \/^Z\/&#039;<\/span><\/p>\n<p><span class=\"token comment\"># \u786e\u8ba4 PID \u7a7a\u95f4\u5df2\u91ca\u653e<\/span><br \/>\n<span class=\"token function\">cat<\/span> \/proc\/sys\/kernel\/pid_max<br \/>\n<span class=\"token function\">ps<\/span> aux <span class=\"token operator\">|<\/span> <span class=\"token function\">wc<\/span> <span class=\"token parameter variable\">-l<\/span><\/p>\n<p>\u6b65\u9aa4\u4e03&#xff1a;\u4ece\u4ee3\u7801\u5c42\u9762\u6839\u6cbb&#xff08;\u4ee5 Python \u4e3a\u4f8b&#xff09;<\/p>\n<p><span class=\"token comment\"># \u4fee\u6539\u670d\u52a1\u4ee3\u7801&#xff0c;\u6b63\u786e\u5904\u7406 SIGCHLD<\/span><br \/>\n<span class=\"token comment\"># \u53c2\u8003\u672c\u6587\u6848\u4f8b\u4e09\u4e2d\u7684 good_worker_signal.py<\/span><\/p>\n<p><span class=\"token comment\"># \u91cd\u542f\u670d\u52a1&#xff0c;\u89c2\u5bdf\u662f\u5426\u4e0d\u518d\u4ea7\u751f\u50f5\u5c38\u8fdb\u7a0b<\/span><br \/>\n<span class=\"token function\">sudo<\/span> systemctl restart your_service<\/p>\n<p><span class=\"token comment\"># \u6301\u7eed\u76d1\u63a7<\/span><br \/>\n<span class=\"token function\">watch<\/span> <span class=\"token parameter variable\">-n<\/span> <span class=\"token number\">5<\/span> <span class=\"token string\">&#034;ps aux | awk &#039;\\\\<span class=\"token variable\">$8<\/span> ~ \/^Z\/&#039;&#034;<\/span><\/p>\n<p>\u6b65\u9aa4\u516b&#xff1a;Docker \u5bb9\u5668\u73af\u5883\u4e0b\u7684\u5904\u7406<\/p>\n<p><span class=\"token comment\"># \u4e3a\u5df2\u6709\u5bb9\u5668\u542f\u7528 init&#xff08;\u9700\u8981\u91cd\u5efa\u5bb9\u5668&#xff09;<\/span><br \/>\n<span class=\"token function\">docker<\/span> run <span class=\"token parameter variable\">&#8211;init<\/span> <span class=\"token parameter variable\">-d<\/span> your_image<\/p>\n<p><span class=\"token comment\"># \u6216\u8005\u5728 Docker Compose \u4e2d\u914d\u7f6e<\/span><br \/>\n<span class=\"token comment\"># \u4fee\u6539 docker-compose.yml&#xff0c;\u6dfb\u52a0 init: true<\/span><\/p>\n<p><span class=\"token comment\"># \u9a8c\u8bc1\u5bb9\u5668\u5185 PID 1 \u662f init<\/span><br \/>\n<span class=\"token function\">docker<\/span> <span class=\"token builtin class-name\">exec<\/span> <span class=\"token operator\">&lt;<\/span>container_id<span class=\"token operator\">&gt;<\/span> <span class=\"token function\">ps<\/span> aux <span class=\"token operator\">|<\/span> <span class=\"token function\">head<\/span> <span class=\"token parameter variable\">-3<\/span><\/p>\n<h3>\u4e8c\u5341\u3001\u603b\u7ed3<\/h3>\n<p>\u8fd9\u7bc7\u6587\u7ae0\u6211\u4eec\u4ece Linux \u8fdb\u7a0b\u751f\u547d\u5468\u671f\u51fa\u53d1&#xff0c;\u5b8c\u6574\u5730\u8bb2\u89e3\u4e86\u50f5\u5c38\u8fdb\u7a0b\u548c\u5b64\u513f\u8fdb\u7a0b\u8fd9\u4e24\u4e2a\u65e2\u6709\u8054\u7cfb\u53c8\u6709\u672c\u8d28\u533a\u522b\u7684\u6982\u5ff5\u3002<\/p>\n<p>\u6838\u5fc3\u8981\u70b9\u56de\u987e&#xff1a;<\/p>\n<p>\u7b2c\u4e00&#xff0c;\u8fdb\u7a0b\u9000\u51fa\u662f\u4e00\u4e2a\u9700\u8981\u7236\u5b50\u534f\u4f5c\u5b8c\u6210\u7684\u8fc7\u7a0b\u3002\u5b50\u8fdb\u7a0b\u8c03\u7528 exit() \u53ea\u662f\u9000\u51fa\u7684\u7b2c\u4e00\u6b65&#xff0c;\u5185\u6838\u4f1a\u4fdd\u7559\u8fdb\u7a0b\u63cf\u8ff0\u7b26&#xff08;\u50f5\u5c38\u72b6\u6001&#xff09;&#xff0c;\u7b49\u5f85\u7236\u8fdb\u7a0b\u8c03\u7528 wait() \u6765\u8bfb\u53d6\u9000\u51fa\u72b6\u6001&#xff0c;\u4e4b\u540e\u5185\u6838\u624d\u4f1a\u5f7b\u5e95\u6e05\u7406\u8fdb\u7a0b&#xff0c;\u91ca\u653e PID\u3002<\/p>\n<p>\u7b2c\u4e8c&#xff0c;\u50f5\u5c38\u8fdb\u7a0b\u7684\u5371\u5bb3\u5728\u4e8e PID \u6cc4\u6f0f&#xff0c;\u800c\u4e0d\u662f\u5185\u5b58\u6cc4\u6f0f\u3002\u5355\u4e2a\u50f5\u5c38\u8fdb\u7a0b\u53ea\u5360\u7528\u4e00\u4e2a PID \u548c\u6781\u5c11\u91cf\u5185\u6838\u5185\u5b58&#xff0c;\u4f46\u5927\u91cf\u79ef\u7d2f\u4f1a\u8017\u5c3d PID \u7a7a\u95f4&#xff0c;\u5bfc\u81f4\u7cfb\u7edf\u65e0\u6cd5\u521b\u5efa\u65b0\u8fdb\u7a0b\u3002<\/p>\n<p>\u7b2c\u4e09&#xff0c;\u5b64\u513f\u8fdb\u7a0b\u901a\u5e38\u662f\u65e0\u5bb3\u7684\u3002\u7236\u8fdb\u7a0b\u5148\u9000\u51fa\u540e&#xff0c;Linux \u5185\u6838\u4f1a\u81ea\u52a8\u628a\u5b64\u513f\u8fdb\u7a0b\u7684\u7236\u8fdb\u7a0b\u6539\u4e3a systemd&#xff08;PID 1&#xff09;&#xff0c;\u7531 systemd \u8d1f\u8d23\u5728\u5b64\u513f\u8fdb\u7a0b\u9000\u51fa\u65f6\u6b63\u786e\u56de\u6536\u3002\u5b64\u513f\u8fdb\u7a0b\u4e0d\u4f1a\u53d8\u6210\u50f5\u5c38\u8fdb\u7a0b\u3002<\/p>\n<p>\u7b2c\u56db&#xff0c;\u50f5\u5c38\u8fdb\u7a0b\u65e0\u6cd5\u88ab\u76f4\u63a5 kill \u6740\u6b7b&#xff0c;\u5fc5\u987b\u901a\u8fc7\u5176\u7236\u8fdb\u7a0b\u6765\u89e3\u51b3\u3002\u53ef\u4ee5\u5c1d\u8bd5\u5411\u7236\u8fdb\u7a0b\u53d1\u9001 SIGCHLD&#xff0c;\u6216\u8005\u5728\u7ef4\u62a4\u7a97\u53e3\u91cd\u542f\u7236\u8fdb\u7a0b\u3002<\/p>\n<p>\u7b2c\u4e94&#xff0c;Docker \u5bb9\u5668\u4e2d\u7684 PID 1 \u95ee\u9898\u662f\u50f5\u5c38\u8fdb\u7a0b\u7684\u9ad8\u53d1\u573a\u666f\u3002\u89e3\u51b3\u65b9\u6848\u662f\u4f7f\u7528 &#8211;init \u53c2\u6570\u6216\u5728\u955c\u50cf\u4e2d\u96c6\u6210 tini&#xff0c;\u8ba9\u4e00\u4e2a\u771f\u6b63\u7684 init \u8fdb\u7a0b\u62c5\u4efb PID 1 \u7684\u804c\u8d23\u3002<\/p>\n<p>\u7b2c\u516d&#xff0c;\u4ece\u4ee3\u7801\u5c42\u9762\u6839\u6cbb\u624d\u662f\u7ec8\u6781\u65b9\u6848\u3002\u65e0\u8bba\u662f C\u3001Python\u3001Java \u8fd8\u662f Go&#xff0c;\u53ea\u8981\u6d89\u53ca fork \u5b50\u8fdb\u7a0b&#xff0c;\u5c31\u5fc5\u987b\u786e\u4fdd\u5b50\u8fdb\u7a0b\u7684\u9000\u51fa\u72b6\u6001\u88ab\u6b63\u786e\u8bfb\u53d6&#xff0c;\u8fd9\u662f\u907f\u514d\u50f5\u5c38\u8fdb\u7a0b\u79ef\u7d2f\u7684\u6839\u672c\u3002<\/p>\n<p>\u4e0e\u7b2c10\u8282&#xff08;\u8d44\u6e90\u5f02\u5e38\u6392\u67e5\u6848\u4f8b&#xff09;\u7684\u8854\u63a5&#xff1a;<\/p>\n<p>\u672c\u8282\u8bb2\u89e3\u7684\u50f5\u5c38\u8fdb\u7a0b\u548c\u5b64\u513f\u8fdb\u7a0b&#xff0c;\u662f\u7b2c10\u8282&#034;\u8d44\u6e90\u5f02\u5e38\u6392\u67e5\u6848\u4f8b&#034;\u7684\u91cd\u8981\u57fa\u7840\u3002\u5728\u5b9e\u9645\u6392\u67e5\u8d44\u6e90\u5f02\u5e38\u65f6&#xff0c;PID \u8017\u5c3d\u3001\u8fdb\u7a0b\u65e0\u6cd5\u521b\u5efa\u7b49\u95ee\u9898\u5f80\u5f80\u8ffd\u6839\u6eaf\u6e90\u90fd\u4f1a\u6307\u5411\u5927\u91cf\u79ef\u7d2f\u7684\u50f5\u5c38\u8fdb\u7a0b&#xff0c;\u4ee5\u53ca\u4ea7\u751f\u8fd9\u4e9b\u50f5\u5c38\u8fdb\u7a0b\u7684\u6709\u95ee\u9898\u7684\u7236\u8fdb\u7a0b\u3002\u7406\u89e3\u4e86\u672c\u8282\u7684\u5185\u5bb9&#xff0c;\u4f60\u5728\u9762\u5bf9\u7b2c10\u8282\u7684\u7efc\u5408\u6392\u67e5\u6848\u4f8b\u65f6&#xff0c;\u4f1a\u6709\u66f4\u6e05\u6670\u7684\u65b9\u5411\u611f\u3002<\/p>\n<p>\u4e0b\u4e00\u7bc7\u9884\u544a&#xff1a;<\/p>\n<p>\u7b2c\u5341\u7ae0\u7b2c10\u8282\u5c06\u8fdb\u884c\u7efc\u5408\u6027\u7684\u8d44\u6e90\u5f02\u5e38\u6392\u67e5\u6848\u4f8b\u5b9e\u6218&#xff0c;\u628a\u524d9\u8282\u7684\u77e5\u8bc6\u70b9\u2014\u2014\u8fdb\u7a0b\u3001\u7ebf\u7a0b\u3001\u4efb\u52a1\u3001\u4fe1\u53f7\u3001\u4f18\u5148\u7ea7\u3001CPU\/\u5185\u5b58\u5206\u6790\u3001\u8d1f\u8f7d\u7406\u89e3\u3001\u50f5\u5c38\u8fdb\u7a0b\u2014\u2014\u4e32\u8054\u8d77\u6765&#xff0c;\u901a\u8fc7\u771f\u5b9e\u7684\u7ebf\u4e0a\u6545\u969c\u6848\u4f8b&#xff0c;\u7cfb\u7edf\u6027\u5730\u6f14\u793a\u4ece\u53d1\u73b0\u5f02\u5e38\u5230\u5b9a\u4f4d\u6839\u56e0\u518d\u5230\u4fee\u590d\u6062\u590d\u7684\u5b8c\u6574\u6392\u67e5\u6d41\u7a0b\u3002\u5982\u679c\u4f60\u8fd8\u6ca1\u6709\u5145\u5206\u638c\u63e1\u524d\u51e0\u8282\u7684\u5185\u5bb9&#xff0c;\u5efa\u8bae\u5148\u56de\u53bb\u6e29\u4e60\u4e00\u4e0b&#xff0c;\u7b2c10\u8282\u7684\u5b9e\u6218\u4f1a\u540c\u65f6\u7528\u5230\u8fd9\u4e9b\u77e5\u8bc6\u3002<\/p>\n<p>\u2026<\/p>\n<p>ok&#xff0c;\u540c\u5b66\u4eec&#xff0c;\u672c\u8282\u8bfe\u5c31\u4e0a\u5230\u8fd9\u513f&#xff0c;\u4e0b\u8bfe~<\/p>\n<h3>&#x1f9e7; \u5b66\u4e60\u798f\u5229 \u00b7 \u9650\u65f6\u5f00\u653e &#x1f9e7;<\/h3>\n<p>\u5f53\u7136&#xff0c;\u65e0\u8bba\u4f60\u662f\u8ba1\u7b97\u673a\u4e13\u4e1a\u5728\u8bfb\u5b66\u751f&#xff0c;\u8fd8\u662f\u521a\u5f00\u59cb\u63a5\u89e6 Linux \u7684\u5165\u95e8\u8005&#xff0c;\u4ea6\u6216\u662f\u5e0c\u671b\u63d0\u5347\u5f00\u53d1\u73af\u5883\u642d\u5efa\u3001\u670d\u52a1\u5668\u7ba1\u7406\u3001\u7cfb\u7edf\u8fd0\u7ef4\u80fd\u529b\u7684\u5f00\u53d1\u8005&#xff0c;\u90fd\u5f3a\u70c8\u5efa\u8bae\u7cfb\u7edf\u5b66\u4e60 Ubuntu \u5168\u4f53\u7cfb\u4e13\u680f&#xff1a;&#x1f449; \u300c\u6eda\u96ea\u7403\u5b66 Ubuntu\u300d&#xff1b;\u6db5\u76d6 Ubuntu \u4ece\u5165\u95e8\u5230\u8fdb\u9636\u7684\u5b8c\u6574\u5b66\u4e60\u5185\u5bb9\u3002<\/p>\n<p>\u8be5\u4e13\u680f\u4ee5\u201c\u5faa\u5e8f\u6e10\u8fdb &#043; \u5b9e\u6218\u9a71\u52a8 &#043; \u573a\u666f\u843d\u5730\u201d\u4e3a\u6838\u5fc3\u7406\u5ff5&#xff0c;\u4ece\u57fa\u7840\u8ba4\u77e5\u5230\u7cfb\u7edf\u5b89\u88c5&#xff0c;\u4ece\u684c\u9762\u64cd\u4f5c\u5230\u547d\u4ee4\u884c\u5b9e\u8df5&#xff0c;\u4ece\u7cfb\u7edf\u7ba1\u7406\u5230\u7f51\u7edc\u914d\u7f6e&#xff0c;\u518d\u5230\u5b89\u5168\u52a0\u56fa\u3001\u5f00\u53d1\u73af\u5883\u642d\u5efa\u3001\u6027\u80fd\u4f18\u5316\u3001\u6545\u969c\u6392\u67e5\u3001\u670d\u52a1\u5668\u90e8\u7f72\u4e0e\u4e91\u5e73\u53f0\u5e94\u7528&#xff0c;\u9010\u5c42\u5c55\u5f00&#xff0c;\u5e2e\u52a9\u4f60\u5feb\u901f\u5efa\u7acb\u5b8c\u6574\u7684 Ubuntu \/ Linux \u6280\u672f\u4f53\u7cfb&#xff0c;\u771f\u6b63\u628a Ubuntu \u7528\u660e\u767d\u3001\u7528\u719f\u7ec3\u3001\u7528\u7a33\u5b9a\u3002<\/p>\n<p>&#x1f4cc; \u5b66\u4e60\u627f\u8bfa&#xff1a; \u901a\u8fc7\u8be5\u4e13\u680f&#xff0c;\u4f60\u5c06\u80fd\u591f&#xff1a;<\/p>\n<ul>\n<li>\u5feb\u901f\u638c\u63e1 Ubuntu \u7cfb\u7edf\u5b89\u88c5\u3001\u914d\u7f6e\u4e0e\u65e5\u5e38\u4f7f\u7528\u80fd\u529b<\/li>\n<li>\u719f\u7ec3\u4f7f\u7528 Linux \u5e38\u7528\u547d\u4ee4\u3001\u7ec8\u7aef\u5de5\u5177\u4e0e\u7cfb\u7edf\u7ba1\u7406\u6280\u5de7<\/li>\n<li>\u642d\u5efa\u7a33\u5b9a\u53ef\u590d\u7528\u7684\u5f00\u53d1\u73af\u5883\u3001\u6d4b\u8bd5\u73af\u5883\u4e0e\u670d\u52a1\u5668\u73af\u5883<\/li>\n<li>\u638c\u63e1\u7f51\u7edc\u914d\u7f6e\u3001SSH \u8fdc\u7a0b\u8bbf\u95ee\u3001\u9632\u706b\u5899\u3001\u5b89\u5168\u52a0\u56fa\u4e0e\u5907\u4efd\u6062\u590d<\/li>\n<li>\u5efa\u7acb\u4ece\u201c\u5165\u95e8\u4f7f\u7528\u201d\u5230\u201c\u72ec\u7acb\u8fd0\u7ef4\u201d\u7684\u5b8c\u6574 Ubuntu \u5b9e\u6218\u80fd\u529b<\/li>\n<\/ul>\n<p>\u5c31\u50cf\u201c\u6eda\u96ea\u7403\u201d\u4e00\u6837&#xff0c;\u5148\u6253\u7262\u57fa\u7840&#xff0c;\u518d\u6301\u7eed\u53e0\u52a0\u7cfb\u7edf\u7ba1\u7406\u3001\u5f00\u53d1\u73af\u5883\u3001\u7f51\u7edc\u5b89\u5168\u3001\u6027\u80fd\u4f18\u5316\u4e0e\u670d\u52a1\u5668\u90e8\u7f72\u80fd\u529b&#xff0c;\u8ba9\u77e5\u8bc6\u4e0d\u65ad\u79ef\u7d2f\u3001\u7ecf\u9a8c\u6301\u7eed\u653e\u5927&#xff0c;\u6700\u7ec8\u5f62\u6210\u5c5e\u4e8e\u81ea\u5df1\u7684 Linux \u6280\u672f\u62a4\u57ce\u6cb3 &#x1f680;<\/p>\n<p>\u6700\u540e&#xff0c;\u5982\u679c\u8fd9\u7bc7\u6587\u7ae0\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9&#xff0c;\u5e2e\u5fd9\u7ed9\u4f5c\u8005\u6765\u4e2a\u4e00\u952e\u4e09\u8fde&#xff0c;\u5173\u6ce8\u3001\u70b9\u8d5e\u3001\u6536\u85cf&#xff0c;\u60a8\u7684\u652f\u6301\u5c31\u662f\u6211\u575a\u6301\u5199\u4f5c\u6700\u5927\u7684\u52a8\u529b\u3002<\/p>\n<p>\u540c\u65f6\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u6280\u672f\u53f7&#xff1a;\u300c\u733f\u5708\u5947\u5999\u5c4b\u300d &#xff0c;\u4ee5\u4fbf\u5b66\u4e60\u66f4\u591a\u540c\u7c7b\u578b\u7684\u6280\u672f\u6587\u7ae0&#xff0c;\u514d\u8d39\u767d\u5ad6\u6700\u65b0 BAT \u4e92\u8054\u7f51\u516c\u53f8\u9762\u8bd5\u9898\u30014000G PDF \u7f16\u7a0b\u7535\u5b50\u4e66\u3001\u7b80\u5386\u6a21\u677f\u3001\u6280\u672f\u6587\u7ae0 Markdown \u6587\u6863\u7b49\u6d77\u91cf\u8d44\u6599\u3002<\/p>\n<p>ps&#xff1a;\u672c\u6587\u6d89\u53ca\u7684 Ubuntu \u5e38\u7528\u547d\u4ee4\u3001\u7cfb\u7edf\u914d\u7f6e\u3001\u73af\u5883\u642d\u5efa\u6b65\u9aa4\u3001\u670d\u52a1\u90e8\u7f72\u601d\u8def\u4e0e\u6392\u969c\u65b9\u6cd5&#xff0c;\u5747\u5df2\u5728\u6587\u7ae0\u4e2d\u7ed3\u5408\u5b9e\u6218\u573a\u666f\u8fdb\u884c\u6574\u7406&#xff0c;\u540c\u5b66\u4eec\u53ef\u4ee5\u76f4\u63a5\u5bf9\u7167\u5b66\u4e60\u3001\u8fb9\u770b\u8fb9\u7ec3\u3002\u539f\u521b\u6574\u7406\u4e0d\u6613&#xff0c;\u5982\u679c\u89c9\u5f97\u5185\u5bb9\u6709\u5e2e\u52a9&#xff0c;\u6b22\u8fce\u70b9\u8d5e\u3001\u6536\u85cf\u3001\u8bc4\u8bba\u652f\u6301\u4e00\u6ce2&#xff0c;\u975e\u5e38\u611f\u8c22\u2757<\/p>\n<h3>&#x1faf5; Who am I&#xff1f;<\/h3>\n<p>\u6211\u662f bug\u83cc&#xff0c;\u4e00\u540d\u957f\u671f\u6df1\u8015\u540e\u7aef\u5f00\u53d1\u3001Linux \u7cfb\u7edf\u5b9e\u8df5\u4e0e\u670d\u52a1\u5668\u8fd0\u7ef4\u9886\u57df\u7684\u4e00\u7ebf\u6280\u672f\u521b\u4f5c\u8005&#xff0c;\u66fe\u62c5\u4efb\u72ec\u89d2\u517d\u4f01\u4e1a\u540e\u7aef\u6280\u672f\u7ecf\u7406\u3001\u7814\u53d1\u67b6\u6784\u5e08\u7b49\u804c\u4f4d&#xff0c;\u957f\u671f\u4e13\u6ce8\u4e8e Java \u540e\u7aef\u3001Linux \/ Ubuntu \u7cfb\u7edf\u7ba1\u7406\u3001\u670d\u52a1\u5668\u90e8\u7f72\u3001\u5206\u5e03\u5f0f\u67b6\u6784\u3001\u5fae\u670d\u52a1\u6cbb\u7406\u3001\u9ad8\u5e76\u53d1\u7cfb\u7edf\u3001\u5de5\u7a0b\u6548\u80fd\u4e0e\u7814\u53d1\u7ba1\u7406\u7b49\u65b9\u5411\u3002<\/p>\n<p>\u76ee\u524d\u6d3b\u8dc3\u4e8e\u591a\u4e2a\u4e3b\u6d41\u6280\u672f\u793e\u533a&#xff0c;\u5305\u62ec&#xff1a;<\/p>\n<p>CSDN&#xff5c;\u7a00\u571f\u6398\u91d1&#xff5c;InfoQ&#xff5c;51CTO&#xff5c;\u534e\u4e3a\u4e91\u5f00\u53d1\u8005\u793e\u533a&#xff5c;\u963f\u91cc\u4e91\u5f00\u53d1\u8005\u793e\u533a&#xff5c;\u817e\u8baf\u4e91\u5f00\u53d1\u8005\u793e\u533a&#xff5c;\u5f00\u6e90\u4e2d\u56fd&#xff5c;\u535a\u5ba2\u56ed&#xff5c;\u58a8\u5929\u8f6e \u7b49\u5e73\u53f0\u3002<\/p>\n<p>\u66fe\u83b7\u5f97&#xff1a;<\/p>\n<ul>\n<li>CSDN \u535a\u5ba2\u4e4b\u661f Top30<\/li>\n<li>\u534e\u4e3a\u4e91\u591a\u5e74\u5ea6\u5341\u4f73\u535a\u4e3b &amp; \u5353\u8d8a\u8d21\u732e\u5956<\/li>\n<li>\u6398\u91d1\u591a\u5e74\u5ea6\u4eba\u6c14\u4f5c\u8005 Top40<\/li>\n<li>CSDN\u3001\u6398\u91d1\u3001InfoQ\u300151CTO \u7b49\u5e73\u53f0\u7b7e\u7ea6\u4f5c\u8005 \/ \u4f18\u8d28\u4f5c\u8005<\/li>\n<\/ul>\n<p>\u622a\u81f3\u76ee\u524d&#xff0c;\u5168\u7f51\u6280\u672f\u5185\u5bb9\u7d2f\u8ba1\u5f71\u54cd\u8bfb\u8005\u4f17\u591a&#xff0c;\u5168\u7f51\u7c89\u4e1d\u5df2\u8d85\u8fc7 30w&#043;\u3002<\/p>\n<p>\u5982\u679c\u4f60\u4e5f\u5173\u6ce8 Linux\u3001Ubuntu\u3001Java \u540e\u7aef\u3001\u670d\u52a1\u5668\u8fd0\u7ef4\u3001\u67b6\u6784\u8bbe\u8ba1\u3001\u6280\u672f\u6210\u957f\u3001\u804c\u573a\u8fdb\u9636\u4e0e\u7814\u53d1\u7ba1\u7406&#xff0c;\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u6280\u672f\u5185\u5bb9\u5408\u96c6\u5165\u53e3&#xff1a;&#x1f449; \u70b9\u51fb\u67e5\u770b &#x1f448;\ufe0f<\/p>\n<p>\u786c\u6838\u6280\u672f\u53f7 \u300c\u733f\u5708\u5947\u5999\u5c4b\u300d \u671f\u5f85\u4f60\u7684\u52a0\u5165\u3002<\/p>\n<p>\u8fd9\u91cc\u4e0d\u4ec5\u5206\u4eab\u6280\u672f\u5e72\u8d27&#xff0c;\u4e5f\u8bb0\u5f55\u4e00\u7ebf\u7814\u53d1\u4eba\u7684\u6210\u957f\u3001\u8e29\u5751\u3001\u601d\u8003\u4e0e\u8fdb\u9636\u8def\u5f84\u3002<\/p>\n<p>\u613f\u6211\u4eec\u4e00\u8d77\u6253\u602a\u5347\u7ea7&#xff0c;\u628a Ubuntu \u7528\u6210\u751f\u4ea7\u529b\u5de5\u5177&#xff0c;\u628a Linux \u7ec3\u6210\u6838\u5fc3\u7ade\u4e89\u529b&#xff0c;\u5728\u6280\u672f\u8def\u4e0a\u6301\u7eed\u8fdb\u9636\u3002<\/p>\n<p> <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261026160142-6adf79663c1c4.png\" \/> <\/p>\n<p>&#8211; End &#8211;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#x1f3c6; \u672c\u6587\u6536\u5f55\u4e8e \u300a\u6eda\u96ea\u7403\u5b66 Ubuntu\u300b \u4e13\u680f\u3002 \u672c\u4e13\u680f\u9762\u5411\u6709\u4e00\u5b9a\u8ba1\u7b97\u673a\u57fa\u7840&#xff0c;\u4f46\u5c1a\u672a\u7cfb\u7edf\u5b66\u4e60 Linux \/ Ubuntu \u7684\u8bfb\u8005&#xff0c;\u91c7\u7528\u201c\u6eda\u96ea\u7403\u5f0f\u5b66\u4e60\u6cd5\u201d&#xff1a;\u5148\u88c5\u597d\u3001\u518d\u4f1a\u7528\u3001\u518d\u7406\u89e3\u3001\u518d\u4f18\u5316\u3001\u518d\u5b9e\u6218&#xff0c;\u5e26\u4f60\u4ece\u7b2c\u4e00\u6b21\u8fdb\u5165 Ubuntu \u684c\u9762 \/ \u7ec8\u7aef\u5f00\u59cb&#xff0c;\u9010\u6b65\u638c\u63e1 Ubuntu \u7684\u65e5\u5e38\u4f7f\u7528\u3001\u547d\u4ee4\u64cd\u4f5c\u3001\u8f6f\u4ef6\u5b89\u88c5\u3001\u7cfb\u7edf\u914d\u7f6e\u3001\u5f00\u53d1\u73af\u5883\u642d\u5efa\u4e0e\u670d\u52a1\u5668\u90e8\u7f72\u80fd\u529b\u3002 \u2003 &#x1f3af; \u9002\u5408\u4eba\u7fa4&amp;#xff1a<\/p>\n","protected":false},"author":2,"featured_media":118330,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[13436,13437,58,103,44],"topic":[],"class_list":["post-118337","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-13436","tag-13437","tag-linux","tag-ubuntu","tag-44"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Ubuntu\u96f6\u57fa\u7840\u5165\u95e8\u5230\u7cbe\u901a\u301010.9\u8bb2\u3011\uff1a\u50f5\u5c38\u8fdb\u7a0b\u4e0e\u5b64\u513f\u8fdb\u7a0b\u2014\u2014Linux \u8fdb\u7a0b\u751f\u547d\u5468\u671f\u7684\u201c\u7070\u8272\u5730\u5e26\u201c - \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\/118337.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Ubuntu\u96f6\u57fa\u7840\u5165\u95e8\u5230\u7cbe\u901a\u301010.9\u8bb2\u3011\uff1a\u50f5\u5c38\u8fdb\u7a0b\u4e0e\u5b64\u513f\u8fdb\u7a0b\u2014\u2014Linux \u8fdb\u7a0b\u751f\u547d\u5468\u671f\u7684\u201c\u7070\u8272\u5730\u5e26\u201c - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"&#x1f3c6; \u672c\u6587\u6536\u5f55\u4e8e \u300a\u6eda\u96ea\u7403\u5b66 Ubuntu\u300b \u4e13\u680f\u3002 \u672c\u4e13\u680f\u9762\u5411\u6709\u4e00\u5b9a\u8ba1\u7b97\u673a\u57fa\u7840&#xff0c;\u4f46\u5c1a\u672a\u7cfb\u7edf\u5b66\u4e60 Linux \/ Ubuntu \u7684\u8bfb\u8005&#xff0c;\u91c7\u7528\u201c\u6eda\u96ea\u7403\u5f0f\u5b66\u4e60\u6cd5\u201d&#xff1a;\u5148\u88c5\u597d\u3001\u518d\u4f1a\u7528\u3001\u518d\u7406\u89e3\u3001\u518d\u4f18\u5316\u3001\u518d\u5b9e\u6218&#xff0c;\u5e26\u4f60\u4ece\u7b2c\u4e00\u6b21\u8fdb\u5165 Ubuntu \u684c\u9762 \/ \u7ec8\u7aef\u5f00\u59cb&#xff0c;\u9010\u6b65\u638c\u63e1 Ubuntu \u7684\u65e5\u5e38\u4f7f\u7528\u3001\u547d\u4ee4\u64cd\u4f5c\u3001\u8f6f\u4ef6\u5b89\u88c5\u3001\u7cfb\u7edf\u914d\u7f6e\u3001\u5f00\u53d1\u73af\u5883\u642d\u5efa\u4e0e\u670d\u52a1\u5668\u90e8\u7f72\u80fd\u529b\u3002 \u2003 &#x1f3af; \u9002\u5408\u4eba\u7fa4&amp;#xff1a\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/118337.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-10-26T16:01:44+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261026160132-6adf795c2eb04.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=\"25 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/118337.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/118337.html\",\"name\":\"Ubuntu\u96f6\u57fa\u7840\u5165\u95e8\u5230\u7cbe\u901a\u301010.9\u8bb2\u3011\uff1a\u50f5\u5c38\u8fdb\u7a0b\u4e0e\u5b64\u513f\u8fdb\u7a0b\u2014\u2014Linux \u8fdb\u7a0b\u751f\u547d\u5468\u671f\u7684\u201c\u7070\u8272\u5730\u5e26\u201c - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-10-26T16:01:44+00:00\",\"dateModified\":\"2026-10-26T16:01:44+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/118337.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/118337.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/118337.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Ubuntu\u96f6\u57fa\u7840\u5165\u95e8\u5230\u7cbe\u901a\u301010.9\u8bb2\u3011\uff1a\u50f5\u5c38\u8fdb\u7a0b\u4e0e\u5b64\u513f\u8fdb\u7a0b\u2014\u2014Linux \u8fdb\u7a0b\u751f\u547d\u5468\u671f\u7684\u201c\u7070\u8272\u5730\u5e26\u201c\"}]},{\"@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":"Ubuntu\u96f6\u57fa\u7840\u5165\u95e8\u5230\u7cbe\u901a\u301010.9\u8bb2\u3011\uff1a\u50f5\u5c38\u8fdb\u7a0b\u4e0e\u5b64\u513f\u8fdb\u7a0b\u2014\u2014Linux \u8fdb\u7a0b\u751f\u547d\u5468\u671f\u7684\u201c\u7070\u8272\u5730\u5e26\u201c - \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\/118337.html","og_locale":"zh_CN","og_type":"article","og_title":"Ubuntu\u96f6\u57fa\u7840\u5165\u95e8\u5230\u7cbe\u901a\u301010.9\u8bb2\u3011\uff1a\u50f5\u5c38\u8fdb\u7a0b\u4e0e\u5b64\u513f\u8fdb\u7a0b\u2014\u2014Linux \u8fdb\u7a0b\u751f\u547d\u5468\u671f\u7684\u201c\u7070\u8272\u5730\u5e26\u201c - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"&#x1f3c6; \u672c\u6587\u6536\u5f55\u4e8e \u300a\u6eda\u96ea\u7403\u5b66 Ubuntu\u300b \u4e13\u680f\u3002 \u672c\u4e13\u680f\u9762\u5411\u6709\u4e00\u5b9a\u8ba1\u7b97\u673a\u57fa\u7840&#xff0c;\u4f46\u5c1a\u672a\u7cfb\u7edf\u5b66\u4e60 Linux \/ Ubuntu \u7684\u8bfb\u8005&#xff0c;\u91c7\u7528\u201c\u6eda\u96ea\u7403\u5f0f\u5b66\u4e60\u6cd5\u201d&#xff1a;\u5148\u88c5\u597d\u3001\u518d\u4f1a\u7528\u3001\u518d\u7406\u89e3\u3001\u518d\u4f18\u5316\u3001\u518d\u5b9e\u6218&#xff0c;\u5e26\u4f60\u4ece\u7b2c\u4e00\u6b21\u8fdb\u5165 Ubuntu \u684c\u9762 \/ \u7ec8\u7aef\u5f00\u59cb&#xff0c;\u9010\u6b65\u638c\u63e1 Ubuntu \u7684\u65e5\u5e38\u4f7f\u7528\u3001\u547d\u4ee4\u64cd\u4f5c\u3001\u8f6f\u4ef6\u5b89\u88c5\u3001\u7cfb\u7edf\u914d\u7f6e\u3001\u5f00\u53d1\u73af\u5883\u642d\u5efa\u4e0e\u670d\u52a1\u5668\u90e8\u7f72\u80fd\u529b\u3002 \u2003 &#x1f3af; \u9002\u5408\u4eba\u7fa4&amp;#xff1a","og_url":"https:\/\/www.wsisp.com\/helps\/118337.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-10-26T16:01:44+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261026160132-6adf795c2eb04.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"25 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/118337.html","url":"https:\/\/www.wsisp.com\/helps\/118337.html","name":"Ubuntu\u96f6\u57fa\u7840\u5165\u95e8\u5230\u7cbe\u901a\u301010.9\u8bb2\u3011\uff1a\u50f5\u5c38\u8fdb\u7a0b\u4e0e\u5b64\u513f\u8fdb\u7a0b\u2014\u2014Linux \u8fdb\u7a0b\u751f\u547d\u5468\u671f\u7684\u201c\u7070\u8272\u5730\u5e26\u201c - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-10-26T16:01:44+00:00","dateModified":"2026-10-26T16:01:44+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/118337.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/118337.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/118337.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Ubuntu\u96f6\u57fa\u7840\u5165\u95e8\u5230\u7cbe\u901a\u301010.9\u8bb2\u3011\uff1a\u50f5\u5c38\u8fdb\u7a0b\u4e0e\u5b64\u513f\u8fdb\u7a0b\u2014\u2014Linux \u8fdb\u7a0b\u751f\u547d\u5468\u671f\u7684\u201c\u7070\u8272\u5730\u5e26\u201c"}]},{"@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\/118337","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=118337"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/118337\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/118330"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=118337"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=118337"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=118337"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=118337"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}