{"id":16500,"date":"2024-11-18T16:18:33","date_gmt":"2024-11-18T08:18:33","guid":{"rendered":"https:\/\/fwq.ai\/blog\/16500\/"},"modified":"2024-11-18T16:18:33","modified_gmt":"2024-11-18T08:18:33","slug":"linux%e5%86%85%e6%a0%b8%e4%b8%ad%e5%b8%b8%e7%94%a8%e7%9a%84%e6%95%b0%e6%8d%ae%e7%bb%93%e6%9e%84%e5%92%8c%e7%ae%97%e6%b3%95","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/16500\/","title":{"rendered":"Linux\u5185\u6838\u4e2d\u5e38\u7528\u7684\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/275\/014\/b21976d5e40133d892f62c70748604f7-0.png\" class=\"aligncenter\" title=\"Linux\u5185\u6838\u4e2d\u5e38\u7528\u7684\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\u63d2\u56fe\" alt=\"Linux\u5185\u6838\u4e2d\u5e38\u7528\u7684\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\u63d2\u56fe\" \/>  <\/p>\n<p style=\"max-width:90%\">Linux\u5185\u6838\u4ee3\u7801\u4e2d\u5e7f\u6cdb\u4f7f\u7528\u4e86\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u4e24\u4e2a\u662f\u94fe\u8868\u548c\u7ea2\u9ed1\u6811\u3002<\/p>\n<h2> <span><\/span><span style=\"padding: 2px 10px 1px;border-top-right-radius: 3px;border-top-left-radius: 3px;margin-right: 3px\">\u94fe\u8868<\/span><span style=\"vertical-align: bottom;border-bottom: 36px solid #efebe9;border-right: 20px solid transparent\"> <\/span> <\/h2>\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px\">Linux\u5185\u6838\u4ee3\u7801\u5927\u91cf\u4f7f\u7528\u4e86\u94fe\u8868\u8fd9\u79cd\u6570\u636e\u7ed3\u6784\u3002\u94fe\u8868\u662f\u5728\u89e3\u51b3\u6570\u7ec4\u4e0d\u80fd\u52a8\u6001\u6269\u5c55\u8fd9\u4e2a\u7f3a\u9677\u800c\u4ea7\u751f\u7684\u4e00\u79cd\u6570\u636e\u7ed3\u6784\u3002\u94fe\u8868\u6240\u5305\u542b\u7684\u5143\u7d20\u53ef\u4ee5\u52a8\u6001\u521b\u5efa\u5e76\u63d2\u5165\u548c\u5220\u9664\u3002\u94fe\u8868\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u662f\u79bb\u6563\u5b58\u653e\u7684\uff0c\u56e0\u6b64\u4e0d\u9700\u8981\u5360\u7528\u8fde\u7eed\u7684\u5185\u5b58\u3002\u94fe\u8868\u901a\u5e38\u7531\u82e5\u5e72\u8282\u70b9\u7ec4\u6210\uff0c\u6bcf\u4e2a\u8282\u70b9\u7684\u7ed3\u6784\u90fd\u662f\u4e00\u6837\u7684\uff0c\u7531\u6709\u6548\u6570\u636e\u533a\u548c\u6307\u9488\u533a\u4e24\u90e8\u5206\u7ec4\u6210\u3002\u6709\u6548\u6570\u636e\u533a\u7528\u6765\u5b58\u50a8\u6709\u6548\u6570\u636e\u4fe1\u606f\uff0c\u800c\u6307\u9488\u533a\u7528\u6765\u6307\u5411\u94fe\u8868\u7684\u524d\u7ee7\u8282\u70b9\u6216\u8005\u540e\u7ee7\u8282\u70b9\u3002\u56e0\u6b64\uff0c\u94fe\u8868\u5c31\u662f\u5229\u7528\u6307\u9488\u5c06\u5404\u4e2a\u8282\u70b9\u4e32\u8054\u8d77\u6765\u7684\u4e00\u79cd\u5b58\u50a8\u7ed3\u6784\u3002<\/p>\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px\"><strong>\uff081\uff09\u5355\u5411\u94fe\u8868<\/strong><\/p>\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px\">\u5355\u5411\u94fe\u8868\u7684\u6307\u9488\u533a\u53ea\u5305\u542b\u4e00\u4e2a\u6307\u5411\u4e0b\u4e00\u4e2a\u8282\u70b9\u7684\u6307\u9488\uff0c\u56e0\u6b64\u4f1a\u5f62\u6210\u4e00\u4e2a\u5355\u4e00\u65b9\u5411\u7684\u94fe\u8868\uff0c\u5982\u4e0b\u4ee3\u7801\u6240\u793a\u3002<\/p>\n<pre>struct&nbsp;list&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;data;&nbsp;&nbsp;&nbsp;\/*\u6709\u6548\u6570\u636e*\/\n&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;list&nbsp;*next;&nbsp;\/*\u6307\u5411\u4e0b\u4e00\u4e2a\u5143\u7d20\u7684\u6307\u9488*\/\n};<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px\">\u5982\u56fe\u6240\u793a\uff0c\u5355\u5411\u94fe\u8868\u5177\u6709\u5355\u5411\u79fb\u52a8\u6027\uff0c\u4e5f\u5c31\u662f\u53ea\u80fd\u8bbf\u95ee\u5f53\u524d\u7684\u8282\u70b9\u7684\u540e\u7ee7\u8282\u70b9\uff0c\u800c\u65e0\u6cd5\u8bbf\u95ee\u5f53\u524d\u8282\u70b9\u7684\u524d\u7ee7\u8282\u70b9\uff0c\u56e0\u6b64\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u8fd0\u7528\u5f97\u6bd4\u8f83\u5c11\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/275\/014\/b21976d5e40133d892f62c70748604f7-1.png\" class=\"aligncenter\" title=\"Linux\u5185\u6838\u4e2d\u5e38\u7528\u7684\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\u63d2\u56fe1\" alt=\"Linux\u5185\u6838\u4e2d\u5e38\u7528\u7684\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\u63d2\u56fe1\" \/>  <\/p>\n<p style=\"max-width:90%\">\u5355\u5411\u94fe\u8868\u793a\u610f\u56fe<\/p>\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px\"><strong>\uff082\uff09\u53cc\u5411\u94fe\u8868<\/strong><\/p>\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px\">\u5982\u56fe\u6240\u793a\uff0c\u53cc\u5411\u94fe\u8868\u548c\u5355\u5411\u94fe\u8868\u7684\u533a\u522b\u662f\u6307\u9488\u533a\u5305\u542b\u4e86\u4e24\u4e2a\u6307\u9488\uff0c\u4e00\u4e2a\u6307\u5411\u524d\u7ee7\u8282\u70b9\uff0c\u53e6\u4e00\u4e2a\u6307\u5411\u540e\u7ee7\u8282\u70b9\uff0c\u5982\u4e0b\u4ee3\u7801\u6240\u793a\u3002<\/p>\n<pre>struct&nbsp;list&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;data;&nbsp;&nbsp;&nbsp;\/*\u6709\u6548\u6570\u636e*\/\n&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;list&nbsp;*next;&nbsp;\/*\u6307\u5411\u4e0b\u4e00\u4e2a\u5143\u7d20\u7684\u6307\u9488*\/\n&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;list&nbsp;*prev;&nbsp;\/*\u6307\u5411\u4e0a\u4e00\u4e2a\u5143\u7d20\u7684\u6307\u9488*\/\n};<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236<br \/>\n<img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/275\/014\/3de4b6bad0e9a6e9097c115049f3e482-2.png\" class=\"aligncenter\" title=\"Linux\u5185\u6838\u4e2d\u5e38\u7528\u7684\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\u63d2\u56fe2\" alt=\"Linux\u5185\u6838\u4e2d\u5e38\u7528\u7684\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\u63d2\u56fe2\" \/>  <\/p>\n<p style=\"max-width:90%\">\u53cc\u5411\u94fe\u8868\u793a\u610f\u56fe<\/p>\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px\"><strong>\uff083\uff09Linux\u5185\u6838\u94fe\u8868\u5b9e\u73b0<\/strong><\/p>\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px\">\u5355\u5411\u94fe\u8868\u548c\u53cc\u5411\u94fe\u8868\u5728\u5b9e\u9645\u4f7f\u7528\u4e2d\u6709\u4e00\u4e9b\u5c40\u9650\u6027\uff0c\u5982\u6570\u636e\u533a\u5fc5\u987b\u662f\u56fa\u5b9a\u6570\u636e\uff0c\u800c\u5b9e\u9645\u9700\u6c42\u662f\u591a\u79cd\u591a\u6837\u7684\u3002\u8fd9\u79cd\u65b9\u6cd5\u65e0\u6cd5\u6784\u5efa\u4e00\u5957\u901a\u7528\u7684\u94fe\u8868\uff0c\u56e0\u4e3a\u6bcf\u4e2a\u4e0d\u540c\u7684\u6570\u636e\u533a\u9700\u8981\u4e00\u5957\u94fe\u8868\u3002\u4e3a\u6b64\uff0cLinux\u5185\u6838\u628a\u6240\u6709\u94fe\u8868\u64cd\u4f5c\u65b9\u6cd5\u7684\u5171\u540c\u90e8\u5206\u63d0\u53d6\u51fa\u6765\uff0c\u628a\u4e0d\u540c\u7684\u90e8\u5206\u7559\u7ed9\u4ee3\u7801\u7f16\u7a0b\u8005\u81ea\u5df1\u53bb\u5904\u7406\u3002Linux\u5185\u6838\u5b9e\u73b0\u4e86\u4e00\u5957\u7eaf\u94fe\u8868\u7684\u5c01\u88c5\uff0c\u94fe\u8868\u8282\u70b9\u6570\u636e\u7ed3\u6784\u53ea\u6709\u6307\u9488\u533a\u800c\u6ca1\u6709\u6570\u636e\u533a\uff0c\u53e6\u5916\u8fd8\u5c01\u88c5\u4e86\u5404\u79cd\u64cd\u4f5c\u51fd\u6570\uff0c\u5982\u521b\u5efa\u8282\u70b9\u51fd\u6570\u3001\u63d2\u5165\u8282\u70b9\u51fd\u6570\u3001\u5220\u9664\u8282\u70b9\u51fd\u6570\u3001\u904d\u5386\u8282\u70b9\u51fd\u6570\u7b49\u3002<\/p>\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px\">Linux\u5185\u6838\u94fe\u8868\u4f7f\u7528struct list_head\u6570\u636e\u7ed3\u6784\u6765\u63cf\u8ff0\u3002<\/p>\n<pre>&lt;include&gt;\n\nstruct&nbsp;list_head&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;list_head&nbsp;*next,&nbsp;*prev;\n};&lt;\/include&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px\">struct list_head\u6570\u636e\u7ed3\u6784\u4e0d\u5305\u542b\u94fe\u8868\u8282\u70b9\u7684\u6570\u636e\u533a\uff0c\u901a\u5e38\u662f\u5d4c\u5165\u5176\u4ed6\u6570\u636e\u7ed3\u6784\uff0c\u5982struct page\u6570\u636e\u7ed3\u6784\u4e2d\u5d4c\u5165\u4e86\u4e00\u4e2alru\u94fe\u8868\u8282\u70b9\uff0c\u901a\u5e38\u662f\u628apage\u6570\u636e\u7ed3\u6784\u6302\u5165LRU\u94fe\u8868\u3002<\/p>\n<pre>&lt;include&gt;\n\nstruct&nbsp;page&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;...\n&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;list_head&nbsp;lru;\n&nbsp;&nbsp;&nbsp;&nbsp;...\n}&lt;\/include&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px\"><strong>\u94fe\u8868\u5934\u7684\u521d\u59cb\u5316\u6709\u4e24\u79cd\u65b9\u6cd5\uff0c\u4e00\u79cd\u662f\u9759\u6001\u521d\u59cb\u5316\uff0c\u53e6\u4e00\u79cd\u52a8\u6001\u521d\u59cb\u5316\u3002<\/strong><\/p>\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px\">\u628anext\u548cprev\u6307\u9488\u90fd\u521d\u59cb\u5316\u5e76\u6307\u5411\u81ea\u5df1\uff0c\u8fd9\u6837\u4fbf\u521d\u59cb\u5316\u4e86\u4e00\u4e2a\u5e26\u5934\u8282\u70b9\u7684\u7a7a\u94fe\u8868\u3002<\/p>\n<pre>&lt;include&gt;\n\n\/*\u9759\u6001\u521d\u59cb\u5316*\/\n#define&nbsp;LIST_HEAD_INIT(name)&nbsp;{&nbsp;&amp;amp;(name),&nbsp;&amp;amp;(name)&nbsp;}\n\n#define&nbsp;LIST_HEAD(name)&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;list_head&nbsp;name&nbsp;=&nbsp;LIST_HEAD_INIT(name)\n\n\/*\u52a8\u6001\u521d\u59cb\u5316*\/\nstatic&nbsp;inline&nbsp;void&nbsp;INIT_LIST_HEAD(struct&nbsp;list_head&nbsp;*list)\n{\n&nbsp;&nbsp;&nbsp;&nbsp;list-&amp;gt;next&nbsp;=&nbsp;list;\n&nbsp;&nbsp;&nbsp;&nbsp;list-&amp;gt;prev&nbsp;=&nbsp;list;\n}&lt;\/include&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px\">\u6dfb\u52a0\u8282\u70b9\u5230\u4e00\u4e2a\u94fe\u8868\u4e2d\uff0c\u5185\u6838\u63d0\u4f9b\u4e86\u51e0\u4e2a\u63a5\u53e3\u51fd\u6570\uff0c\u5982list_add()\u662f\u628a\u4e00\u4e2a\u8282\u70b9\u6dfb\u52a0\u5230\u8868\u5934\uff0clist_add_tail()\u662f\u63d2\u5165\u8868\u5c3e\u3002<\/p>\n<pre>&lt;include&gt;\n\nvoid&nbsp;list_add(struct&nbsp;list_head&nbsp;*new,&nbsp;struct&nbsp;list_head&nbsp;*head)\nlist_add_tail(struct&nbsp;list_head&nbsp;*new,&nbsp;struct&nbsp;list_head&nbsp;*head)&lt;\/include&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px\">\u904d\u5386\u8282\u70b9\u7684\u63a5\u53e3\u51fd\u6570\u3002<\/p>\n<pre>#define&nbsp;list_for_each(pos,&nbsp;head)&nbsp;\nfor&nbsp;(pos&nbsp;=&nbsp;(head)-&amp;gt;next;&nbsp;pos&nbsp;!=&nbsp;(head);&nbsp;pos&nbsp;=&nbsp;pos-&amp;gt;next)<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px\">\u8fd9\u4e2a\u5b8f\u53ea\u662f\u904d\u5386\u4e00\u4e2a\u4e00\u4e2a\u8282\u70b9\u7684\u5f53\u524d\u4f4d\u7f6e\uff0c\u90a3\u4e48\u5982\u4f55\u83b7\u53d6\u8282\u70b9\u672c\u8eab\u7684\u6570\u636e\u7ed3\u6784\u5462\uff1f\u8fd9\u91cc\u8fd8\u9700\u8981\u4f7f\u7528list_entry()\u5b8f\u3002<\/p>\n<pre>#define&nbsp;list_entry(ptr,&nbsp;type,&nbsp;member)&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;container_of(ptr,&nbsp;type,&nbsp;member)\ncontainer_of()\u5b8f\u7684\u5b9a\u4e49\u5728kernel.h\u5934\u6587\u4ef6\u4e2d\u3002\n#define&nbsp;container_of(ptr,&nbsp;type,&nbsp;member)&nbsp;({&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;const&nbsp;typeof(&nbsp;((type&nbsp;*)0)-&amp;gt;member&nbsp;)&nbsp;*__mptr&nbsp;=&nbsp;(ptr);&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;(type&nbsp;*)(&nbsp;(char&nbsp;*)__mptr&nbsp;-&nbsp;offsetof(type,member)&nbsp;);})\n\n#define&nbsp;offsetof(TYPE,&nbsp;MEMBER)&nbsp;((size_t)&nbsp;&amp;amp;((TYPE&nbsp;*)0)-&amp;gt;MEMBER)<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px\">\u5176\u4e2doffsetof()\u5b8f\u662f\u901a\u8fc7\u628a0\u5730\u5740\u8f6c\u6362\u4e3atype\u7c7b\u578b\u7684\u6307\u9488\uff0c\u7136\u540e\u53bb\u83b7\u53d6\u8be5\u7ed3\u6784\u4f53\u4e2dmember\u6210\u5458\u7684\u6307\u9488\uff0c\u4e5f\u5c31\u662f\u83b7\u53d6\u4e86member\u5728type\u7ed3\u6784\u4f53\u4e2d\u7684\u504f\u79fb\u91cf\u3002\u6700\u540e\u7528\u6307\u9488ptr\u51cf\u53bboffset\uff0c\u5c31\u5f97\u5230type\u7ed3\u6784\u4f53\u7684\u771f\u5b9e\u5730\u5740\u4e86\u3002<\/p>\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px\">\u4e0b\u9762\u662f\u904d\u5386\u94fe\u8868\u7684\u4e00\u4e2a\u4f8b\u5b50\u3002<\/p>\n<pre>&lt;drivers&gt;\n\nstatic&nbsp;ssize_t&nbsp;class_osdblk_list(struct&nbsp;class&nbsp;*c,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;class_attribute&nbsp;*attr,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;*data)\n{\n&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;n&nbsp;=&nbsp;0;\n&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;list_head&nbsp;*tmp;\n\n&nbsp;&nbsp;&nbsp;&nbsp;list_for_each(tmp,&nbsp;&amp;amp;osdblkdev_list)&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;osdblk_device&nbsp;*osdev;\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;osdev&nbsp;=&nbsp;list_entry(tmp,&nbsp;struct&nbsp;osdblk_device,&nbsp;node);\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;n&nbsp;+=&nbsp;sprintf(data+n,&nbsp;\"%d&nbsp;%d&nbsp;%llu&nbsp;%llu&nbsp;%s\n\",\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;osdev-&amp;gt;id,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;osdev-&amp;gt;major,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;osdev-&amp;gt;obj.partition,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;osdev-&amp;gt;obj.id,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;osdev-&amp;gt;osd_path);\n&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;n;\n}&lt;\/drivers&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<h2> <span><\/span><span style=\"padding: 2px 10px 1px;border-top-right-radius: 3px;border-top-left-radius: 3px;margin-right: 3px\">\u7ea2\u9ed1\u6811<\/span><span style=\"vertical-align: bottom;border-bottom: 36px solid #efebe9;border-right: 20px solid transparent\"> <\/span> <\/h2>\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px\">\u7ea2\u9ed1\u6811\uff08Red Black Tree\uff09\u88ab\u5e7f\u6cdb\u5e94\u7528\u5728\u5185\u6838\u7684\u5185\u5b58\u7ba1\u7406\u548c\u8fdb\u7a0b\u8c03\u5ea6\u4e2d\uff0c\u7528\u4e8e\u5c06\u6392\u5e8f\u7684\u5143\u7d20\u7ec4\u7ec7\u5230\u6811\u4e2d\u3002\u7ea2\u9ed1\u6811\u88ab\u5e7f\u6cdb\u5e94\u7528\u5728\u8ba1\u7b97\u673a\u79d1\u5b66\u7684\u5404\u4e2a\u9886\u57df\u4e2d\uff0c\u5b83\u5728\u901f\u5ea6\u548c\u5b9e\u73b0\u590d\u6742\u5ea6\u4e4b\u95f4\u63d0\u4f9b\u4e00\u4e2a\u5f88\u597d\u7684\u5e73\u8861\u3002<\/p>\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px\">\u7ea2\u9ed1\u6811\u662f\u5177\u6709\u4ee5\u4e0b\u7279\u5f81\u7684\u4e8c\u53c9\u6811\u3002<\/p>\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px\">\u6bcf\u4e2a\u8282\u70b9\u6216\u7ea2\u6216\u9ed1\u3002<\/p>\n<ul>\n<li>  \u6bcf\u4e2a\u53f6\u8282\u70b9\u662f\u9ed1\u8272\u7684\u3002 <\/li>\n<li>  \u5982\u679c\u7ed3\u70b9\u90fd\u662f\u7ea2\u8272\uff0c\u90a3\u4e48\u4e24\u4e2a\u5b50\u7ed3\u70b9\u90fd\u662f\u9ed1\u8272\u3002 <\/li>\n<li>  \u4ece\u4e00\u4e2a\u5185\u90e8\u7ed3\u70b9\u5230\u53f6\u7ed3\u70b9\u7684\u7b80\u5355\u8def\u5f84\u4e0a\uff0c\u5bf9\u6240\u6709\u53f6\u8282\u70b9\u6765\u8bf4\uff0c\u9ed1\u8272\u7ed3\u70b9\u7684\u6570\u76ee\u90fd\u662f\u76f8\u540c\u7684\u3002 <\/li>\n<\/ul>\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px\">\u7ea2\u9ed1\u6811\u7684\u4e00\u4e2a\u4f18\u70b9\u662f\uff0c\u6240\u6709\u91cd\u8981\u7684\u64cd\u4f5c\uff08\u4f8b\u5982\u63d2\u5165\u3001\u5220\u9664\u3001\u641c\u7d22\uff09\u90fd\u53ef\u4ee5\u5728<em>O<\/em>(log <em>n<\/em>)\u65f6\u95f4\u5185\u5b8c\u6210\uff0c<em>n<\/em>\u4e3a\u6811\u4e2d\u5143\u7d20\u7684\u6570\u76ee\u3002\u7ecf\u5178\u7684\u7b97\u6cd5\u6559\u79d1\u4e66\u90fd\u4f1a\u8bb2\u89e3\u7ea2\u9ed1\u6811\u7684\u5b9e\u73b0\uff0c\u8fd9\u91cc\u53ea\u662f\u5217\u51fa\u4e00\u4e2a\u5185\u6838\u4e2d\u4f7f\u7528\u7ea2\u9ed1\u6811\u7684\u4f8b\u5b50\uff0c\u4f9b\u8bfb\u8005\u5728\u5b9e\u9645\u7684\u9a71\u52a8\u548c\u5185\u6838\u7f16\u7a0b\u4e2d\u53c2\u8003\u3002\u8fd9\u4e2a\u4f8b\u5b50\u53ef\u4ee5\u5728\u5185\u6838\u4ee3\u7801\u7684documentation\/Rbtree.txt\u6587\u4ef6\u4e2d\u627e\u5230\u3002<\/p>\n<pre>#include&nbsp;&lt;linux&gt;\n#include&nbsp;&lt;linux&gt;\n#include&nbsp;&lt;linux&gt;\n#include&nbsp;&lt;linux&gt;\n#include&nbsp;&lt;linux&gt;\n#include&nbsp;&lt;linux&gt;\n#include&nbsp;&lt;linux&gt;\n\nMODULE_AUTHOR(\"figo.zhang\");\nMODULE_DESCRIPTION(\"&nbsp;\");\nMODULE_LICENSE(\"GPL\");\n\n&nbsp;&nbsp;struct&nbsp;mytype&nbsp;{&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;rb_node&nbsp;node;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;key;&nbsp;\n};\n\n\/*\u7ea2\u9ed1\u6811\u6839\u8282\u70b9*\/\n&nbsp;struct&nbsp;rb_root&nbsp;mytree&nbsp;=&nbsp;RB_ROOT;\n\/*\u6839\u636ekey\u6765\u67e5\u627e\u8282\u70b9*\/\nstruct&nbsp;mytype&nbsp;*my_search(struct&nbsp;rb_root&nbsp;*root,&nbsp;int&nbsp;new)\n&nbsp;&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;rb_node&nbsp;*node&nbsp;=&nbsp;root-&amp;gt;rb_node;\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;(node)&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;mytype&nbsp;*data&nbsp;=&nbsp;container_of(node,&nbsp;struct&nbsp;mytype,&nbsp;node);\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(data-&amp;gt;key&nbsp;&amp;gt;&nbsp;new)\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;node&nbsp;=&nbsp;node-&amp;gt;rb_left;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else&nbsp;if&nbsp;(data-&amp;gt;key&nbsp;rb_right;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;data;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;NULL;\n&nbsp;&nbsp;}\n\n\/*\u63d2\u5165\u4e00\u4e2a\u5143\u7d20\u5230\u7ea2\u9ed1\u6811\u4e2d*\/\n&nbsp;&nbsp;int&nbsp;my_insert(struct&nbsp;rb_root&nbsp;*root,&nbsp;struct&nbsp;mytype&nbsp;*data)\n&nbsp;&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;rb_node&nbsp;**new&nbsp;=&nbsp;&amp;amp;(root-&amp;gt;rb_node),&nbsp;*parent=NULL;\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/*&nbsp;\u5bfb\u627e\u53ef\u4ee5\u6dfb\u52a0\u65b0\u8282\u70b9\u7684\u5730\u65b9&nbsp;*\/\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;(*new)&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;mytype&nbsp;*this&nbsp;=&nbsp;container_of(*new,&nbsp;struct&nbsp;mytype,&nbsp;node);\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parent&nbsp;=&nbsp;*new;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(this-&amp;gt;key&nbsp;&amp;gt;&nbsp;data-&amp;gt;key)\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new&nbsp;=&nbsp;&amp;amp;((*new)-&amp;gt;rb_left);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else&nbsp;if&nbsp;(this-&amp;gt;key&nbsp;key)&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new&nbsp;=&nbsp;&amp;amp;((*new)-&amp;gt;rb_right);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;-1;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/*&nbsp;\u6dfb\u52a0\u4e00\u4e2a\u65b0\u8282\u70b9&nbsp;*\/\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rb_link_node(&amp;amp;data-&amp;gt;node,&nbsp;parent,&nbsp;new);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rb_insert_color(&amp;amp;data-&amp;gt;node,&nbsp;root);\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;0;\n&nbsp;&nbsp;}\n\nstatic&nbsp;int&nbsp;__init&nbsp;my_init(void)\n{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;i;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;mytype&nbsp;*data;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;rb_node&nbsp;*node;\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/*\u63d2\u5165\u5143\u7d20*\/\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(i&nbsp;=0;&nbsp;i&nbsp;key&nbsp;=&nbsp;i;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my_insert(&amp;amp;mytree,&nbsp;data);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/*\u904d\u5386\u7ea2\u9ed1\u6811\uff0c\u6253\u5370\u6240\u6709\u8282\u70b9\u7684key\u503c*\/\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(node&nbsp;=&nbsp;rb_first(&amp;amp;mytree);&nbsp;node;&nbsp;node&nbsp;=&nbsp;rb_next(node))&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printk(\"key=%d\n\",&nbsp;rb_entry(node,&nbsp;struct&nbsp;mytype,&nbsp;node)-&amp;gt;key);\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;0;\n}\n\nstatic&nbsp;void&nbsp;__exit&nbsp;my_exit(void)\n{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;mytype&nbsp;*data;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;rb_node&nbsp;*node;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(node&nbsp;=&nbsp;rb_first(&amp;amp;mytree);&nbsp;node;&nbsp;node&nbsp;=&nbsp;rb_next(node))&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data&nbsp;=&nbsp;rb_entry(node,&nbsp;struct&nbsp;mytype,&nbsp;node);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(data)&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rb_erase(&amp;amp;data-&amp;gt;node,&nbsp;&amp;amp;mytree);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;kfree(data);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n}\nmodule_init(my_init);\nmodule_exit(my_exit);&lt;\/linux&gt;&lt;\/linux&gt;&lt;\/linux&gt;&lt;\/linux&gt;&lt;\/linux&gt;&lt;\/linux&gt;&lt;\/linux&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px\">mytree\u662f\u7ea2\u9ed1\u6811\u7684\u6839\u8282\u70b9\uff0cmy_insert()\u5b9e\u73b0\u63d2\u5165\u4e00\u4e2a\u5143\u7d20\u5230\u7ea2\u9ed1\u6811\u4e2d\uff0cmy_search()\u6839\u636ekey\u6765\u67e5\u627e\u8282\u70b9\u3002\u5185\u6838\u5927\u91cf\u4f7f\u7528\u7ea2\u9ed1\u6811\uff0c\u5982\u865a\u62df\u5730\u5740\u7a7a\u95f4VMA\u7684\u7ba1\u7406\u3002<\/p>\n<h2> <span><\/span><span style=\"padding: 2px 10px 1px;border-top-right-radius: 3px;border-top-left-radius: 3px;margin-right: 3px\">\u65e0\u9501\u73af\u5f62\u7f13\u51b2\u533a<\/span><span style=\"vertical-align: bottom;border-bottom: 36px solid #efebe9;border-right: 20px solid transparent\"> <\/span> <\/h2>\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px\">\u751f\u4ea7\u8005\u548c\u6d88\u8d39\u8005\u6a21\u578b\u662f\u8ba1\u7b97\u673a\u7f16\u7a0b\u4e2d\u6700\u5e38\u89c1\u7684\u4e00\u79cd\u6a21\u578b\u3002\u751f\u4ea7\u8005\u4ea7\u751f\u6570\u636e\uff0c\u800c\u6d88\u8d39\u8005\u6d88\u8017\u6570\u636e\uff0c\u5982\u4e00\u4e2a\u7f51\u7edc\u8bbe\u5907\uff0c\u786c\u4ef6\u8bbe\u5907\u63a5\u6536\u7f51\u7edc\u5305\uff0c\u7136\u540e\u5e94\u7528\u7a0b\u5e8f\u8bfb\u53d6\u7f51\u7edc\u5305\u3002\u73af\u5f62\u7f13\u51b2\u533a\u662f\u5b9e\u73b0\u751f\u4ea7\u8005\u548c\u6d88\u8d39\u8005\u6a21\u578b\u7684\u7ecf\u5178\u7b97\u6cd5\u3002\u73af\u5f62\u7f13\u51b2\u533a\u901a\u5e38\u6709\u4e00\u4e2a\u8bfb\u6307\u9488\u548c\u4e00\u4e2a\u5199\u6307\u9488\u3002\u8bfb\u6307\u9488\u6307\u5411\u73af\u5f62\u7f13\u51b2\u533a\u4e2d\u53ef\u8bfb\u7684\u6570\u636e\uff0c\u5199\u6307\u9488\u6307\u5411\u73af\u5f62\u7f13\u51b2\u533a\u53ef\u5199\u7684\u6570\u636e\u3002\u901a\u8fc7\u79fb\u52a8\u8bfb\u6307\u9488\u548c\u5199\u6307\u9488\u5b9e\u73b0\u7f13\u51b2\u533a\u6570\u636e\u7684\u8bfb\u53d6\u548c\u5199\u5165\u3002<\/p>\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px\">\u5728Linux\u5185\u6838\u4e2d\uff0cKFIFO\u662f\u91c7\u7528\u65e0\u9501\u73af\u5f62\u7f13\u51b2\u533a\u7684\u5b9e\u73b0\u3002<strong>FIFO\u7684\u5168\u79f0\u662f\u201cFirst In First Out\u201d<\/strong>\uff0c\u5373\u5148\u8fdb\u5148\u51fa\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5b83\u91c7\u7528\u73af\u5f62\u7f13\u51b2\u533a\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e2a\u65e0\u8fb9\u754c\u7684\u5b57\u8282\u6d41\u670d\u52a1\u3002<strong>\u91c7\u7528\u73af\u5f62\u7f13\u51b2\u533a\u7684\u597d\u5904\u662f\uff0c\u5f53\u4e00\u4e2a\u6570\u636e\u5143\u7d20\u88ab\u6d88\u8017\u4e4b\u540e\uff0c\u5176\u4f59\u6570\u636e\u5143\u7d20\u4e0d\u9700\u8981\u79fb\u52a8\u5176\u5b58\u50a8\u4f4d\u7f6e\uff0c\u4ece\u800c\u51cf\u5c11\u590d\u5236\uff0c\u63d0\u9ad8\u6548\u7387<\/strong>\u3002<\/p>\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px\"><strong>\uff081\uff09\u521b\u5efaKFIFO<\/strong><\/p>\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px\">\u5728\u4f7f\u7528KFIFO\u4e4b\u524d\u9700\u8981\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u8fd9\u91cc\u6709\u9759\u6001\u521d\u59cb\u5316\u548c\u52a8\u6001\u521d\u59cb\u5316\u4e24\u79cd\u65b9\u5f0f\u3002<\/p>\n<pre>&lt;include&gt;\n\nint&nbsp;kfifo_alloc(fifo,&nbsp;size,&nbsp;gfp_mask)&lt;\/include&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px\">\u8be5\u51fd\u6570\u521b\u5efa\u5e76\u5206\u914d\u4e00\u4e2a\u5927\u5c0f\u4e3asize\u7684KFIFO\u73af\u5f62\u7f13\u51b2\u533a\u3002\u7b2c\u4e00\u4e2a\u53c2\u6570fifo\u662f\u6307\u5411\u8be5\u73af\u5f62\u7f13\u51b2\u533a\u7684struct kfifo\u6570\u636e\u7ed3\u6784\uff1b\u7b2c\u4e8c\u4e2a\u53c2\u6570size\u662f\u6307\u5b9a\u7f13\u51b2\u533a\u5143\u7d20\u7684\u6570\u91cf\uff1b\u7b2c\u4e09\u4e2a\u53c2\u6570gfp_mask\u8868\u793a\u5206\u914dKFIFO\u5143\u7d20\u4f7f\u7528\u7684\u5206\u914d\u63a9\u7801\u3002<\/p>\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px\">\u9759\u6001\u5206\u914d\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u7684\u5b8f\u3002<\/p>\n<pre>#define&nbsp;DEFINE_KFIFO(fifo,&nbsp;type,&nbsp;size)\n#define&nbsp;INIT_KFIFO(fifo)<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px\"><strong>\uff082\uff09\u5165\u5217<\/strong><\/p>\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px\">\u628a\u6570\u636e\u5199\u5165KFIFO\u73af\u5f62\u7f13\u51b2\u533a\u53ef\u4ee5\u4f7f\u7528kfifo_in()\u51fd\u6570\u63a5\u53e3\u3002<\/p>\n<pre>int&nbsp;kfifo_in(fifo,&nbsp;buf,&nbsp;n)<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px\">\u8be5\u51fd\u6570\u628abuf\u6307\u9488\u6307\u5411\u7684<em>n<\/em>\u4e2a\u6570\u636e\u590d\u5236\u5230KFIFO\u73af\u5f62\u7f13\u51b2\u533a\u4e2d\u3002\u7b2c\u4e00\u4e2a\u53c2\u6570fifo\u6307\u7684\u662fKFIFO\u73af\u5f62\u7f13\u51b2\u533a\uff1b\u7b2c\u4e8c\u4e2a\u53c2\u6570buf\u6307\u5411\u8981\u590d\u5236\u7684\u6570\u636e\u7684buffer\uff1b\u7b2c\u4e09\u4e2a\u6570\u636e\u662f\u8981\u590d\u5236\u6570\u636e\u5143\u7d20\u7684\u6570\u91cf\u3002<\/p>\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px\"><strong>\uff083\uff09\u51fa\u5217<\/strong><\/p>\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px\">\u4eceKFIFO\u73af\u5f62\u7f13\u51b2\u533a\u4e2d\u5217\u51fa\u6216\u8005\u6458\u53d6\u6570\u636e\u53ef\u4ee5\u4f7f\u7528kfifo_out()\u51fd\u6570\u63a5\u53e3\u3002<\/p>\n<pre>#define&nbsp;&nbsp;&nbsp;&nbsp;kfifo_out(fifo,&nbsp;buf,&nbsp;n)<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px\">\u8be5\u51fd\u6570\u662f\u4ecefifo\u6307\u5411\u7684\u73af\u5f62\u7f13\u51b2\u533a\u4e2d\u590d\u5236<em>n<\/em>\u4e2a\u6570\u636e\u5143\u7d20\u5230buf\u6307\u5411\u7684\u7f13\u51b2\u533a\u4e2d\u3002\u5982\u679cKFIFO\u73af\u5f62\u7f13\u51b2\u533a\u7684\u6570\u636e\u5143\u7d20\u5c0f\u4e8e<em>n<\/em>\u4e2a\uff0c\u90a3\u4e48\u590d\u5236\u51fa\u53bb\u7684\u6570\u636e\u5143\u7d20\u5c0f\u4e8e<em>n<\/em>\u4e2a\u3002<\/p>\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px\"><strong>\uff084\uff09\u83b7\u53d6\u7f13\u51b2\u533a\u5927\u5c0f<\/strong><\/p>\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px\">KFIFO\u63d0\u4f9b\u4e86\u51e0\u4e2a\u63a5\u53e3\u51fd\u6570\u6765\u67e5\u8be2\u73af\u5f62\u7f13\u51b2\u533a\u7684\u72b6\u6001\u3002<\/p>\n<pre>#define&nbsp;kfifo_size(fifo)\n#define&nbsp;kfifo_len(fifo)\n#define&nbsp;kfifo_is_empty(fifo)\n#define&nbsp;kfifo_is_full(fifo)<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px\">kfifo_size()\u7528\u6765\u83b7\u53d6\u73af\u5f62\u7f13\u51b2\u533a\u7684\u5927\u5c0f\uff0c\u4e5f\u5c31\u662f\u6700\u5927\u53ef\u4ee5\u5bb9\u7eb3\u591a\u5c11\u4e2a\u6570\u636e\u5143\u7d20\u3002kfifo_len()\u7528\u6765\u83b7\u53d6\u5f53\u524d\u73af\u5f62\u7f13\u51b2\u533a\u4e2d\u6709\u591a\u5c11\u4e2a\u6709\u6548\u6570\u636e\u5143\u7d20\u3002kfifo_is_empty()\u5224\u65ad\u73af\u5f62\u7f13\u51b2\u533a\u662f\u5426\u4e3a\u7a7a\u3002kfifo_is_full()\u5224\u65ad\u73af\u5f62\u7f13\u51b2\u533a\u662f\u5426\u4e3a\u6ee1\u3002<\/p>\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px\"><strong>\uff085\uff09\u4e0e\u7528\u6237\u7a7a\u95f4\u6570\u636e\u4ea4\u4e92<\/strong><\/p>\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px\">KFIFO\u8fd8\u5c01\u88c5\u4e86\u4e24\u4e2a\u51fd\u6570\u4e0e\u7528\u6237\u7a7a\u95f4\u6570\u636e\u4ea4\u4e92\u3002<\/p>\n<pre>#define&nbsp;&nbsp;&nbsp;&nbsp;kfifo_from_user(fifo,&nbsp;from,&nbsp;len,&nbsp;copied)\n#define&nbsp;&nbsp;&nbsp;&nbsp;kfifo_to_user(fifo,&nbsp;to,&nbsp;len,&nbsp;copied)<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px\">kfifo_from_user()\u662f\u628afrom\u6307\u5411\u7684\u7528\u6237\u7a7a\u95f4\u7684len\u4e2a\u6570\u636e\u5143\u7d20\u590d\u5236\u5230KFIFO\u4e2d\uff0c\u6700\u540e\u4e00\u4e2a\u53c2\u6570copied\u8868\u793a\u6210\u529f\u590d\u5236\u4e86\u51e0\u4e2a\u6570\u636e\u5143\u7d20\u3002<\/p>\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px\">kfifo_to_user()\u5219\u76f8\u53cd\uff0c\u628aKFIFO\u7684\u6570\u636e\u5143\u7d20\u590d\u5236\u5230\u7528\u6237\u7a7a\u95f4\u3002\u8fd9\u4e24\u4e2a\u5b8f\u7ed3\u5408\u4e86copy_to_user()\u3001copy_from_user()\u4ee5\u53caKFIFO\u7684\u673a\u5236\uff0c\u7ed9\u9a71\u52a8\u5f00\u53d1\u8005\u63d0\u4f9b\u4e86\u65b9\u4fbf\u3002<\/p>\n<p>  <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fLinux\u5185\u6838\u4e2d\u5e38\u7528\u7684\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\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>Linux\u5185\u6838\u4ee3\u7801\u4e2d\u5e7f\u6cdb\u4f7f\u7528\u4e86\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u4e24\u4e2a\u662f\u94fe\u8868\u548c\u7ea2\u9ed1\u6811\u3002 \u94fe\u8868 Linux\u5185\u6838\u4ee3\u7801\u5927\u91cf\u4f7f\u7528\u4e86\u94fe\u8868\u8fd9\u79cd\u6570\u636e\u7ed3\u6784\u3002\u94fe\u8868\u662f\u5728\u89e3\u51b3\u6570\u7ec4\u4e0d\u80fd\u52a8\u6001\u6269\u5c55\u8fd9\u4e2a\u7f3a\u9677\u800c\u4ea7\u751f\u7684\u4e00\u79cd\u6570\u636e\u7ed3\u6784\u3002\u94fe\u8868\u6240\u5305\u542b\u7684\u5143\u7d20\u53ef\u4ee5\u52a8\u6001\u521b\u5efa\u5e76\u63d2\u5165\u548c\u5220\u9664\u3002\u94fe\u8868\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u662f\u79bb\u6563\u5b58\u653e\u7684\uff0c\u56e0\u6b64\u4e0d\u9700\u8981\u5360\u7528\u8fde\u7eed\u7684\u5185\u5b58\u3002\u94fe\u8868\u901a\u5e38\u7531\u82e5\u5e72\u8282\u70b9\u7ec4\u6210\uff0c\u6bcf\u4e2a\u8282\u70b9\u7684\u7ed3\u6784\u90fd\u662f\u4e00\u6837\u7684\uff0c\u7531\u6709\u6548\u6570\u636e\u533a\u548c\u6307\u9488\u533a\u4e24\u90e8\u5206\u7ec4\u6210\u3002\u6709\u6548\u6570\u636e\u533a\u7528\u6765\u5b58\u50a8\u6709\u6548\u6570\u636e\u4fe1\u606f\uff0c\u800c\u6307\u9488\u533a\u7528\u6765\u6307\u5411\u94fe\u8868\u7684\u524d\u7ee7\u8282\u70b9\u6216\u8005\u540e\u7ee7\u8282\u70b9\u3002\u56e0\u6b64\uff0c\u94fe\u8868\u5c31\u662f\u5229\u7528\u6307\u9488\u5c06\u5404\u4e2a\u8282\u70b9\u4e32\u8054\u8d77\u6765\u7684\u4e00\u79cd\u5b58\u50a8\u7ed3\u6784\u3002 \uff081\uff09\u5355\u5411\u94fe\u8868 \u5355\u5411\u94fe\u8868\u7684\u6307\u9488\u533a\u53ea\u5305\u542b\u4e00\u4e2a\u6307\u5411\u4e0b\u4e00\u4e2a\u8282\u70b9\u7684\u6307\u9488\uff0c\u56e0\u6b64\u4f1a\u5f62\u6210\u4e00\u4e2a\u5355\u4e00\u65b9\u5411\u7684\u94fe\u8868\uff0c\u5982\u4e0b\u4ee3\u7801\u6240\u793a\u3002 struct&nbsp;list&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;data;&nbsp;&nbsp;&nbsp;\/*\u6709\u6548\u6570\u636e*\/ &nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;list&nbsp;*next;&nbsp;\/*\u6307\u5411\u4e0b\u4e00\u4e2a\u5143\u7d20\u7684\u6307\u9488*\/ }; \u767b\u5f55\u540e\u590d\u5236 \u5982\u56fe\u6240\u793a\uff0c\u5355\u5411\u94fe\u8868\u5177\u6709\u5355\u5411\u79fb\u52a8\u6027\uff0c\u4e5f\u5c31\u662f\u53ea\u80fd\u8bbf\u95ee\u5f53\u524d\u7684\u8282\u70b9\u7684\u540e\u7ee7\u8282\u70b9\uff0c\u800c\u65e0\u6cd5\u8bbf\u95ee\u5f53\u524d\u8282\u70b9\u7684\u524d\u7ee7\u8282\u70b9\uff0c\u56e0\u6b64\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u8fd0\u7528\u5f97\u6bd4\u8f83\u5c11\u3002 \u5355\u5411\u94fe\u8868\u793a\u610f\u56fe \uff082\uff09\u53cc\u5411\u94fe\u8868 \u5982\u56fe\u6240\u793a\uff0c\u53cc\u5411\u94fe\u8868\u548c\u5355\u5411\u94fe\u8868\u7684\u533a\u522b\u662f\u6307\u9488\u533a\u5305\u542b\u4e86\u4e24\u4e2a\u6307\u9488\uff0c\u4e00\u4e2a\u6307\u5411\u524d\u7ee7\u8282\u70b9\uff0c\u53e6\u4e00\u4e2a\u6307\u5411\u540e\u7ee7\u8282\u70b9\uff0c\u5982\u4e0b\u4ee3\u7801\u6240\u793a\u3002 struct&nbsp;list&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;data;&nbsp;&nbsp;&nbsp;\/*\u6709\u6548\u6570\u636e*\/ &nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;list&nbsp;*next;&nbsp;\/*\u6307\u5411\u4e0b\u4e00\u4e2a\u5143\u7d20\u7684\u6307\u9488*\/ &nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;list&nbsp;*prev;&nbsp;\/*\u6307\u5411\u4e0a\u4e00\u4e2a\u5143\u7d20\u7684\u6307\u9488*\/ }; \u767b\u5f55\u540e\u590d\u5236 \u53cc\u5411\u94fe\u8868\u793a\u610f\u56fe \uff083\uff09Linux\u5185\u6838\u94fe\u8868\u5b9e\u73b0 \u5355\u5411\u94fe\u8868\u548c\u53cc\u5411\u94fe\u8868\u5728\u5b9e\u9645\u4f7f\u7528\u4e2d\u6709\u4e00\u4e9b\u5c40\u9650\u6027\uff0c\u5982\u6570\u636e\u533a\u5fc5\u987b\u662f\u56fa\u5b9a\u6570\u636e\uff0c\u800c\u5b9e\u9645\u9700\u6c42\u662f\u591a\u79cd\u591a\u6837\u7684\u3002\u8fd9\u79cd\u65b9\u6cd5\u65e0\u6cd5\u6784\u5efa\u4e00\u5957\u901a\u7528\u7684\u94fe\u8868\uff0c\u56e0\u4e3a\u6bcf\u4e2a\u4e0d\u540c\u7684\u6570\u636e\u533a\u9700\u8981\u4e00\u5957\u94fe\u8868\u3002\u4e3a\u6b64\uff0cLinux\u5185\u6838\u628a\u6240\u6709\u94fe\u8868\u64cd\u4f5c\u65b9\u6cd5\u7684\u5171\u540c\u90e8\u5206\u63d0\u53d6\u51fa\u6765\uff0c\u628a\u4e0d\u540c\u7684\u90e8\u5206\u7559\u7ed9\u4ee3\u7801\u7f16\u7a0b\u8005\u81ea\u5df1\u53bb\u5904\u7406\u3002Linux\u5185\u6838\u5b9e\u73b0\u4e86\u4e00\u5957\u7eaf\u94fe\u8868\u7684\u5c01\u88c5\uff0c\u94fe\u8868\u8282\u70b9\u6570\u636e\u7ed3\u6784\u53ea\u6709\u6307\u9488\u533a\u800c\u6ca1\u6709\u6570\u636e\u533a\uff0c\u53e6\u5916\u8fd8\u5c01\u88c5\u4e86\u5404\u79cd\u64cd\u4f5c\u51fd\u6570\uff0c\u5982\u521b\u5efa\u8282\u70b9\u51fd\u6570\u3001\u63d2\u5165\u8282\u70b9\u51fd\u6570\u3001\u5220\u9664\u8282\u70b9\u51fd\u6570\u3001\u904d\u5386\u8282\u70b9\u51fd\u6570\u7b49\u3002 Linux\u5185\u6838\u94fe\u8868\u4f7f\u7528struct list_head\u6570\u636e\u7ed3\u6784\u6765\u63cf\u8ff0\u3002 &lt;include&gt; struct&nbsp;list_head&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;list_head&nbsp;*next,&nbsp;*prev; };&lt;\/include&gt; \u767b\u5f55\u540e\u590d\u5236 struct list_head\u6570\u636e\u7ed3\u6784\u4e0d\u5305\u542b\u94fe\u8868\u8282\u70b9\u7684\u6570\u636e\u533a\uff0c\u901a\u5e38\u662f\u5d4c\u5165\u5176\u4ed6\u6570\u636e\u7ed3\u6784\uff0c\u5982struct page\u6570\u636e\u7ed3\u6784\u4e2d\u5d4c\u5165\u4e86\u4e00\u4e2alru\u94fe\u8868\u8282\u70b9\uff0c\u901a\u5e38\u662f\u628apage\u6570\u636e\u7ed3\u6784\u6302\u5165LRU\u94fe\u8868\u3002 &lt;include&gt; struct&nbsp;page&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&#8230; &nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;list_head&nbsp;lru; &nbsp;&nbsp;&nbsp;&nbsp;&#8230; }&lt;\/include&gt; \u767b\u5f55\u540e\u590d\u5236 \u94fe\u8868\u5934\u7684\u521d\u59cb\u5316\u6709\u4e24\u79cd\u65b9\u6cd5\uff0c\u4e00\u79cd\u662f\u9759\u6001\u521d\u59cb\u5316\uff0c\u53e6\u4e00\u79cd\u52a8\u6001\u521d\u59cb\u5316\u3002 \u628anext\u548cprev\u6307\u9488\u90fd\u521d\u59cb\u5316\u5e76\u6307\u5411\u81ea\u5df1\uff0c\u8fd9\u6837\u4fbf\u521d\u59cb\u5316\u4e86\u4e00\u4e2a\u5e26\u5934\u8282\u70b9\u7684\u7a7a\u94fe\u8868\u3002 &lt;include&gt; \/*\u9759\u6001\u521d\u59cb\u5316*\/ #define&nbsp;LIST_HEAD_INIT(name)&nbsp;{&nbsp;&amp;amp;(name),&nbsp;&amp;amp;(name)&nbsp;} #define&nbsp;LIST_HEAD(name)&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;list_head&nbsp;name&nbsp;=&nbsp;LIST_HEAD_INIT(name) \/*\u52a8\u6001\u521d\u59cb\u5316*\/ static&nbsp;inline&nbsp;void&nbsp;INIT_LIST_HEAD(struct&nbsp;list_head&nbsp;*list) { &nbsp;&nbsp;&nbsp;&nbsp;list-&amp;gt;next&nbsp;=&nbsp;list; &nbsp;&nbsp;&nbsp;&nbsp;list-&amp;gt;prev&nbsp;=&nbsp;list; }&lt;\/include&gt; \u767b\u5f55\u540e\u590d\u5236 \u6dfb\u52a0\u8282\u70b9\u5230\u4e00\u4e2a\u94fe\u8868\u4e2d\uff0c\u5185\u6838\u63d0\u4f9b\u4e86\u51e0\u4e2a\u63a5\u53e3\u51fd\u6570\uff0c\u5982list_add()\u662f\u628a\u4e00\u4e2a\u8282\u70b9\u6dfb\u52a0\u5230\u8868\u5934\uff0clist_add_tail()\u662f\u63d2\u5165\u8868\u5c3e\u3002 [&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-16500","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/16500","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=16500"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/16500\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=16500"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=16500"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=16500"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}