{"id":2198,"date":"2010-12-09T16:16:17","date_gmt":"2010-12-09T08:16:17","guid":{"rendered":"http:\/\/edsionte.com\/techblog\/?p=2198"},"modified":"2010-12-15T16:22:41","modified_gmt":"2010-12-15T08:22:41","slug":"2198","status":"publish","type":"post","link":"http:\/\/edsionte.com\/techblog\/archives\/2198","title":{"rendered":"\u8fdb\u7a0b\u63cf\u8ff0\u7b26\u7684\u5904\u7406"},"content":{"rendered":"<h4><strong>\u8fdb\u7a0b\u63cf\u8ff0\u7b26\u7684\u5904\u7406<\/strong><\/h4>\n<p>\u5bf9\u4e8e\u6bcf\u4e00\u4e2a\u8fdb\u7a0b\u800c\u8a00\uff0c\u5185\u6838\u4e3a\u5176\u5355\u72ec\u5206\u914d\u4e86\u4e00\u4e2a\u5185\u5b58\u533a\u57df\uff0c\u8fd9\u4e2a\u533a\u57df\u5b58\u50a8\u7684\u662f\u5185\u6838\u6808\u548c\u8be5\u8fdb\u7a0b\u6240\u5bf9\u5e94\u7684\u4e00\u4e2a\u5c0f\u578b\u8fdb\u7a0b\u63cf\u8ff0\u7b26\u2014\u2014thread_info\u7ed3\u6784\u3002<\/p>\n<pre class=\"brush:c\">struct thread_info {\r\n\tstruct task_struct\t*task;\t\t\/* main task structure *\/\r\n\tstruct exec_domain\t*exec_domain;\t\/* execution domain *\/\r\n\tunsigned long\t\tflags;\t\t\/* low level flags *\/\r\n\tunsigned long\t\tstatus;\t\t\/* thread-synchronous flags *\/\r\n\t__u32\t\t\tcpu;\t\t\/* current CPU *\/\r\n\t__s32\t\t\tpreempt_count; \/* 0 =&gt; preemptable, &lt;0 =&gt; BUG *\/\r\n\tmm_segment_t\t\taddr_limit;\r\n\tstruct restart_block    restart_block;\r\n\tunsigned long           previous_esp;\r\n\t__u8\t\t\tsupervisor_stack[0];\r\n};<\/pre>\n<p>\u4e4b\u6240\u4ee5\u5c06thread_info\u7ed3\u6784\u79f0\u4e4b\u4e3a\u5c0f\u578b\u7684\u8fdb\u7a0b\u63cf\u8ff0\u7b26\uff0c\u662f\u56e0\u4e3a\u5728\u8fd9\u4e2a\u7ed3\u6784\u4e2d\u5e76\u6ca1\u6709\u76f4\u63a5\u5305\u542b\u4e0e\u8fdb\u7a0b\u76f8\u5173\u7684\u5b57\u6bb5\uff0c\u800c\u662f\u901a\u8fc7task\u5b57\u6bb5\u6307\u5411\u5177\u4f53\u67d0\u4e2a\u8fdb\u7a0b\u63cf\u8ff0\u7b26\u3002\u901a\u5e38\u8fd9\u5757\u5185\u5b58\u533a\u57df\u7684\u5927\u5c0f\u662f8KB\uff0c\u4e5f\u5c31\u662f\u4e24\u4e2a\u9875\u7684\u5927\u5c0f\uff08\u6709\u65f6\u5019\u4e5f\u4f7f\u7528\u4e00\u4e2a\u9875\u6765\u5b58\u50a8\uff0c\u53734KB\uff09\u3002\u4e00\u4e2a\u8fdb\u7a0b\u7684\u5185\u6838\u6808\u548cthread_info\u7ed3\u6784\u4e4b\u95f4\u7684\u903b\u8f91\u5173\u7cfb\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n<p><a rel=\"attachment wp-att-2201\" href=\"http:\/\/edsionte.com\/techblog\/archives\/2198\/screenshot-5\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2201\" title=\"Screenshot\" src=\"http:\/\/edsionte.com\/techblog\/wordpress\/wp-content\/uploads\/2010\/12\/Screenshot1.png\" alt=\"\" width=\"593\" height=\"308\" srcset=\"http:\/\/edsionte.com\/techblog\/wordpress\/wp-content\/uploads\/2010\/12\/Screenshot1.png 741w, http:\/\/edsionte.com\/techblog\/wordpress\/wp-content\/uploads\/2010\/12\/Screenshot1-300x155.png 300w\" sizes=\"auto, (max-width: 593px) 100vw, 593px\" \/><\/a><\/p>\n<p>\u4ece\u4e0a\u56fe\u53ef\u77e5\uff0c\u5185\u6838\u6808\u662f\u4ece\u8be5\u5185\u5b58\u533a\u57df\u7684\u9876\u5c42\u5411\u4e0b\uff08\u4ece\u9ad8\u5730\u5740\u5230\u4f4e\u5730\u5740\uff09\u589e\u957f\u7684\uff0c\u800cthread_info\u7ed3\u6784\u5219\u662f\u4ece\u8be5\u533a\u57df\u7684\u5f00\u59cb\u5904\u5411\u4e0a\uff08\u4ece\u4f4e\u5730\u5740\u5230\u9ad8\u5730\u5740\uff09\u589e\u957f\u3002\u5185\u6838\u6808\u7684\u6808\u9876\u5730\u5740\u5b58\u50a8\u5728esp\u5bc4\u5b58\u5668\u4e2d\u3002\u6240\u4ee5\uff0c\u5f53\u8fdb\u7a0b\u4ece\u7528\u6237\u6001\u5207\u6362\u5230\u5185\u6838\u6001\u540e\uff0cesp\u5bc4\u5b58\u5668\u6307\u5411\u8fd9\u4e2a\u533a\u57df\u7684\u672b\u7aef\u3002<\/p>\n<p>\u4ece\u4ee3\u7801\u7684\u89d2\u5ea6\u6765\u770b\uff0c\u5185\u6838\u6808\u548cthread_info\u7ed3\u6784\u662f\u88ab\u5b9a\u4e49\u5728\u4e00\u4e2a\u8054\u5408\u4f53\u5f53\u4e2d\u7684\uff1a<\/p>\n<pre class=\"brush:c\"> \/\/\u5b9a\u4e49\u5728linux\/include\/linux\/sched.h\u4e2d\r\n union thread_union {\r\n         struct thread_info thread_info;\r\n         unsigned long stack[THREAD_SIZE\/sizeof(long)];\r\n };<\/pre>\n<p>\u5176\u4e2d\uff0cTHREAD_SIZE\u7684\u503c\u53d68192\u65f6\uff0cstack\u6570\u7ec4\u7684\u5927\u5c0f\u4e3a2048\uff1bTHREAD_SIZE\u7684\u503c\u53d64096\u65f6\uff0cstack\u6570\u7ec4\u7684\u5927\u5c0f\u4e3a1024\u3002\u73b0\u5728\u6211\u4eec\u5e94\u8be5\u601d\u8003\uff0c\u4e3a\u4f55\u8981\u5c06\u5185\u6838\u6808\u548cthread_info\uff08\u5176\u5b9e\u4e5f\u5c31\u76f8\u5f53\u4e8etask_struct\uff0c\u53ea\u4e0d\u8fc7\u4f7f\u7528thread_info\u7ed3\u6784\u66f4\u8282\u7701\u7a7a\u95f4\uff09\u7d27\u5bc6\u7684\u653e\u5728\u4e00\u8d77\uff1f\u6700\u4e3b\u8981\u7684\u539f\u56e0\u5c31\u662f\u5185\u6838\u53ef\u4ee5\u5f88\u5bb9\u6613\u7684\u901a\u8fc7esp\u5bc4\u5b58\u5668\u7684\u503c\u83b7\u5f97\u5f53\u524d\u6b63\u5728\u8fd0\u884c\u8fdb\u7a0b\u7684thread_info\u7ed3\u6784\u7684\u5730\u5740\uff0c\u8fdb\u800c\u83b7\u5f97\u5f53\u524d\u8fdb\u7a0b\u63cf\u8ff0\u7b26\u7684\u5730\u5740\u3002<\/p>\n<pre class=\"brush:c\">\/\/\u5b9a\u4e49\u5728\/linux\/include\/asm-i386\/thread_info.h\u4e2d\r\n  static inline struct thread_info *current_thread_info(void)\r\n  {\r\n          struct thread_info *ti;\r\n          __asm__(\"andl %%esp,%0; \":\"=r\" (ti) : \"0\" (~(THREAD_SIZE - 1)));\r\n          return ti;\r\n  }<\/pre>\n<p>\u8fd9\u6761\u5185\u8054\u6c47\u7f16\u8bed\u53e5\u4f1a\u5c4f\u853d\u6389esp\u5bc4\u5b58\u5668\u4e2d\u7684\u5185\u6838\u6808\u9876\u5730\u5740\u7684\u4f4e13\u4f4d\uff08\u621612\u4f4d\uff0c\u5f53THREAD_SIZE\u4e3a4096\u65f6\uff09\u3002\u6b64\u65f6ti\u6240\u6307\u7684\u5730\u5740\u5c31\u662f\u8fd9\u7247\u5185\u5b58\u533a\u57df\u7684\u8d77\u59cb\u5730\u5740\uff0c\u4e5f\u5c31\u521a\u597d\u662fthread_info\u7ed3\u6784\u7684\u5730\u5740\u3002\u4f46\u662f\uff0cthread_info\u7ed3\u6784\u7684\u5730\u5740\u5e76\u4e0d\u4f1a\u5bf9\u6211\u4eec\u76f4\u63a5\u6709\u7528\u3002\u6211\u4eec\u901a\u5e38\u53ef\u4ee5\u8f7b\u677e\u7684\u901a\u8fc7current\u5b8f\u83b7\u5f97\u5f53\u524d\u8fdb\u7a0b\u7684task_struct\u7ed3\u6784\uff0c\u8fd9\u4e2a\u5b8f\u662f\u5982\u4f55\u5b9e\u73b0\u7684\uff1f<\/p>\n<pre class=\"brush:c\">\/\/\u5b9a\u4e49\u5728linux\/include\/asm-i386\/current.h\u4e2d\r\n   static inline struct task_struct * get_current(void)\r\n   {\r\n          return current_thread_info()-&gt;task;\r\n  }\r\n  #define current get_current()<\/pre>\n<p>\u901a\u8fc7\u4e0a\u8ff0\u6e90\u7801\u53ef\u4ee5\u53d1\u73b0\uff0ccurrent\u5b8f\u8fd4\u56de\u7684\u662fthread_info\u7ed3\u6784task\u5b57\u6bb5\u3002\u800ctask\u6b63\u597d\u6307\u5411\u4e0ethread_info\u7ed3\u6784\u5173\u8054\u7684\u90a3\u4e2a\u8fdb\u7a0b\u63cf\u8ff0\u7b26\u3002\u5f97\u5230current\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u83b7\u5f97\u5f53\u524d\u6b63\u5728\u8fd0\u884c\u8fdb\u7a0b\u7684\u63cf\u8ff0\u7b26\u4e2d\u4efb\u4f55\u4e00\u4e2a\u5b57\u6bb5\u4e86\uff0c\u6bd4\u5982\u6211\u4eec\u901a\u5e38\u6240\u505a\u7684\uff1acurrent-&gt;pid\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fdb\u7a0b\u63cf\u8ff0\u7b26\u7684\u5904\u7406 \u5bf9\u4e8e\u6bcf\u4e00\u4e2a\u8fdb\u7a0b\u800c\u8a00\uff0c\u5185\u6838\u4e3a\u5176\u5355\u72ec\u5206\u914d\u4e86\u4e00\u4e2a\u5185\u5b58\u533a\u57df\uff0c\u8fd9\u4e2a\u533a\u57df\u5b58\u50a8\u7684\u662f\u5185\u6838\u6808\u548c\u8be5\u8fdb\u7a0b\u6240\u5bf9\u5e94\u7684\u4e00 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[110],"tags":[184,183,182],"class_list":["post-2198","post","type-post","status-publish","format-standard","hentry","category-linux","tag-task_struct","tag-183","tag-182"],"views":23217,"_links":{"self":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/2198","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=2198"}],"version-history":[{"count":0,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/2198\/revisions"}],"wp:attachment":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/media?parent=2198"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/categories?post=2198"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/tags?post=2198"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}