{"id":1292,"date":"2026-01-10T10:22:58","date_gmt":"2026-01-10T02:22:58","guid":{"rendered":"https:\/\/www.52runoob.com\/?p=1292"},"modified":"2026-01-10T10:22:58","modified_gmt":"2026-01-10T02:22:58","slug":"linux%e5%86%85%e6%a0%b8%e5%8f%8c%e5%90%91%e9%93%be%e8%a1%a8%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/www.52runoob.com\/index.php\/2026\/01\/10\/linux%e5%86%85%e6%a0%b8%e5%8f%8c%e5%90%91%e9%93%be%e8%a1%a8%e8%af%a6%e8%a7%a3\/","title":{"rendered":"linux\u5185\u6838\u53cc\u5411\u94fe\u8868\u8be6\u89e3"},"content":{"rendered":"\n<p>\u4e0b\u9762\u6211\u7528<strong>\u5185\u6838\u6e90\u7801\u7ea7 + \u56fe\u89e3\u601d\u7ef4 + \u9762\u8bd5\/\u5b9e\u6218\u5bfc\u5411<\/strong>\uff0c\u7cfb\u7edf\u5730\u7ed9\u4f60\u8bb2\u6e05\u695a<br>\ud83d\udc49 <strong>Linux \u5185\u6838\u53cc\u5411\u94fe\u8868\uff08<code>list_head<\/code>\uff09<\/strong>\u3002<br>\u8fd9\u662f<strong>\u5185\u6838\u3001\u9a71\u52a8\u3001\u64cd\u4f5c\u7cfb\u7edf\u9762\u8bd5\u5fc5\u8003\u70b9<\/strong>\u4e4b\u4e00\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u4e00\u3001\u4e3a\u4ec0\u4e48 Linux \u5185\u6838\u8981\u81ea\u5df1\u5b9e\u73b0\u53cc\u5411\u94fe\u8868\uff1f<\/h1>\n\n\n\n<p>\u5185\u6838<strong>\u4e0d\u80fd\u4f7f\u7528 C \u6807\u51c6\u5e93<\/strong>\uff0c\u5fc5\u987b\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\ud83d\udeab \u4e0d\u80fd <code>malloc\/free<\/code><\/li>\n\n\n\n<li>\ud83d\udeab \u4e0d\u80fd\u7528 STL \/ glib<\/li>\n\n\n\n<li>\u2705 \u9ad8\u6027\u80fd<\/li>\n\n\n\n<li>\u2705 \u5f3a\u7c7b\u578b\u63a7\u5236<\/li>\n\n\n\n<li>\u2705 \u901a\u7528\u3001\u53ef\u590d\u7528<\/li>\n<\/ul>\n\n\n\n<p>\ud83d\udc49 \u6240\u4ee5\u5185\u6838\u8bbe\u8ba1\u4e86 <strong>\u4fb5\u5165\u5f0f\u53cc\u5411\u94fe\u8868\uff08intrusive list\uff09<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u4e8c\u3001\u6838\u5fc3\u6570\u636e\u7ed3\u6784\uff1a<code>struct list_head<\/code><\/h1>\n\n\n\n<p>\ud83d\udcc1 \u6e90\u7801\u4f4d\u7f6e\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ninclude\/linux\/list.h\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">1\ufe0f\u20e3 \u5b9a\u4e49<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nstruct list_head {\n    struct list_head *next, *prev;\n};\n\n<\/pre><\/div>\n\n\n<p>\u26a0\ufe0f \u6ce8\u610f\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u94fe\u8868\u8282\u70b9\u4e0d\u4fdd\u5b58\u6570\u636e<\/strong><\/li>\n\n\n\n<li>\u6570\u636e\u7ed3\u6784\u4e2d<strong>\u5d4c\u5165 list_head<\/strong><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u4e09\u3001\u4fb5\u5165\u5f0f\u94fe\u8868\uff08\u91cd\u70b9\u7406\u89e3\uff09<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">1\ufe0f\u20e3 \u666e\u901a\u94fe\u8868\uff08\u7528\u6237\u6001\uff09<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nstruct node {\n    int data;\n    struct node *next;\n};\n\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">2\ufe0f\u20e3 \u5185\u6838\u4fb5\u5165\u5f0f\u94fe\u8868<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nstruct task_struct {\n    int pid;\n    struct list_head tasks;\n};\n\n<\/pre><\/div>\n\n\n<p>\ud83d\udccc <strong>\u94fe\u8868\u6307\u9488\u5d4c\u5165\u5230\u4e1a\u52a1\u7ed3\u6784\u4e2d<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u56db\u3001\u94fe\u8868\u521d\u59cb\u5316\uff08\u975e\u5e38\u91cd\u8981\uff09<\/h1>\n\n\n\n<h3 class=\"wp-block-heading\">1\ufe0f\u20e3 \u9759\u6001\u521d\u59cb\u5316<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nLIST_HEAD(my_list);\n\n<\/pre><\/div>\n\n\n<p>\u7b49\u4ef7\u4e8e\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nstruct list_head my_list = { &amp;amp;my_list, &amp;amp;my_list };\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2\ufe0f\u20e3 \u52a8\u6001\u521d\u59cb\u5316<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nstruct list_head my_list;\nINIT_LIST_HEAD(&amp;amp;my_list);\n\n<\/pre><\/div>\n\n\n<p>\ud83d\udccc \u7a7a\u94fe\u8868\uff1a<br><code>head.next == head.prev == &amp;head<\/code><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u4e94\u3001\u8282\u70b9\u63d2\u5165\u539f\u7406\uff08\u6e90\u7801\u7ea7\uff09<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">1\ufe0f\u20e3 \u5185\u90e8\u6838\u5fc3\u51fd\u6570<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nstatic inline void __list_add(\n    struct list_head *new,\n    struct list_head *prev,\n    struct list_head *next)\n{\n    next-&gt;prev = new;\n    new-&gt;next  = next;\n    new-&gt;prev  = prev;\n    prev-&gt;next = new;\n}\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">2\ufe0f\u20e3 \u5934\u63d2 &amp; \u5c3e\u63d2<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nlist_add(new, head);      \/\/ \u5934\u63d2\nlist_add_tail(new, head); \/\/ \u5c3e\u63d2\n\n<\/pre><\/div>\n\n\n<p>\ud83d\udd0d \u672c\u8d28\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nlist_add(new, head)        =&gt; __list_add(new, head, head-&gt;next)\nlist_add_tail(new, head)  =&gt; __list_add(new, head-&gt;prev, head)\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u516d\u3001\u8282\u70b9\u5220\u9664\uff08\u6781\u6613\u8003\uff09<\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nstatic inline void __list_del(\n    struct list_head * prev,\n    struct list_head * next)\n{\n    next-&gt;prev = prev;\n    prev-&gt;next = next;\n}\n\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nlist_del(entry);\n\n<\/pre><\/div>\n\n\n<p>\u26a0\ufe0f \u6ce8\u610f\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>list_del<\/code> <strong>\u4e0d\u91ca\u653e\u5185\u5b58<\/strong><\/li>\n\n\n\n<li>\u53ea\u662f\u6458\u94fe<\/li>\n<\/ul>\n\n\n\n<p>\ud83d\udc49 \u9a71\u52a8\u4e2d\u5fc5\u987b\u914d\u5408 <code>kfree()<\/code><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u4e03\u3001container_of\uff08\u7075\u9b42\u7ea7\u5b8f\uff09<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">1\ufe0f\u20e3 \u5b8f\u5b9a\u4e49<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n#define container_of(ptr, type, member) ({          \\\n    const typeof(((type *)0)-&gt;member) *__mptr = (ptr); \\\n    (type *)((char *)__mptr - offsetof(type, member)); \\\n})\n\n<\/pre><\/div>\n\n\n<p>\ud83d\udccc <strong>\u4f5c\u7528\uff1a<\/strong><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u901a\u8fc7 <code>list_head*<\/code> \u627e\u56de\u5916\u5c42\u7ed3\u6784\u4f53\u5730\u5740<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">2\ufe0f\u20e3 \u4f7f\u7528\u793a\u4f8b<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nstruct task_struct *task;\ntask = list_entry(pos, struct task_struct, tasks);\n\n<\/pre><\/div>\n\n\n<p>\u672c\u8d28\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ncontainer_of(pos, struct task_struct, tasks);\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u516b\u3001\u904d\u5386\u94fe\u8868\uff08\u9762\u8bd5\u9ad8\u9891\uff09<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">1\ufe0f\u20e3 \u666e\u901a\u904d\u5386<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nstruct list_head *pos;\n\nlist_for_each(pos, &amp;amp;my_list) {\n    struct node *n = list_entry(pos, struct node, list);\n}\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">2\ufe0f\u20e3 \u5b89\u5168\u904d\u5386\uff08\u5220\u9664\u65f6\u5fc5\u987b\u7528\uff09<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nstruct list_head *pos, *n;\n\nlist_for_each_safe(pos, n, &amp;amp;my_list) {\n    list_del(pos);\n}\n\n<\/pre><\/div>\n\n\n<p>\ud83d\udccc <code>safe<\/code> = \u5148\u4fdd\u5b58 next<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">3\ufe0f\u20e3 \u76f4\u63a5\u904d\u5386\u7ed3\u6784\u4f53\uff08\u63a8\u8350\uff09<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nstruct node *p;\n\nlist_for_each_entry(p, &amp;amp;my_list, list) {\n    printk(&quot;%d\\n&quot;, p-&gt;data);\n}\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u4e5d\u3001\u5b8c\u6574\u793a\u4f8b\uff08\u9a71\u52a8 \/ \u5185\u6838\u98ce\u683c\uff09<\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nstruct my_node {\n    int data;\n    struct list_head list;\n};\n\nLIST_HEAD(my_list);\n\nstruct my_node *n1 = kmalloc(sizeof(*n1), GFP_KERNEL);\nn1-&gt;data = 10;\nINIT_LIST_HEAD(&amp;amp;n1-&gt;list);\n\nlist_add(&amp;amp;n1-&gt;list, &amp;amp;my_list);\n\nstruct my_node *pos;\nlist_for_each_entry(pos, &amp;amp;my_list, list) {\n    printk(&quot;%d\\n&quot;, pos-&gt;data);\n}\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u5341\u3001Linux \u94fe\u8868\u7684\u8bbe\u8ba1\u4f18\u52bf\uff08\u5fc5\u7b54\uff09<\/h1>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u4f18\u70b9<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td>O(1) \u63d2\u5220<\/td><td>\u53cc\u5411<\/td><\/tr><tr><td>\u96f6\u989d\u5916\u5f00\u9500<\/td><td>\u65e0\u8282\u70b9\u7ed3\u6784<\/td><\/tr><tr><td>\u9ad8\u590d\u7528<\/td><td>\u4efb\u610f\u7ed3\u6784<\/td><\/tr><tr><td>cache \u53cb\u597d<\/td><td>\u5d4c\u5165\u5f0f<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u5341\u4e00\u3001\u5e38\u89c1\u5751\uff08\u9762\u8bd5\u4f1a\u95ee\uff09<\/h1>\n\n\n\n<h3 class=\"wp-block-heading\">\u274c \u5fd8\u8bb0\u521d\u59cb\u5316<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nlist_add(new, head); \/\/ \u91ce\u6307\u9488\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">\u274c \u5220\u9664\u65f6\u666e\u901a\u904d\u5386<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nlist_for_each(pos, head) {\n    list_del(pos); \/\/ \u5d29\n}\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">\u274c \u5220\u9664\u540e\u7ee7\u7eed\u4f7f\u7528<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nlist_del(entry);\n\/\/ entry-&gt;next \u53ef\u80fd\u5df2\u5931\u6548\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u5341\u4e8c\u3001\u4e00\u53e5\u8bdd\u7ec8\u6781\u603b\u7ed3\uff08\u80cc\uff09<\/h1>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>Linux \u5185\u6838\u94fe\u8868\u662f\u4fb5\u5165\u5f0f\u53cc\u5411\u5faa\u73af\u94fe\u8868\uff0c<br>\u7528 <code>list_head<\/code> \u7ec4\u7ec7\u7ed3\u6784\uff0c<br>\u7528 <code>container_of<\/code> \u627e\u56de\u6570\u636e\uff0c<br>\u7528\u5b8f\u4fdd\u8bc1 O(1) \u4e0e\u9ad8\u6027\u80fd\u3002<\/strong><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0b\u9762\u6211\u7528\u5185\u6838\u6e90\u7801\u7ea7 + \u56fe\u89e3\u601d\u7ef4 + \u9762\u8bd5\/\u5b9e\u6218\u5bfc\u5411\uff0c\u7cfb\u7edf\u5730\u7ed9\u4f60\u8bb2\u6e05\u695a\ud83d\udc49 Lin&#8230; <a class=\"more-link\" href=\"https:\/\/www.52runoob.com\/index.php\/2026\/01\/10\/linux%e5%86%85%e6%a0%b8%e5%8f%8c%e5%90%91%e9%93%be%e8%a1%a8%e8%af%a6%e8%a7%a3\/\">Continue Reading &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[],"class_list":["post-1292","post","type-post","status-publish","format-standard","hentry","category-linux"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/1292","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/comments?post=1292"}],"version-history":[{"count":1,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/1292\/revisions"}],"predecessor-version":[{"id":1293,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/1292\/revisions\/1293"}],"wp:attachment":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/media?parent=1292"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/categories?post=1292"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/tags?post=1292"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}