{"id":99338,"date":"2026-09-02T13:25:15","date_gmt":"2026-09-02T05:25:15","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/99338.html"},"modified":"2026-09-02T13:25:15","modified_gmt":"2026-09-02T05:25:15","slug":"%e3%80%90linux%e3%80%91-%e8%bf%9b%e7%a8%8b%ef%bc%883%ef%bc%89%e6%b7%b1%e5%ba%a6%e8%a7%a3%e6%9e%90%ef%bc%9a%e4%bb%8e%e6%9f%a5%e7%9c%8b%e8%bf%9b%e7%a8%8b%e5%88%b0%e8%bf%9b%e7%a8%8b%e7%8a%b6%e6%80%81","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/99338.html","title":{"rendered":"\u3010Linux\u3011 \u8fdb\u7a0b\uff083\uff09\u6df1\u5ea6\u89e3\u6790\uff1a\u4ece\u67e5\u770b\u8fdb\u7a0b\u5230\u8fdb\u7a0b\u72b6\u6001"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1080\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260902052511-6a97b337057ef.png\" width=\"1920\" \/><\/p>\n<hr \/>\n<h2 style=\"background-color:transparent\"><span style=\"color:#a2e043\"><span style=\"background-color:#7b7f82\">\u00a0 Linux \u8fdb\u7a0b\u7ba1\u7406\u6df1\u5ea6\u89e3\u6790&#xff1a;\u4ece\u67e5\u770b\u8fdb\u7a0b\u5230\u8fdb\u7a0b\u72b6\u6001\u00a0\u00a0<\/span><\/span><\/h2>\n<h3><span style=\"color:#1a439c\">\u4e00\u3001\u67e5\u770b\u8fdb\u7a0b<\/span><\/h3>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u5728 Linux \u4e2d&#xff0c;\u8fdb\u7a0b\u662f\u8d44\u6e90\u5206\u914d\u7684\u57fa\u672c\u5355\u4f4d\u3002<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u7cfb\u7edf\u4e2d\u6bcf\u65f6\u6bcf\u523b\u90fd\u6709\u5927\u91cf\u8fdb\u7a0b\u5728\u8fd0\u884c&#xff0c;\u5982\u4f55\u67e5\u770b\u5b83\u4eec\u662f\u7406\u89e3\u8fdb\u7a0b\u7ba1\u7406\u7684\u7b2c\u4e00\u6b65\u3002<\/span><\/p>\n<h4><span style=\"color:#511b78\">1.1 ps \u547d\u4ee4<\/span><\/h4>\n<p><span style=\"color:#0d0016\">ps&#xff08;process status&#xff09;\u662f\u6700\u5e38\u7528\u7684\u8fdb\u7a0b\u67e5\u770b\u5de5\u5177&#xff0c;\u5b83\u62a5\u544a\u5f53\u524d\u7cfb\u7edf\u4e2d\u8fdb\u7a0b\u7684\u5feb\u7167\u4fe1\u606f\u3002<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u5e38\u7528\u7ec4\u5408&#xff1a;ps aux<\/span><\/p>\n<p>ps aux<\/p>\n<p><span style=\"color:#0d0016\">\u8f93\u51fa\u793a\u4f8b&#xff1a;<\/span><\/p>\n<p>USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND<br \/>\nroot         1  0.0  0.1 169248 13136 ?        Ss   08:00   0:02 \/sbin\/init<br \/>\nroot         2  0.0  0.0      0     0 ?        S    08:00   0:00 [kthreadd]<br \/>\nwxx       1234  0.5  1.2 523416 98304 ?        Sl   08:05   0:15 .\/myapp<\/p>\n<p><span style=\"color:#0d0016\">\u5b57\u6bb5\u89e3\u8bfb&#xff1a;<\/span><\/p>\n<table>\n<tr><span style=\"color:#0d0016\">\u5b57\u6bb5<\/span><span style=\"color:#0d0016\">\u542b\u4e49<\/span><\/tr>\n<tbody>\n<tr>\n<td><span style=\"color:#0d0016\">USER<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u8fdb\u7a0b\u6240\u5c5e\u7528\u6237<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">PID<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u8fdb\u7a0b ID&#xff0c;\u5168\u5c40\u552f\u4e00<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">%CPU<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u5360\u7528 CPU \u767e\u5206\u6bd4<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">%MEM<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u5360\u7528\u7269\u7406\u5185\u5b58\u767e\u5206\u6bd4<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">VSZ<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u865a\u62df\u5185\u5b58\u5927\u5c0f&#xff08;KB&#xff09;<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">RSS<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u5e38\u9a7b\u5185\u5b58\u5927\u5c0f&#xff08;KB&#xff09;&#xff0c;\u5373\u5b9e\u9645\u5360\u7528\u7269\u7406\u5185\u5b58<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">TTY<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u8fdb\u7a0b\u6240\u5c5e\u7ec8\u7aef&#xff0c;? \u8868\u793a\u4e0e\u7ec8\u7aef\u65e0\u5173&#xff08;\u5b88\u62a4\u8fdb\u7a0b&#xff09;<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">STAT<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u8fdb\u7a0b\u72b6\u6001&#xff08;\u540e\u6587\u8be6\u89e3&#xff09;<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">START<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u8fdb\u7a0b\u542f\u52a8\u65f6\u95f4<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">TIME<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u7d2f\u8ba1\u5360\u7528 CPU \u65f6\u95f4<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">COMMAND<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u542f\u52a8\u8fdb\u7a0b\u7684\u547d\u4ee4<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"color:#0d0016\">\u53e6\u4e00\u4e2a\u5e38\u7528\u7ec4\u5408&#xff1a;ps -ef<\/span><\/p>\n<p>ps -ef<\/p>\n<p><span style=\"color:#0d0016\">-e \u663e\u793a\u6240\u6709\u8fdb\u7a0b&#xff0c;-f \u663e\u793a\u5b8c\u6574\u683c\u5f0f\u3002<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u4e0e aux \u7684\u533a\u522b\u5728\u4e8e&#xff1a;-ef \u4f1a\u663e\u793a PPID&#xff08;\u7236\u8fdb\u7a0b ID&#xff09;&#xff0c;\u8fd9\u5728\u8ffd\u8e2a\u8fdb\u7a0b\u7236\u5b50\u5173\u7cfb\u65f6\u975e\u5e38\u6709\u7528\u3002<\/span><\/p>\n<p>UID        PID  PPID  C STIME TTY          TIME CMD<br \/>\nroot         1     0  0 08:00 ?        00:00:02 \/sbin\/init<br \/>\nroot         2     0  0 08:00 ?        00:00:00 [kthreadd]<br \/>\nwxx       1234  1100  0 08:05 ?        00:00:15 .\/myapp<\/p>\n<hr \/>\n<h4><span style=\"color:#511b78\">1.2 top \u547d\u4ee4<\/span><\/h4>\n<p><span style=\"color:#0d0016\">ps \u7ed9\u7684\u662f\u5feb\u7167&#xff0c;\u800c top \u63d0\u4f9b\u7684\u662f\u5b9e\u65f6\u52a8\u6001\u5237\u65b0\u7684\u8fdb\u7a0b\u89c6\u56fe&#xff0c;\u9ed8\u8ba4\u6bcf 3 \u79d2\u5237\u65b0\u4e00\u6b21\u3002<\/span><\/p>\n<p>top<\/p>\n<p><span style=\"color:#0d0016\">\u5173\u952e\u4fe1\u606f\u533a\u57df&#xff1a;<\/span><\/p>\n<ul>\n<li><span style=\"color:#0d0016\">\u7b2c\u4e00\u884c&#xff1a;\u7cfb\u7edf\u65f6\u95f4\u3001\u8fd0\u884c\u65f6\u957f\u3001\u767b\u5f55\u7528\u6237\u6570\u3001\u7cfb\u7edf\u8d1f\u8f7d&#xff08;1\/5\/15 \u5206\u949f\u5e73\u5747&#xff09;<\/span><\/li>\n<li><span style=\"color:#0d0016\">\u7b2c\u4e8c\u884c&#xff1a;\u8fdb\u7a0b\u603b\u6570\u3001\u8fd0\u884c\u4e2d\u3001\u7761\u7720\u3001\u505c\u6b62\u3001\u50f5\u5c38\u8fdb\u7a0b\u6570\u91cf<\/span><\/li>\n<li><span style=\"color:#0d0016\">\u7b2c\u4e09\u884c&#xff1a;CPU \u4f7f\u7528\u60c5\u51b5&#xff08;us \u7528\u6237\u6001\u3001sy \u5185\u6838\u6001\u3001id \u7a7a\u95f2&#xff09;<\/span><\/li>\n<li><span style=\"color:#0d0016\">\u7b2c\u56db\u4e94\u884c&#xff1a;\u5185\u5b58\u548c\u4ea4\u6362\u5206\u533a\u4f7f\u7528\u60c5\u51b5<\/span><\/li>\n<li><span style=\"color:#0d0016\">\u4e0b\u65b9\u5217\u8868&#xff1a;\u6309 CPU \u4f7f\u7528\u7387\u964d\u5e8f\u6392\u5217\u7684\u8fdb\u7a0b<\/span><\/li>\n<\/ul>\n<p><span style=\"color:#0d0016\">\u5e38\u7528\u4ea4\u4e92\u6309\u952e&#xff1a;<\/span><\/p>\n<table>\n<tr><span style=\"color:#0d0016\">\u6309\u952e<\/span><span style=\"color:#0d0016\">\u529f\u80fd<\/span><\/tr>\n<tbody>\n<tr>\n<td><span style=\"color:#0d0016\">P<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u6309 CPU \u4f7f\u7528\u7387\u6392\u5e8f<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">M<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u6309\u5185\u5b58\u4f7f\u7528\u7387\u6392\u5e8f<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">T<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u6309\u7d2f\u8ba1 CPU \u65f6\u95f4\u6392\u5e8f<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">k<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u6740\u6b7b\u6307\u5b9a PID \u7684\u8fdb\u7a0b<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">q<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u9000\u51fa top<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<hr \/>\n<h4><span style=\"color:#511b78\">1.3 \/proc \u6587\u4ef6\u7cfb\u7edf<\/span><\/h4>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Linux \u4e2d\u4e00\u5207\u7686\u6587\u4ef6&#xff0c;\u8fdb\u7a0b\u4fe1\u606f\u4e5f\u4e0d\u4f8b\u5916\u3002<\/span><span style=\"color:#0d0016\">\/proc \u662f\u4e00\u4e2a\u865a\u62df\u6587\u4ef6\u7cfb\u7edf&#xff0c;\u5b83\u4ee5\u6587\u4ef6\u7684\u5f62\u5f0f\u66b4\u9732\u5185\u6838\u6570\u636e\u7ed3\u6784&#xff0c;\u5e76\u4e0d\u5360\u7528\u5b9e\u9645\u78c1\u76d8\u7a7a\u95f4\u3002<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u6bcf\u4e2a\u8fdb\u7a0b\u5728 \/proc \u4e0b\u90fd\u6709\u4e00\u4e2a\u4ee5\u5176 PID \u547d\u540d\u7684\u76ee\u5f55&#xff1a;<\/span><\/p>\n<p>ls \/proc\/1234\/<\/p>\n<p><span style=\"color:#0d0016\">\u5173\u952e\u6587\u4ef6&#xff1a;<\/span><\/p>\n<table>\n<tr><span style=\"color:#0d0016\">\u6587\u4ef6<\/span><span style=\"color:#0d0016\">\u5185\u5bb9<\/span><\/tr>\n<tbody>\n<tr>\n<td><span style=\"color:#0d0016\">\/proc\/[pid]\/status<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u8fdb\u7a0b\u72b6\u6001\u4fe1\u606f&#xff08;\u540d\u79f0\u3001PID\u3001PPID\u3001\u72b6\u6001\u3001\u5185\u5b58\u7b49&#xff09;<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">\/proc\/[pid]\/cmdline<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u542f\u52a8\u547d\u4ee4&#xff08;\u4ee5 \\\\0 \u5206\u9694\u53c2\u6570&#xff09;<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">\/proc\/[pid]\/cwd<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u7684\u7b26\u53f7\u94fe\u63a5<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">\/proc\/[pid]\/exe<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u53ef\u6267\u884c\u6587\u4ef6\u7684\u7b26\u53f7\u94fe\u63a5<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">\/proc\/[pid]\/maps<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u8fdb\u7a0b\u5730\u5740\u7a7a\u95f4\u6620\u5c04<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">\/proc\/[pid]\/fd\/<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u8fdb\u7a0b\u6253\u5f00\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u76ee\u5f55<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"color:#0d0016\">\u67e5\u770b\u8fdb\u7a0b\u72b6\u6001&#xff1a;<\/span><\/p>\n<p>cat \/proc\/1234\/status<\/p>\n<p>Name:   myapp<br \/>\nState:  S (sleeping)<br \/>\nTgid:   1234<br \/>\nPid:    1234<br \/>\nPPid:   1100<br \/>\n&#8230;<br \/>\nVmRSS:   98304 kB<\/p>\n<p><span style=\"color:#0d0016\">\u672c\u8d28&#xff1a;ps\u3001top \u7b49\u5de5\u5177\u5e95\u5c42\u90fd\u662f\u8bfb\u53d6 \/proc \u6587\u4ef6\u7cfb\u7edf\u6765\u83b7\u53d6\u8fdb\u7a0b\u4fe1\u606f\u7684\u3002<\/span><\/p>\n<hr \/>\n<h3><span style=\"color:#1a439c\">\u4e8c\u3001\u901a\u8fc7\u7cfb\u7edf\u8c03\u7528\u83b7\u53d6\u8fdb\u7a0b\u6807\u8bc6\u7b26<\/span><\/h3>\n<h4><span style=\"color:#511b78\">2.1 getpid() \u4e0e getppid()<\/span><\/h4>\n<p><span style=\"color:#0d0016\">\u5728\u7528\u6237\u6001\u7a0b\u5e8f\u4e2d&#xff0c;\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4e24\u4e2a\u7cfb\u7edf\u8c03\u7528\u6765\u83b7\u53d6\u8fdb\u7a0b ID&#xff1a;<\/span><\/p>\n<p>#include &lt;sys\/types.h&gt;<br \/>\n#include &lt;unistd.h&gt;<\/p>\n<p>pid_t getpid(void);   \/\/ \u83b7\u53d6\u5f53\u524d\u8fdb\u7a0b\u7684 PID<br \/>\npid_t getppid(void);  \/\/ \u83b7\u53d6\u5f53\u524d\u8fdb\u7a0b\u7684\u7236\u8fdb\u7a0b PID&#xff08;PPID&#xff09;<\/p>\n<ul>\n<li><span style=\"color:#0d0016\">getpid() \u8fd4\u56de\u5f53\u524d\u8fdb\u7a0b\u7684 PID<\/span><\/li>\n<li><span style=\"color:#0d0016\">getppid() \u8fd4\u56de\u7236\u8fdb\u7a0b\u7684 PID&#xff0c;\u5982\u679c\u7236\u8fdb\u7a0b\u5df2\u9000\u51fa&#xff0c;\u5219\u8fd4\u56de 1&#xff08;init \u8fdb\u7a0b&#xff09;<\/span><\/li>\n<\/ul>\n<hr \/>\n<h4><span style=\"color:#511b78\">2.2 \u4ee3\u7801\u793a\u4f8b<\/span><\/h4>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;sys\/types.h&gt;<br \/>\n#include &lt;unistd.h&gt;<\/p>\n<p>int main() {<br \/>\n    pid_t pid &#061; getpid();<br \/>\n    pid_t ppid &#061; getppid();<\/p>\n<p>    printf(&#034;\u5f53\u524d\u8fdb\u7a0b PID: %d\\\\n&#034;, pid);<br \/>\n    printf(&#034;\u7236\u8fdb\u7a0b PPID: %d\\\\n&#034;, ppid);<\/p>\n<p>    \/\/ \u8ba9\u8fdb\u7a0b\u6682\u505c&#xff0c;\u65b9\u4fbf\u7528 ps \u67e5\u770b<br \/>\n    while (1) {<br \/>\n        sleep(1);<br \/>\n    }<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<p><span style=\"color:#0d0016\">\u7f16\u8bd1\u8fd0\u884c&#xff1a;<\/span><\/p>\n<p>gcc -o getpid_demo getpid_demo.c<br \/>\n.\/getpid_demo<\/p>\n<p><span style=\"color:#0d0016\">\u8f93\u51fa&#xff1a;<\/span><\/p>\n<p>\u5f53\u524d\u8fdb\u7a0b PID: 5678<br \/>\n\u7236\u8fdb\u7a0b PPID: 5600<\/p>\n<p><span style=\"color:#0d0016\">\u53e6\u5f00\u4e00\u4e2a\u7ec8\u7aef\u9a8c\u8bc1&#xff1a;<\/span><\/p>\n<p>ps aux | grep getpid_demo<\/p>\n<hr \/>\n<h4><span style=\"color:#511b78\">2.3 \u5185\u6838\u89c6\u89d2&#xff1a;PID \u662f\u5982\u4f55\u7ba1\u7406\u7684<\/span><\/h4>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u5728\u5185\u6838\u4e2d&#xff0c;\u6bcf\u4e2a\u8fdb\u7a0b\u7531 task_struct \u7ed3\u6784\u4f53<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u63cf\u8ff0&#xff08;\u5b9a\u4e49\u5728include\/linux\/sched.h&#xff09;\u3002\u5176\u4e2d\u4e0e PID \u76f8\u5173\u7684\u5b57\u6bb5&#xff1a;<\/span><\/p>\n<p>struct task_struct {<br \/>\n    pid_t pid;          \/\/ \u8fdb\u7a0b ID<br \/>\n    pid_t tgid;         \/\/ \u7ebf\u7a0b\u7ec4 ID&#xff08;\u5bf9\u7528\u6237\u6001&#xff0c;\u8fdb\u7a0b\u7684 tgid &#061;&#061; pid&#xff09;<br \/>\n    struct task_struct *real_parent;  \/\/ \u771f\u6b63\u7684\u7236\u8fdb\u7a0b<br \/>\n    struct task_struct *parent;       \/\/ \u7236\u8fdb\u7a0b&#xff08;\u901a\u5e38\u76f8\u540c&#xff09;<br \/>\n    \/\/ &#8230;<br \/>\n};<\/p>\n<p><span style=\"color:#0d0016\">getpid() \u7cfb\u7edf\u8c03\u7528\u5728\u5185\u6838\u4e2d\u7684\u5b9e\u73b0\u975e\u5e38\u76f4\u63a5\u2014\u2014\u8fd4\u56de\u5f53\u524d\u8fdb\u7a0b task_struct \u4e2d\u7684 tgid \u5b57\u6bb5&#xff1a;<\/span><\/p>\n<p>\/\/ kernel\/sys.c<br \/>\nSYSCALL_DEFINE0(getpid)<br \/>\n{<br \/>\n    return task_tgid_vnr(current);  \/\/ \u8fd4\u56de\u5f53\u524d\u8fdb\u7a0b\u7684 tgid<br \/>\n}<\/p>\n<p><span style=\"color:#b95514\">\u4e3a\u4ec0\u4e48\u662f tgid \u800c\u4e0d\u662f pid&#xff1f;<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u56e0\u4e3a\u5728 Linux \u4e2d&#xff0c;\u7ebf\u7a0b\u548c\u8fdb\u7a0b\u5728\u5185\u6838\u5c42\u9762\u90fd\u7528 task_struct \u8868\u793a\u3002\u5bf9\u591a\u7ebf\u7a0b\u7a0b\u5e8f&#xff0c;\u6bcf\u4e2a\u7ebf\u7a0b\u6709\u81ea\u5df1\u7684 pid&#xff08;\u5185\u6838\u7ebf\u7a0b ID&#xff09;&#xff0c;\u4f46\u5b83\u4eec\u5171\u4eab\u540c\u4e00\u4e2a tgid&#xff08;\u7ebf\u7a0b\u7ec4 ID&#xff09;\u3002\u7528\u6237\u6001\u770b\u5230\u7684&#034;\u8fdb\u7a0b ID&#034;\u5b9e\u9645\u4e0a\u662f tgid\u3002<\/span><\/p>\n<hr \/>\n<h3><span style=\"color:#1a439c\">\u4e09\u3001\u901a\u8fc7\u7cfb\u7edf\u8c03\u7528\u521b\u5efa\u8fdb\u7a0b\u2014\u2014fork \u521d\u8bc6<\/span><\/h3>\n<h4><span style=\"color:#511b78\">3.1 fork \u51fd\u6570\u539f\u578b<\/span><\/h4>\n<p>#include &lt;unistd.h&gt;<\/p>\n<p>pid_t fork(void);<\/p>\n<p><span style=\"color:#0d0016\">fork \u662f Unix\/Linux \u4e2d\u6700\u6838\u5fc3\u7684\u8fdb\u7a0b\u521b\u5efa\u673a\u5236\u3002\u8c03\u7528 fork \u540e&#xff0c;\u5185\u6838\u4f1a&#xff1a;<\/span><\/p>\n<li><span style=\"color:#0d0016\">\u4e3a\u5b50\u8fdb\u7a0b\u5206\u914d\u65b0\u7684 task_struct<\/span><\/li>\n<li><span style=\"color:#0d0016\">\u590d\u5236\u7236\u8fdb\u7a0b\u7684\u8d44\u6e90&#xff08;\u9875\u8868\u3001\u6587\u4ef6\u63cf\u8ff0\u7b26\u8868\u3001\u4fe1\u53f7\u5904\u7406\u7b49&#xff09;<\/span><\/li>\n<li><span style=\"color:#0d0016\">\u7236\u5b50\u8fdb\u7a0b\u4ece fork \u8fd4\u56de\u5904\u7ee7\u7eed\u6267\u884c<\/span><\/li>\n<hr \/>\n<h4><span style=\"color:#511b78\">3.2 fork \u7684\u8fd4\u56de\u503c \u2014\u2014\u00a0 \u4e00\u6b21\u8c03\u7528&#xff0c;\u4e24\u6b21\u8fd4\u56de<\/span><\/h4>\n<p><span style=\"color:#0d0016\">\u8fd9\u662f fork \u6700\u7cbe\u5999\u4e5f\u6700\u5bb9\u6613\u56f0\u60d1\u7684\u5730\u65b9&#xff1a;<\/span><\/p>\n<table>\n<tr><span style=\"color:#0d0016\">\u8fd4\u56de\u503c<\/span><span style=\"color:#0d0016\">\u542b\u4e49<\/span><\/tr>\n<tbody>\n<tr>\n<td><span style=\"color:#0d0016\">&gt; 0<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u5728\u7236\u8fdb\u7a0b\u4e2d&#xff0c;\u8fd4\u56de\u5b50\u8fdb\u7a0b\u7684 PID<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">&#061; 0<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u5728\u5b50\u8fdb\u7a0b\u4e2d&#xff0c;\u8fd4\u56de 0<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">-1<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u521b\u5efa\u5931\u8d25&#xff08;\u901a\u5e38\u662f\u8fdb\u7a0b\u6570\u8fbe\u5230\u4e0a\u9650\u6216\u5185\u5b58\u4e0d\u8db3&#xff09;<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"color:#0d0016\">\u7ecf\u5178\u793a\u4f8b&#xff1a;<\/span><\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;sys\/types.h&gt;<br \/>\n#include &lt;unistd.h&gt;<\/p>\n<p>int main() {<br \/>\n    printf(&#034;fork \u4e4b\u524d&#xff0c;PID &#061; %d\\\\n&#034;, getpid());<\/p>\n<p>    pid_t ret &#061; fork();<\/p>\n<p>    if (ret &lt; 0) {<br \/>\n        perror(&#034;fork \u5931\u8d25&#034;);<br \/>\n        return 1;<br \/>\n    } else if (ret &#061;&#061; 0) {<br \/>\n        \/\/ \u5b50\u8fdb\u7a0b\u6267\u884c\u8def\u5f84<br \/>\n        printf(&#034;\u3010\u5b50\u8fdb\u7a0b\u3011PID &#061; %d, PPID &#061; %d, fork \u8fd4\u56de\u503c &#061; %d\\\\n&#034;,<br \/>\n               getpid(), getppid(), ret);<br \/>\n    } else {<br \/>\n        \/\/ \u7236\u8fdb\u7a0b\u6267\u884c\u8def\u5f84<br \/>\n        printf(&#034;\u3010\u7236\u8fdb\u7a0b\u3011PID &#061; %d, \u521b\u5efa\u7684\u5b50\u8fdb\u7a0b PID &#061; %d, fork \u8fd4\u56de\u503c &#061; %d\\\\n&#034;,<br \/>\n               getpid(), ret, ret);<br \/>\n    }<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<p><span style=\"color:#0d0016\">\u7f16\u8bd1\u8fd0\u884c&#xff1a;<\/span><\/p>\n<p>gcc -o fork_demo fork_demo.c<br \/>\n.\/fork_demo<\/p>\n<p><span style=\"color:#0d0016\">\u53ef\u80fd\u7684\u8f93\u51fa&#xff1a;<\/span><\/p>\n<p>fork \u4e4b\u524d&#xff0c;PID &#061; 6000<br \/>\n\u3010\u7236\u8fdb\u7a0b\u3011PID &#061; 6000, \u521b\u5efa\u7684\u5b50\u8fdb\u7a0b PID &#061; 6001, fork \u8fd4\u56de\u503c &#061; 6001<br \/>\n\u3010\u5b50\u8fdb\u7a0b\u3011PID &#061; 6001, PPID &#061; 6000, fork \u8fd4\u56de\u503c &#061; 0<\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color:#ed7976\">\u00a0\u00a0\u6ce8\u610f&#xff1a;\u7236\u5b50\u8fdb\u7a0b\u7684\u6267\u884c\u987a\u5e8f\u662f\u4e0d\u786e\u5b9a\u7684&#xff0c;\u53d6\u51b3\u4e8e\u5185\u6838\u8c03\u5ea6\u5668\u3002\u53ef\u80fd\u7236\u8fdb\u7a0b\u5148\u6253\u5370&#xff0c;\u4e5f\u53ef\u80fd\u5b50\u8fdb\u7a0b\u5148\u6253\u5370\u3002<\/span><\/p>\n<hr \/>\n<h4><span style=\"color:#511b78\">3.3 \u5199\u65f6\u62f7\u8d1d&#xff08;Copy-On-Write, COW&#xff09;<\/span><\/h4>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u65e9\u671f\u7684 fork \u5b9e\u73b0\u4f1a\u628a\u7236\u8fdb\u7a0b\u7684\u5168\u90e8\u5730\u5740\u7a7a\u95f4\u5b8c\u6574\u590d\u5236\u4e00\u4efd\u7ed9\u5b50\u8fdb\u7a0b&#xff0c;\u8fd9\u975e\u5e38\u4f4e\u6548\u2014\u2014\u56e0\u4e3a\u5f88\u591a\u60c5\u51b5\u4e0b\u5b50\u8fdb\u7a0b\u7d27\u63a5\u7740\u5c31\u8c03\u7528 exec \u66ff\u6362\u6389\u6574\u4e2a\u5730\u5740\u7a7a\u95f4&#xff0c;\u4e4b\u524d\u7684\u590d\u5236\u5168\u767d\u8d39\u4e86\u3002<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u73b0\u4ee3 Linux \u91c7\u7528\u5199\u65f6\u62f7\u8d1d\u4f18\u5316&#xff1a;<\/span><\/p>\n<li><span style=\"color:#0d0016\">fork \u65f6&#xff0c;\u5b50\u8fdb\u7a0b\u7684\u9875\u8868\u6307\u5411\u7236\u8fdb\u7a0b\u7684\u7269\u7406\u9875\u9762&#xff08;\u53ea\u8bfb\u5171\u4eab&#xff09;<\/span><\/li>\n<li><span style=\"color:#0d0016\">\u7236\u5b50\u8fdb\u7a0b\u90fd\u5c06\u8fd9\u4e9b\u9875\u9762\u6807\u8bb0\u4e3a\u53ea\u8bfb<\/span><\/li>\n<li><span style=\"color:#0d0016\">\u5f53\u4efb\u4e00\u65b9\u5c1d\u8bd5\u5199\u5165\u67d0\u4e2a\u9875\u9762\u65f6&#xff0c;\u89e6\u53d1\u7f3a\u9875\u5f02\u5e38&#xff08;Page Fault&#xff09;<\/span><\/li>\n<li><span style=\"color:#0d0016\">\u5185\u6838\u4e3a\u5199\u5165\u65b9\u590d\u5236\u4e00\u4efd\u72ec\u7acb\u7684\u7269\u7406\u9875\u9762&#xff0c;\u5e76\u4fee\u6539\u9875\u8868\u6620\u5c04<\/span><\/li>\n<li><span style=\"color:#0d0016\">\u4e4b\u540e\u53cc\u65b9\u5404\u81ea\u62e5\u6709\u8be5\u9875\u9762\u7684\u72ec\u7acb\u526f\u672c<\/span><\/li>\n<p>fork \u524d:<br \/>\n  \u7236\u8fdb\u7a0b\u9875\u8868 \u2500\u2500\u2192 \u7269\u7406\u9875\u9762 A&#xff08;\u53ef\u8bfb\u5199&#xff09;<\/p>\n<p>fork \u540e&#xff08;COW&#xff09;:<br \/>\n  \u7236\u8fdb\u7a0b\u9875\u8868 \u2500\u2500\u2192 \u7269\u7406\u9875\u9762 A&#xff08;\u53ea\u8bfb&#xff09;  \u2190\u2500\u2500 \u5b50\u8fdb\u7a0b\u9875\u8868<\/p>\n<p>\u7236\u8fdb\u7a0b\u5199\u5165\u9875\u9762 A \u65f6:<br \/>\n  \u7236\u8fdb\u7a0b\u9875\u8868 \u2500\u2500\u2192 \u7269\u7406\u9875\u9762 A&#039;&#xff08;\u53ef\u8bfb\u5199&#xff0c;A \u7684\u526f\u672c&#xff09;<br \/>\n  \u5b50\u8fdb\u7a0b\u9875\u8868 \u2500\u2500\u2192 \u7269\u7406\u9875\u9762 A&#xff08;\u53ea\u8bfb&#xff09;<\/p>\n<p><span style=\"color:#b95514\">COW \u7684\u6838\u5fc3\u4f18\u52bf&#xff1a;<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u53ea\u6709\u771f\u6b63\u53d1\u751f\u5199\u5165\u65f6\u624d\u590d\u5236&#xff0c;fork \u4e4b\u540e\u7acb\u5373 exec \u7684\u573a\u666f\u51e0\u4e4e\u96f6\u62f7\u8d1d\u5f00\u9500\u3002<\/span><\/p>\n<hr \/>\n<h4><span style=\"color:#511b78\">3.4 fork \u4e4b\u540e\u7236\u5b50\u8fdb\u7a0b\u5171\u4eab\u4ec0\u4e48&#xff1f;<\/span><\/h4>\n<table>\n<tr><span style=\"color:#0d0016\">\u8d44\u6e90<\/span><span style=\"color:#0d0016\">\u662f\u5426\u5171\u4eab<\/span><span style=\"color:#0d0016\">\u8bf4\u660e<\/span><\/tr>\n<tbody>\n<tr>\n<td><span style=\"color:#0d0016\">\u4ee3\u7801\u6bb5&#xff08;text&#xff09;<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u5171\u4eab&#xff08;\u53ea\u8bfb&#xff09;<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u4e0d\u53ef\u4fee\u6539&#xff0c;\u5929\u7136\u5171\u4eab<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">\u6570\u636e\u6bb5\/\u5806\/\u6808<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u5199\u65f6\u62f7\u8d1d<\/span><\/td>\n<td><span style=\"color:#0d0016\">COW \u673a\u5236<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">\u6587\u4ef6\u63cf\u8ff0\u7b26\u8868<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u5171\u4eab&#xff08;\u5f15\u7528\u8ba1\u6570&#xff09;<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u7236\u5b50\u5171\u4eab\u540c\u4e00\u6587\u4ef6\u504f\u79fb<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">\u4fe1\u53f7\u5904\u7406\u65b9\u5f0f<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u590d\u5236<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u5b50\u8fdb\u7a0b\u7ee7\u627f\u7236\u8fdb\u7a0b\u7684\u4fe1\u53f7\u5904\u7406<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">\u8fdb\u7a0b ID<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u4e0d\u540c<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u5b50\u8fdb\u7a0b\u83b7\u5f97\u65b0 PID<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">\u6302\u8d77\u7684\u4fe1\u53f7<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u4e0d\u7ee7\u627f<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u5b50\u8fdb\u7a0b\u7684\u6302\u8d77\u4fe1\u53f7\u96c6\u88ab\u6e05\u7a7a<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"color:#0d0016\">\u6587\u4ef6\u63cf\u8ff0\u7b26\u5171\u4eab\u7684\u5751&#xff1a;<\/span><\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;fcntl.h&gt;<\/p>\n<p>int main() {<br \/>\n    int fd &#061; open(&#034;test.txt&#034;, O_WRONLY | O_CREAT, 0644);<\/p>\n<p>    pid_t pid &#061; fork();<\/p>\n<p>    if (pid &#061;&#061; 0) {<br \/>\n        \/\/ \u5b50\u8fdb\u7a0b\u5199\u5165<br \/>\n        write(fd, &#034;child\\\\n&#034;, 6);<br \/>\n    } else {<br \/>\n        \/\/ \u7236\u8fdb\u7a0b\u5199\u5165<br \/>\n        write(fd, &#034;parent\\\\n&#034;, 7);<br \/>\n    }<\/p>\n<p>    close(fd);<br \/>\n    return 0;<br \/>\n}<\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u7531\u4e8e\u7236\u5b50\u8fdb\u7a0b\u5171\u4eab\u6587\u4ef6\u504f\u79fb\u91cf&#xff0c;\u4e24\u6b21\u5199\u5165\u4e0d\u4f1a\u4e92\u76f8\u8986\u76d6&#xff0c;\u800c\u662f\u4f9d\u6b21\u8ffd\u52a0\u3002\u8fd9\u4e5f\u662f\u4e3a\u4ec0\u4e48 fork \u540e\u9700\u8981\u6ce8\u610f\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u7ba1\u7406\u3002<\/span><\/p>\n<hr \/>\n<h3><span style=\"color:#1a439c\">\u56db\u3001\u8fdb\u7a0b\u72b6\u6001<\/span><\/h3>\n<h4><span style=\"color:#511b78\">4.1 Linux \u5185\u6838\u6e90\u7801\u600e\u4e48\u8bf4<\/span><\/h4>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u8fdb\u7a0b\u72b6\u6001\u5b9a\u4e49\u5728\u5185\u6838\u5934\u6587\u4ef6 include\/linux\/sched.h \u4e2d&#xff0c;\u901a\u8fc7 task_struct \u7684 __state \u5b57\u6bb5\u8868\u793a&#xff1a;<\/span><\/p>\n<p>struct task_struct {<br \/>\n    volatile long state;    \/* -1 unrunnable, 0 runnable, &gt;0 stopped *\/<br \/>\n    \/\/ &#8230;<br \/>\n};<\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u5177\u4f53\u7684\u72b6\u6001\u6807\u5fd7\u5b9a\u4e49\u5728 include\/linux\/sched.h&#xff1a;<\/span><\/p>\n<p>\/* Used in tsk-&gt;state: *\/<br \/>\n#define TASK_RUNNING            0x0000<br \/>\n#define TASK_INTERRUPTIBLE      0x0001<br \/>\n#define TASK_UNINTERRUPTIBLE    0x0002<br \/>\n#define TASK_STOPPED            0x0004<br \/>\n#define TASK_TRACED             0x0008<br \/>\n\/* Used in tsk-&gt;exit_state: *\/<br \/>\n#define EXIT_ZOMBIE             0x0010<br \/>\n#define EXIT_DEAD               0x0020<br \/>\n\/* Used in tsk-&gt;state again: *\/<br \/>\n#define TASK_PARKED             0x0040<br \/>\n#define TASK_DEAD               0x0080<br \/>\n#define TASK_WAKEKILL           0x0100<br \/>\n#define TASK_WAKING             0x0200<br \/>\n#define TASK_NOLOAD             0x0400<br \/>\n#define TASK_NEW                0x0800<br \/>\n#define TASK_STATE_MAX          0x1000<\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u6ce8\u610f&#xff1a;state \u5b57\u6bb5\u7528\u4e8e\u8fd0\u884c\u4e2d\u7684\u72b6\u6001&#xff0c;exit_state \u5b57\u6bb5\u4e13\u95e8\u7528\u4e8e\u9000\u51fa\u8fc7\u7a0b\u4e2d\u7684\u72b6\u6001&#xff08;\u50f5\u5c38\u3001\u6b7b\u4ea1&#xff09;\u3002<\/span><\/p>\n<hr \/>\n<h4><span style=\"color:#511b78\">4.2 \u4e03\u5927\u8fdb\u7a0b\u72b6\u6001\u8be6\u89e3<\/span><\/h4>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"724\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260902052512-6a97b33898716.png\" width=\"1280\" \/><\/p>\n<table>\n<tr><span style=\"color:#0d0016\">\u72b6\u6001<\/span><span style=\"color:#0d0016\">\u5b57\u7b26<\/span><span style=\"color:#0d0016\">\u5185\u6838\u5b8f<\/span><span style=\"color:#0d0016\">\u6838\u5fc3\u542b\u4e49<\/span><\/tr>\n<tbody>\n<tr>\n<td><span style=\"color:#0d0016\">\u8fd0\u884c\u6001<\/span><\/td>\n<td><span style=\"color:#0d0016\">R<\/span><\/td>\n<td style=\"width:201px\"><span style=\"color:#0d0016\">TASK_RUNNING<\/span><\/td>\n<td style=\"width:275px\"><span style=\"color:#0d0016\">\u6b63\u5728\u8fd0\u884c\u6216\u5728\u5c31\u7eea\u961f\u5217\u7b49\u5f85 CPU<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">\u53ef\u4e2d\u65ad\u7761\u7720<\/span><\/td>\n<td><span style=\"color:#0d0016\">S<\/span><\/td>\n<td style=\"width:201px\"><span style=\"color:#0d0016\">TASK_INTERRUPTIBLE<\/span><\/td>\n<td style=\"width:275px\"><span style=\"color:#0d0016\">\u7b49\u5f85\u4e8b\u4ef6&#xff0c;\u53ef\u88ab\u4fe1\u53f7\u5524\u9192<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">\u4e0d\u53ef\u4e2d\u65ad\u7761\u7720<\/span><\/td>\n<td><span style=\"color:#0d0016\">D<\/span><\/td>\n<td style=\"width:201px\"><span style=\"color:#0d0016\">TASK_UNINTERRUPTIBLE<\/span><\/td>\n<td style=\"width:275px\"><span style=\"color:#0d0016\">\u7b49\u5f85 I\/O&#xff0c;\u4e0d\u53ef\u88ab\u4fe1\u53f7\u5524\u9192<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">\u505c\u6b62\u6001<\/span><\/td>\n<td><span style=\"color:#0d0016\">T<\/span><\/td>\n<td style=\"width:201px\"><span style=\"color:#0d0016\">TASK_STOPPED<\/span><\/td>\n<td style=\"width:275px\"><span style=\"color:#0d0016\">\u88ab\u4fe1\u53f7\u6682\u505c&#xff0c;\u7b49\u5f85 SIGCONT \u6062\u590d<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">\u8ffd\u8e2a\u6001<\/span><\/td>\n<td><span style=\"color:#0d0016\">t<\/span><\/td>\n<td style=\"width:201px\"><span style=\"color:#0d0016\">TASK_TRACED<\/span><\/td>\n<td style=\"width:275px\"><span style=\"color:#0d0016\">\u88ab\u8c03\u8bd5\u5668\u6682\u505c&#xff08;gdb \u65ad\u70b9\u547d\u4e2d&#xff09;<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">\u50f5\u5c38\u6001<\/span><\/td>\n<td><span style=\"color:#0d0016\">Z<\/span><\/td>\n<td style=\"width:201px\"><span style=\"color:#0d0016\">EXIT_ZOMBIE<\/span><\/td>\n<td style=\"width:275px\"><span style=\"color:#0d0016\">\u5df2\u9000\u51fa&#xff0c;\u7236\u8fdb\u7a0b\u672a\u56de\u6536\u8d44\u6e90<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">\u6b7b\u4ea1\u6001<\/span><\/td>\n<td><span style=\"color:#0d0016\">X<\/span><\/td>\n<td style=\"width:201px\"><span style=\"color:#0d0016\">EXIT_DEAD<\/span><\/td>\n<td style=\"width:275px\"><span style=\"color:#0d0016\">\u6700\u7ec8\u72b6\u6001&#xff0c;task_struct \u5373\u5c06\u91ca\u653e<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h5><span style=\"color:#b95514\">\u2460 R \u2014 TASK_RUNNING&#xff08;\u8fd0\u884c\u6001\/\u5c31\u7eea\u6001&#xff09;<\/span><\/h5>\n<p>#define TASK_RUNNING  0x0000<\/p>\n<ul>\n<li><span style=\"color:#0d0016\">\u8fdb\u7a0b\u6b63\u5728 CPU \u4e0a\u6267\u884c&#xff0c;\u6216\u8005\u5728\u8fd0\u884c\u961f\u5217\u4e2d\u7b49\u5f85\u8c03\u5ea6<\/span><\/li>\n<li><span style=\"color:#0d0016\">\u8fd9\u662f\u552f\u4e00\u53ef\u4ee5\u5728 CPU \u4e0a\u8fd0\u884c\u7684\u72b6\u6001<\/span><\/li>\n<li><span style=\"color:#0d0016\">ps \u4e2d\u663e\u793a\u4e3a R<\/span><\/li>\n<\/ul>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u6ce8\u610f&#xff1a;Linux \u4e2d\u6ca1\u6709\u5355\u72ec\u7684&#034;\u5c31\u7eea\u6001&#034;&#xff0c;\u5c31\u7eea\u548c\u8fd0\u884c\u7edf\u4e00\u79f0\u4e3a TASK_RUNNING&#xff0c;\u533a\u522b\u4ec5\u5728\u4e8e\u662f\u5426\u6b63\u5728\u88ab CPU \u6267\u884c\u3002<\/span><\/p>\n<hr \/>\n<h5><span style=\"color:#b95514\">\u2461 S \u2014 TASK_INTERRUPTIBLE&#xff08;\u53ef\u4e2d\u65ad\u7761\u7720\u6001&#xff09;<\/span><\/h5>\n<p>#define TASK_INTERRUPTIBLE  0x0001<\/p>\n<ul>\n<li><span style=\"color:#0d0016\">\u8fdb\u7a0b\u5728\u7b49\u5f85\u67d0\u4e8b\u4ef6\u53d1\u751f&#xff08;\u5982\u7b49\u5f85\u7ec8\u7aef\u8f93\u5165\u3001\u7b49\u5f85\u7f51\u7edc\u6570\u636e\u3001\u7b49\u5f85\u9501&#xff09;<\/span><\/li>\n<li><span style=\"color:#0d0016\">\u53ef\u4ee5\u88ab\u4fe1\u53f7\u5524\u9192&#xff08;\u5982 Ctrl&#043;C \u53d1\u9001\u7684 SIGINT&#xff09;<\/span><\/li>\n<li><span style=\"color:#0d0016\">ps \u4e2d\u663e\u793a\u4e3a S&#xff0c;\u8fd9\u662f\u6700\u5e38\u89c1\u7684\u72b6\u6001<\/span><\/li>\n<\/ul>\n<p><span style=\"color:#0d0016\">\u5178\u578b\u573a\u666f&#xff1a;sleep(10)\u3001read() \u963b\u585e\u7b49\u5f85\u3001wait() \u7b49\u5f85\u5b50\u8fdb\u7a0b\u3002<\/span><\/p>\n<hr \/>\n<h5><span style=\"color:#b95514\">\u2462 D \u2014 TASK_UNINTERRUPTIBLE&#xff08;\u4e0d\u53ef\u4e2d\u65ad\u7761\u7720\u6001&#xff09;<\/span><\/h5>\n<p>#define TASK_UNINTERRUPTIBLE  0x0002<\/p>\n<ul>\n<li><span style=\"color:#0d0016\">\u8fdb\u7a0b\u5728\u7b49\u5f85\u4e0d\u53ef\u4e2d\u65ad\u7684\u4e8b\u4ef6&#xff08;\u901a\u5e38\u662f I\/O \u64cd\u4f5c&#xff0c;\u5982\u78c1\u76d8\u8bfb\u5199&#xff09;<\/span><\/li>\n<li><span style=\"color:#0d0016\">\u4e0d\u80fd\u88ab\u4fe1\u53f7\u5524\u9192&#xff0c;\u53ea\u80fd\u7531\u7b49\u5f85\u7684\u4e8b\u4ef6\u5b8c\u6210\u6765\u5524\u9192<\/span><\/li>\n<li><span style=\"color:#0d0016\">ps \u4e2d\u663e\u793a\u4e3a D<\/span><\/li>\n<\/ul>\n<p><span style=\"color:#b95514\">\u4e3a\u4ec0\u4e48\u9700\u8981\u4e0d\u53ef\u4e2d\u65ad&#xff1f;<\/span><span style=\"color:#0d0016\"> <\/span><\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u5047\u8bbe\u8fdb\u7a0b\u6b63\u5728\u6267\u884c\u78c1\u76d8 read \u7cfb\u7edf\u8c03\u7528&#xff0c;\u5185\u6838\u5df2\u5411\u78c1\u76d8\u53d1\u51fa I\/O \u8bf7\u6c42\u3002\u5982\u679c\u6b64\u65f6\u5141\u8bb8\u4fe1\u53f7\u4e2d\u65ad&#xff0c;\u8fdb\u7a0b\u53ef\u80fd\u5728 I\/O \u5b8c\u6210\u524d\u5c31\u9000\u51fa&#xff0c;\u5bfc\u81f4\u5185\u6838\u6001\u7684 I\/O \u64cd\u4f5c\u65e0\u6cd5\u6b63\u5e38\u5b8c\u6210&#xff0c;\u53ef\u80fd\u9020\u6210\u6570\u636e\u4e0d\u4e00\u81f4\u3002\u56e0\u6b64 I\/O \u7b49\u5f85\u671f\u95f4\u5fc5\u987b\u8bbe\u4e3a\u4e0d\u53ef\u4e2d\u65ad\u3002<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u6392\u67e5\u7cfb\u7edf\u8d1f\u8f7d\u9ad8\u65f6&#xff0c;\u5982\u679c\u770b\u5230\u5927\u91cf D \u72b6\u6001\u8fdb\u7a0b&#xff0c;\u901a\u5e38\u610f\u5473\u7740\u78c1\u76d8 I\/O \u5b58\u5728\u74f6\u9888\u3002<\/span><\/p>\n<hr \/>\n<h5><span style=\"color:#b95514\">\u2463 T \u2014 TASK_STOPPED&#xff08;\u505c\u6b62\u6001&#xff09;<\/span><\/h5>\n<p>#define TASK_STOPPED  0x0004<\/p>\n<ul>\n<li><span style=\"color:#0d0016\">\u8fdb\u7a0b\u88ab\u6682\u505c\u6267\u884c&#xff0c;\u6536\u5230 SIGSTOP \u6216 SIGTSTP&#xff08;Ctrl&#043;Z&#xff09;\u4fe1\u53f7<\/span><\/li>\n<li><span style=\"color:#0d0016\">\u53ef\u4ee5\u901a\u8fc7 SIGCONT \u4fe1\u53f7\u6062\u590d\u8fd0\u884c<\/span><\/li>\n<li><span style=\"color:#0d0016\">ps \u4e2d\u663e\u793a\u4e3a T<\/span><\/li>\n<\/ul>\n<p><span style=\"color:#0d0016\">\u5178\u578b\u573a\u666f&#xff1a;\u5728\u7ec8\u7aef\u4e2d\u6309 Ctrl&#043;Z \u5c06\u524d\u53f0\u8fdb\u7a0b\u6302\u5230\u540e\u53f0\u3002<\/span><\/p>\n<p>sleep 100<br \/>\n# \u6309 Ctrl&#043;Z<br \/>\n# [1]&#043;  Stopped                 sleep 100<\/p>\n<p>jobs          # \u67e5\u770b\u505c\u6b62\u7684\u4f5c\u4e1a<br \/>\nfg %1         # \u6062\u590d\u5230\u524d\u53f0\u8fd0\u884c<br \/>\nbg %1         # \u5728\u540e\u53f0\u7ee7\u7eed\u8fd0\u884c<\/p>\n<hr \/>\n<h5><span style=\"color:#b95514\">\u2464 t \u2014 TASK_TRACED&#xff08;\u8ffd\u8e2a\u6001&#xff09;<\/span><\/h5>\n<p>#define TASK_TRACED  0x0008<\/p>\n<ul>\n<li><span style=\"color:#0d0016\">\u8fdb\u7a0b\u88ab\u8c03\u8bd5\u5668\u8ffd\u8e2a&#xff08;\u5982 gdb \u9644\u52a0\u5230\u8fdb\u7a0b&#xff09;<\/span><\/li>\n<li><span style=\"color:#0d0016\">\u5f53\u8fdb\u7a0b\u88ab\u65ad\u70b9\u547d\u4e2d\u65f6\u8fdb\u5165\u6b64\u72b6\u6001<\/span><\/li>\n<li><span style=\"color:#0d0016\">ps \u4e2d\u663e\u793a\u4e3a t&#xff08;\u5c0f\u5199&#xff09;<\/span><\/li>\n<\/ul>\n<p><span style=\"color:#0d0016\">\u4e0e T&#xff08;STOPPED&#xff09;\u7684\u533a\u522b&#xff1a;TRACED \u662f\u88ab\u8c03\u8bd5\u5668\u6682\u505c&#xff0c;\u53ea\u80fd\u7531\u8c03\u8bd5\u5668\u901a\u8fc7 ptrace \u6062\u590d\u3002<\/span><\/p>\n<hr \/>\n<h5><span style=\"color:#b95514\">\u2465 Z \u2014 EXIT_ZOMBIE&#xff08;\u50f5\u5c38\u6001&#xff09;<\/span><\/h5>\n<p>#define EXIT_ZOMBIE  0x0010<\/p>\n<ul>\n<li><span style=\"color:#0d0016\">\u8fdb\u7a0b\u5df2\u7ecf\u6267\u884c\u7ed3\u675f&#xff0c;\u4f46\u7236\u8fdb\u7a0b\u5c1a\u672a\u8c03\u7528 wait() \/ waitpid() \u56de\u6536\u5176\u8d44\u6e90<\/span><\/li>\n<li><span style=\"color:#0d0016\">\u8fdb\u7a0b\u7684 task_struct \u4ecd\u7136\u4fdd\u7559&#xff08;\u5360\u7528\u4e00\u4e2a PID \u69fd\u4f4d&#xff09;&#xff0c;\u4f46\u7528\u6237\u6001\u8d44\u6e90\u5df2\u91ca\u653e<\/span><\/li>\n<li><span style=\"color:#0d0016\">ps \u4e2d\u663e\u793a\u4e3a Z&#xff0c;\u901a\u5e38\u6807\u6ce8\u4e3a &lt;defunct&gt;<\/span><\/li>\n<\/ul>\n<p><span style=\"color:#0d0016\">\u50f5\u5c38\u8fdb\u7a0b\u662f\u600e\u4e48\u4ea7\u751f\u7684&#xff1a;<\/span><\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;unistd.h&gt;<\/p>\n<p>int main() {<br \/>\n    pid_t pid &#061; fork();<\/p>\n<p>    if (pid &#061;&#061; 0) {<br \/>\n        \/\/ \u5b50\u8fdb\u7a0b&#xff1a;\u7acb\u523b\u9000\u51fa<br \/>\n        printf(&#034;\u5b50\u8fdb\u7a0b\u9000\u51fa&#xff0c;PID&#061;%d\\\\n&#034;, getpid());<br \/>\n        _exit(0);<br \/>\n    } else {<br \/>\n        \/\/ \u7236\u8fdb\u7a0b&#xff1a;\u4e0d\u8c03\u7528 wait&#xff0c;\u4e00\u76f4\u8fd0\u884c<br \/>\n        printf(&#034;\u7236\u8fdb\u7a0b\u8fd0\u884c\u4e2d&#xff0c;\u4e0d\u56de\u6536\u5b50\u8fdb\u7a0b\\\\n&#034;);<br \/>\n        while (1) sleep(1);<br \/>\n    }<br \/>\n    return 0;<br \/>\n}<\/p>\n<p><span style=\"color:#0d0016\">\u8fd0\u884c\u540e\u67e5\u770b&#xff1a;<\/span><\/p>\n<p>ps aux | grep Z<br \/>\n# wxx  6500  0.0  0.0  0  0 ?  Z  10:00  0:00 [zombie_demo] &lt;defunct&gt;<\/p>\n<p><span style=\"color:#0d0016\">\u5982\u4f55\u907f\u514d\u50f5\u5c38\u8fdb\u7a0b&#xff1a;<\/span><\/p>\n<li><span style=\"color:#0d0016\">\u7236\u8fdb\u7a0b\u8c03\u7528 wait() \u6216 waitpid() \u56de\u6536<\/span><\/li>\n<li><span style=\"color:#0d0016\">\u7236\u8fdb\u7a0b\u6ce8\u518c SIGCHLD \u4fe1\u53f7\u5904\u7406\u51fd\u6570&#xff0c;\u5728\u5176\u4e2d\u8c03\u7528 waitpid()<\/span><\/li>\n<li><span style=\"color:#0d0016\">\u7236\u8fdb\u7a0b\u5148\u9000\u51fa&#xff0c;\u5b50\u8fdb\u7a0b\u88ab init&#xff08;PID 1&#xff09;\u6536\u517b&#xff0c;init \u4f1a\u81ea\u52a8\u56de\u6536<\/span><\/li>\n<p>\/\/ \u65b9\u6cd52&#xff1a;\u4fe1\u53f7\u5904\u7406\u56de\u6536<br \/>\nvoid sigchld_handler(int sig) {<br \/>\n    while (waitpid(-1, NULL, WNOHANG) &gt; 0);<br \/>\n}<\/p>\n<p>int main() {<br \/>\n    signal(SIGCHLD, sigchld_handler);<br \/>\n    \/\/ &#8230;<br \/>\n}<\/p>\n<hr \/>\n<h5><span style=\"color:#b95514\">\u2466 X \u2014 EXIT_DEAD&#xff08;\u6b7b\u4ea1\u6001&#xff09;<\/span><\/h5>\n<p>#define EXIT_DEAD  0x0020<\/p>\n<ul>\n<li><span style=\"color:#0d0016\">\u8fdb\u7a0b\u7684\u6700\u7ec8\u72b6\u6001&#xff0c;task_struct \u5373\u5c06\u88ab\u91ca\u653e<\/span><\/li>\n<li><span style=\"color:#0d0016\">\u8fd9\u4e2a\u72b6\u6001\u975e\u5e38\u77ed\u6682&#xff0c;ps \u51e0\u4e4e\u6355\u6349\u4e0d\u5230<\/span><\/li>\n<li><span style=\"color:#0d0016\">ps \u4e2d\u663e\u793a\u4e3a X<\/span><\/li>\n<\/ul>\n<hr \/>\n<h4><span style=\"color:#511b78\">4.3 \u8fdb\u7a0b\u72b6\u6001\u8f6c\u6362\u56fe<\/span><\/h4>\n<p>                    fork()<br \/>\n  \u65b0\u5efa \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2192 R (\u8fd0\u884c\/\u5c31\u7eea)<br \/>\n                          \u2502<br \/>\n              \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510<br \/>\n              \u2502           \u2502           \u2502<br \/>\n         \u7b49\u5f85\u4e8b\u4ef6      \u65f6\u95f4\u7247\u5230     \u6536\u5230\u4fe1\u53f7<br \/>\n              \u2502           \u2502           \u2502<br \/>\n              \u25bc           \u25bc           \u25bc<br \/>\n        S (\u53ef\u4e2d\u65ad\u7761\u7720)   R          T (\u505c\u6b62)<br \/>\n        D (\u4e0d\u53ef\u4e2d\u65ad\u7761\u7720)              \u2502<br \/>\n              \u2502                       \u2502<br \/>\n         \u4e8b\u4ef6\u5b8c\u6210\/\u4fe1\u53f7               SIGCONT<br \/>\n              \u2502                       \u2502<br \/>\n              \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518<br \/>\n                          \u2502<br \/>\n                          \u25bc<br \/>\n                          R<br \/>\n                          \u2502<br \/>\n                     exit() \/ \u6536\u5230\u7ec8\u6b62\u4fe1\u53f7<br \/>\n                          \u2502<br \/>\n                          \u25bc<br \/>\n                    Z (\u50f5\u5c38) \u2500\u2500\u2192 wait() \u56de\u6536 \u2500\u2500\u2192 X (\u6b7b\u4ea1)<\/p>\n<hr \/>\n<h3><span style=\"color:#1a439c\">\u4e94\u3001\u8fdb\u7a0b\u72b6\u6001\u67e5\u770b<\/span><\/h3>\n<h4><span style=\"color:#511b78\">5.1 ps \u4e2d\u7684 STAT \u5217<\/span><\/h4>\n<p><span style=\"color:#0d0016\">\u5728 ps aux \u7684\u8f93\u51fa\u4e2d&#xff0c;STAT \u5217\u663e\u793a\u8fdb\u7a0b\u72b6\u6001\u3002\u9664\u4e86\u57fa\u672c\u72b6\u6001\u5b57\u7b26\u5916&#xff0c;\u8fd8\u53ef\u80fd\u6709\u9644\u52a0\u5b57\u7b26&#xff1a;<\/span><\/p>\n<table>\n<tr><span style=\"color:#0d0016\">\u5b57\u7b26<\/span><span style=\"color:#0d0016\">\u542b\u4e49<\/span><\/tr>\n<tbody>\n<tr>\n<td><span style=\"color:#0d0016\">R<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u8fd0\u884c\/\u5c31\u7eea<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">S<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u53ef\u4e2d\u65ad\u7761\u7720<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">D<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u4e0d\u53ef\u4e2d\u65ad\u7761\u7720<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">T<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u505c\u6b62<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">t<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u8ffd\u8e2a\u505c\u6b62<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">Z<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u50f5\u5c38<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">X<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u6b7b\u4ea1<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">s<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u4f1a\u8bdd\u9996\u8fdb\u7a0b&#xff08;session leader&#xff09;<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">l<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u591a\u7ebf\u7a0b\u8fdb\u7a0b<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">&#043;<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u4f4d\u4e8e\u524d\u53f0\u8fdb\u7a0b\u7ec4<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">&lt;<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u9ad8\u4f18\u5148\u7ea7&#xff08;nice \u503c\u4e3a\u8d1f&#xff09;<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">N<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u4f4e\u4f18\u5148\u7ea7&#xff08;nice \u503c\u4e3a\u6b63&#xff09;<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">L<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u6709\u9875\u9762\u9501\u5b9a\u5728\u5185\u5b58\u4e2d<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"color:#0d0016\">\u4f8b\u5982 Ssl&#043; \u8868\u793a&#xff1a;\u53ef\u4e2d\u65ad\u7761\u7720&#xff08;S&#xff09;&#043; \u4f1a\u8bdd\u9996\u8fdb\u7a0b&#xff08;s&#xff09;&#043; \u591a\u7ebf\u7a0b&#xff08;l&#xff09;&#043; \u524d\u53f0\u8fdb\u7a0b\u7ec4&#xff08;&#043;&#xff09;\u3002<\/span><\/p>\n<hr \/>\n<h4><span style=\"color:#511b78\">5.2 \u5b9e\u7528\u67e5\u770b\u6280\u5de7<\/span><\/h4>\n<p><span style=\"color:#0d0016\">\u67e5\u770b\u6307\u5b9a\u8fdb\u7a0b\u7684\u72b6\u6001&#xff1a;<\/span><\/p>\n<p>ps -o pid,stat,cmd -p 1234<\/p>\n<p><span style=\"color:#0d0016\">\u53ea\u770b\u50f5\u5c38\u8fdb\u7a0b&#xff1a;<\/span><\/p>\n<p>ps aux | awk &#039;$8 ~ \/Z\/&#039;<\/p>\n<p><span style=\"color:#0d0016\">\u67e5\u770b\u8fdb\u7a0b\u72b6\u6001\u5206\u5e03\u7edf\u8ba1&#xff1a;<\/span><\/p>\n<p>ps aux | awk &#039;{print $8}&#039; | sort | uniq -c | sort -rn<\/p>\n<p><span style=\"color:#0d0016\">\u8f93\u51fa\u793a\u4f8b&#xff1a;<\/span><\/p>\n<p>     42 S<br \/>\n      3 R<br \/>\n      1 Ss<br \/>\n      1 S&#043;<br \/>\n      1 Z<\/p>\n<p><span style=\"color:#0d0016\">\u7528 top \u67e5\u770b\u72b6\u6001&#xff1a;<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u5728 top \u4e2d\u6309 f \u8fdb\u5165\u5b57\u6bb5\u9009\u62e9&#xff0c;\u6dfb\u52a0 S&#xff08;Process Status&#xff09;\u5217\u5373\u53ef\u5b9e\u65f6\u67e5\u770b\u6bcf\u4e2a\u8fdb\u7a0b\u7684\u72b6\u6001\u3002<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u67e5\u770b\u8fdb\u7a0b\u7684\u5b8c\u6574\u72b6\u6001\u4fe1\u606f&#xff1a;<\/span><\/p>\n<p>cat \/proc\/1234\/status | grep State<br \/>\n# State:S (sleeping)<\/p>\n<hr \/>\n<h3><span style=\"color:#1a439c\">\u603b\u7ed3<\/span><\/h3>\n<table>\n<tr><span style=\"color:#0d0016\">\u4e3b\u9898<\/span><span style=\"color:#0d0016\">\u6838\u5fc3\u8981\u70b9<\/span><\/tr>\n<tbody>\n<tr>\n<td><span style=\"color:#0d0016\">\u67e5\u770b\u8fdb\u7a0b<\/span><\/td>\n<td><span style=\"color:#0d0016\">ps aux \/ ps -ef \u5feb\u7167&#xff0c;top \u5b9e\u65f6&#xff0c;\u5e95\u5c42\u8bfb \/proc<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">\u83b7\u53d6 PID<\/span><\/td>\n<td><span style=\"color:#0d0016\">getpid() \u8fd4\u56de\u5f53\u524d\u8fdb\u7a0b PID&#xff0c;getppid() \u8fd4\u56de\u7236\u8fdb\u7a0b PID<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">fork \u521b\u5efa\u8fdb\u7a0b<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u4e00\u6b21\u8c03\u7528\u4e24\u6b21\u8fd4\u56de&#xff1b;\u7236\u8fd4\u56de\u5b50 PID&#xff0c;\u5b50\u8fd4\u56de 0&#xff1b;COW \u5199\u65f6\u62f7\u8d1d<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">\u8fdb\u7a0b\u72b6\u6001<\/span><\/td>\n<td><span style=\"color:#0d0016\">R \u8fd0\u884c\u3001S \u53ef\u4e2d\u65ad\u7761\u7720\u3001D \u4e0d\u53ef\u4e2d\u65ad\u7761\u7720\u3001T \u505c\u6b62\u3001t \u8ffd\u8e2a\u3001Z \u50f5\u5c38\u3001X \u6b7b\u4ea1<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">\u72b6\u6001\u67e5\u770b<\/span><\/td>\n<td><span style=\"color:#0d0016\">ps \u7684 STAT \u5217&#xff0c;\/proc\/[pid]\/status&#xff0c;\u6ce8\u610f\u9644\u52a0\u5b57\u7b26\u542b\u4e49<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u7406\u89e3\u8fdb\u7a0b\u72b6\u6001\u662f\u6df1\u5165 Linux \u7cfb\u7edf\u7f16\u7a0b\u7684\u57fa\u77f3\u3002\u4ece fork \u521b\u5efa\u8fdb\u7a0b&#xff0c;\u5230\u8fdb\u7a0b\u5728\u5404\u4e2a\u72b6\u6001\u4e4b\u95f4\u6d41\u8f6c&#xff0c;\u518d\u5230\u6700\u7ec8\u88ab\u56de\u6536&#xff0c;\u6574\u4e2a\u751f\u547d\u5468\u671f\u90fd\u56f4\u7ed5\u7740 task_struct \u548c\u5185\u6838\u8c03\u5ea6\u5668\u5c55\u5f00\u3002\u638c\u63e1\u8fd9\u4e9b\u539f\u7406&#xff0c;\u624d\u80fd\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\u7cbe\u51c6\u5b9a\u4f4d\u8fdb\u7a0b\u5361\u6b7b\u3001\u50f5\u5c38\u8fdb\u7a0b\u3001\u8d1f\u8f7d\u8fc7\u9ad8\u7b49\u95ee\u9898\u3002<\/span><\/p>\n<hr \/>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1292\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260902052512-6a97b338e7cea.png\" width=\"2558\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Linux \u8fdb\u7a0b\u7ba1\u7406\u6df1\u5ea6\u89e3\u6790&#xff1a;\u4ece\u67e5\u770b\u8fdb\u7a0b\u5230\u8fdb\u7a0b\u72b6\u6001\u00a0\u00a0\u4e00\u3001\u67e5\u770b\u8fdb\u7a0b\u5728 Linux \u4e2d&#xff0c;\u8fdb\u7a0b\u662f\u8d44\u6e90\u5206\u914d\u7684\u57fa\u672c\u5355\u4f4d\u3002\u7cfb\u7edf\u4e2d\u6bcf\u65f6\u6bcf\u523b\u90fd\u6709\u5927\u91cf\u8fdb\u7a0b\u5728\u8fd0\u884c&#xff0c;\u5982\u4f55\u67e5\u770b\u5b83\u4eec\u662f\u7406\u89e3\u8fdb\u7a0b\u7ba1\u7406\u7684\u7b2c\u4e00\u6b65\u30021.1 ps \u547d\u4ee4ps&#xff08;process status&#xff09;\u662f\u6700\u5e38\u7528\u7684\u8fdb\u7a0b\u67e5\u770b\u5de5\u5177&#xff0c;\u5b83\u62a5\u544a\u5f53\u524d\u7cfb\u7edf\u4e2d\u8fdb\u7a0b\u7684\u5feb\u7167\u4fe1\u606f\u3002\u5e38\u7528\u7ec4\u5408&#xff1a;ps auxps aux\u8f93\u51fa\u793a\u4f8b&#xff1a;USER       PID %<\/p>\n","protected":false},"author":2,"featured_media":99335,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[55,58,43,78],"topic":[],"class_list":["post-99338","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-c","tag-linux","tag-43","tag-78"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u3010Linux\u3011 \u8fdb\u7a0b\uff083\uff09\u6df1\u5ea6\u89e3\u6790\uff1a\u4ece\u67e5\u770b\u8fdb\u7a0b\u5230\u8fdb\u7a0b\u72b6\u6001 - \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\/99338.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u3010Linux\u3011 \u8fdb\u7a0b\uff083\uff09\u6df1\u5ea6\u89e3\u6790\uff1a\u4ece\u67e5\u770b\u8fdb\u7a0b\u5230\u8fdb\u7a0b\u72b6\u6001 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"Linux \u8fdb\u7a0b\u7ba1\u7406\u6df1\u5ea6\u89e3\u6790&#xff1a;\u4ece\u67e5\u770b\u8fdb\u7a0b\u5230\u8fdb\u7a0b\u72b6\u6001\u00a0\u00a0\u4e00\u3001\u67e5\u770b\u8fdb\u7a0b\u5728 Linux \u4e2d&#xff0c;\u8fdb\u7a0b\u662f\u8d44\u6e90\u5206\u914d\u7684\u57fa\u672c\u5355\u4f4d\u3002\u7cfb\u7edf\u4e2d\u6bcf\u65f6\u6bcf\u523b\u90fd\u6709\u5927\u91cf\u8fdb\u7a0b\u5728\u8fd0\u884c&#xff0c;\u5982\u4f55\u67e5\u770b\u5b83\u4eec\u662f\u7406\u89e3\u8fdb\u7a0b\u7ba1\u7406\u7684\u7b2c\u4e00\u6b65\u30021.1 ps \u547d\u4ee4ps&#xff08;process status&#xff09;\u662f\u6700\u5e38\u7528\u7684\u8fdb\u7a0b\u67e5\u770b\u5de5\u5177&#xff0c;\u5b83\u62a5\u544a\u5f53\u524d\u7cfb\u7edf\u4e2d\u8fdb\u7a0b\u7684\u5feb\u7167\u4fe1\u606f\u3002\u5e38\u7528\u7ec4\u5408&#xff1a;ps auxps aux\u8f93\u51fa\u793a\u4f8b&#xff1a;USER    PID %\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/99338.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-09-02T05:25:15+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260902052511-6a97b337057ef.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=\"6 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/99338.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/99338.html\",\"name\":\"\u3010Linux\u3011 \u8fdb\u7a0b\uff083\uff09\u6df1\u5ea6\u89e3\u6790\uff1a\u4ece\u67e5\u770b\u8fdb\u7a0b\u5230\u8fdb\u7a0b\u72b6\u6001 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-09-02T05:25:15+00:00\",\"dateModified\":\"2026-09-02T05:25:15+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/99338.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/99338.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/99338.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u3010Linux\u3011 \u8fdb\u7a0b\uff083\uff09\u6df1\u5ea6\u89e3\u6790\uff1a\u4ece\u67e5\u770b\u8fdb\u7a0b\u5230\u8fdb\u7a0b\u72b6\u6001\"}]},{\"@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":"\u3010Linux\u3011 \u8fdb\u7a0b\uff083\uff09\u6df1\u5ea6\u89e3\u6790\uff1a\u4ece\u67e5\u770b\u8fdb\u7a0b\u5230\u8fdb\u7a0b\u72b6\u6001 - \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\/99338.html","og_locale":"zh_CN","og_type":"article","og_title":"\u3010Linux\u3011 \u8fdb\u7a0b\uff083\uff09\u6df1\u5ea6\u89e3\u6790\uff1a\u4ece\u67e5\u770b\u8fdb\u7a0b\u5230\u8fdb\u7a0b\u72b6\u6001 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"Linux \u8fdb\u7a0b\u7ba1\u7406\u6df1\u5ea6\u89e3\u6790&#xff1a;\u4ece\u67e5\u770b\u8fdb\u7a0b\u5230\u8fdb\u7a0b\u72b6\u6001\u00a0\u00a0\u4e00\u3001\u67e5\u770b\u8fdb\u7a0b\u5728 Linux \u4e2d&#xff0c;\u8fdb\u7a0b\u662f\u8d44\u6e90\u5206\u914d\u7684\u57fa\u672c\u5355\u4f4d\u3002\u7cfb\u7edf\u4e2d\u6bcf\u65f6\u6bcf\u523b\u90fd\u6709\u5927\u91cf\u8fdb\u7a0b\u5728\u8fd0\u884c&#xff0c;\u5982\u4f55\u67e5\u770b\u5b83\u4eec\u662f\u7406\u89e3\u8fdb\u7a0b\u7ba1\u7406\u7684\u7b2c\u4e00\u6b65\u30021.1 ps \u547d\u4ee4ps&#xff08;process status&#xff09;\u662f\u6700\u5e38\u7528\u7684\u8fdb\u7a0b\u67e5\u770b\u5de5\u5177&#xff0c;\u5b83\u62a5\u544a\u5f53\u524d\u7cfb\u7edf\u4e2d\u8fdb\u7a0b\u7684\u5feb\u7167\u4fe1\u606f\u3002\u5e38\u7528\u7ec4\u5408&#xff1a;ps auxps aux\u8f93\u51fa\u793a\u4f8b&#xff1a;USER    PID %","og_url":"https:\/\/www.wsisp.com\/helps\/99338.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-09-02T05:25:15+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260902052511-6a97b337057ef.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"6 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/99338.html","url":"https:\/\/www.wsisp.com\/helps\/99338.html","name":"\u3010Linux\u3011 \u8fdb\u7a0b\uff083\uff09\u6df1\u5ea6\u89e3\u6790\uff1a\u4ece\u67e5\u770b\u8fdb\u7a0b\u5230\u8fdb\u7a0b\u72b6\u6001 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-09-02T05:25:15+00:00","dateModified":"2026-09-02T05:25:15+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/99338.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/99338.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/99338.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u3010Linux\u3011 \u8fdb\u7a0b\uff083\uff09\u6df1\u5ea6\u89e3\u6790\uff1a\u4ece\u67e5\u770b\u8fdb\u7a0b\u5230\u8fdb\u7a0b\u72b6\u6001"}]},{"@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\/99338","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=99338"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/99338\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/99335"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=99338"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=99338"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=99338"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=99338"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}