{"id":3496,"date":"2017-08-05T18:33:49","date_gmt":"2017-08-05T10:33:49","guid":{"rendered":"https:\/\/ixyzero.com\/blog\/?p=3496"},"modified":"2017-08-05T18:33:49","modified_gmt":"2017-08-05T10:33:49","slug":"linux%e4%b8%ad%e7%9a%84%e5%ae%9a%e6%97%b6%e5%99%a8%e4%b8%8e%e4%bc%91%e7%9c%a0","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/3496.html","title":{"rendered":"Linux\u4e2d\u7684\u5b9a\u65f6\u5668\u4e0e\u4f11\u7720"},"content":{"rendered":"<p>=Start=<\/p>\n<h4 id=\"id-\u6a21\u677f-\u7f18\u7531\uff1a\">\u7f18\u7531\uff1a<\/h4>\n<p>\u5b66\u4e60\u9700\u8981<\/p>\n<h4 id=\"id-\u6a21\u677f-\u6b63\u6587\uff1a\">\u6b63\u6587\uff1a<\/h4>\n<h5 id=\"id-\u6a21\u677f-\u53c2\u8003\u89e3\u7b54\uff1a\">\u53c2\u8003\u89e3\u7b54\uff1a<\/h5>\n<p><a href=\"http:\/\/man7.org\/linux\/man-pages\/man2\/setitimer.2.html\">http:\/\/man7.org\/linux\/man-pages\/man2\/setitimer.2.html<\/a><\/p>\n<p>\u5728 2.4 \u7684\u5185\u6838\u4e2d\uff0c\u5e76\u6ca1\u6709\u63d0\u4f9b POSIX timer \u7684\u652f\u6301\uff0c\u8981\u5728\u8fdb\u7a0b\u73af\u5883\u4e2d\u652f\u6301\u591a\u4e2a\u5b9a\u65f6\u5668\uff0c\u53ea\u80fd\u81ea\u5df1\u6765\u5b9e\u73b0\uff0c\u597d\u5728 Linux \u63d0\u4f9b\u4e86 setitimer(2) \u7684\u63a5\u53e3\u3002\u5b83\u662f\u4e00\u4e2a\u5177\u6709\u95f4\u9694\u529f\u80fd\u7684\u5b9a\u65f6\u5668 (interval timer)\uff0c\u4f46\u5982\u679c\u60f3\u5728\u8fdb\u7a0b\u73af\u5883\u4e2d\u652f\u6301\u591a\u4e2a\u8ba1\u65f6\u5668\uff0c\u4e0d\u5f97\u4e0d\u81ea\u5df1\u6765\u7ba1\u7406\u6240\u6709\u7684\u8ba1\u65f6\u5668\u3002<\/p>\n<pre class=\"lang:default decode:true \">#include &lt;sys\/time.h&gt;\r\nint setitimer(int which, const struct itimerval *new_value,struct itimerval *old_value);<\/pre>\n<hr \/>\n<p><a href=\"http:\/\/man7.org\/linux\/man-pages\/man2\/timer_create.2.html\">http:\/\/man7.org\/linux\/man-pages\/man2\/timer_create.2.html<\/a><\/p>\n<p>Linux \u81ea 2.6 \u5f00\u59cb\uff0c\u5df2\u7ecf\u5f00\u59cb\u652f\u6301 POSIX timer \u6240\u5b9a\u4e49\u7684\u5b9a\u65f6\u5668\uff0c\u5b83\u4e3b\u8981\u7531\u4e0b\u9762\u7684\u63a5\u53e3\u6784\u6210 :<\/p>\n<pre class=\"lang:default decode:true\">#include &lt;signal.h&gt;\r\n#include &lt;time.h&gt;\r\ntimer_create()\r\ntimer_settime()\r\ntimer_gettime()\r\ntimer_delete()<\/pre>\n<hr \/>\n<p><a href=\"http:\/\/man7.org\/linux\/man-pages\/man2\/timerfd_create.2.html\">http:\/\/man7.org\/linux\/man-pages\/man2\/timerfd_create.2.html<\/a><\/p>\n<p>Linux \u63d0\u4f9b\u4e86\u57fa\u4e8e\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u76f8\u5173\u5b9a\u65f6\u5668\u63a5\u53e3\uff1a<\/p>\n<pre class=\"lang:default decode:true\">#include &lt;sys\/timerfd.h&gt; \r\nint timerfd_create(int clockid, int flags);\r\nint timerfd_settime(int fd, int flags, const struct itimerspec *new_value, struct itimerspec *old_value);\r\nint timerfd_gettime(int fd, struct itimerspec *curr_value);<\/pre>\n<hr \/>\n<p>\u9664\u4e86\u4ee5\u4e0a3\u79cdLinux\u539f\u751f\u63d0\u4f9b\u7684\u8ba1\u65f6\u5668\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e9b\u5176\u5b83\u65b9\u6cd5\u6765\u8fbe\u5230\u7c7b\u4f3c\u7684\u76ee\u7684\uff1a<\/p>\n<ol>\n<li>\u7528 sleep\/usleep\/select \u51fd\u6570\u8ba9\u8fdb\u7a0b\u7761\u7720\u4e00\u6bb5\u65f6\u95f4\uff0c\u4f7f\u7528alarm\u5b9a\u65f6\u53d1\u51fa\u4e00\u4e2a\u4fe1\u53f7\uff0c\u7136\u540e\u7528signal\/sigaction\u51fd\u6570\u63a5\u6536\u4fe1\u53f7\u5e76\u8fdb\u884c\u7279\u5b9a\u5904\u7406\uff1b<\/li>\n<li>\u8fd8\u6709\u90a3\u5c31\u662f\u7528 gettimeofday\/difftime \u7b49\u81ea\u5df1\u6765\u8ba1\u7b97\u65f6\u95f4\u95f4\u9694\uff0c\u7136\u540e\u65f6\u95f4\u5230\u4e86\u5c31\u6267\u884c\u67d0\u4e00\u4efb\u52a1\uff0c\u4f46\u662f\u8fd9\u79cd\u65b9\u6cd5\u6548\u7387\u4f4e\uff0c\u6240\u4ee5\u4e0d\u5e38\u7528\u3002<\/li>\n<\/ol>\n<h5 id=\"id-\u6a21\u677f-\u53c2\u8003\u94fe\u63a5\uff1a\">\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n<p><span style=\"color: #ff0000;\"><strong>Linux \u4e0b\u5b9a\u65f6\u5668\u7684\u5b9e\u73b0\u65b9\u5f0f\u5206\u6790<\/strong><\/span><br \/>\n<a href=\"https:\/\/www.ibm.com\/developerworks\/cn\/linux\/l-cn-timers\/index.html\">https:\/\/www.ibm.com\/developerworks\/cn\/linux\/l-cn-timers\/index.html<\/a><\/p>\n<p>\u6d45\u6790 Linux \u4e2d\u7684\u65f6\u95f4\u7f16\u7a0b\u548c\u5b9e\u73b0\u539f\u7406\uff0c\u7b2c 1 \u90e8\u5206: Linux \u5e94\u7528\u5c42\u7684\u65f6\u95f4\u7f16\u7a0b<br \/>\n<a href=\"https:\/\/www.ibm.com\/developerworks\/cn\/linux\/1307_liuming_linuxtime1\/\">https:\/\/www.ibm.com\/developerworks\/cn\/linux\/1307_liuming_linuxtime1\/<\/a><\/p>\n<p>\u6d45\u6790 Linux \u4e2d\u7684\u65f6\u95f4\u7f16\u7a0b\u548c\u5b9e\u73b0\u539f\u7406\uff0c\u7b2c 3 \u90e8\u5206: Linux \u5185\u6838\u7684\u5de5\u4f5c<br \/>\n<a href=\"https:\/\/www.ibm.com\/developerworks\/cn\/linux\/1308_liuming_linuxtime3\/index.html\">https:\/\/www.ibm.com\/developerworks\/cn\/linux\/1308_liuming_linuxtime3\/index.html<\/a><\/p>\n<p><strong>Linux \u5b9e\u73b0\u5b9a\u65f6\u5668<\/strong><br \/>\n<a href=\"https:\/\/liuliqiang.info\/post\/four-way-to-implement-linux-cron\/\">https:\/\/liuliqiang.info\/post\/four-way-to-implement-linux-cron\/<\/a><br \/>\n<a href=\"http:\/\/man7.org\/linux\/man-pages\/man2\/setitimer.2.html\">http:\/\/man7.org\/linux\/man-pages\/man2\/setitimer.2.html<\/a><\/p>\n<p>\u8ba1\u65f6\u5668\u7684\u539f\u7406\u548c\u5b9e\u73b0<br \/>\n<a href=\"https:\/\/objectkuan.gitbooks.io\/ucore-docs\/lab7\/lab7_3_2_timer_implement.html\">https:\/\/objectkuan.gitbooks.io\/ucore-docs\/lab7\/lab7_3_2_timer_implement.html<\/a><\/p>\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/12764771\/timers-in-linux-in-c\">https:\/\/stackoverflow.com\/questions\/12764771\/timers-in-linux-in-c<\/a><br \/>\n<a href=\"https:\/\/stackoverflow.com\/questions\/5540245\/loops-timers-in-c\">https:\/\/stackoverflow.com\/questions\/5540245\/loops-timers-in-c<\/a><br \/>\n<a href=\"https:\/\/stackoverflow.com\/questions\/12463554\/timer-library-in-c\">https:\/\/stackoverflow.com\/questions\/12463554\/timer-library-in-c<\/a><\/p>\n<p><span style=\"color: #ff0000;\"><strong>Linux\u5b9a\u65f6\u5668\u7684\u4f7f\u7528<\/strong><\/span><br \/>\n<a href=\"http:\/\/www.cppblog.com\/jerryma\/archive\/2012\/01\/31\/164704.html\">http:\/\/www.cppblog.com\/jerryma\/archive\/2012\/01\/31\/164704.html<\/a><\/p>\n<p><strong>linux\u4e0b\u4f7f\u7528select\u5b9e\u73b0\u7cbe\u786e\u5b9a\u65f6\u5668<\/strong><br \/>\n<a href=\"http:\/\/www.cnblogs.com\/jjdiaries\/p\/3404380.html\">http:\/\/www.cnblogs.com\/jjdiaries\/p\/3404380.html<\/a><\/p>\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u5b66\u4e60\u9700\u8981 \u6b63\u6587\uff1a \u53c2\u8003\u89e3\u7b54\uff1a http:\/\/man7.org\/linux\/man-pa [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,11,7],"tags":[30,895,896,897,898],"class_list":["post-3496","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-linux","category-programing","tag-linux","tag-setitimer","tag-timer_create","tag-timerfd_create","tag-898"],"views":4618,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/3496","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=3496"}],"version-history":[{"count":1,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/3496\/revisions"}],"predecessor-version":[{"id":3497,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/3496\/revisions\/3497"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=3496"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=3496"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=3496"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}