{"id":3505,"date":"2017-08-05T18:34:55","date_gmt":"2017-08-05T10:34:55","guid":{"rendered":"https:\/\/ixyzero.com\/blog\/?p=3505"},"modified":"2017-08-05T18:34:55","modified_gmt":"2017-08-05T10:34:55","slug":"linux%e4%b8%8b%e4%bd%bf%e7%94%a8select%e5%ae%9e%e7%8e%b0%e7%b2%be%e7%a1%ae%e5%ae%9a%e6%97%b6%e5%99%a8","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/3505.html","title":{"rendered":"Linux\u4e0b\u4f7f\u7528select\u5b9e\u73b0\u7cbe\u786e\u5b9a\u65f6\u5668"},"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>\u9996\u5148\u770b\u770bselect\u51fd\u6570\u539f\u578b\u5982\u4e0b\uff1a<\/p>\n<pre class=\"lang:default decode:true \">int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);<\/pre>\n<p>\u53c2\u6570\u8bf4\u660e\uff1a<\/p>\n<p>slect\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570nfds\u4e3afdset\u96c6\u5408\u4e2d\u6700\u5927\u63cf\u8ff0\u7b26\u503c\u52a01\uff0cfdset\u662f\u4e00\u4e2a\u4f4d\u6570\u7ec4\uff0c\u5176\u5927\u5c0f\u9650\u5236\u4e3a__FD_SETSIZE\uff081024\uff09\uff0c\u4f4d\u6570\u7ec4\u7684\u6bcf\u4e00\u4f4d\u4ee3\u8868\u5176\u5bf9\u5e94\u7684\u63cf\u8ff0\u7b26\u662f\u5426\u9700\u8981\u88ab\u68c0\u67e5\u3002<\/p>\n<p>select\u7684\u7b2c\u4e8c\u4e09\u56db\u4e2a\u53c2\u6570\u8868\u793a\u9700\u8981\u5173\u6ce8\u8bfb\u3001\u5199\u3001\u9519\u8bef\u4e8b\u4ef6\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u4f4d\u6570\u7ec4\uff0c\u8fd9\u4e9b\u53c2\u6570\u65e2\u662f\u8f93\u5165\u53c2\u6570\u4e5f\u662f\u8f93\u51fa\u53c2\u6570\uff0c\u53ef\u80fd\u4f1a\u88ab\u5185\u6838\u4fee\u6539\u7528\u4e8e\u6807\u793a\u54ea\u4e9b\u63cf\u8ff0\u7b26\u4e0a\u53d1\u751f\u4e86\u5173\u6ce8\u7684\u4e8b\u4ef6\u3002\u6240\u4ee5\u6bcf\u6b21\u8c03\u7528select\u524d\u90fd\u9700\u91cd\u65b0\u521d\u59cb\u5316fdset\u3002<\/p>\n<p>timeout\u53c2\u6570\u4e3a\u8d85\u65f6\u65f6\u95f4\uff0c\u8be5\u7ed3\u6784\u4f1a\u88ab\u5185\u6838\u4fee\u6539\uff0c\u5176\u503c\u4e3a\u8d85\u65f6\u5269\u4f59\u7684\u65f6\u95f4\u3002<\/p>\n<pre class=\"lang:default decode:true \">#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;sys\/time.h&gt;\r\n#include &lt;sys\/types.h&gt;\r\n#include &lt;unistd.h&gt;\r\n\r\nint main(int argc, char const *argv[])\r\n{\r\n    fd_set rfds;\r\n    \/* Watch stdin (fd 0) to see when it has input. *\/\r\n    FD_ZERO(&amp;rfds);\r\n    FD_SET(0, &amp;rfds);\r\n\r\n    struct timeval tv;\r\n    \/* Wait up to five seconds. *\/\r\n    tv.tv_sec = 5;\r\n    tv.tv_usec = 0;\r\n\r\n    int retval;\r\n    retval = select(1, &amp;rfds, NULL, NULL, &amp;tv);\r\n    \/* Don't rely on the value of tv now! *\/\r\n\r\n    if (retval == -1)\r\n        perror(\"select()\");\r\n    else if (retval)\r\n        printf(\"Data is available now.\\n\");\r\n    \/* FD_ISSET(0, &amp;rfds) will be true. *\/\r\n    else\r\n        printf(\"No data within five seconds.\\n\");\r\n\r\n    printf(\"Hi, this is just a printf to test if select() will block code below select() or not.\\n\");  \/\/ result: block\r\n\r\n    return EXIT_SUCCESS;\r\n}<\/pre>\n<hr \/>\n<p>\u5229\u7528select\u5b9e\u73b0\u5b9a\u65f6\u5668\uff0c\u9700\u8981\u5229\u7528\u5176timeout\u53c2\u6570,\u6ce8\u610f\u5230\uff1a<br \/>\n1\uff09select\u51fd\u6570\u4f7f\u7528\u4e86\u4e00\u4e2a\u7ed3\u6784\u4f53timeval\u4f5c\u4e3a\u5176\u53c2\u6570\u3002<br \/>\n2\uff09select\u51fd\u6570\u4f1a\u66f4\u65b0timeval\u7684\u503c\uff0ctimeval\u4fdd\u6301\u7684\u503c\u4e3a\u5269\u4f59\u65f6\u95f4\u3002<\/p>\n<p>\u5982\u679c\u6211\u4eec\u6307\u5b9a\u4e86\u53c2\u6570timeval\u7684\u503c\uff0c\u800c\u5c06\u5176\u4ed6\u53c2\u6570\u90fd\u7f6e\u4e3a0\u6216\u8005NULL\uff0c\u90a3\u4e48\u5728\u65f6\u95f4\u8017\u5c3d\u540e\uff0cselect\u51fd\u6570\u4fbf\u8fd4\u56de\uff0c\u57fa\u4e8e\u8fd9\u4e00\u70b9\uff0c\u6211\u4eec\u53ef\u4ee5\u5229\u7528select\u5b9e\u73b0\u7cbe\u786e\u5b9a\u65f6\u3002<\/p>\n<p>timeval\u7684\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n<pre class=\"lang:default decode:true \">struct timeval {\r\n    long tv_sec; \/* seconds *\/\r\n    long tv_usec; \/* microseconds *\/\r\n}<\/pre>\n<p>\u6211\u4eec\u53ef\u4ee5\u770b\u51fa\u5176\u53ef\u4ee5\u7cbe\u786e\u5230 microseconds \u4e5f\u5373\u5fae\u79d2\u3002\u4e0b\u9762\u662f\u5b9e\u9645\u4ee3\u7801\uff1a<\/p>\n<pre class=\"lang:default decode:true \">#include &lt;stdio.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 print_current_timestamp(void)\r\n{\r\n    time_t ltime; \/* calendar time *\/\r\n    ltime = time(NULL); \/* get current cal time *\/\r\n    \/\/ printf(\"%s\", asctime(localtime(&amp;ltime)));\r\n    printf(\"%s\", ctime(&amp;ltime));\r\n}\r\n\r\nvoid seconds_sleep(unsigned seconds){\r\n    struct timeval tv;\r\n    tv.tv_sec = seconds;\r\n    tv.tv_usec = 0;\r\n    int err;\r\n    do {\r\n       err = select(0, NULL, NULL, NULL, &amp;tv);\r\n    } while(err&lt;0 &amp;&amp; errno==EINTR);\r\n}\r\n\r\nvoid milliseconds_sleep(unsigned long mSec){\r\n    struct timeval tv;\r\n    tv.tv_sec = mSec\/1000;\r\n    tv.tv_usec = (mSec%1000)*1000;\r\n    int err;\r\n    do {\r\n       err = select(0, NULL, NULL, NULL, &amp;tv);\r\n    } while(err&lt;0 &amp;&amp; errno==EINTR);\r\n}\r\n\r\nvoid microseconds_sleep(unsigned long uSec){\r\n    struct timeval tv;\r\n    tv.tv_sec = uSec\/1000000;\r\n    tv.tv_usec = uSec%1000000;\r\n    int err;\r\n    do {\r\n        err = select(0, NULL, NULL, NULL, &amp;tv);\r\n    } while(err&lt;0 &amp;&amp; errno==EINTR);\r\n}\r\n\r\nint main(int argc, char const *argv[])\r\n{\r\n\tint i;\r\n    for(i=0; i&lt;5; ++i) {\r\n\t    \/\/ printf(\"[%d]\\t%lu\\n\", i, (unsigned long)time(NULL));\r\n\t    \/\/ print_current_time_with_ms();\r\n\t    print_current_timestamp();\r\n\t    \/\/ seconds_sleep(1);\r\n\t    \/\/ milliseconds_sleep(1500);\r\n\t    microseconds_sleep(1900000);\r\n    }\r\n    return 0;\r\n}<\/pre>\n<h5 id=\"id-\u6a21\u677f-\u53c2\u8003\u94fe\u63a5\uff1a\">\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n<div>Linux C\u7684select\u51fd\u6570\u7684\u4f7f\u7528<\/div>\n<div><a href=\"http:\/\/blog.csdn.net\/u010889616\/article\/details\/48142483\">http:\/\/blog.csdn.net\/u010889616\/article\/details\/48142483<\/a><\/div>\n<div><\/div>\n<div>linux c\u4e2dselect\u4f7f\u7528\u6280\u5de7<\/div>\n<div><a href=\"http:\/\/www.cnblogs.com\/allensun-193\/p\/4890570.html\">http:\/\/www.cnblogs.com\/allensun-193\/p\/4890570.html<\/a><\/div>\n<div><\/div>\n<div>linux c\/c++\u7f51\u7edc\u7f16\u7a0b\u4e4b\u2014select\u6a21\u578b<\/div>\n<div><a href=\"http:\/\/blog.csdn.net\/coder_yi_liu\/article\/details\/8249421\">http:\/\/blog.csdn.net\/coder_yi_liu\/article\/details\/8249421<\/a><\/div>\n<div><\/div>\n<div>linux c\u5b66\u4e60\u7b14\u8bb0&#8212;-select\u51fd\u6570\u8be6\u89e3<\/div>\n<div><a href=\"http:\/\/lobert.iteye.com\/blog\/1768502\">http:\/\/lobert.iteye.com\/blog\/1768502<\/a><\/div>\n<div><\/div>\n<div><span style=\"color: #ff0000;\"><strong>linux\u4e0b\u4f7f\u7528select\u5b9e\u73b0\u7cbe\u786e\u5b9a\u65f6\u5668<\/strong><\/span><\/div>\n<div><a href=\"http:\/\/www.cnblogs.com\/jjdiaries\/p\/3404380.html\">http:\/\/www.cnblogs.com\/jjdiaries\/p\/3404380.html<\/a><\/div>\n<div><\/div>\n<div>Linux\u4e0b\u83b7\u53d6\u5f53\u524d\u65f6\u95f4(\u6beb\u79d2\u7cbe\u5ea6) # clock_gettime()<\/div>\n<div><a href=\"https:\/\/stackoverflow.com\/questions\/3756323\/getting-the-current-time-in-milliseconds\">https:\/\/stackoverflow.com\/questions\/3756323\/getting-the-current-time-in-milliseconds<\/a><\/div>\n<div><a href=\"https:\/\/stackoverflow.com\/questions\/11765301\/how-do-i-get-the-unix-timestamp-in-c-as-an-int\">https:\/\/stackoverflow.com\/questions\/11765301\/how-do-i-get-the-unix-timestamp-in-c-as-an-int<\/a><\/div>\n<div><\/div>\n<div>=END=<\/div>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u5b66\u4e60\u9700\u8981 \u6b63\u6587\uff1a \u53c2\u8003\u89e3\u7b54\uff1a \u9996\u5148\u770b\u770bselect\u51fd\u6570\u539f\u578b\u5982\u4e0b\uff1a int select [&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":[100,903,30,899],"class_list":["post-3505","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-linux","category-programing","tag-c","tag-clock_gettime","tag-linux","tag-select"],"views":2925,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/3505","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=3505"}],"version-history":[{"count":1,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/3505\/revisions"}],"predecessor-version":[{"id":3506,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/3505\/revisions\/3506"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=3505"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=3505"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=3505"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}