{"id":220844,"date":"2021-06-28T10:00:22","date_gmt":"2021-06-28T02:00:22","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=220844"},"modified":"2021-06-21T10:51:18","modified_gmt":"2021-06-21T02:51:18","slug":"linux-pcb","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/linux-pcb.html","title":{"rendered":"\u5728Linux\u4e2d\u5982\u4f55\u5bf9\u8fdb\u7a0b\u7684\u63cf\u8ff0"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u8fdb\u7a0b\u662f\u64cd\u4f5c\u7cfb\u7edf\u79cd\u8c03\u5ea6\u7684\u5b9e\u4f53\uff0c\u5bf9\u8fdb\u7a0b\u62e5\u6709\u8d44\u6e90\u7684\u63cf\u8ff0\u79f0\u4e3a\u8fdb\u7a0b\u63a7\u5236\u5757(PCB, Process Contrl Block)\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u901a\u8fc7 task_struct \u63cf\u8ff0\u8fdb\u7a0b<\/strong><\/div>\n

\u5185\u6838\u91cc\uff0c\u901a\u8fc7 task_struct \u7ed3\u6784\u4f53\u6765\u63cf\u8ff0\u4e00\u4e2a\u8fdb\u7a0b\uff0c\u79f0\u4e3a\u8fdb\u7a0b\u63cf\u8ff0\u7b26 (process descriptor)\uff0c\u5b83\u4fdd\u5b58\u7740\u652f\u6491\u4e00\u4e2a\u8fdb\u7a0b\u6b63\u5e38\u8fd0\u884c\u7684\u6240\u6709\u4fe1\u606f\u3002task_struct \u7ed3\u6784\u4f53\u5185\u5bb9\u592a\u591a\uff0c\u8fd9\u91cc\u53ea\u5217\u51fa\u90e8\u5206\u6210\u5458\u53d8\u91cf\uff0c\u611f\u5174\u8da3\u7684\u8bfb\u8005\u53ef\u4ee5\u53bb\u6e90\u7801 include\/linux\/sched.h\u5934\u6587\u4ef6\u67e5\u770b\u3002<\/p>\n

struct\u00a0task_struct\u00a0{\r\n\u00a0\r\n#ifdef\u00a0CONFIG_THREAD_INFO_IN_TASK\r\n\u00a0\u00a0\/*\r\n\u00a0\u00a0\u00a0*\u00a0For\u00a0reasons\u00a0of\u00a0header\u00a0soup\u00a0(see\u00a0current_thread_info()),\u00a0this\r\n\u00a0\u00a0\u00a0*\u00a0must\u00a0be\u00a0the\u00a0first\u00a0element\u00a0of\u00a0task_struct.\r\n\u00a0\u00a0\u00a0*\/\r\n\u00a0\u00a0struct\u00a0thread_info\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0thread_info;\r\n#endif\r\n\u00a0\u00a0volatile\u00a0long\u00a0state;\r\n\u00a0\u00a0void\u00a0*stack;\r\n\u00a0\u00a0......\r\n\u00a0\u00a0struct\u00a0mm_struct\u00a0*mm;\r\n\u00a0\u00a0......\r\n\u00a0\u00a0pid_t\u00a0pid;\r\n\u00a0\u00a0......\r\n\u00a0\u00a0struct\u00a0task_struct\u00a0*parent;\r\n\u00a0\u00a0......\r\n\u00a0\u00a0char\u00a0comm[TASK_COMM_LEN];\r\n\u00a0\u00a0......\r\n\u00a0\u00a0struct\u00a0files_struct\u00a0*files;\r\n\u00a0\u00a0......\r\n\u00a0\u00a0struct\u00a0signal_struct\u00a0*signal;\r\n}<\/pre>\n

task_struct \u4e2d\u7684\u4e3b\u8981\u4fe1\u606f\u5206\u7c7b\uff1a<\/p>\n

1.\u6807\u793a\u7b26\uff1a\u63cf\u8ff0\u672c\u8fdb\u7a0b\u7684\u552f\u4e00\u6807\u8bc6\u7b26 pid\uff0c\u7528\u6765\u533a\u522b\u5176\u4ed6\u8fdb\u7a0b\u3002<\/p>\n

2.\u72b6\u6001\uff1a\u4efb\u52a1\u72b6\u6001\uff0c\u9000\u51fa\u4ee3\u7801\uff0c\u9000\u51fa\u4fe1\u53f7\u7b49<\/p>\n

3.\u4f18\u5148\u7ea7\uff1a\u76f8\u5bf9\u4e8e\u5176\u4ed6\u8fdb\u7a0b\u7684\u4f18\u5148\u7ea7<\/p>\n

4.\u7a0b\u5e8f\u8ba1\u6570\u5668\uff1a\u7a0b\u5e8f\u4e2d\u5373\u5c06\u88ab\u6267\u884c\u7684\u4e0b\u4e00\u6761\u6307\u4ee4\u7684\u5730\u5740<\/p>\n

5.\u5185\u5b58\u6307\u9488\uff1a\u5305\u62ec\u7a0b\u5e8f\u4ee3\u7801\u548c\u8fdb\u7a0b\u76f8\u5173\u6570\u636e\u7684\u6307\u9488\uff0c\u8fd8\u6709\u548c\u5176\u4ed6\u8fdb\u7a0b\u5171\u4eab\u7684\u5185\u5b58\u5757\u7684\u6307\u9488<\/p>\n

6.\u4e0a\u4e0b\u6587\u6570\u636e\uff1a\u8fdb\u7a0b\u6267\u884c\u65f6\u5904\u7406\u5668\u7684\u5bc4\u5b58\u5668\u4e2d\u7684\u6570\u636e<\/p>\n

7.I\/O\u72b6\u6001\u4fe1\u606f\uff1a\u5305\u62ec\u663e\u793a\u7684I\/O\u8bf7\u6c42\uff0c\u5206\u914d\u7684\u8fdb\u7a0bI\/O\u8bbe\u5907\u548c\u8fdb\u7a0b\u4f7f\u7528\u7684\u6587\u4ef6\u5217\u8868<\/p>\n

8.\u8bb0\u8d26\u4fe1\u606f\uff1a\u53ef\u80fd\u5305\u62ec\u5904\u7406\u5668\u65f6\u95f4\u603b\u548c\uff0c\u4f7f\u7528\u7684\u65f6\u949f\u603b\u548c\uff0c\u65f6\u95f4\u9650\u5236\uff0c\u8bb0\u5e10\u53f7\u7b49<\/p>\n

    \n
  • struct thread_info thread_info: \u8fdb\u7a0b\u88ab\u8c03\u5ea6\u6267\u884c\u7684\u4fe1\u606f<\/li>\n
  • volatile long state\uff1a-1\u662f\u4e0d\u8fd0\u884c\u7684\uff0c=0\u662f\u8fd0\u884c\u72b6\u6001\uff0c>0\u662f\u505c\u6b62\u72b6\u6001\u3002\u4e0b\u9762\u662f\u51e0\u4e2a\u6bd4\u8f83\u91cd\u8981\u7684\u8fdb\u7a0b\u72b6\u6001\u4ee5\u53ca\u5b83\u4eec\u4e4b\u95f4\u7684\u8f6c\u6362\u6d41\u7a0b\u3002<\/li>\n<\/ul>\n

    \"\"<\/p>\n

      \n
    1. \u9e3f\u8499\u5b98\u65b9\u6218\u7565\u5408\u4f5c\u5171\u5efa\u2014\u2014HarmonyOS\u6280\u672f\u793e\u533a<\/li>\n
    2. void *stack\uff1a\u6307\u5411\u5185\u6838\u6808\u7684\u6307\u9488\uff0c\u5185\u6838\u901a\u8fc7 dup_task_struct \u4e3a\u6bcf\u4e2a\u8fdb\u7a0b\u90fd\u5206\u914d\u5185\u6838\u6808\u7a7a\u95f4\uff0c\u5e76\u8bb0\u5f55\u5728\u6b64\u3002<\/li>\n
    3. struct mm_struct *mm: \u4e0e\u8fdb\u7a0b\u5730\u5740\u7a7a\u95f4\u76f8\u5173\u7684\u4fe1\u606f\u3002<\/li>\n<\/ol>\n

      \"\"<\/p>\n

        \n
      • pid_t pid: \u8fdb\u7a0b\u6807\u8bc6\u7b26<\/li>\n
      • char comm[TASK_COMM_LEN]: \u8fdb\u7a0b\u7684\u540d\u79f0<\/li>\n
      • struct files_struct *files: \u6253\u5f00\u7684\u6587\u4ef6\u8868<\/li>\n
      • struct signal_struct *signal: \u4fe1\u53f7\u5904\u7406\u76f8\u5173<\/li>\n<\/ul>\n
        task_struct, thread_info \u548c\u5185\u6838\u6808 sp \u7684\u5173\u7cfb<\/strong><\/div>\n

        \u63a5\u7740\u770b\u4e0b thread_info \u7ed3\u6784\uff1a<\/p>\n

        struct\u00a0thread_info\u00a0{\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0unsigned\u00a0long\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0flags;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/*\u00a0low\u00a0level\u00a0flags\u00a0*\/\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0mm_segment_t\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0addr_limit;\u00a0\u00a0\u00a0\u00a0\u00a0\/*\u00a0address\u00a0limit\u00a0*\/\r\n#ifdef\u00a0CONFIG_ARM64_SW_TTBR0_PAN\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0u64\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ttbr0;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/*\u00a0saved\u00a0TTBR0_EL1\u00a0*\/\r\n#endif\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0union\u00a0{\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0u64\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0preempt_count;\u00a0\u00a0\/*\u00a00\u00a0=>\u00a0preemptible,\u00a0<0\u00a0=>\u00a0bug\u00a0*\/\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0struct\u00a0{\r\n#ifdef\u00a0CONFIG_CPU_BIG_ENDIAN\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0u32\u00a0\u00a0\u00a0\u00a0\u00a0need_resched;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0u32\u00a0\u00a0\u00a0\u00a0\u00a0count;\r\n#else\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0u32\u00a0\u00a0\u00a0\u00a0\u00a0count;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0u32\u00a0\u00a0\u00a0\u00a0\u00a0need_resched;\r\n#endif\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\u00a0preempt;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0};\r\n#ifdef\u00a0CONFIG_SHADOW_CALL_STACK\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0void\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0*scs_base;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0void\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0*scs_sp;\r\n#endif\r\n};<\/pre>\n

        \u63a5\u7740\u518d\u6765\u770b\u4e0b\u5185\u6838\u6808\u7684\u5b9a\u4e49:<\/p>\n

        union\u00a0thread_union\u00a0{\r\n#ifndef\u00a0CONFIG_ARCH_TASK_STRUCT_ON_STACK\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0struct\u00a0task_struct\u00a0task;\r\n#endif\r\n#ifndef\u00a0CONFIG_THREAD_INFO_IN_TASK\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0struct\u00a0thread_info\u00a0thread_info;\r\n#endif\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0unsigned\u00a0long\u00a0stack[THREAD_SIZE\/sizeof(long)];\r\n};<\/pre>\n

        \u5f53 CONFIG_THREAD_INFO_IN_TASK \u8fd9\u4e2a\u914d\u7f6e\u6253\u5f00\u7684\u65f6\u5019\uff0c\u5219 thread_union \u7ed3\u6784\u4e2d\u53ea\u5b58\u5728 stask \u6210\u5458\u4e86\u3002<\/p>\n

        \u5185\u6838\u5728\u542f\u52a8\u7684\u65f6\u5019\u4f1a\u5728 head.S \u91cc\u901a\u8fc7 __primary_switched \u6765\u505a\u5185\u6838\u6808\u7684\u521d\u59cb\u5316\uff1a<\/p>\n

        SYM_FUNC_START_LOCAL(__primary_switched)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0adrp\u00a0\u00a0\u00a0\u00a0x4,\u00a0init_thread_union\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0add\u00a0\u00a0\u00a0\u00a0\u00a0sp,\u00a0x4,\u00a0#THREAD_SIZE\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0adr_l\u00a0\u00a0\u00a0x5,\u00a0init_task\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0msr\u00a0\u00a0\u00a0\u00a0\u00a0sp_el0,\u00a0x5\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/\u00a0Save\u00a0thread_info<\/pre>\n

        \u5c06 init_thread_union \u7684\u5730\u5740\u4fdd\u5b58\u5230 x4\uff0c\u7136\u540e\u504f\u79fb THREAD_SIZE \u6808\u5927\u5c0f\uff0c\u7528\u4e8e\u521d\u59cb\u5316 sp\u3002\u5c06 init_task \u8fdb\u7a0b\u63cf\u8ff0\u7b26\u5730\u5740\u8d4b\u503c\u7ed9 x5\uff0c\u5e76\u4fdd\u5b58\u5230 sp_el0\u3002<\/p>\n

        \u4e0b\u9762\u518d\u770b\u4e0b init_thread_union \u548c init_task \u7684\u5b9a\u4e49\uff1a<\/p>\n

        #include\/linux\/sched\/task.h\r\nextern\u00a0union\u00a0thread_union\u00a0init_thread_union;\r\n\u00a0\r\n#init\/init_task.c\r\nstruct\u00a0task_struct\u00a0init_task\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0__aligned(L1_CACHE_BYTES)\r\n=\u00a0{\r\n#ifdef\u00a0CONFIG_THREAD_INFO_IN_TASK\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0.thread_info\u00a0\u00a0\u00a0\u00a0=\u00a0INIT_THREAD_INFO(init_task),\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0.stack_refcount\u00a0=\u00a0REFCOUNT_INIT(1),\r\n#endif\r\n.....\r\n\u00a0};<\/pre>\n

        \u6545\u8fd9\u4e09\u8005\u7684\u5173\u7cfb\u53ef\u4ee5\u901a\u8fc7\u4e0b\u56fe\u63cf\u8ff0\uff1a<\/p>\n

        \"\"<\/p>\n

        \u5982\u4f55\u83b7\u53d6\u5f53\u524d\u8fdb\u7a0b<\/strong><\/div>\n

        \u5185\u6838\u4e2d\u7ecf\u5e38\u901a\u8fc7 current \u5b8f\u6765\u83b7\u5f97\u5f53\u524d\u8fdb\u7a0b\u5bf9\u5e94\u7684 struct task_sturct \u7ed3\u6784\uff0c\u6211\u4eec\u501f\u52a9 current\uff0c\u7ed3\u5408\u4e0a\u9762\u4ecb\u7ecd\u7684\u5185\u5bb9\uff0c\u770b\u4e0b\u5177\u4f53\u7684\u5b9e\u73b0\u3002<\/p>\n

        static\u00a0__always_inline\u00a0struct\u00a0task_struct\u00a0*get_current(void)\r\n{\r\n\u00a0\u00a0\u00a0\u00a0unsigned\u00a0long\u00a0sp_el0;\r\n\u00a0\r\n\u00a0\u00a0\u00a0\u00a0asm\u00a0(\"mrs\u00a0%0,\u00a0sp_el0\"\u00a0:\u00a0\"=r\"\u00a0(sp_el0));\r\n\u00a0\r\n\u00a0\u00a0\u00a0\u00a0return\u00a0(struct\u00a0task_struct\u00a0*)sp_el0;\r\n}\r\n\u00a0\r\n#define\u00a0current\u00a0get_current()<\/pre>\n

        \u4ee3\u7801\u6bd4\u8f83\u7b80\u5355\uff0c\u53ef\u4ee5\u770b\u51fa\u901a\u8fc7\u8bfb\u53d6\u7528\u6237\u7a7a\u95f4\u6808\u6307\u9488\u5bc4\u5b58\u5668 sp_el0 \u7684\u503c\uff0c\u7136\u540e\u5c06\u6b64\u503c\u5f3a\u8f6c\u6210 task_struct \u7ed3\u6784\u5c31\u53ef\u4ee5\u83b7\u5f97\u5f53\u524d\u8fdb\u7a0b\u3002(sp_el0 \u91cc\u5b58\u653e\u7684\u662f init_task\uff0c\u5373 thread_info \u5730\u5740\uff0cthread_info \u53c8\u662f\u5728 task_sturct \u7684\u5f00\u59cb\u5904\uff0c\u4ece\u800c\u627e\u5230\u5f53\u524d\u8fdb\u7a0b\u3002)<\/p>\n","protected":false},"excerpt":{"rendered":"

        \u5185\u6838\u91cc\uff0c\u901a\u8fc7 task_struct \u7ed3\u6784\u4f53\u6765\u63cf\u8ff0\u4e00\u4e2a\u8fdb\u7a0b\uff0c\u79f0\u4e3a\u8fdb\u7a0b\u63cf\u8ff0\u7b26 (process descript […]<\/p>\n","protected":false},"author":323,"featured_media":208904,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-220844","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/220844","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/323"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=220844"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/220844\/revisions"}],"predecessor-version":[{"id":220850,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/220844\/revisions\/220850"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/208904"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=220844"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=220844"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=220844"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}