Linux\u4e2d\u7684\u8fdb\u7a0b\u662f\u4ec0\u4e48?<\/strong><\/div>\n\u8bb2\u5230\u8fdb\u7a0b\uff0c\u6211\u4eec\u8981\u5148\u4e86\u89e3\u4e00\u4e0b\u53e6\u4e00\u4e2a\u6982\u5ff5\uff1a\u7a0b\u5e8f\u3002<\/p>\n
\u7a0b\u5e8f\u8bf4\u767d\u4e86\u5c31\u662f\u8eba\u5728\u7535\u8111\u786c\u76d8\u4e0a\u7684\u4e00\u4e2a\u6587\u4ef6\u800c\u5df2(\u5982\u540c\u786c\u76d8\u5973\u795e\u4e00\u6837)\uff0c\u5728\u88ab CPU \u6267\u884c\u4e4b\u524d\uff0c\u5b83\u5565\u4e5f\u505a\u4e0d\u4e86\u3002<\/p>\n
\u5f53\u7a0b\u5e8f\u88ab\u6267\u884c\u4e4b\u540e\uff0c\u5b83\u8fd0\u884c\u7684\u5b9e\u4f8b\u5c31\u79f0\u4e3a\u8fdb\u7a0b \u3002\u4e00\u4e2a\u7a0b\u5e8f\u53ef\u4ee5\u5bf9\u5e94\u591a\u4e2a\u8fdb\u7a0b\u3002<\/p>\n
\u8fdb\u7a0b\u662f\u7cfb\u7edf\u7684\u5de5\u4f5c\u5355\u5143\u3002\u7cfb\u7edf\u7531\u591a\u4e2a\u8fdb\u7a0b\u7ec4\u6210\uff0c\u5176\u4e2d\u6709\u7684\u662f\u64cd\u4f5c\u7cfb\u7edf\u8fdb\u7a0b(\u6267\u884c\u7cfb\u7edf\u4ee3\u7801)\uff0c\u5176\u4ed6\u7684\u662f\u7528\u6237\u8fdb\u7a0b(\u6267\u884c\u7528\u6237\u4ee3\u7801)\u3002\u6240\u6709\u8fd9\u4e9b\u8fdb\u7a0b\u90fd\u4f1a\u5e76\u53d1\u6267\u884c\uff0c\u4f8b\u5982\u901a\u8fc7\u5728\u5355 CPU \u4e0a\u91c7\u7528\u591a\u8def\u590d\u7528\u6765\u5b9e\u73b0\u3002<\/p>\n
\u4f60\u53ef\u4ee5\u4f7f\u7528 ps \u547d\u4ee4\u67e5\u770b Linux \u7cfb\u7edf\u4e2d\u7684\u6240\u6709\u8fdb\u7a0b \u3002<\/p>\n
$\u00a0ps\u00a0-ax\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0PID\u00a0TTY\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0STAT\u00a0\u00a0\u00a0TIME\u00a0COMMAND\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a01\u00a0?\u00a0\u00a0\u00a0\u00a0\u00a0Ss\u00a0\u00a0\u00a0\u00a0\u00a0\u00a00:01\u00a0\/usr\/lib\/systemd\/systemd\u00a0rhgb\u00a0--switched-root\u00a0--sys\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a02\u00a0?\u00a0\u00a0\u00a0\u00a0\u00a0S\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a00:00\u00a0[kthreadd]\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a03\u00a0?\u00a0\u00a0\u00a0\u00a0\u00a0I<\u00a0\u00a0\u00a0\u00a0\u00a0\u00a00:00\u00a0[rcu_gp]\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a04\u00a0?\u00a0\u00a0\u00a0\u00a0\u00a0I<\u00a0\u00a0\u00a0\u00a0\u00a0\u00a00:00\u00a0[rcu_par_gp]<\/pre>\n\u5f53\u4e00\u4e2a\u8fdb\u7a0b\u8c03\u7528 fork \u51fd\u6570\u751f\u6210\u53e6\u4e00\u4e2a\u8fdb\u7a0b\uff0c\u539f\u8fdb\u7a0b\u5c31\u79f0\u4e3a\u7236\u8fdb\u7a0b\uff0c\u65b0\u751f\u6210\u7684\u8fdb\u7a0b\u5219\u79f0\u4e3a\u5b50\u8fdb\u7a0b\u3002<\/p>\n
Linux \u7cfb\u7edf\u4e2d\u8fd9\u6837\u7236\u5b50\u8fdb\u7a0b\u975e\u5e38\u591a\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 pstree \u547d\u4ee4\u67e5\u770b\u7cfb\u7edf\u4e0a\u7684\u8fdb\u7a0b\u300c\u8c31\u7cfb\u300d\u3002<\/p>\n
$\u00a0pstree\u00a0-psn\r\nsystemd(1)\u2500\u252c\u2500systemd-journal(952)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u251c\u2500systemd-udevd(963)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u251c\u2500systemd-oomd(1137)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u251c\u2500systemd-resolve(1138)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u251c\u2500systemd-userdbd(1139)\u2500\u252c\u2500systemd-userwor(12707)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u2502\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u251c\u2500systemd-userwor(12714)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u2502\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u2514\u2500systemd-userwor(12715)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u251c\u2500auditd(1140)\u2500\u2500\u2500{auditd}(1141)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u251c\u2500dbus-broker-lau(1164)\u2500\u2500\u2500dbus-broker(1165)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u251c\u2500avahi-daemon(1166)\u2500\u2500\u2500avahi-daemon(1196)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u251c\u2500bluetoothd(1167)<\/pre>\n\u6bcf\u4e2a\u8fdb\u7a0b\u5728\u7cfb\u7edf\u4e2d\u90fd\u88ab\u5206\u914d\u4e86\u4e00\u4e2a\u7f16\u53f7\u3002\u5728\u8fd9\u6240\u6709\u7684\u8fdb\u7a0b\u4e2d\uff0c\u6709\u4e2a\u975e\u5e38\u7279\u6b8a\u7684\u8fdb\u7a0b\uff0c\u5b83\u7684 ID \u53f7\u662f 1 \u3002\u5b83\u662f\u7cfb\u7edf\u5728\u5f15\u5bfc\u8fc7\u7a0b\u4e2d\u6267\u884c\u7684\u7b2c\u4e00\u4e2a\u8fdb\u7a0b\uff0cPID 1 \u4e4b\u540e\u7684\u6bcf\u4e2a\u540e\u7eed\u8fdb\u7a0b\u90fd\u662f\u5b83\u7684\u540e\u4ee3\u3002<\/p>\n
\u4ec0\u4e48\u662f\u50f5\u5c38\u8fdb\u7a0b?<\/strong><\/div>\n\u524d\u9762\u63d0\u5230\u8fc7\uff0c\u5728 Linux \u73af\u5883\u4e2d\uff0c\u6211\u4eec\u662f\u901a\u8fc7 fork \u51fd\u6570\u6765\u521b\u5efa\u5b50\u8fdb\u7a0b\u7684\u3002\u521b\u5efa\u5b8c\u6bd5\u4e4b\u540e\uff0c\u7236\u5b50\u8fdb\u7a0b\u72ec\u7acb\u8fd0\u884c\uff0c\u7236\u8fdb\u7a0b\u65e0\u6cd5\u9884\u77e5\u5b50\u8fdb\u7a0b\u4ec0\u4e48\u65f6\u5019\u7ed3\u675f\u3002<\/p>\n
\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u5b50\u8fdb\u7a0b\u9000\u51fa\u540e\uff0c\u7236\u8fdb\u7a0b\u4f1a\u4f7f\u7528 wait \u6216 waitpid \u51fd\u6570\u8fdb\u884c\u56de\u6536\u5b50\u8fdb\u7a0b\u7684\u8d44\u6e90\uff0c\u5e76\u83b7\u5f97\u5b50\u8fdb\u7a0b\u7684\u7ec8\u6b62\u72b6\u6001\u3002<\/p>\n
\u4f46\u662f\uff0c\u5982\u679c\u7236\u8fdb\u7a0b\u5148\u4e8e\u5b50\u8fdb\u7a0b\u7ed3\u675f\uff0c\u5219\u5b50\u8fdb\u7a0b\u6210\u4e3a\u5b64\u513f\u8fdb\u7a0b\u3002\u5b64\u513f\u8fdb\u7a0b\u5c06\u88ab init \u8fdb\u7a0b(\u8fdb\u7a0b\u53f7\u4e3a1)\u9886\u517b\uff0c\u5e76\u7531 init \u8fdb\u7a0b\u5bf9\u5b64\u513f\u8fdb\u7a0b\u5b8c\u6210\u72b6\u6001\u6536\u96c6\u5de5\u4f5c\u3002<\/p>\n
\u800c\u5982\u679c\u5b50\u8fdb\u7a0b\u5148\u4e8e\u7236\u8fdb\u7a0b\u9000\u51fa\uff0c\u540c\u65f6\u7236\u8fdb\u7a0b\u592a\u5fd9\u4e86\uff0c\u65e0\u7455\u56de\u6536\u5b50\u8fdb\u7a0b\u7684\u8d44\u6e90\uff0c\u5b50\u8fdb\u7a0b\u6b8b\u7559\u8d44\u6e90(PCB)\u5b58\u653e\u4e8e\u5185\u6838\u4e2d\uff0c\u53d8\u6210\u50f5\u5c38(Zombie)\u8fdb\u7a0b\uff0c\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n
<\/p>\n
\u50f5\u5c38\u8fdb\u7a0b\u662f\u600e\u4e48\u4ea7\u751f\u7684?<\/strong><\/div>\n\u524d\u9762\u5df2\u7ecf\u4ecb\u7ecd\u4e86\u50f5\u5c38\u8fdb\u7a0b\u4ea7\u751f\u7684\u539f\u7406\uff0c\u4e0b\u9762\u6211\u4eec\u901a\u8fc7\u4ee3\u7801\u6765\u6a21\u62df\u50f5\u5c38\u8fdb\u7a0b\u7684\u4ea7\u751f\u3002<\/p>\n
#include\r\n#include\r\n#include\r\n#include\r\n\u00a0\r\nint\u00a0main(void)\r\n{\r\n\u00a0\u00a0\u00a0\u00a0pid_t\u00a0pid;\r\n\u00a0\u00a0\u00a0\u00a0pid\u00a0=\u00a0fork();\r\n\u00a0\u00a0\u00a0\u00a0if\u00a0(pid\u00a0==\u00a00)\u00a0{\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0printf(\"I\u00a0am\u00a0child,\u00a0my\u00a0parent=\u00a0%d,\u00a0going\u00a0to\u00a0sleep\u00a03s\\n\",\u00a0getppid());\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0sleep(3);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0printf(\"-------------child\u00a0die--------------\\n\");\r\n\u00a0\u00a0\u00a0\u00a0}\u00a0else\u00a0if\u00a0(pid\u00a0>\u00a00)\u00a0{\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0printf(\"I\u00a0am\u00a0parent,\u00a0pid\u00a0=\u00a0%d,\u00a0myson\u00a0=\u00a0%d,\u00a0going\u00a0to\u00a0sleep\u00a05s\\n\",\u00a0getpid(),\u00a0pid);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0sleep(5);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0system(\"ps\u00a0-o\u00a0pid,ppid,state,tty,command\");\r\n\u00a0\u00a0\u00a0\u00a0}\u00a0else\u00a0{\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0perror(\"fork\");\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return\u00a01;\r\n\u00a0\u00a0\u00a0\u00a0}\r\n\u00a0\r\n\u00a0\u00a0\u00a0\u00a0return\u00a00;\r\n}<\/pre>\n\u5728\u8fd9\u4e2a\u7a0b\u5e8f\u91cc\uff0c\u7236\u8fdb\u7a0b\u521b\u5efa\u5b50\u8fdb\u7a0b\u4e4b\u540e\uff0c\u5c31\u4f11\u7720 5 \u79d2\u949f\u3002\u800c\u5b50\u8fdb\u7a0b\u53ea\u4f11\u7720 3 \u79d2\u949f\u5c31\u9000\u51fa\uff0c\u5728\u5b83\u9000\u51fa\u4e4b\u540e\uff0c\u7236\u8fdb\u7a0b\u8fd8\u672a\u82cf\u9192\uff0c\u56e0\u6b64\u6ca1\u4eba\u7ed9\u5b50\u8fdb\u7a0b\u300c\u6536\u5c38\u300d\uff0c\u6240\u4ee5\u5b83\u5c31\u53d8\u6210\u4e86\u50f5\u5c38\u8fdb\u7a0b\u3002<\/p>\n
<\/p>\n
\u5982\u4f55\u6740\u6b7b\u50f5\u5c38\u8fdb\u7a0b<\/strong><\/div>\n\u5bf9\u4e8e\u666e\u901a\u8fdb\u7a0b\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528 kill \u547d\u4ee4\u6765\u6740\u6b7b\u5b83\u4eec\u3002kill \u547d\u4ee4\u5b83\u8fd8\u6709\u51e0\u4e2a\u5144\u5f1f\uff0c\u6bd4\u5982 pkill \u548c killall \uff0c\u867d\u7136\u5b83\u4eec\u540d\u79f0\u91cc\u90fd\u5e26 kill \u8fd9\u6837\u6740\u6c14\u817e\u817e\u7684\u5b57\u773c\uff0c\u4f46\u5b83\u4eec\u5b9e\u9645\u4e0a\u662f\u88ab\u8bbe\u8ba1\u4e3a\u5411\u4e00\u4e2a\u6216\u591a\u4e2a\u8fdb\u7a0b\u53d1\u9001\u4fe1\u53f7\u3002<\/p>\n
\u5728\u672a\u6307\u5b9a\u7684\u60c5\u51b5\u4e0b\uff0c\u8fd9\u51e0\u4e2a\u547d\u4ee4\u9ed8\u8ba4\u53d1\u9001\u7684\u662f SIGTERM \u4fe1\u53f7\u3002<\/p>\n
\u666e\u901a\u8fdb\u7a0b\u53ef\u4ee5\u88ab kill \uff0c\u4f46\u50f5\u5c38\u8fdb\u7a0b\u662f\u4e0d\u884c\u7684\u3002\u4e3a\u4ec0\u4e48?\u56e0\u4e3a\u50f5\u5c38\u8fdb\u7a0b\u672c\u8eab\u5c31\u5df2\u7ecf\u300c\u6b7b\u300d\u8fc7\u4e00\u6b21\u4e86!\u5982\u679c\u8fd8\u53ef\u4ee5\u518d\u300c\u6b7b\u300d\uff0c\u90a3\u300c\u50f5\u5c38\u300d\u8fd9\u4e2a\u540d\u53f7\u5c31\u6ca1\u591a\u5927\u610f\u4e49\u4e86\u3002<\/p>\n
\u50f5\u5c38\u8fdb\u7a0b\u5176\u5b9e\u5df2\u7ecf\u5c31\u662f\u9000\u51fa\u7684\u8fdb\u7a0b\uff0c\u56e0\u6b64\u65e0\u6cd5\u518d\u5229\u7528kill\u547d\u4ee4\u6740\u6b7b\u50f5\u5c38\u8fdb\u7a0b\u3002\u50f5\u5c38\u8fdb\u7a0b\u7684\u7f6a\u9b41\u7978\u9996\u662f\u7236\u8fdb\u7a0b\u6ca1\u6709\u56de\u6536\u5b83\u7684\u8d44\u6e90\uff0c\u90a3\u6211\u4eec\u53ef\u4ee5\u60f3\u529e\u6cd5\u5b83\u5176\u5b83\u8fdb\u7a0b\u53bb\u56de\u6536\u50f5\u5c38\u8fdb\u7a0b\u7684\u8d44\u6e90\uff0c\u8fd9\u4e2a\u8fdb\u7a0b\u5c31\u662f init \u8fdb\u7a0b\u3002<\/p>\n
\u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u6740\u6b7b\u7236\u8fdb\u7a0b\uff0cinit \u8fdb\u7a0b\u5c31\u4f1a\u5f88\u5584\u826f\u5730\u628a\u90a3\u4e9b\u50f5\u5c38\u8fdb\u7a0b\u9886\u517b\u8fc7\u6765\uff0c\u5e76\u5408\u7406\u7684\u56de\u6536\u5b83\u4eec\u7684\u8d44\u6e90\uff0c\u90a3\u4e9b\u50f5\u5c38\u8fdb\u7a0b\u5c31\u5f97\u5230\u4e86\u59a5\u5584\u7684\u5904\u7406\u4e86\u3002<\/p>\n
\u4f8b\u5982\uff0c\u5982\u679c PID 5878 \u662f\u4e00\u4e2a\u50f5\u5c38\u8fdb\u7a0b\uff0c\u5b83\u7684\u7236\u8fdb\u7a0b\u662f PID 4809\uff0c\u90a3\u4e48\u8981\u6740\u6b7b\u50f5\u5c38\u8fdb\u7a0b (5878)\uff0c\u60a8\u53ef\u4ee5\u7ed3\u675f\u7236\u8fdb\u7a0b (4809)\uff1a<\/p>\n
$\u00a0sudo\u00a0kill\u00a0-9\u00a04809\u00a0\u00a0#4809\u00a0is\u00a0the\u00a0parent,\u00a0not\u00a0the\u00a0zombie<\/pre>\n\u6740\u6b7b\u7236\u8fdb\u7a0b\u65f6\u8981\u975e\u5e38\u5c0f\u5fc3\uff0c\u5982\u679c\u4e00\u4e2a\u8fdb\u7a0b\u7684\u7236\u8fdb\u7a0b\u5c31\u662f PID 1 \uff0c\u5e76\u4e14\u4f60\u8fd8\u6740\u6b7b\u4e86\u5b83\uff0c\u90a3\u4e48\u7cfb\u7edf\u5c06\u76f4\u63a5\u91cd\u542f!<\/p>\n
\u8fd9\u5c06\u662f\u4e00\u4e2a\u66f4\u53ef\u6015\u7684\u6545\u4e8b!<\/p>\n","protected":false},"excerpt":{"rendered":"
\u8bb2\u5230\u8fdb\u7a0b\uff0c\u6211\u4eec\u8981\u5148\u4e86\u89e3\u4e00\u4e0b\u53e6\u4e00\u4e2a\u6982\u5ff5\uff1a\u7a0b\u5e8f\u3002 \u7a0b\u5e8f\u8bf4\u767d\u4e86\u5c31\u662f\u8eba\u5728\u7535\u8111\u786c\u76d8\u4e0a\u7684\u4e00\u4e2a\u6587\u4ef6\u800c\u5df2(\u5982\u540c\u786c\u76d8\u5973\u795e\u4e00\u6837)\uff0c […]<\/p>\n","protected":false},"author":323,"featured_media":225922,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-229931","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/229931","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/users\/323"}],"replies":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/comments?post=229931"}],"version-history":[{"count":3,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/229931\/revisions"}],"predecessor-version":[{"id":229984,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/229931\/revisions\/229984"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media\/225922"}],"wp:attachment":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media?parent=229931"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/categories?post=229931"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/tags?post=229931"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}