{"id":18193,"date":"2024-11-18T11:56:59","date_gmt":"2024-11-18T03:56:59","guid":{"rendered":"https:\/\/fwq.ai\/blog\/18193\/"},"modified":"2024-11-18T11:56:59","modified_gmt":"2024-11-18T03:56:59","slug":"linux%e6%80%8e%e4%b9%88%e5%ae%9e%e7%8e%b0%e5%ae%9a%e6%97%b6%e5%99%a8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/18193\/","title":{"rendered":"linux\u600e\u4e48\u5b9e\u73b0\u5b9a\u65f6\u5668"},"content":{"rendered":"<blockquote><p>\n  linux\u4e2d\u7684\u5b9a\u65f6\u5668\u5b9e\u73b0\uff1a\u5b9a\u65f6\u5668\u5bf9\u8c61\uff1a\u521b\u5efa\u5bf9\u8c61\uff0c\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4\u548c\u5904\u7406\u7a0b\u5e8f\uff0c\u6dfb\u52a0\u81f3\u5185\u6838\u3002\u5185\u6838\u51fd\u6570\uff1a\u4f7f\u7528alarm()\u521b\u5efa\u975e\u7cbe\u786e\u5b9a\u65f6\u5668\u3001sleep()\u6216usleep()\u4f7f\u8fdb\u7a0b\u4f11\u7720\u3002\u7528\u6237\u7a7a\u95f4\u5e93\uff1a\u4f7f\u7528libevent\u6216boost.asio\u7b49\u5e93\u521b\u5efa\u548c\u7ba1\u7406\u5b9a\u65f6\u5668\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202406\/04\/2024060413061879910.jpg\" class=\"aligncenter\" title=\"linux\u600e\u4e48\u5b9e\u73b0\u5b9a\u65f6\u5668\u63d2\u56fe\" alt=\"linux\u600e\u4e48\u5b9e\u73b0\u5b9a\u65f6\u5668\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5728 Linux \u4e2d\u5b9e\u73b0\u5b9a\u65f6\u5668<\/strong><\/p>\n<p><strong>\u5b9a\u65f6\u5668\u6982\u8ff0<\/strong><\/p>\n<p>\u5b9a\u65f6\u5668\u662f\u4e00\u79cd\u5185\u6838\u673a\u5236\uff0c\u5b83\u5141\u8bb8\u5728\u7279\u5b9a\u7684\u65f6\u95f4\u95f4\u9694\u6216\u5ef6\u8fdf\u540e\u6267\u884c\u4efb\u52a1\u3002\u5728 Linux \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\u5b9a\u65f6\u5668\u3002<\/p>\n<p><strong>\u65b9\u6cd5 1\uff1a\u5b9a\u65f6\u5668\u5bf9\u8c61<\/strong><\/p>\n<p>\u5b9a\u65f6\u5668\u5bf9\u8c61\u662f Linux \u4e2d\u6700\u5e38\u7528\u7684\u5b9a\u65f6\u5668\u5b9e\u73b0\u65b9\u5f0f\u3002\u5b83\u5141\u8bb8\u60a8\u521b\u5efa\u5b9a\u65f6\u5668\u5bf9\u8c61\uff0c\u5e76\u6307\u5b9a\u8d85\u65f6\u65f6\u95f4\u548c\u5904\u7406\u7a0b\u5e8f\u3002\u5f53\u5b9a\u65f6\u5668\u8fc7\u671f\u65f6\uff0c\u5904\u7406\u7a0b\u5e8f\u5c06\u88ab\u8c03\u7528\u3002<\/p>\n<p><strong>\u5b9e\u73b0\u6b65\u9aa4\uff1a<\/strong><\/p>\n<ol>\n<li>\n<p>\u521b\u5efa\u4e00\u4e2a\u5b9a\u65f6\u5668\u5bf9\u8c61\uff1a<\/p>\n<pre>struct timer_list my_timer;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<li>\n<p>\u521d\u59cb\u5316\u5b9a\u65f6\u5668\u5bf9\u8c61\uff1a<\/p>\n<pre>init_timer(&amp;my_timer);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<li>\n<p>\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4\uff08\u4ee5\u6beb\u79d2\u4e3a\u5355\u4f4d\uff09\uff1a<\/p>\n<pre>my_timer.expires = jiffies + msecs_to_jiffies(TIMEOUT_MS);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<li>\n<p>\u8bbe\u7f6e\u5904\u7406\u7a0b\u5e8f\uff1a<\/p>\n<pre>my_timer.function = my_timer_handler;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<li>\n<p>\u6dfb\u52a0\u5b9a\u65f6\u5668\u5230\u5185\u6838\uff1a<\/p>\n<pre>add_timer(&amp;my_timer);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<\/ol>\n<p><strong>\u65b9\u6cd5 2\uff1a\u5185\u6838\u51fd\u6570<\/strong><\/p>\n<p>\u9664\u4e86\u5b9a\u65f6\u5668\u5bf9\u8c61\uff0cLinux \u8fd8\u63d0\u4f9b\u4e86\u51e0\u4e2a\u5185\u6838\u51fd\u6570\u6765\u521b\u5efa\u5b9a\u65f6\u5668\u3002<\/p>\n<ul>\n<li> <strong>alarm()\uff1a<\/strong>\u521b\u5efa\u4e00\u4e2a\u975e\u7cbe\u786e\u7684\u5b9a\u65f6\u5668\uff0c\u5b83\u5c06\u5728\u6307\u5b9a\u7684\u65f6\u95f4\u540e\u5411\u8fdb\u7a0b\u53d1\u9001 SIGALRM \u4fe1\u53f7\u3002<\/li>\n<li> <strong>sleep()\uff1a<\/strong>\u4f7f\u5f53\u524d\u8fdb\u7a0b\u4f11\u7720\u6307\u5b9a\u7684\u65f6\u95f4\u3002<\/li>\n<li> <strong>usleep()\uff1a<\/strong>\u4f7f\u5f53\u524d\u8fdb\u7a0b\u4f11\u7720\u6307\u5b9a\u7684\u5fae\u79d2\u6570\u3002<\/li>\n<\/ul>\n<p><strong>\u65b9\u6cd5 3\uff1a\u7528\u6237\u7a7a\u95f4\u5e93<\/strong><\/p>\n<p>\u4e00\u4e9b\u7528\u6237\u7a7a\u95f4\u5e93\u4e5f\u63d0\u4f9b\u4e86\u521b\u5efa\u548c\u7ba1\u7406\u5b9a\u65f6\u5668\u7684\u529f\u80fd\u3002<\/p>\n<ul>\n<li> <strong>libevent\uff1a<\/strong>\u4e00\u4e2a\u529f\u80fd\u4e30\u5bcc\u7684\u4e8b\u4ef6\u5904\u7406\u5e93\uff0c\u5b83\u5305\u542b\u4e00\u4e2a\u8de8\u5e73\u53f0\u7684\u5b9a\u65f6\u5668\u5b9e\u73b0\u3002<\/li>\n<li> <strong>Boost.Asio\uff1a<\/strong>\u53e6\u4e00\u4e2a\u8de8\u5e73\u53f0\u7684\u4e8b\u4ef6\u5904\u7406\u5e93\uff0c\u5b83\u5177\u6709\u5b9a\u65f6\u5668\u529f\u80fd\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u600e\u4e48\u5b9e\u73b0\u5b9a\u65f6\u5668\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\u4e2d\u7684\u5b9a\u65f6\u5668\u5b9e\u73b0\uff1a\u5b9a\u65f6\u5668\u5bf9\u8c61\uff1a\u521b\u5efa\u5bf9\u8c61\uff0c\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4\u548c\u5904\u7406\u7a0b\u5e8f\uff0c\u6dfb\u52a0\u81f3\u5185\u6838\u3002\u5185\u6838\u51fd\u6570\uff1a\u4f7f\u7528alarm()\u521b\u5efa\u975e\u7cbe\u786e\u5b9a\u65f6\u5668\u3001sleep()\u6216usleep()\u4f7f\u8fdb\u7a0b\u4f11\u7720\u3002\u7528\u6237\u7a7a\u95f4\u5e93\uff1a\u4f7f\u7528libevent\u6216boost.asio\u7b49\u5e93\u521b\u5efa\u548c\u7ba1\u7406\u5b9a\u65f6\u5668\u3002 \u5982\u4f55\u5728 Linux \u4e2d\u5b9e\u73b0\u5b9a\u65f6\u5668 \u5b9a\u65f6\u5668\u6982\u8ff0 \u5b9a\u65f6\u5668\u662f\u4e00\u79cd\u5185\u6838\u673a\u5236\uff0c\u5b83\u5141\u8bb8\u5728\u7279\u5b9a\u7684\u65f6\u95f4\u95f4\u9694\u6216\u5ef6\u8fdf\u540e\u6267\u884c\u4efb\u52a1\u3002\u5728 Linux \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\u5b9a\u65f6\u5668\u3002 \u65b9\u6cd5 1\uff1a\u5b9a\u65f6\u5668\u5bf9\u8c61 \u5b9a\u65f6\u5668\u5bf9\u8c61\u662f Linux \u4e2d\u6700\u5e38\u7528\u7684\u5b9a\u65f6\u5668\u5b9e\u73b0\u65b9\u5f0f\u3002\u5b83\u5141\u8bb8\u60a8\u521b\u5efa\u5b9a\u65f6\u5668\u5bf9\u8c61\uff0c\u5e76\u6307\u5b9a\u8d85\u65f6\u65f6\u95f4\u548c\u5904\u7406\u7a0b\u5e8f\u3002\u5f53\u5b9a\u65f6\u5668\u8fc7\u671f\u65f6\uff0c\u5904\u7406\u7a0b\u5e8f\u5c06\u88ab\u8c03\u7528\u3002 \u5b9e\u73b0\u6b65\u9aa4\uff1a \u521b\u5efa\u4e00\u4e2a\u5b9a\u65f6\u5668\u5bf9\u8c61\uff1a struct timer_list my_timer; \u767b\u5f55\u540e\u590d\u5236 \u521d\u59cb\u5316\u5b9a\u65f6\u5668\u5bf9\u8c61\uff1a init_timer(&amp;my_timer); \u767b\u5f55\u540e\u590d\u5236 \u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4\uff08\u4ee5\u6beb\u79d2\u4e3a\u5355\u4f4d\uff09\uff1a my_timer.expires = jiffies + msecs_to_jiffies(TIMEOUT_MS); \u767b\u5f55\u540e\u590d\u5236 \u8bbe\u7f6e\u5904\u7406\u7a0b\u5e8f\uff1a my_timer.function = my_timer_handler; \u767b\u5f55\u540e\u590d\u5236 \u6dfb\u52a0\u5b9a\u65f6\u5668\u5230\u5185\u6838\uff1a add_timer(&amp;my_timer); \u767b\u5f55\u540e\u590d\u5236 \u65b9\u6cd5 2\uff1a\u5185\u6838\u51fd\u6570 \u9664\u4e86\u5b9a\u65f6\u5668\u5bf9\u8c61\uff0cLinux \u8fd8\u63d0\u4f9b\u4e86\u51e0\u4e2a\u5185\u6838\u51fd\u6570\u6765\u521b\u5efa\u5b9a\u65f6\u5668\u3002 alarm()\uff1a\u521b\u5efa\u4e00\u4e2a\u975e\u7cbe\u786e\u7684\u5b9a\u65f6\u5668\uff0c\u5b83\u5c06\u5728\u6307\u5b9a\u7684\u65f6\u95f4\u540e\u5411\u8fdb\u7a0b\u53d1\u9001 SIGALRM \u4fe1\u53f7\u3002 sleep()\uff1a\u4f7f\u5f53\u524d\u8fdb\u7a0b\u4f11\u7720\u6307\u5b9a\u7684\u65f6\u95f4\u3002 usleep()\uff1a\u4f7f\u5f53\u524d\u8fdb\u7a0b\u4f11\u7720\u6307\u5b9a\u7684\u5fae\u79d2\u6570\u3002 \u65b9\u6cd5 3\uff1a\u7528\u6237\u7a7a\u95f4\u5e93 \u4e00\u4e9b\u7528\u6237\u7a7a\u95f4\u5e93\u4e5f\u63d0\u4f9b\u4e86\u521b\u5efa\u548c\u7ba1\u7406\u5b9a\u65f6\u5668\u7684\u529f\u80fd\u3002 libevent\uff1a\u4e00\u4e2a\u529f\u80fd\u4e30\u5bcc\u7684\u4e8b\u4ef6\u5904\u7406\u5e93\uff0c\u5b83\u5305\u542b\u4e00\u4e2a\u8de8\u5e73\u53f0\u7684\u5b9a\u65f6\u5668\u5b9e\u73b0\u3002 Boost.Asio\uff1a\u53e6\u4e00\u4e2a\u8de8\u5e73\u53f0\u7684\u4e8b\u4ef6\u5904\u7406\u5e93\uff0c\u5b83\u5177\u6709\u5b9a\u65f6\u5668\u529f\u80fd\u3002 \u4ee5\u4e0a\u5c31\u662f\u600e\u4e48\u5b9e\u73b0\u5b9a\u65f6\u5668\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/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-18193","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/18193","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=18193"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/18193\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=18193"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=18193"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=18193"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}