{"id":726,"date":"2017-03-06T16:09:00","date_gmt":"2017-03-06T08:09:00","guid":{"rendered":"http:\/\/www.5x44.cn\/?p=726"},"modified":"2017-03-06T16:09:00","modified_gmt":"2017-03-06T08:09:00","slug":"sleep%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/www.5x44.cn\/?p=726","title":{"rendered":"sleep\u51fd\u6570"},"content":{"rendered":"<p>\u6211\u4eec\u5df2\u7ecf\u5728\u4f8b\u5b50\u4e2d\u4f7f\u7528\u4e86\u5f88\u591a\u6b21sleep\u51fd\u6570\uff0c\u5e76\u5728Figures10.7\u548c10.8\u4e2d\u663e\u793a\u4e86\u4e24\u4e2a\u7f3a\u9677\u3002<\/p>\n<table border=\"0\" width=\"100%\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<table class=\"allBorders\" style=\"height: 161px;\" border=\"1\" width=\"512\" rules=\"none\" cellspacing=\"0\" cellpadding=\"5\">\n<tbody>\n<tr>\n<td class=\"docTableCell\" align=\"left\" valign=\"top\">\n<pre><span style=\"color: #993300;\">#include &lt;unistd.h&gt;\r\n\r\nunsigned int sleep(unsigned int <span class=\"docEmphItalicAlt\">seconds<\/span>);\r\n<\/span><\/pre>\n<\/td>\n<\/tr>\n<tr>\n<td class=\"docTableCell\" align=\"right\" valign=\"top\">\n<p class=\"docText\">Returns: 0 or number of unslept seconds<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u8fd9\u4e2a\u51fd\u6570\u5f15\u53d1\u8c03\u7528\u7684\u8fdb\u7a0b\u88ab\u6302\u8d77\uff0c\u76f4\u5230\uff1a<\/p>\n<ol>\n<li><em>seconds<\/em>\u6307\u5b9a\u7684\u65f6\u95f4\u903e\u671f\u3002<\/li>\n<li>\u4e00\u4e2a\u4fe1\u53f7\u88ab\u8fdb\u7a0b\u6355\u83b7\u5e76\u4e14\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u8fd4\u56de\u3002<\/li>\n<\/ol>\n<p>\u4f7f\u7528<span style=\"color: #993300;\">alarm<\/span>\u4fe1\u53f7\uff0c\u56e0\u4e3a\u5176\u5b83\u8fdb\u7a0b\u7684\u6d3b\u52a8\uff0c\u5b9e\u9645\u8fd4\u56de\u65f6\u95f4\u53ef\u80fd\u8981\u6bd4\u9700\u6c42\u7684\u65f6\u95f4\u8981\u957f\u4e00\u4e9b\u3002<\/p>\n<p>\u7b2c\u4e00\u79cd\u60c5\u51b5\u51fd\u6570\u8fd4\u56de0\u3002\u5f53<span style=\"color: #993300;\">sleep<\/span>\u65e9\u4e8e\u9884\u5b9a\u65f6\u95f4\u65f6\uff0c\u65f6\u56e0\u4e3a\u67d0\u4e9b\u4fe1\u53f7\u88ab\u6355\u83b7\uff08\u7b2c\u4e8c\u79cd\u60c5\u51b5\uff09\uff0c\u8fd4\u56de\u7684\u503c\u662f\u5269\u4f59\u7684\u79d2\u6570\u3002<\/p>\n<p>\u867d\u7136<span style=\"color: #993300;\">sleep<\/span>\u80fd\u88ab<span style=\"color: #993300;\">alarm<\/span>\u51fd\u6570\u5b9e\u73b0\uff08APUE10.10\u8282\uff09\uff0c\u4f46\u8fd9\u4e0d\u662f\u5fc5\u987b\u7684\u3002\u5982\u679c\u4f7f\u7528\u4e86<span style=\"color: #993300;\">alarm<\/span>\uff0c\u90a3\u4e48\u8fd9\u4e24\u4e2a\u51fd\u6570\u4f1a\u76f8\u4e92\u5e72\u6270\u3002\uff08If alarm is used, however, there can be interactions between the two functions.\uff09POSIX.1\u6807\u51c6\u4e0d\u5305\u542b\u6240\u6709\u672a\u6307\u5b9a\u7684\u76f8\u4e92\u4f5c\u7528\u3002\uff08The POSIX.1 standard leaves all these interactions unspecified.\uff09\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u6267\u884c<span style=\"color: #993300;\">alarm(10)<\/span>\u540e3\u79d2\u79cd\u6267\u884c<span style=\"color: #993300;\">sleep(5)<\/span>\uff0c\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1fsleep\u4f1a\u8fd4\u56de5\u79d2\uff08\u5047\u8bbe\u4e0e\u6b64\u540c\u65f6\u6ca1\u6709\u5176\u5b83\u4fe1\u53f7\u88ab\u6355\u83b7\uff09\uff0c\u4f46\u662f\u4e24\u79d2\u540e\u4f1a\u6709\u5176\u4ed6\u7684SIGALRM\u4fe1\u53f7\u4ea7\u751f\u4e48\uff1f\u8fd9\u4f9d\u8d56\u4e8e\u5177\u4f53\u5b9e\u73b0\u3002<\/p>\n<p>\u3010Solaris 9\u4f7f\u7528<span style=\"color: #993300;\">alarm<\/span>\u5b9e\u73b0<span style=\"color: #993300;\">sleep<\/span>\u3002Solaris\u7684\u624b\u518c\u63cf\u8ff0<span style=\"color: #993300;\">sleep<\/span>\u8bf4\uff1a\u4e4b\u524d\u8ba1\u5212\u7684<span style=\"color: #993300;\">alarm<\/span>\u88ab\u9002\u5f53\u5904\u7406\u3002\u4f8b\u5982\uff0c\u62ff\u521a\u624d\u90a3\u4e2a\u4f8b\u5b50\u6765\u8bf4\uff0c\u5728<span style=\"color: #993300;\">sleep<\/span>\u8fd4\u56de\u524d\uff0c\u5b83\u5c06\u4f1a\u91cd\u65b0\u4e0d\u719f<span style=\"color: #993300;\">alarm<\/span>\u57282\u79d2\u540e\u53d1\u751f\uff1b<span style=\"color: #993300;\">sleep<\/span>\u8fd4\u56de0\u3002\uff08\u660e\u663e\uff0c<span style=\"color: #993300;\">sleep<\/span>\u5fc5\u987b\u4fdd\u5b58<span style=\"color: #993300;\">SIGALRM<\/span>\u7684\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u7684\u5730\u5740\u5e76\u4e14\u5728\u8fd0\u884c\u524d\u91cd\u7f6e\u5b83\u3002\uff09\u540c\u6837\uff0c\u5982\u679c\u6267\u884c<span style=\"color: #993300;\">alarm(6)<\/span>\u4e4b\u540e\u7b49\u5f853\u79d2\u540e\u6267\u884c<span style=\"color: #993300;\">sleep(5)<\/span>\uff0c<span style=\"color: #993300;\">sleep<\/span>3\u79d2\u540e\u8fd4\u56de\uff08\u5f53alarm\u5173\u95ed\u65f6\uff09\uff0c\u800c\u4e0d\u662f5\u79d2\u540e\u8fd4\u56de\u3002\u8fd9\u91cc\uff0csleeep\u8fd4\u56de2\uff08\u672a\u4f7f\u7528\u7684\u65f6\u95f4\uff09\u3002<\/p>\n<p>FreeBSD 5.2.1, Linux 2.4.22\u548cMac OS X 10.3\uff0c\u6362\u53e5\u8bdd\u8bf4\uff0c\u4f7f\u7528\u5176\u5b83\u6280\u672f\u7684\uff1a\u7531<span style=\"color: #993300;\">nanosleep<\/span>(2)\u51fd\u6570\u5ef6\u8fdf\u3002\u8fd9\u4e2a\u51fd\u6570\u5728Single UNIX Specification\u4e2d\u5b9e\u65f6\u6269\u5c55\uff0c\u5b83\u6709\u5f88\u9ad8\u7684\u7cbe\u5ea6\u3002\uff08This function is specified to be a high-resolution delay by the real-time extensions in the Single UNIX Specification.\uff09\u8fd9\u4e2a\u51fd\u6570\u5141\u8bb8<span style=\"color: #993300;\">sleep<\/span>\u7684\u5b9e\u73b0\u4fe1\u53f7\u72ec\u7acb\u3002\uff08This function allows the implementation of sleep to be independent of signals.\uff09<\/p>\n<p>\u4e3a\u4e86\u79fb\u690d\uff0c\u4f60\u4e0d\u5e94\u8be5\u505a\u4efb\u4f55\u5173\u4e8e<span style=\"color: #993300;\">sleep<\/span>\u5b9e\u73b0\u7684\u5047\u8bbe\uff0c\u4f46\u662f\u5982\u679c\u4f60\u6709\u4efb\u4f55\u6df7\u5408\u4f7f\u7528<span style=\"color: #993300;\">sleep<\/span>\u548c\u5176\u5b83\u65f6\u95f4\u51fd\u6570\u7684\u60f3\u6cd5\uff0c\u4f60\u9700\u8981\u77e5\u9053\u5b83\u4eec\u4e4b\u95f4\u7684\u76f8\u4e92\u4f5c\u7528\u3002\u3011<\/p>\n<h1>\u4f8b\u5b50<\/h1>\n<p>Figure10.29\u663e\u793a\u4e86POSIX.1\u7684<span style=\"color: #993300;\">sleep<\/span>\u51fd\u6570\u5b9e\u73b0\u3002\u8fd9\u4e2a\u51fd\u6570\u662fFigure10.7\u7684\u4e00\u4e2a\u4fee\u6539\u7248\u672c\uff0c\u5b83\u80fd\u53ef\u9760\u7684\u5904\u7406\u4fe1\u53f7\uff0c\u907f\u514d\u65e9\u671f\u7248\u672c\u4e2d\u7684\u7ade\u4e89\u6761\u4ef6\u3002\u6211\u4eec\u4ecd\u7136\u6ca1\u6709\u5904\u7406\u4e4b\u524d\u8bbe\u7f6ealarm\u548csleep\u4e4b\u95f4\u7684\u5e72\u6270\u95ee\u9898\u3002\uff08\u5c31\u50cf\u6211\u4eec\u63d0\u5230\u7684\uff0c\u8fd9\u4e9b\u4e92\u6270\u95ee\u9898\u6ca1\u6709\u88abPOSIX.1\u5b9a\u4e49\u3002\uff09<\/p>\n<p>\u8fd9\u6bd4\u4e4b\u524dFigure10.7\u7684\u4ee3\u7801\u8981\u591a\u3002\u6211\u4eec\u4e0d\u4f7f\u7528\u4efb\u4f55\u975e\u672c\u5730\u8df3\u8f6c\u7684\u5f62\u5f0f\uff08\u5c31\u50cfFigure10.8\u4e2d\u90a3\u6837\uff0c\u53bb\u907f\u514dalarm\u548cpause\u4e4b\u95f4\u7684\u7ade\u4e89\u6761\u4ef6\u3002\uff09\uff0c\u6240\u4ee5\u5728\u8fd9\u91cc\u5f53<span style=\"color: #993300;\">SIGALRM<\/span>\u7684\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u6b63\u5728\u6267\u884c\u65f6\u5e76\u4e0d\u4f1a\u88ab\u5f71\u54cd\u5230\u3002<\/p>\n<table border=\"0\" width=\"100%\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<h3 class=\"docExampleTitle\">Figure 10.29. Reliable implementation of <tt>sleep<\/tt><\/h3>\n<pre><span style=\"color: #993300;\">#include \"apue.h\"\r\n\r\nstatic void\r\nsig_alrm(int signo)\r\n{\r\n    \/* nothing to do, just returning wakes up sigsuspend() *\/\r\n}\r\n\r\nunsigned int\r\nsleep(unsigned int nsecs)\r\n{\r\n    struct sigaction    newact, oldact;\r\n    sigset_t            newmask, oldmask, suspmask;\r\n    unsigned int        unslept;\r\n\r\n    \/* set our handler, save previous information *\/\r\n    newact.sa_handler = sig_alrm;\r\n    sigemptyset(&amp;newact.sa_mask);\r\n    newact.sa_flags = 0;\r\n    sigaction(SIGALRM, &amp;newact, &amp;oldact);\r\n\r\n    \/* block SIGALRM and save current signal mask *\/\r\n    sigemptyset(&amp;newmask);\r\n    sigaddset(&amp;newmask, SIGALRM);\r\n    sigprocmask(SIG_BLOCK, &amp;newmask, &amp;oldmask);\r\n\r\n    alarm(nsecs);\r\n\r\n    suspmask = oldmask;\r\n    sigdelset(&amp;suspmask, SIGALRM);    \/* make sure SIGALRM isn't blocked *\/\r\n    sigsuspend(&amp;suspmask);            \/* wait for any signal to be caught *\/\r\n\r\n    \/* some signal has been caught,   SIGALRM is now blocked *\/\r\n\r\n    unslept = alarm(0);\r\n    sigaction(SIGALRM, &amp;oldact, NULL);  \/* reset previous action *\/\r\n\r\n    \/* reset signal mask, which unblocks SIGALRM *\/\r\n    sigprocmask(SIG_SETMASK, &amp;oldmask, NULL);\r\n    return(unslept);\r\n}\r\n<\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>\u6211\u4eec\u5df2\u7ecf\u5728\u4f8b\u5b50\u4e2d\u4f7f\u7528\u4e86\u5f88\u591a\u6b21sleep\u51fd\u6570\uff0c\u5e76\u5728Figures10.7\u548c10.8\u4e2d\u663e\u793a\u4e86\u4e24\u4e2a\u7f3a\u9677\u3002 #include &lt;unistd.h&gt; unsi&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/www.5x44.cn\/?p=726\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-726","post","type-post","status-publish","format-standard","hentry","category-apue"],"_links":{"self":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/726","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=726"}],"version-history":[{"count":4,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/726\/revisions"}],"predecessor-version":[{"id":730,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/726\/revisions\/730"}],"wp:attachment":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=726"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=726"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=726"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}