{"id":3499,"date":"2017-08-05T18:34:30","date_gmt":"2017-08-05T10:34:30","guid":{"rendered":"https:\/\/ixyzero.com\/blog\/?p=3499"},"modified":"2017-08-05T18:34:30","modified_gmt":"2017-08-05T10:34:30","slug":"linux%e4%b8%8bc%e8%af%ad%e8%a8%80%e4%b8%ad%e7%9a%84%e7%ae%80%e5%8d%95%e5%ae%9a%e6%97%b6%e5%99%a8%e6%a0%b7%e4%be%8b","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/3499.html","title":{"rendered":"Linux\u4e0bC\u8bed\u8a00\u4e2d\u7684\u7b80\u5355\u5b9a\u65f6\u5668\u6837\u4f8b"},"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>alarm()+signal()\/sigaction()\u5b9e\u73b0\u7684\u4e0d\u90a3\u4e48\u7cbe\u786e\u7684\u5b9a\u65f6\u5668\u529f\u80fd<\/p>\n<pre class=\"lang:default decode:true\">#include &lt;stdio.h&gt;\r\n#include &lt;unistd.h&gt;\r\n#include &lt;signal.h&gt;\r\n#include &lt;sys\/time.h&gt; \/* struct timeval *\/\r\n#include &lt;time.h&gt; \/* time() *\/\r\n#include &lt;errno.h&gt;\r\n\r\n#include &lt;inttypes.h&gt;\r\n#include &lt;math.h&gt;\r\nvoid print_current_time_with_ms(void)\r\n{\r\n    long            ms; \/\/ Milliseconds\r\n    time_t          s;  \/\/ Seconds\r\n    struct timespec spec;\r\n    clock_gettime(CLOCK_REALTIME, &amp;spec);\r\n\r\n    s  = spec.tv_sec;\r\n    ms = round(spec.tv_nsec \/ 1.0e6); \/\/ Convert nanoseconds to milliseconds\r\n\r\n    printf(\"Current time: %\"PRIdMAX\".%03ld seconds since the Epoch\\n\", (intmax_t)s, ms);\r\n}\r\n\r\nvoid sigalrm_fn(int sig)\r\n{\r\n\tprint_current_time_with_ms();\r\n\talarm(2);\t\/\/ every 2 seconds\r\n\treturn;\r\n}\r\n\r\nint main(void)\r\n{\r\n\tsignal(SIGALRM, sigalrm_fn);\r\n\talarm(1);\t\/\/ first call ?\r\n\twhile(1)\r\n\t\tpause();\r\n\treturn 0;\r\n}\r\n\/* $ .\/a.out\r\nCurrent time: 1501747559.026 seconds since the Epoch\r\nCurrent time: 1501747561.026 seconds since the Epoch\r\nCurrent time: 1501747563.028 seconds since the Epoch\r\nCurrent time: 1501747565.032 seconds since the Epoch\r\nCurrent time: 1501747567.035 seconds since the Epoch\r\n*\/<\/pre>\n<p>setitimer()+sigaction()\u7684\u4e00\u4e2a\u5b9e\u73b0<\/p>\n<pre class=\"lang:default decode:true \">#include &lt;stdio.h&gt;\r\n#include &lt;signal.h&gt;\r\n#include &lt;sys\/time.h&gt;\r\n\r\nint limit = 10;\r\n\r\n\/* signal process *\/\r\nvoid timeout_info(int signo)\r\n{\r\n    if(limit == 0)\r\n    {\r\n        printf(\"Sorry, time limit reached.\\n\");\r\n        return;\r\n    }\r\n    printf(\"Only %d seconds left.\\n\", limit--);\r\n}\r\n\r\n\/* init sigaction *\/\r\nvoid init_sigaction(void)\r\n{\r\n    struct sigaction act;\r\n\r\n    act.sa_handler = timeout_info;\r\n    act.sa_flags   = 0;\r\n    sigemptyset(&amp;act.sa_mask);\r\n    sigaction(SIGPROF, &amp;act, NULL);\r\n}\r\n\r\n\/* init *\/\r\nvoid init_time(void)\r\n{\r\n    struct itimerval val;\r\n\r\n    val.it_value.tv_sec = 1;\r\n    val.it_value.tv_usec = 0;\r\n    val.it_interval = val.it_value;\r\n    setitimer(ITIMER_PROF, &amp;val, NULL);\r\n}\r\n\r\nint main(void)\r\n{\r\n    init_sigaction();\r\n    init_time();\r\n    printf(\"You have only 10 seconds for thinking.\\n\");\r\n\r\n    while(1);\r\n    return 0;\r\n}<\/pre>\n<p>\u591a\u4e2asetitimer()\u7684\u793a\u4f8b<\/p>\n<pre class=\"lang:default decode:true\">#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;unistd.h&gt;\r\n#include &lt;signal.h&gt;\r\n#include &lt;time.h&gt;\r\n#include &lt;sys\/time.h&gt;\r\n\r\n#include &lt;inttypes.h&gt;\r\n#include &lt;math.h&gt;\r\nvoid print_current_time_with_ms(void)\r\n{\r\n    long            ms; \/\/ Milliseconds\r\n    time_t          s;  \/\/ Seconds\r\n    struct timespec spec;\r\n    clock_gettime(CLOCK_REALTIME, &amp;spec);\r\n\r\n    s  = spec.tv_sec;\r\n    ms = round(spec.tv_nsec \/ 1.0e6); \/\/ Convert nanoseconds to milliseconds\r\n\r\n    printf(\"Current time: %\"PRIdMAX\".%03ld seconds since the Epoch\\n\", (intmax_t)s, ms);\r\n}\r\n\r\nint sec = 5;\r\n\r\nvoid sigroutine(int signo){\r\n\tswitch (signo) {\r\n\t\tcase SIGALRM:\r\n\t\t\tprintf(\"Catch a signal -- SIGALRM\\n\");\r\n\t\t\tprint_current_time_with_ms();\r\n\t\t\tsignal(SIGALRM, sigroutine);\r\n\t\t\tbreak;\r\n\t\tcase SIGVTALRM:\r\n\t\t\tprintf(\"Catch a signal -- SIGVTALRM\\n\");\r\n\t\t\tprint_current_time_with_ms();\r\n\t\t\tsignal(SIGVTALRM, sigroutine);\r\n\t\t\tbreak;\r\n\t}\r\n\treturn;\r\n}\r\n\r\nint main() {\r\n\tprintf(\"process id is %d\\n\", getpid());\r\n\r\n\tsignal(SIGALRM, sigroutine);\r\n\tsignal(SIGVTALRM, sigroutine);\r\n\r\n\tstruct itimerval value, ovalue, value2; \/\/(1)\r\n\r\n\tvalue.it_value.tv_sec = 1;\r\n\tvalue.it_value.tv_usec = 0;\r\n\tvalue.it_interval.tv_sec = 1;\r\n\tvalue.it_interval.tv_usec = 0;\r\n\tsetitimer(ITIMER_REAL, &amp;value, &amp;ovalue); \/\/(2)\r\n\r\n\tvalue2.it_value.tv_sec = 0;\r\n\tvalue2.it_value.tv_usec = 500000;\r\n\tvalue2.it_interval.tv_sec = 0;\r\n\tvalue2.it_interval.tv_usec = 500000;\r\n\tsetitimer(ITIMER_VIRTUAL, &amp;value2, &amp;ovalue);\r\n\t\r\n\tfor(;;)\r\n\t\t;\r\n\treturn 0;\r\n}<\/pre>\n<h5 id=\"id-\u6a21\u677f-\u53c2\u8003\u94fe\u63a5\uff1a\">\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n<p><strong>Linux\u5b9a\u65f6\u5668\u7684\u4f7f\u7528<\/strong><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>Linux\u4e0b\u5b9a\u65f6\u5668\u7684\u4f7f\u7528<br \/>\n<a href=\"http:\/\/www.jyguagua.com\/?p=1700\">http:\/\/www.jyguagua.com\/?p=1700<\/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 alarm()+signal()\/sigaction() [&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":[900,100,30,899,895],"class_list":["post-3499","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-linux","category-programing","tag-alarm","tag-c","tag-linux","tag-select","tag-setitimer"],"views":2957,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/3499","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=3499"}],"version-history":[{"count":1,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/3499\/revisions"}],"predecessor-version":[{"id":3500,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/3499\/revisions\/3500"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=3499"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=3499"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=3499"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}