{"id":434,"date":"2025-12-16T11:11:52","date_gmt":"2025-12-16T03:11:52","guid":{"rendered":"https:\/\/www.garfield-blog.top\/?p=434"},"modified":"2025-12-16T11:12:10","modified_gmt":"2025-12-16T03:12:10","slug":"%e3%80%90linux%e3%80%91%e7%ad%89%e5%be%85%e9%98%9f%e5%88%97%e6%a6%82%e8%bf%b0","status":"publish","type":"post","link":"https:\/\/www.garfield-blog.top\/?p=434","title":{"rendered":"\u3010Linux\u3011\u7b49\u5f85\u961f\u5217\u6982\u8ff0"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u6982\u8ff0<\/h2>\n\n\n\n<p><code>Linux<\/code>\u5185\u6838\u7684<strong>\u7b49\u5f85\u961f\u5217\uff08Wait Queue\uff09<\/strong>\u662f\u91cd\u8981\u7684\u6570\u636e\u7ed3\u6784\uff0c\u4e0e\u8fdb\u7a0b\u8c03\u5ea6\u673a\u5236\u7d27\u5bc6\u76f8\u5173\u8054\uff0c\u53ef\u4ee5\u7528\u6765\u540c\u6b65\u5bf9\u7cfb\u7edf\u8d44\u6e90\u7684\u8bbf\u95ee\u3001\u5f02\u6b65\u4e8b\u4ef6\u901a\u77e5\u3001\u8de8\u8fdb\u7a0b\u901a\u4fe1\u7b49\u3002<br>\u5728<code>Linux<\/code>\u4e2d\uff0c\u7b49\u5f85\u961f\u5217\u4ee5\u5faa\u73af\u94fe\u8868\u4e3a\u57fa\u7840\u7ed3\u6784\uff0c\u5305\u62ec\u4e24\u79cd\u6570\u636e\u7ed3\u6784\uff1a<strong>\u7b49\u5f85\u961f\u5217\u5934(wait queue head)<\/strong>\u548c<strong>\u7b49\u5f85\u961f\u5217\u5143\u7d20(wait queue)<\/strong>\uff0c\u6574\u4e2a\u7b49\u5f85\u961f\u5217\u7531\u7b49\u5f85\u961f\u5217\u5934\u8fdb\u884c\u7ba1\u7406<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u6838\u5fc3\u539f\u7406<\/h2>\n\n\n\n<p>\u5185\u6838\u7b49\u5f85\u961f\u5217\u7684\u5b9e\u73b0\u4f9d\u9760\u7684\u662f\u5bf9\u4e8e\u8fdb\u7a0b\u72b6\u6001\u7684\u7ba1\u7406\u548c\u4efb\u52a1\u8c03\u5ea6\u961f\u5217\u7684\u7ba1\u7406\u6765\u5b9e\u73b0\u4efb\u52a1\u7684\u5524\u9192\u548c\u4f11\u7720<\/p>\n\n\n\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\u7b49\u5f85\u961f\u5217\u4e2d\u7684\u4efb\u52a1\u5e76\u4e0d\u4e0e\u7ebf\u7a0b\u4e00\u4e00\u5bf9\u5e94\uff0c\u8fd9\u4e2a\u7b49\u5f85\u8005\u53ef\u4ee5\u5bf9\u5e94\u591a\u79cd\u7c7b\u578b\uff0c\u5305\u62ec\u7ebf\u7a0b\u3001\u8fdb\u7a0b\u3001\u6587\u4ef6<code>\/poll\/epoll<\/code>\u56de\u8c03\u578b\u7b49\u5f85\u8005<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u6838\u5fc3\u6570\u636e\u7ed3\u6784<\/h2>\n\n\n\n<p>\u5185\u6838\u4e2d\u4e3a\u7b49\u5f85\u961f\u5217\u5b9a\u4e49\u4e86\u4e00\u4e2a\u6838\u5fc3\u6570\u636e\u7ed3\u6784<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/linux-5.4\/include\/linux\/wait.h\nstruct wait_queue_entry {\n    unsigned int         flags;\n    void                 *private;\n    wait_queue_func_t    func;\n    struct list_head     entry;\n};\n\nstruct wait_queue_head {\n    spinlock_t          lock;\n    struct list_head    head;\n};\ntypedef struct wait_queue_head wait_queue_head_t;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u5173\u952eAPI<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u521d\u59cb\u5316<\/h3>\n\n\n\n<p>\u521b\u5efa\u7b49\u5f85\u961f\u5217\u65f6\u4f1a\u8c03\u7528<code>DECLARE_WAITQUEUE<\/code>\u8fd9\u4e2a\u5b8f\u6765\u521b\u5efa\u5e76\u521d\u59cb\u5316\u4e00\u4e2a\u7b49\u5f85\u961f\u5217\u8282\u70b9<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#define __WAITQUEUE_INITIALIZER(name, tsk) {                    \\\n    .private    = tsk,                          \\\n    .func       = default_wake_function,                \\\n    .entry      = { NULL, NULL } }\n\n#define DECLARE_WAITQUEUE(name, tsk)                        \\\n    struct wait_queue_entry name = __WAITQUEUE_INITIALIZER(name, tsk)<\/code><\/pre>\n\n\n\n<p>\u8fd9\u91cc\u7684<code>default_wake_function<\/code>\u51fd\u6570\u4f1a\u5728<code>__wake_up_common<\/code>\u51fd\u6570\u4e2d\u88ab\u8c03\u7528\u6267\u884c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>static int __wake_up_common(struct wait_queue_head *wq_head, unsigned int mode,\n            int nr_exclusive, int wake_flags, void *key,\n            wait_queue_entry_t *bookmark)\n{\n    \/\/ ...\n\n    list_for_each_entry_safe_from(curr, next, &amp;wq_head-&gt;head, entry) {\n        \/\/ ...\n        ret = curr-&gt;func(curr, mode, wake_flags, key);\n        \/\/ ...\n    }\n\n    \/\/ ...\n\n    return nr_exclusive;\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u65b0\u589e\u8282\u70b9<\/h3>\n\n\n\n<p>\u8c03\u7528<code>add_wait_queue<\/code>\u51fd\u6570\u5c06\u521b\u5efa\u7684\u8282\u70b9\u6dfb\u52a0\u5230\u7b49\u5f85\u961f\u5217\u4e2d\uff0c\u5728\u4e0b\u4e00\u6b21\u65f6\u949f\u4e2d\u65ad\uff0c\u8fdb\u7a0b\u5207\u6362\u65f6\u4efb\u52a1\u8c03\u5ea6\u5668\u4f1a\u67e5\u770b\u5c31\u7eea\u961f\u5217\u4e2d\u7684\u8fdb\u7a0b<\/p>\n\n\n\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\u8fdb\u7a0b\u8c03\u5ea6\u5668\u7528\u6765\u7ba1\u7406\u8fdb\u7a0b\u7684\u6570\u636e\u7ed3\u6784\u662f<code>runqueue<\/code>\uff0c\u4e0d\u662f\u7b49\u5f85\u961f\u5217<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e3b\u52a8\u8ba9\u51faCPU<\/h3>\n\n\n\n<p>\u5728\u5c06\u8282\u70b9\u6dfb\u52a0\u5230\u7b49\u5f85\u961f\u5217\u540e\u9700\u8981\u624b\u52a8\u8c03\u7528<code>schedule()<\/code>\u51fd\u6570\u6765\u8ba9\u51fa<code>CPU<\/code>\uff0c\u56e0\u4e3a\u53ea\u662f\u5c06\u4efb\u52a1\u6dfb\u52a0\u5230\u7b49\u5f85\u4e2d\u4e0d\u4f1a\u963b\u585e\u4efb\u52a1\u3002\u8fd9\u91cc\u4e3b\u52a8\u8ba9\u51fa<code>CPU<\/code>\u624d\u4f1a\u5728\u4e0b\u4e00\u6b21\u8fdb\u7a0b\u8c03\u5ea6\u4e2d\u5c06\u81ea\u8eab\u6302\u8d77<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u5220\u9664\u8282\u70b9<\/h3>\n\n\n\n<p>\u8c03\u7528<code>remove_wait_queue<\/code>\u51fd\u6570\u5c06\u521b\u5efa\u7684\u8282\u70b9\u4ece\u7b49\u5f85\u961f\u5217\u4e2d\u5220\u9664<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u53c2\u8003\u6587\u6863<\/h2>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><a href=\"https:\/\/github.com\/torvalds\/linux\/releases\/tag\/v5.4\">Release v5.4 \u00b7 torvalds\/linux<\/a><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>\u6982\u8ff0 Linux\u5185\u6838\u7684\u7b49\u5f85\u961f\u5217\uff08Wait Queue\uff09\u662f\u91cd\u8981\u7684\u6570\u636e\u7ed3\u6784\uff0c\u4e0e\u8fdb\u7a0b\u8c03\u5ea6\u673a\u5236\u7d27\u5bc6\u76f8\u5173\u8054\uff0c\u53ef\u4ee5\u7528\u6765\u540c\u6b65 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[6],"tags":[],"class_list":["post-434","post","type-post","status-publish","format-standard","hentry","category-linux-knowledge"],"_links":{"self":[{"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=\/wp\/v2\/posts\/434","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=434"}],"version-history":[{"count":1,"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=\/wp\/v2\/posts\/434\/revisions"}],"predecessor-version":[{"id":435,"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=\/wp\/v2\/posts\/434\/revisions\/435"}],"wp:attachment":[{"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=434"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=434"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=434"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}