{"id":3272,"date":"2011-09-05T10:13:20","date_gmt":"2011-09-05T02:13:20","guid":{"rendered":"http:\/\/edsionte.com\/techblog\/?p=3272"},"modified":"2011-09-05T10:15:13","modified_gmt":"2011-09-05T02:15:13","slug":"%e7%ba%bf%e7%a8%8b%e9%82%a3%e4%ba%9b%e4%ba%8b%e5%84%bf2-%e5%ae%9e%e8%b7%b5","status":"publish","type":"post","link":"http:\/\/edsionte.com\/techblog\/archives\/3272","title":{"rendered":"\u7ebf\u7a0b\u90a3\u4e9b\u4e8b\u513f(2)-\u5b9e\u8df5"},"content":{"rendered":"<p>\u5728\u591a\u7ebf\u7a0b\u7a0b\u5e8f\u4e2d\uff0c\u4e00\u4e2a\u65b0\u7684\u7ebf\u7a0b\u901a\u5e38\u7531\u4e00\u4e2a\u8fdb\u7a0b\u8c03\u7528phtread_create()\u51fd\u6570\u800c\u8bde\u751f\u7684\u3002\u65b0\u7ebf\u7a0b\u521b\u5efa\u540e\uff0c\u901a\u5e38\u5c06\u8fd9\u4e2a\u8fdb\u7a0b\u79f0\u4e3a\u4e3b\u7ebf\u7a0b\u3002\u4f60\u4e5f\u8bb8\u4f1a\u6709\u6240\u8ff7\u60d1\uff1a\u4e00\u4e2a\u8fdb\u7a0b\u600e\u4e48\u4f1a\u7f16\u7a0b\u7ebf\u7a0b\uff1f\u6b64\u523b\u6709\u51e0\u4e2a\u7ebf\u7a0b\uff0c\u51e0\u4e2a\u8fdb\u7a0b\uff1f<\/p>\n<p>\u5176\u5b9e\u901a\u8fc7<a href=\"http:\/\/edsionte.com\/techblog\/archives\/3223\" target=\"_blank\">\u4e0a\u6587<\/a>\u5bf9\u7ebf\u7a0b\u3001\u8f7b\u91cf\u7ea7\u8fdb\u7a0b\u4ee5\u53ca\u7ebf\u7a0b\u7ec4\u4e4b\u95f4\u5173\u7cfb\u7684\u7406\u89e3\u540e\uff0c\u8fd9\u4e2a\u95ee\u9898\u4f3c\u4e4e\u4e5f\u4e0d\u96be\u56de\u7b54\u3002\u6211\u4eec\u53ef\u4ee5\u5c06\u6240\u6709\u7684\u8fdb\u7a0b\u90fd\u770b\u4f5c\u4e00\u4e2a\u7ebf\u7a0b\u7ec4\uff0c\u53ea\u4e0d\u8fc7\u666e\u901a\u8fdb\u7a0b\u7684\u7ebf\u7a0b\u7ec4\u53ea\u5305\u542b\u5b83\u81ea\u5df1\u4e00\u4e2a\u7ebf\u7a0b\uff0c\u5b83\u4e0d\u80fd\u4e0e\u5176\u4ed6\u7ebf\u7a0b\u5171\u4eab\u8d44\u6e90\uff0c\u53ea\u80fd\u72ec\u4eab\u81ea\u5df1\u7684\u8d44\u6e90\uff08\u800c\u6210\u4e3a\u8fdb\u7a0b\uff09\u3002<\/p>\n<p>\u5bf9\u4e8e\u591a\u7ebf\u7a0b\u7a0b\u5e8f\u6765\u8bf4\uff0c\u4e00\u65e6\u5728\u8fdb\u7a0b\u5185\u521b\u5efa\u4e86\u4e00\u4e2a\u7ebf\u7a0b\uff0c\u90a3\u4e48\u8be5\u8fdb\u7a0b\u4e5f\u5c31\u6447\u8eab\u53d8\u6210\u4e86\u4e00\u4e2a\u7ebf\u7a0b\u3002\u4e3b\u7ebf\u7a0b\u548c\u5b50\u7ebf\u7a0b\u5171\u4eab\u201c\u4ee5\u524d\u201d\u90a3\u4e2a\u8fdb\u7a0b\u6240\u72ec\u4eab\u7684\u8d44\u6e90\u3002\u4e3b\u7ebf\u7a0b\u548c\u5b50\u7ebf\u7a0b\u4e4b\u95f4\u662f\u5e76\u5217\u5173\u7cfb\uff0c\u4e0d\u5b58\u5728\u7c7b\u4f3cfork()\u51fd\u6570\u90a3\u6837\u7684\u7236\u5b50\u8fdb\u7a0b\u5173\u7cfb\uff0c\u8fd9\u4e5f\u5c31\u662f\u4e0d\u5c06\u521b\u5efa\u7ebf\u7a0b\u7684\u8fdb\u7a0b\u79f0\u4e3a\u7236\u7ebf\u7a0b\u7684\u539f\u56e0\u3002<\/p>\n<p>\u5982\u679c\u4f60\u8fd8\u5bf9\u4e0a\u8ff0\u7684\u63cf\u8ff0\u6709\u6240\u7591\u60d1\uff0c\u90a3\u4e48\u901a\u8fc7\u4e0b\u9762\u7684\u5b9e\u9a8c\u7ed3\u679c\u53ef\u4ee5\u7406\u89e3\u7684\u66f4\u52a0\u6df1\u523b\u3002\u4e0b\u8ff0\u7684\u7a0b\u5e8f\u5c31\u662f\u4e00\u4e2a\u666e\u901a\u7684\u7ebf\u7a0b\u521b\u5efa\uff0c\u53ea\u4e0d\u8fc7\u4e3b\u7ebf\u7a0b\u548c\u5b50\u7ebf\u7a0b\u589e\u52a0\u4e86\u5ef6\u65f6\u4ee5\u65b9\u4fbf\u6211\u4eec\u67e5\u770b\u5b9e\u9a8c\u7ed3\u679c\u3002<\/p>\n<pre class=\"brush:c\">int *thread(void* arg)\r\n{\r\n\tpthread_t newthid;\r\n\tnewthid = pthread_self();\/\/get the current thread's id\r\n\tprintf(\"this a new thread and thread ID is=%lu\\n\", newthid);\r\n\tsleep(500);\r\n\treturn NULL;\r\n}\r\n\r\nint main()\r\n{\r\n\tpthread_t thid;\r\n\r\n\tprintf(\"main thread,ID is %lu\\n\", pthread_self());\/\/get the main thread's id\r\n\tif (pthread_create(&amp;thid, NULL, (void *)thread, NULL) != 0) {\r\n\t\tprintf(\"thread creation failed\\n\");\r\n\t\texit(1);\r\n\t}\r\n\r\n\tprintf(\"my Id is %lu, new thread ID is %lu\\n\", pthread_self(), thid);\r\n\tsleep(1000);\r\n\treturn 0;\r\n}<\/pre>\n<p>\u6211\u4eec\u5e26\u5f00\u4e00\u4e2a\u7ec8\u7aef\uff08\u79f0\u4e3a\u7ec8\u7aef1\uff09\u8fd0\u884c\u4e0a\u8ff0\u7a0b\u5e8f\uff0c\u518d\u53e6\u4e00\u4e2a\u7ec8\u7aef\uff08\u79f0\u4e3a\u7ec8\u7aef2\uff09\u91cc\u4f7f\u7528ps -eLf\u547d\u4ee4\u67e5\u770b\u7cfb\u7edf\u5f53\u524d\u7684\u7ebf\u7a0b\u4fe1\u606f\u3002<\/p>\n<pre class=\"brush:c\">UID        PID  PPID   LWP  C NLWP STIME TTY          TIME CMD\r\nedsionte  2210  2208  2210  0    1 09:10 pts\/0    00:00:00 bash\r\nedsionte  2429  2210  2429  0    2 09:52 pts\/0    00:00:00 .\/createthread\r\nedsionte  2429  2210  2430  0    2 09:52 pts\/0    00:00:00 .\/createthread\r\nedsionte  2431  2208  2431  5    1 09:52 pts\/1    00:00:00 bash\r\nedsionte  2449  2431  2449  0    1 09:52 pts\/1    00:00:00 ps -eLf<\/pre>\n<p>\u8bf7\u6ce8\u610f\u4e0a\u8ff0\u4fe1\u606f\u4e2d\u4e09\u7c7bID\u4fe1\u606f\uff1aPID\uff0cPPID\u548cLWP\u3002LWP\u662f\u8f7b\u91cf\u7ea7\u8fdb\u7a0b\u7684pid\uff0cNLWP\u4e3a\u7ebf\u7a0b\u7ec4\u4e2d\u7ebf\u7a0b\u7684\u4e2a\u6570\u3002\u4e0b\u9762\u5bf9\u4e0a\u8ff0\u7684\u5b9e\u9a8c\u7ed3\u679c\u4f5c\u4ee5\u89e3\u91ca\u3002<\/p>\n<p><strong>1.\u8fd0\u884c\u5b9e\u9a8c\u7a0b\u5e8f\u7684\u7ec8\u7aef\u5bf9\u5e94\u7684pid\u4e3a2210\uff1b<\/strong><\/p>\n<p><strong>2.\u6211\u4eec\u7684\u5b9e\u9a8c\u7a0b\u5e8f\u4ea7\u751f\u4e86\u4e24\u4e2a\u7ebf\u7a0b\uff0c\u5176pid\u90fd\u662f2429\u3002\u8fd9\u8bf4\u660e\u8fd9\u4e24\u4e2a\u7ebf\u7a0b\u662f\u5e76\u5217\u5173\u7cfb\uff0c\u5b83\u4eec\u5c5e\u4e8e\u540c\u4e00\u4e2a\u7ebf\u7a0b\u7ec4\uff0c\u8be5\u7ebf\u7a0b\u7ec4\u7684pid\u4e3a2429\u3002<\/strong><\/p>\n<p><strong>3.\u5b9e\u9a8c\u7a0b\u5e8f\u4ea7\u751f\u7684\u4e24\u4e2a\u7ebf\u7a0b\u7684PPID\u5747\u4e3a2210\uff0c\u518d\u6b21\u8bf4\u660e\u8fd9\u4e24\u4e2a\u7ebf\u7a0b\u4e4b\u95f4\u6ca1\u6709\u7236\u5b50\u5173\u7cfb\uff0c\u4ed6\u4eec\u7684\u7236\u4eb2\u5747\u4e3a\u7ec8\u7aef1\u5bf9\u5e94\u7684\u8fdb\u7a0b\u3002<\/strong><\/p>\n<p><strong>4.\u6bcf\u4e2a\u7ebf\u7a0b\u90fd\u4e0e\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u8fdb\u7a0b\u5173\u8054\uff0c\u56e0\u6b64\u4e24\u4e2a\u7ebf\u7a0b\u7684LWP\u4e0d\u540c\uff0c\u5206\u522b\u4e3a2429\u548c2430\u3002<\/strong><\/p>\n<p><strong>5.\u8fd9\u4e24\u4e2a\u7ebf\u7a0b\u5f62\u6210\u4e00\u4e2a\u7ebf\u7a0b\u7ec4\uff0c\u56e0\u6b64\u4ed6\u4eec\u5bf9\u5e94\u7684NLWP\u4e3a2\u3002<\/strong><\/p>\n<p><strong>6.\u901a\u8fc7pid\uff0cppid\u548cLWP\u7684\u5206\u914d\u60c5\u51b5\u53ef\u4ee5\u770b\u5230\uff0c\u5185\u6838\u5bf9\u4e8e\u8fdb\u7a0b\u548c\u8f7b\u91cf\u7ea7\u8fdb\u7a0b\u7684id\u5206\u914d\u662f\u7edf\u4e00\u7ba1\u7406\u7684\uff0c\u8fd9\u6e90\u4e8e\u4ed6\u4eec\u4f7f\u7528\u76f8\u540c\u7684\u6570\u636e\u7ed3\u6784task_struct\u3002<\/strong><\/p>\n<p>\u4e0a\u8ff0\u5206\u6790\u57fa\u672c\u4e0a\u7528\u5b9e\u9a8c\u7ed3\u679c\u8be0\u91ca\u4e86\u8fdb\u7a0b\u3001\u7ebf\u7a0b\u548c\u8f7b\u91cf\u7ea7\u8fdb\u7a0b\u4e4b\u95f4\u7684\u5173\u7cfb\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u591a\u7ebf\u7a0b\u7a0b\u5e8f\u4e2d\uff0c\u4e00\u4e2a\u65b0\u7684\u7ebf\u7a0b\u901a\u5e38\u7531\u4e00\u4e2a\u8fdb\u7a0b\u8c03\u7528phtread_create()\u51fd\u6570\u800c\u8bde\u751f\u7684\u3002\u65b0\u7ebf\u7a0b\u521b\u5efa\u540e\uff0c\u901a\u5e38 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[246],"tags":[248,118,249],"class_list":["post-3272","post","type-post","status-publish","format-standard","hentry","category-posix","tag-posix","tag-118","tag-249"],"views":23150,"_links":{"self":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/3272","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/comments?post=3272"}],"version-history":[{"count":0,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/3272\/revisions"}],"wp:attachment":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/media?parent=3272"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/categories?post=3272"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/tags?post=3272"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}