{"id":17687,"date":"2024-11-18T13:19:48","date_gmt":"2024-11-18T05:19:48","guid":{"rendered":"https:\/\/fwq.ai\/blog\/17687\/"},"modified":"2024-11-18T13:19:48","modified_gmt":"2024-11-18T05:19:48","slug":"%e6%8e%a2%e5%af%bblinux%e5%86%85%e6%a0%b8%e6%ba%90%e4%bb%a3%e7%a0%81%e6%89%80%e5%9c%a8%e6%96%87%e4%bb%b6%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/17687\/","title":{"rendered":"\u63a2\u5bfbLinux\u5185\u6838\u6e90\u4ee3\u7801\u6240\u5728\u6587\u4ef6\u8be6\u89e3"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/465\/014\/171040986730447.jpg\" class=\"aligncenter\" title=\"\u63a2\u5bfbLinux\u5185\u6838\u6e90\u4ee3\u7801\u6240\u5728\u6587\u4ef6\u8be6\u89e3\u63d2\u56fe\" alt=\"\u63a2\u5bfbLinux\u5185\u6838\u6e90\u4ee3\u7801\u6240\u5728\u6587\u4ef6\u8be6\u89e3\u63d2\u56fe\" \/><\/p>\n<p>\u63a2\u5bfbLinux\u5185\u6838\u6e90\u4ee3\u7801\u6240\u5728\u6587\u4ef6\u8be6\u89e3<\/p>\n<p>Linux\u4f5c\u4e3a\u4e00\u6b3e\u5f00\u6e90\u64cd\u4f5c\u7cfb\u7edf\uff0c\u5176\u5185\u6838\u6e90\u4ee3\u7801\u4f5c\u4e3a\u5176\u8bbe\u8ba1\u4e0e\u529f\u80fd\u7684\u6838\u5fc3\u90e8\u5206\uff0c\u88ab\u5e7f\u6cdb\u7814\u7a76\u548c\u4f7f\u7528\u3002Linux\u5185\u6838\u6e90\u4ee3\u7801\u5305\u542b\u4e86\u8bb8\u591a\u6587\u4ef6\uff0c\u6bcf\u4e2a\u6587\u4ef6\u8d1f\u8d23\u4e0d\u540c\u7684\u529f\u80fd\u6a21\u5757\u3002\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u6df1\u5165\u63a2\u5bfbLinux\u5185\u6838\u6e90\u4ee3\u7801\u4e2d\u51e0\u4e2a\u5173\u952e\u6587\u4ef6\u7684\u4f5c\u7528\u548c\u5177\u4f53\u4ee3\u7801\u793a\u4f8b\uff0c\u5e2e\u52a9\u8bfb\u8005\u66f4\u597d\u5730\u7406\u89e3Linux\u5185\u6838\u7684\u8bbe\u8ba1\u548c\u8fd0\u884c\u3002<\/p>\n<ol>\n<li> <strong>kernel\/sched\/core.c<\/strong> &#8211; \u8fd9\u4e2a\u6587\u4ef6\u5b9e\u73b0\u4e86Linux\u5185\u6838\u4e2d\u7684\u8c03\u5ea6\u5668\u3002\u8c03\u5ea6\u5668\u8d1f\u8d23\u51b3\u5b9a\u54ea\u4e2a\u8fdb\u7a0b\u5728\u4ec0\u4e48\u65f6\u5019\u6267\u884c\uff0c\u4ee5\u5b9e\u73b0\u65f6\u95f4\u7247\u8f6e\u8f6c\u3001\u4f18\u5148\u7ea7\u8c03\u5ea6\u7b49\u529f\u80fd\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/li>\n<\/ol>\n<pre>#include &lt;linux&gt;\n#include &lt;linux&gt;\n\nvoid schedule(void)\n{\n    struct task_struct *prev, *next;\n\n    prev = current;\n    \n    next = pick_next_task(); \/\/ \u9009\u62e9\u4e0b\u4e00\u4e2a\u8981\u8fd0\u884c\u7684\u8fdb\u7a0b\n\n    switch_to(next); \/\/ \u5207\u6362\u5230\u4e0b\u4e00\u4e2a\u8fdb\u7a0b\u6267\u884c\n}&lt;\/linux&gt;&lt;\/linux&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li> <strong>kernel\/slab.c<\/strong> &#8211; \u8fd9\u4e2a\u6587\u4ef6\u5b9e\u73b0\u4e86Linux\u5185\u6838\u4e2d\u7684Slab\u5206\u914d\u5668\u3002Slab\u5206\u914d\u5668\u7528\u4e8e\u9ad8\u6548\u5730\u5206\u914d\u548c\u56de\u6536\u5185\u5b58\uff0c\u63d0\u9ad8\u7cfb\u7edf\u7684\u6027\u80fd\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/li>\n<\/ol>\n<pre>#include &lt;linux&gt;\n\nvoid *kmalloc(size_t size, gfp_t flags)\n{\n    struct kmem_cache *cache;\n    void *ptr;\n\n    cache = get_cache_for_size(size); \/\/ \u6839\u636e\u5206\u914d\u5927\u5c0f\u83b7\u53d6\u5408\u9002\u7684\u7f13\u5b58\n    ptr = alloc_slab(cache); \/\/ \u4ece\u7f13\u5b58\u4e2d\u5206\u914d\u5185\u5b58\n\n    return ptr;\n}&lt;\/linux&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li> <strong>kernel\/fs\/namei.c<\/strong> &#8211; \u8fd9\u4e2a\u6587\u4ef6\u5b9e\u73b0\u4e86Linux\u5185\u6838\u4e2d\u7684\u8def\u5f84\u89e3\u6790\u548c\u6587\u4ef6\u7cfb\u7edf\u64cd\u4f5c\u3002\u8def\u5f84\u89e3\u6790\u7528\u4e8e\u5c06\u7528\u6237\u6001\u4f20\u5165\u7684\u6587\u4ef6\u8def\u5f84\u8f6c\u6362\u6210\u5185\u6838\u4e2d\u7684inode\uff0c\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/li>\n<\/ol>\n<pre>#include &lt;linux&gt;\n#include &lt;linux&gt;\n\nint vfs_open(const char *pathname, int flags, int mode)\n{\n    struct path path;\n    struct file *file;\n    \n    int err = kern_path(pathname, LOOKUP_FOLLOW, &amp;amp;path);\n\n    if (!err)\n    {\n        file = filp_open(&amp;amp;path, flags, mode);\n        if (IS_ERR(file))\n        {\n            err = PTR_ERR(file);\n        }\n    }\n    \n    return err;\n}&lt;\/linux&gt;&lt;\/linux&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u901a\u8fc7\u5bf9\u4ee5\u4e0a\u51e0\u4e2a\u5173\u952e\u6587\u4ef6\u7684\u4ee3\u7801\u793a\u4f8b\u8fdb\u884c\u5206\u6790\uff0c\u8bfb\u8005\u53ef\u4ee5\u66f4\u52a0\u6df1\u5165\u5730\u4e86\u89e3Linux\u5185\u6838\u6e90\u4ee3\u7801\u7684\u7ed3\u6784\u548c\u529f\u80fd\u3002\u9664\u4e86\u8fd9\u4e9b\u6587\u4ef6\u4e4b\u5916\uff0cLinux\u5185\u6838\u8fd8\u5305\u542b\u4e86\u8bb8\u591a\u5176\u4ed6\u91cd\u8981\u7684\u6587\u4ef6\uff0c\u6db5\u76d6\u4e86\u8fdb\u7a0b\u7ba1\u7406\u3001\u5185\u5b58\u7ba1\u7406\u3001\u6587\u4ef6\u7cfb\u7edf\u7b49\u65b9\u9762\u7684\u5b9e\u73b0\u3002\u8fdb\u4e00\u6b65\u7814\u7a76\u548c\u63a2\u7d22Linux\u5185\u6838\u6e90\u4ee3\u7801\uff0c\u6709\u52a9\u4e8e\u7406\u89e3\u64cd\u4f5c\u7cfb\u7edf\u7684\u8bbe\u8ba1\u539f\u7406\u548c\u5185\u90e8\u5de5\u4f5c\u673a\u5236\uff0c\u63d0\u5347\u7cfb\u7edf\u7f16\u7a0b\u7684\u80fd\u529b\u548c\u6c34\u5e73\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u63a2\u5bfbLinux\u5185\u6838\u6e90\u4ee3\u7801\u6240\u5728\u6587\u4ef6\u8be6\u89e3\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u5bfbLinux\u5185\u6838\u6e90\u4ee3\u7801\u6240\u5728\u6587\u4ef6\u8be6\u89e3 Linux\u4f5c\u4e3a\u4e00\u6b3e\u5f00\u6e90\u64cd\u4f5c\u7cfb\u7edf\uff0c\u5176\u5185\u6838\u6e90\u4ee3\u7801\u4f5c\u4e3a\u5176\u8bbe\u8ba1\u4e0e\u529f\u80fd\u7684\u6838\u5fc3\u90e8\u5206\uff0c\u88ab\u5e7f\u6cdb\u7814\u7a76\u548c\u4f7f\u7528\u3002Linux\u5185\u6838\u6e90\u4ee3\u7801\u5305\u542b\u4e86\u8bb8\u591a\u6587\u4ef6\uff0c\u6bcf\u4e2a\u6587\u4ef6\u8d1f\u8d23\u4e0d\u540c\u7684\u529f\u80fd\u6a21\u5757\u3002\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u6df1\u5165\u63a2\u5bfbLinux\u5185\u6838\u6e90\u4ee3\u7801\u4e2d\u51e0\u4e2a\u5173\u952e\u6587\u4ef6\u7684\u4f5c\u7528\u548c\u5177\u4f53\u4ee3\u7801\u793a\u4f8b\uff0c\u5e2e\u52a9\u8bfb\u8005\u66f4\u597d\u5730\u7406\u89e3Linux\u5185\u6838\u7684\u8bbe\u8ba1\u548c\u8fd0\u884c\u3002 kernel\/sched\/core.c &#8211; \u8fd9\u4e2a\u6587\u4ef6\u5b9e\u73b0\u4e86Linux\u5185\u6838\u4e2d\u7684\u8c03\u5ea6\u5668\u3002\u8c03\u5ea6\u5668\u8d1f\u8d23\u51b3\u5b9a\u54ea\u4e2a\u8fdb\u7a0b\u5728\u4ec0\u4e48\u65f6\u5019\u6267\u884c\uff0c\u4ee5\u5b9e\u73b0\u65f6\u95f4\u7247\u8f6e\u8f6c\u3001\u4f18\u5148\u7ea7\u8c03\u5ea6\u7b49\u529f\u80fd\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4ee3\u7801\u793a\u4f8b\uff1a #include &lt;linux&gt; #include &lt;linux&gt; void schedule(void) { struct task_struct *prev, *next; prev = current; next = pick_next_task(); \/\/ \u9009\u62e9\u4e0b\u4e00\u4e2a\u8981\u8fd0\u884c\u7684\u8fdb\u7a0b switch_to(next); \/\/ \u5207\u6362\u5230\u4e0b\u4e00\u4e2a\u8fdb\u7a0b\u6267\u884c }&lt;\/linux&gt;&lt;\/linux&gt; \u767b\u5f55\u540e\u590d\u5236 kernel\/slab.c &#8211; \u8fd9\u4e2a\u6587\u4ef6\u5b9e\u73b0\u4e86Linux\u5185\u6838\u4e2d\u7684Slab\u5206\u914d\u5668\u3002Slab\u5206\u914d\u5668\u7528\u4e8e\u9ad8\u6548\u5730\u5206\u914d\u548c\u56de\u6536\u5185\u5b58\uff0c\u63d0\u9ad8\u7cfb\u7edf\u7684\u6027\u80fd\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4ee3\u7801\u793a\u4f8b\uff1a #include &lt;linux&gt; void *kmalloc(size_t size, gfp_t flags) { struct kmem_cache *cache; void *ptr; cache = get_cache_for_size(size); \/\/ \u6839\u636e\u5206\u914d\u5927\u5c0f\u83b7\u53d6\u5408\u9002\u7684\u7f13\u5b58 ptr = alloc_slab(cache); \/\/ \u4ece\u7f13\u5b58\u4e2d\u5206\u914d\u5185\u5b58 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-17687","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/17687","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=17687"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/17687\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=17687"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=17687"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=17687"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}