{"id":3052,"date":"2011-05-24T10:00:38","date_gmt":"2011-05-24T02:00:38","guid":{"rendered":"http:\/\/edsionte.com\/techblog\/?p=3052"},"modified":"2011-05-25T00:18:46","modified_gmt":"2011-05-24T16:18:46","slug":"%e4%bd%bf%e7%94%a8list_head%e5%bb%ba%e7%ab%8b%e5%8f%8c%e8%81%94%e8%a1%a8","status":"publish","type":"post","link":"http:\/\/edsionte.com\/techblog\/archives\/3052","title":{"rendered":"\u4f7f\u7528list_head\u5efa\u7acb\u53cc\u8054\u8868"},"content":{"rendered":"<p>\u5185\u6838\u4e2d\u4e0e\u94fe\u8868\u6709\u5173\u7684\u64cd\u4f5c\u90fd\u96c6\u4e2d\u5728list.h\u6587\u4ef6\u4e2d\uff0c\u8be5\u6587\u4ef6\u4e0d\u4ec5\u5b9a\u4e49\u4e86\u94fe\u8868\u6570\u636e\u7ed3\u6784\uff0c\u800c\u4e14\u5305\u542b\u5927\u91cf\u5bf9\u94fe\u8868\u64cd\u4f5c\u7684\u51fd\u6570\u3002<\/p>\n<p>\u4e0b\u9762\u901a\u8fc7\u4e00\u4e2a\u5185\u6838\u6a21\u5757\u7b80\u5355\u8bf4\u660e\u5185\u6838\u53cc\u94fe\u8868\u7684\u7528\u6cd5\u3002\u8be5\u5185\u6838\u6a21\u5757\u7684\u52a0\u8f7d\u51fd\u6570\u9996\u5148\u5efa\u7acb\u4e86\u4e00\u4e2a\u5177\u6709N\u4e2a\u8282\u70b9\u7684\u53cc\u94fe\u8868\uff0c\u7136\u540e\u518d\u904d\u5386\u8be5\u94fe\u8868\uff1b\u5728\u8be5\u5185\u6838\u6a21\u5757\u5378\u8f7d\u51fd\u6570\u4e2d\uff0c\u4f9d\u6b21\u5c06\u6bcf\u4e2a\u8282\u70b9\u4ece\u53cc\u94fe\u8868\u4e2d\u5220\u9664\uff0c\u5e76\u91ca\u653e\u6bcf\u4e2a\u8282\u70b9\u6240\u5360\u7528\u7684\u5185\u6838\u7a7a\u95f4\u7684\u5185\u5b58\u3002<\/p>\n<p>\u5728\u5177\u4f53\u8bf4\u660e\u8be5\u7a0b\u5e8f\u4e4b\u524d\uff0c\u6709\u5fc5\u8981\u641e\u6e05\u695a\u4e24\u4e2a\u6982\u5ff5\uff1a\u94fe\u8868\u4e2d\u7684\u6570\u636e\u8282\u70b9\u548c\u94fe\u8868\u8282\u70b9\u3002\u5185\u6838\u5728list.h\u4e2d\u5b9a\u4e49\u7684\u94fe\u8868\u8282\u70b9list_head\u79f0\u4e4b\u4e3a\u94fe\u8868\u8282\u70b9\u3002\u8be5\u7ed3\u6784\u9664\u4e86\u6307\u5411\u524d\u540e\u8282\u70b9\u7684\u6307\u9488\u5916\uff0c\u5e76\u4e0d\u5305\u542b\u4efb\u4f55\u6570\u636e\u53d8\u91cf\u3002\u56e0\u6b64\uff0c\u5728\u5b9e\u9645\u4f7f\u7528\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u5fc5\u987b\u5c06\u6240\u9700\u6570\u636e\u548c\u94fe\u8868\u8282\u70b9\u5c01\u88c5\u5728\u4e00\u4e2a\u6570\u636e\u7ed3\u6784\u4e2d\uff0c\u8fd9\u4e2a\u65b0\u7684\u6570\u636e\u7ed3\u6784\u5c31\u662f\u6570\u636e\u8282\u70b9\u3002\u6bd4\u5982\uff0c\u5728\u793a\u4f8b\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u7684\u6570\u636e\u8282\u70b9\u9664\u4e86\u5305\u542b\u94fe\u8868\u8282\u70b9\u5916\uff0c\u8fd8\u5305\u542b\u4e00\u4e2a\u6570\u636e\u6210\u5458num\u3002<\/p>\n<pre class=\"brush:c\">#define N 10\r\n\/\/\u94fe\u8868\u8282\u70b9\r\nstruct numlist {\r\n\tint num;\/\/\u6570\u636e\r\n\tstruct list_head list;\/\/\u6307\u5411\u53cc\u8054\u8868\u524d\u540e\u8282\u70b9\u7684\u6307\u9488\r\n};\r\nstruct numlist numhead;\/\/\u5934\u8282\u70b9<\/pre>\n<p>\u5728\u4f7f\u7528\u53cc\u8054\u8868\u7684\u64cd\u4f5c\u51fd\u6570\u65f6\u4e5f\u5e94\u8be5\u6ce8\u610f\u6570\u636e\u8282\u70b9\u548c\u94fe\u8868\u8282\u70b9\u4e4b\u95f4\u7684\u5dee\u5f02\u3002\u6240\u6709\u4e0e\u53cc\u8054\u8868\u6709\u5173\u7684\u64cd\u4f5c\u51fd\u6570\u90fd\u662f\u5bf9\u94fe\u8868\u8282\u70b9\u7684\u64cd\u4f5c\uff0c\u4e5f\u5c31\u662f\u8bf4\u6211\u4eec\u5e94\u8be5\u5bf9\u8fd9\u4e9b\u51fd\u6570\u4f20\u5165\u94fe\u8868\u8282\u70b9\uff08\u6216\u6307\u9488\uff09\u800c\u4e0d\u662f\u6570\u636e\u8282\u70b9\u3002<\/p>\n<p>\u5728\u52a0\u8f7d\u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7INIT_LIST_HEAD\u5b8f\u521d\u59cb\u5316\u94fe\u8868\u7684\u5934\u6307\u9488\u3002\u901a\u8fc7\u67e5\u770b\u8be5\u5b8f\u7684\u5b9a\u4e49\u53ef\u77e5\uff0c\u5fc5\u987b\u5411\u5176\u4f20\u5165\u4e00\u4e2alist_head\u7ed3\u6784\u7684\u6307\u9488\u53d8\u91cf\uff0c\u56e0\u6b64\u6211\u4eec\u5c06&amp;numhead.list\u4f20\u5165\u5176\u4e2d\u3002<\/p>\n<p>\u63a5\u7740\uff0c\u901a\u8fc7\u5faa\u73af\u5206\u522b\u521b\u5efaN\u4e2a\u6570\u636e\u8282\u70b9\uff0c\u5e76\u5728\u521b\u5efa\u5b8c\u6bcf\u4e2a\u8282\u70b9\u540e\uff0c\u5c06\u5176\u52a0\u5165\u6574\u4e2a\u53cc\u94fe\u8868\u4e2d\u3002\u5c06\u8282\u70b9\u52a0\u5165\u53cc\u94fe\u8868\u7684\u8fc7\u7a0b\u662f\u901a\u8fc7list_add_tail\u51fd\u6570\u5b8c\u6210\u7684\uff0c\u8be5\u51fd\u6570\u5c06\u65b0\u8282\u70b9\u52a0\u5165\u5230\u53cc\u94fe\u8868\u7684\u672b\u5c3e\u3002<\/p>\n<pre class=\"brush:c\">static int __init doublelist_init(void)\r\n{\r\n\t\/\/\u521d\u59cb\u5316\u5934\u8282\u70b9\r\n\tstruct numlist *listnode;\/\/\u6bcf\u6b21\u7533\u8bf7\u94fe\u8868\u8282\u70b9\u65f6\u6240\u7528\u7684\u6307\u9488\r\n\tstruct list_head *pos;\r\n\tstruct numlist *p;\r\n\tint i;\r\n\r\n\tprintk(\"doublelist is starting...\\n\");\r\n\tINIT_LIST_HEAD(&amp;numhead.list);\r\n\r\n\t\/\/\u5efa\u7acbN\u4e2a\u8282\u70b9\uff0c\u4f9d\u6b21\u52a0\u5165\u5230\u94fe\u8868\u5f53\u4e2d\r\n\t\tfor (i = 0; i < N; i++) {\r\n\t\tlistnode = (struct numlist *)kmalloc(sizeof(struct numlist), GFP_KERNEL);\r\n\t\tlistnode->num = i+1;\r\n\t\tlist_add_tail(&listnode->list, &numhead.list);\r\n\t\tprintk(\"Node %d has added to the doublelist...\\n\", i+1);\r\n\t}\r\n<\/pre>\n<p>\u52a0\u8f7d\u51fd\u6570\u521b\u5efa\u5b8c\u53cc\u94fe\u8868\u540e\uff0c\u7d27\u63a5\u7740\u53c8\u5bf9\u8be5\u53cc\u94fe\u8868\u8fdb\u884c\u4e86\u904d\u5386\u64cd\u4f5c\u3002\u6211\u4eec\u5728\u4e0a\u9762\u8bf4\u8fc7\u53cc\u94fe\u8868\u7684\u64cd\u4f5c\u51fd\u6570\u662f\u5bf9\u94fe\u8868\u8282\u70b9list_head\u8fdb\u884c\u64cd\u4f5c\u7684\uff0c\u56e0\u6b64\u904d\u5386\u5b8flist_for_each\u4e5f\u662f\u5bf9\u94fe\u8868\u8282\u70b9\u7684\u904d\u5386\u3002\u5982\u679c\u6211\u4eec\u9700\u8981\u5f97\u5230\u6570\u636e\u8282\u70b9\u4e2d\u6bcf\u4e2a\u6210\u5458\u7684\u503c\uff0c\u90a3\u4e48\u5c31\u5fc5\u987b\u83b7\u5f97\u5f53\u524d\u6570\u636e\u8282\u70b9\u7684\u6307\u9488\uff0c\u4e5f\u5c31\u662f\u8be5\u8282\u70b9\u7684\u9996\u5730\u5740\u3002\u56e0\u6b64list_entry\u5b8f\u7684\u4f5c\u7528\u5c31\u662f\u901a\u8fc7\u5f53\u524d\u6b63\u5728\u904d\u5386\u7684\u94fe\u8868\u8282\u70b9\u7684\u6307\u9488pos\u83b7\u5f97\u5176\u6240\u5c5e\u6570\u636e\u8282\u70b9\u7684\u6307\u9488p\u3002<\/p>\n<p>\u8fd9\u91cc\u7279\u522b\u8bf4\u660e\u4e00\u4e0blist_entry\u5b8f\u7684\u53c2\u6570\uff0cpos\u6240\u6307\u5411\u7684list_head\u7ed3\u6784\u5728numlist\u7ed3\u6784\u4e2d\u7684\u5b57\u6bb5\u79f0\u4e3alist\u3002<\/p>\n<pre class=\"brush:c\">\t\/\/\u904d\u5386\u94fe\u8868\r\n\ti = 1;\r\n\tlist_for_each(pos, &amp;numhead.list) {\r\n\t\tp = list_entry(pos, struct numlist, list);\r\n\t\tprintk(\"Node %d's data:%d\\n\", i, p-&gt;num);\r\n\t\ti++;\r\n\t}\r\n\r\n\treturn 0;\r\n}<\/pre>\n<p>\u5728\u5378\u8f7d\u51fd\u6570\u4e2d\u5c06\u4f1a\u4f9d\u6b21\u5220\u9664\u94fe\u8868\u4e2d\u7684\u8282\u70b9\u3002list_for_each_safe\u904d\u5386\u5b8f\u662f\u4e13\u95e8\u4e3a\u5220\u9664\u94fe\u8868\u7ed3\u70b9\u800c\u8bbe\u8ba1\u7684\uff0c\u5b83\u5728\u904d\u5386\u5f53\u524d\u8282\u70b9\u5bf9\u540c\u65f6\u4fdd\u5b58\u4e0b\u4e00\u4e2a\u8282\u70b9\u7684\u5730\u5740\u3002\u56e0\u6b64\u5728\u6bcf\u6b21\u904d\u5386\u7684\u65f6\u5019\u4e00\u65b9\u9762\u4f7f\u7528list_del\u5c06\u5f53\u524d\u94fe\u8868\u8282\u70b9\u4ece\u6574\u4e2a\u94fe\u8868\u4e2d\u5220\u9664\uff0c\u4e00\u65b9\u9762\u4f7f\u7528kfree\u51fd\u6570\u91ca\u653e\u8be5\u6570\u636e\u8282\u70b9\u6240\u5360\u7684\u5185\u5b58\u7a7a\u95f4\u3002<\/p>\n<pre class=\"brush:c\">static void __exit doublelist_exit(void)\r\n{\r\n\tstruct list_head *pos, *n;\r\n\tstruct numlist *p;\r\n\tint i;\r\n\r\n\t\/\/\u4f9d\u6b21\u5220\u9664N\u4e2a\u8282\u70b9\r\n\ti = 1;\r\n\tlist_for_each_safe(pos, n, &amp;numhead.list) {\r\n\t\tlist_del(pos);\/\/\u4ece\u53cc\u8054\u8868\u4e2d\u5220\u9664\u5f53\u524d\u8282\u70b9\r\n\t\tp = list_entry(pos, struct numlist, list);\/\/\u5f97\u5230\u5f53\u524d\u6570\u636e\u8282\u70b9\u7684\u9996\u5730\u5740\uff0c\u5373\u6307\u9488\r\n\t\tkfree(p);\/\/\u91ca\u653e\u8be5\u6570\u636e\u8282\u70b9\u6240\u5360\u7a7a\u95f4\r\n\t\tprintk(\"Node %d has removed from the doublelist...\\n\", i++);\r\n\t}\r\n\tprintk(\"doublelist is exiting..\\n\");\r\n}<\/pre>\n<p>\u8be5\u5b9e\u4f8b\u7a0b\u5e8f\u7684\u5b8c\u6574\u4ee3\u7801\u53ef\u5728\u6b64\u4e0b\u8f7d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5185\u6838\u4e2d\u4e0e\u94fe\u8868\u6709\u5173\u7684\u64cd\u4f5c\u90fd\u96c6\u4e2d\u5728list.h\u6587\u4ef6\u4e2d\uff0c\u8be5\u6587\u4ef6\u4e0d\u4ec5\u5b9a\u4e49\u4e86\u94fe\u8868\u6570\u636e\u7ed3\u6784\uff0c\u800c\u4e14\u5305\u542b\u5927\u91cf\u5bf9\u94fe\u8868\u64cd\u4f5c\u7684\u51fd\u6570\u3002 [&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":[228,230,229],"class_list":["post-3052","post","type-post","status-publish","format-standard","hentry","category-linux","tag-list_head","tag-230","tag-229"],"views":8518,"_links":{"self":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/3052","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=3052"}],"version-history":[{"count":0,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/3052\/revisions"}],"wp:attachment":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/media?parent=3052"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/categories?post=3052"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/tags?post=3052"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}