{"id":636,"date":"2017-01-13T16:21:29","date_gmt":"2017-01-13T08:21:29","guid":{"rendered":"http:\/\/www.5x44.cn\/?p=636"},"modified":"2017-01-13T16:21:29","modified_gmt":"2017-01-13T08:21:29","slug":"alarm%e5%92%8cpause%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/www.5x44.cn\/?p=636","title":{"rendered":"alarm\u548cpause\u51fd\u6570"},"content":{"rendered":"<p><span style=\"color: #993300;\">alarm<\/span>\u51fd\u6570\u5141\u8bb8\u6211\u4eec\u8bbe\u7f6e\u4e00\u4e2a\u8ba1\u65f6\u5668\uff0c\u8be5\u8ba1\u65f6\u5668\u4f1a\u5728\u672a\u6765\u8d85\u65f6\u5230\u671f\u3002\u5f53\u8ba1\u65f6\u95f4\u903e\u671f\uff0c\u4f1a\u4ea7\u751f<span style=\"color: #993300;\">SIGALRM<\/span>\u4fe1\u53f7\u3002\u5982\u679c\u6211\u4eec\u5ffd\u7565\u6216\u4e0d\u6355\u83b7\u8be5\u4fe1\u53f7\uff0c\u5b83\u7684\u9ed8\u8ba4\u52a8\u4f5c\u662f\u7ec8\u6b62\u8fdb\u7a0b\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: 176px;\" border=\"1\" width=\"1094\" 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 alarm(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 seconds until previously set alarm<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u53c2\u6570<em>seconds<\/em>\u662f\u4e00\u4e2a\u5355\u4f4d\u4e3a\u79d2\u7684\u65f6\u95f4\uff0c\u5f53\u8fd9\u4e2a\u65f6\u95f4\u5230\u4e86\u5c31\u4f1a\u4ea7\u751f\u4fe1\u53f7\u3002\u8981\u77e5\u9053\u5f53\u65f6\u95f4\u5230\u4e86\uff0c\u4fe1\u53f7\u662f\u7531\u5185\u6838\u4ea7\u751f\uff0c\u4f46\u662f\u5728\u8fdb\u7a0b\u83b7\u5f97\u63a7\u5236\u8be5\u4fe1\u53f7\u4e4b\u524d\uff0c\u8fd8\u662f\u6709\u4e00\u6bb5\u989d\u5916\u7684\u65f6\u95f4\uff0c\u56e0\u4e3a\u8fd9\u662f\u5904\u7406\u5668\u8c03\u5ea6\u5ef6\u65f6\u3002<\/p>\n<p><em>\u65e9\u671f\u7684UNIX\u7cfb\u7edf\u5b9e\u73b0\u53ef\u80fd\u4f1a\u901a\u8fc7\u65e9\u4e00\u79d2\u53d1\u9001\u4fe1\u53f7\u6765\u51cf\u5c11\u5ef6\u65f6\u3002POSIX.1\u4e0d\u5141\u8bb8\u8fd9\u4e48\u505a\u3002<\/em><\/p>\n<p>\u6bcf\u4e2a\u5904\u7406\u5668\u53ea\u80fd\u6709\u4e00\u4e2a\u95f9\u949f\u3002\u5982\u679c\u5f53\u6211\u4eec\u8c03\u7528<span style=\"color: #993300;\">alarm<\/span>\u65f6\uff0c\u4e4b\u524d\u8be5\u5904\u7406\u5668\u6ce8\u518c\u7684\u8ba1\u65f6\u5668\u8fd8\u6ca1\u6709\u903e\u671f\uff0c\u90a3\u4e48\u4f1a\u8fd4\u56de\u4e0a\u4e00\u4e2a\u8ba1\u65f6\u5668\u7684\u5269\u4f59\u65f6\u95f4\u4e3a\u5f53\u524d\u8c03\u7528\u7684\u8fd4\u56de\u503c\u3002\u4e4b\u524d\u6ce8\u518c\u7684\u8ba1\u65f6\u5668\u4f1a\u88ab\u65b0\u7684\u8ba1\u65f6\u5668\u4ee3\u66ff\u3002<\/p>\n<p>\u5982\u679c\u4e4b\u524d\u6ce8\u518c\u7684\u8ba1\u65f6\u5668\u8fd8\u6ca1\u6709\u903e\u671f\u5e76\u4e14\u5982\u679c\u5f53\u524d<span style=\"color: #993300;\">alarm<\/span>\u51fd\u6570\u7684<em>seconds<\/em>\u503c\u662f0\uff0c\u90a3\u4e48\u4e4b\u524d\u7684\u8ba1\u65f6\u5668\u4f1a\u88ab\u53d6\u6d88\u3002\u4e4b\u524d\u8ba1\u65f6\u5668\u7684\u5269\u4f59\u65f6\u95f4\u540c\u6837\u4f1a\u505a\u4e3a\u51fd\u6570\u8fd4\u56de\u503c\u8fd4\u56de\u3002<\/p>\n<p>\u867d\u7136<span style=\"color: #993300;\">SIGALRM<\/span>\u7684\u9ed8\u8ba4\u52a8\u4f5c\u662f\u7ec8\u6b62\u8fdb\u7a0b\uff0c\u4f46\u591a\u6570\u8fdb\u7a0b\u4ecd\u7136\u6355\u83b7\u8be5\u4fe1\u53f7\u3002\u5982\u679c\u8fdb\u7a0b\u60f3\u53bb\u7ec8\u6b62\u81ea\u5df1\uff0c\u5b83\u80fd\u5728\u7ed3\u675f\u4e4b\u524d\u6267\u884c\u4e00\u4e9b\u6e05\u7406\u5de5\u4f5c\u3002\u5982\u679c\u6211\u4eec\u503e\u5411\u4e8e\u6355\u83b7<span style=\"color: #993300;\">SIGALRM<\/span>\uff0c\u6211\u4eec\u9700\u8981\u5728\u8c03\u7528<span style=\"color: #993300;\">alarm<\/span>\u4e4b\u524d\u5c0f\u5fc3\u5b89\u88c5\u5b83\u7684\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u3002\u5982\u679c\u5148\u8c03\u7528\u4e86<span style=\"color: #993300;\">alarm<\/span>\u51fd\u6570\u5e76\u53d1\u9001\u4e86<span style=\"color: #993300;\">SIGALRM<\/span>\u4fe1\u53f7\uff0c\u800c\u8fd9\u65f6\u8fd8\u6ca1\u5b89\u88c5\u597d\u4fe1\u53f7\u5904\u7406\u51fd\u6570\uff0c\u90a3\u4e48\u8fdb\u7a0b\u4f1a\u7ec8\u6b62\u3002<\/p>\n<p><span style=\"color: #993300;\">pause<\/span>\u51fd\u6570\u6302\u8d77\u8c03\u7528\u5b83\u7684\u8fdb\u7a0b\u76f4\u5230\u6355\u83b7\u76f8\u5e94\u4fe1\u53f7\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: 187px;\" border=\"1\" width=\"515\" 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\nint pause(void);\r\n<\/span><\/pre>\n<\/td>\n<\/tr>\n<tr>\n<td class=\"docTableCell\" align=\"right\" valign=\"top\">\n<p class=\"docText\">Returns: 1 with <tt>errno<\/tt> set to <tt>EINTR<\/tt><\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u53ea\u6709\u5f53\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u6267\u884c\u5e76\u8fd4\u56de\u65f6<span style=\"color: #993300;\">pause<\/span>\u51fd\u6570\u624d\u4f1a\u8fd4\u56de\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b<span style=\"color: #993300;\">pause<\/span>\u8fd4\u56de1\uff0c<span style=\"color: #993300;\">errno<\/span>\u8bbe\u7f6e\u6210<span style=\"color: #993300;\">EINTR<\/span>\u3002<\/p>\n<h2>\u4f8b\u5b50\uff1a<\/h2>\n<p>\u4f7f\u7528alarm\u548cpause\uff0c\u6211\u4eec\u53ef\u4ee5\u8ba9\u4e00\u4e2a\u8fdb\u7a0b\u4f11\u7720\u6307\u5b9a\u65f6\u95f4\uff1a<\/p>\n<table border=\"0\" width=\"100%\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<h5 class=\"docExampleTitle\">Figure 10.7. Simple, incomplete implementation of <tt>sleep<\/tt><\/h5>\n<pre>#include     &lt;signal.h&gt;\r\n#include     &lt;unistd.h&gt;\r\n\r\nstatic void\r\nsig_alrm(int signo)\r\n{\r\n    \/* nothing to do, just return to wake up the pause *\/\r\n}\r\n\r\nunsigned int\r\nsleep1(unsigned int nsecs)\r\n{\r\n    if (signal(SIGALRM, sig_alrm) == SIG_ERR)\r\n        return(nsecs);\r\n    alarm(nsecs);       \/* start the timer *\/\r\n    pause();            \/* next caught signal wakes us up *\/\r\n    return(alarm(0));   \/* turn off timer, return unslept time *\/\r\n}\r\n<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u8fd9\u4e2a\u51fd\u6570\u770b\u4e0a\u53bb\u50cf\u662f<span style=\"color: #993300;\">sleep<\/span>\u51fd\u6570\uff0c\u4f46\u662f\u8fd9\u4e2a\u7b80\u5355\u5b9e\u73b0\u6709\u4e09\u4e2a\u95ee\u9898\uff1a<\/p>\n<ol>\n<li>\u5982\u679c\u8c03\u7528\u8005\u5df2\u7ecf\u8bbe\u7f6e\u4e86\u4e00\u4e2a<span style=\"color: #993300;\">alarm<\/span>\uff0c\u90a3\u4e48\u8fd9\u4e2a\u8ba1\u65f6\u5668\u4f1a\u88ab\u65b0\u7684\u4ee3\u66ff\u3002\u6211\u4eec\u80fd\u7ea0\u6b63\u8fd9\u4e2a\u95ee\u9898\uff0c\u901a\u8fc7\u89c2\u5bdf<span style=\"color: #993300;\">alarm<\/span>\u7684\u8fd4\u56de\u503c\u3002\u5982\u679c\u4e4b\u524d\u8bbe\u7f6e\u7684\u8ba1\u65f6\u5668\u4f1a\u5148\u5230\u65f6\uff0c\u90a3\u6211\u4eec\u8981\u7b49\u5f85\u5b83\u903e\u671f\u540e\u518d\u8bbe\u7f6e\u6211\u4eec\u7684\u8ba1\u65f6\u5668\u3002\u5982\u679c\u4e4b\u524d\u7684\u8ba1\u65f6\u5668\u4f1a\u5728\u6211\u4eec\u7684\u8ba1\u65f6\u5668\u4e4b\u540e\u903e\u671f\uff0c\u90a3\u6211\u4eec\u53ef\u4ee5\u5148\u6682\u65f6\u8bb0\u5f55\u5269\u4e0b\u7684\u65f6\u95f4\uff0c\u7b49\u6211\u4eec\u7684\u8ba1\u65f6\u5668\u903e\u671f\u540e\uff0c\u91cd\u65b0\u8bbe\u7f6e\u5b83\u7684\u65f6\u95f4\u3002<\/li>\n<li>\u6211\u4eec\u5df2\u7ecf\u4fee\u6539\u4e86<span style=\"color: #993300;\">SIGALRM<\/span>\u7684\u52a8\u4f5c\u3002\u5982\u679c\u6211\u4eec\u6b63\u5728\u4e3a\u53e6\u4e00\u4e2a\u8c03\u7528\u5199\u51fd\u6570\uff0c\u90a3\u6211\u4eec\u5e94\u8be5\u628a\u5f53\u524d\u7684\u52a8\u4f5c\u8bb0\u5f55\u4e0b\u6765\uff0c\u7136\u540e\u518d\u4fee\u6539\u52a8\u4f5c\uff0c\u7b49\u8c03\u7528\u5b8c\u6bd5\u540e\u518d\u56de\u590d\u5b83\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7<span style=\"color: #993300;\">signal<\/span>\u7684\u8fd4\u56de\u503c\u6765\u4fdd\u5b58\u4e4b\u524d\u7684\u52a8\u4f5c\u3002<\/li>\n<li>\u5728\u7b2c\u4e00\u4e2a<span style=\"color: #993300;\">alarm<\/span>\u548c<span style=\"color: #993300;\">pause<\/span>\u4e4b\u95f4\u6709\u7ade\u4e89\u6761\u4ef6\u3002\u5728\u7cfb\u7edf\u7e41\u5fd9\u65f6\uff0c\u53ef\u80fd\u8ba1\u65f6\u5668\u903e\u671f\u5e76\u8c03\u7528\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u5728\u8c03\u7528<span style=\"color: #993300;\">pause<\/span>\u4e4b\u524d\u3002\u5982\u679c\u53d1\u751f\u4e86\uff0c\u8c03\u7528\u8005\u4f1a\u88ab<span style=\"color: #993300;\">pause<\/span>\u6c38\u4e45\u7684\u6302\u8d77\uff08\u5047\u8bbe\u5176\u5b83\u4fe1\u53f7\u4e0d\u4f1a\u88ab\u6355\u6349\uff09\u3002<\/li>\n<\/ol>\n<h2>\u4f8b\u5b50\uff1a<\/h2>\n<p>SVR2\u7684<span style=\"color: #993300;\">sleep<\/span>\u5b9e\u73b0\u4f7f\u7528\u4e86<span style=\"color: #993300;\">setjmp<\/span>\u548c<span style=\"color: #993300;\">longjmp<\/span>\uff0c\u7528\u4e8e\u907f\u514d\u7ade\u4e89\u6761\u4ef6\u3002\u672c\u4f8b\u662f\u4e2a\u7b80\u5355\u7684\u7248\u672c\uff0c\u88ab\u79f0\u4f5csleep2\uff0c\u5982Figure 10.8\u6240\u793a\u3002\uff08\u4e3a\u4e86\u51cf\u5c11\u957f\u5ea6\uff0c\u6211\u4eec\u6ca1\u6709\u5904\u7406\u4e0a\u9762\u6240\u8bf4\u7684\u4e09\u4e2a\u95ee\u9898\u4e2d\u76841\u30012\uff09<\/p>\n<p>sleep2\u51fd\u6570\u907f\u514d\u4e86Figure 10.7\u7684\u7ade\u4e89\u6761\u4ef6\u3002\u5373\u4f7f<span style=\"color: #993300;\">pause<\/span>\u6c38\u8fdc\u4e0d\u88ab\u6267\u884c\uff0c\u5f53<span style=\"color: #993300;\">SIGALRM<\/span>\u53d1\u751f\u65f6\uff0csleep2\u51fd\u6570\u90fd\u4f1a\u8fd4\u56de\u3002<\/p>\n<table border=\"0\" width=\"100%\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<h5 class=\"docExampleTitle\"><span style=\"color: #993300;\">Figure 10.8. Another (imperfect) implementation of <tt>sleep<\/tt><\/span><\/h5>\n<pre><span style=\"color: #993300;\">#include   &lt;setjmp.h&gt;\r\n#include   &lt;signal.h&gt;\r\n#include   &lt;unistd.h&gt;\r\n\r\nstatic jmp_buf  env_alrm;\r\n\r\nstatic void\r\nsig_alrm(int signo)\r\n{\r\n    longjmp(env_alrm, 1);\r\n}\r\n\r\nunsigned int\r\nsleep2(unsigned int nsecs)\r\n{\r\n    if (signal(SIGALRM, sig_alrm) == SIG_ERR)\r\n        return(nsecs);\r\n    if (setjmp(env_alrm) == 0) {\r\n        alarm(nsecs);       \/* start the timer *\/\r\n        pause();            \/* next caught signal wakes us up *\/\r\n    }\r\n    return(alarm(0));       \/* turn off timer, return unslept time *\/\r\n}\r\n<\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u8be5\u51fd\u6570\u6709\u4e00\u4e2a\u4e0d\u5bb9\u6613\u88ab\u53d1\u73b0\u7684\u95ee\u9898\u2014\u2014\u5f53\u9700\u8981\u548c\u5176\u5b83\u4fe1\u53f7\u4e92\u52a8\u7684\u65f6\u5019\u3002\u5982\u679c<span style=\"color: #993300;\">SIGALRM<\/span>\u4e2d\u65ad\u4e86\u5176\u5b83\u4fe1\u53f7\u5904\u7406\u51fd\u6570\uff0c\u5f53\u6211\u4eec\u8c03\u7528<span style=\"color: #993300;\">longjmp<\/span>\u65f6\uff0c\u6211\u4eec\u5ffd\u7565\u4e86\u5b83\u5176\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u3002<\/p>\n<table border=\"0\" width=\"100%\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<h5 class=\"docExampleTitle\">Figure 10.9. Calling <tt>sleep2<\/tt> from a program that catches other signals<\/h5>\n<pre>#include \"apue.h\"\r\n\r\nunsigned int        sleep2(unsigned int);\r\nstatic void         sig_int(int);\r\n\r\nint\r\nmain(void)\r\n{\r\n    unsigned int        unslept;\r\n\r\n    if (signal(SIGINT, sig_int) == SIG_ERR)\r\n        err_sys(\"signal(SIGINT) error\");\r\n    unslept = sleep2(5);\r\n    printf(\"sleep2 returned: %u\\n\", unslept);\r\n    exit(0);\r\n}\r\n\r\nstatic void\r\nsig_int(int signo)\r\n{\r\n    int            i, j;\r\n    volatile int   k;\r\n\r\n    \/*\r\n     * Tune these loops to run for more than 5 seconds\r\n     * on whatever system this test program is run.\r\n     *\/\r\n    printf(\"\\nsig_int starting\\n\");\r\n    for (i = 0; i &lt; 300000; i++)\r\n        for (j = 0; j &lt; 4000; j++)\r\n            k += i * j;\r\n    printf(\"sig_int finished\\n\");\r\n}\r\n<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Figure 10.9\u663e\u793a\u4e86\u8fd9\u4e2a\u65b9\u6848\u3002\u5728<span style=\"color: #993300;\">SIGINT<\/span>\u5904\u7406\u51fd\u6570\u7684\u5faa\u73af\u4e2d\u5df2\u7ecf\u5199\u4e86\uff0c\u5b83\u7684\u6267\u884c\u5728\u67d0\u4e9b\u7cfb\u7edf\u4e0a\u8981\u5f88\u4e45\u3002\u6211\u4eec\u53ea\u662f\u7b80\u5355\u7684\u60f3\u8ba9\u5b83\u6267\u884c\u7684\u65f6\u95f4\u6bd4sleeep2\u65f6\u95f4\u957f\u4e00\u4e9b\u3002\u6574\u578b<span style=\"color: #993300;\">k<\/span>\u88ab\u58f0\u660e\u4e3a<span style=\"color: #993300;\">volatile<\/span>\u4e3a\u4e86\u9632\u6b62\u7f16\u8bd1\u5668\u4f18\u5316\u5faa\u73af\u4e2d\u7684<span style=\"color: #993300;\">k<\/span>\u3002\u6267\u884cFigure 10.9\u4e2d\u7684\u7a0b\u5e8f\u663e\u793a\u5982\u4e0b\uff1a<\/p>\n<table border=\"0\" width=\"100%\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<pre><span style=\"color: #993300;\">    $ <span class=\"docEmphStrong\">.\/a.out<\/span>\r\n<span class=\"docEmphStrong\">\r\n    ^?<\/span>                      <span class=\"docEmphItalicAlt\">we type the interrupt character<\/span>\r\n    sig_int starting\r\n    sleep2 returned: 0\r\n<\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>sleep1\u548csleep2\u8fd9\u4e24\u4e2a\u51fd\u6570\u7684\u76ee\u7684\u662f\u663e\u793a\u4f7f\u7528\u4fe1\u53f7\u7684\u9690\u60a3\u3002\u4e0b\u9762\u7684\u7ae0\u8282\u5c06\u4f1a\u56f4\u7ed5\u6240\u6709\u8fd9\u4e9b\u95ee\u9898\uff0c\u5982\u4f55\u53ef\u9760\u7684\u5904\u7406\u4fe1\u53f7\u5e76\u4e0d\u6dfb\u52a0\u591a\u4f59\u7684\u4ee3\u7801\u3002<\/p>\n<h2>\u4f8b\u5b50\uff1a<\/h2>\n<p><span style=\"color: #993300;\">alarm<\/span>\u548c<span style=\"color: #993300;\">sleep<\/span>\u51fd\u6570\u7684\u901a\u5e38\u7528\u6cd5\u662f\uff0c\u653e\u4e00\u4e2a\u8ba1\u65f6\u5668\u53bb\u9650\u5236\u67d0\u4e9b\u64cd\u4f5c\u4ea7\u751f\u963b\u585e\u3002\u4f8b\u5982\uff1a\u5982\u679c\u6211\u4eec<span style=\"color: #993300;\">read<\/span>\u64cd\u4f5c\u4e00\u4e2a\u8bbe\u5907\uff08\u8be5\u8bbe\u5907\u53ef\u80fd\u4ea7\u751f\u963b\u585e\uff0c\u5b83\u662f\u4e00\u4e2a\u201c\u6162\u201d\u901f\u8bbe\u5907\uff09\uff0c\u5728<span style=\"color: #993300;\">read<\/span>\u4e00\u6bb5\u65f6\u95f4\u540e\u8d85\u65f6\u3002Figure 10.10\u663e\u793a\u4e86\u4ece\u6807\u51c6\u8f93\u5165\u8bfb\u4e00\u884c\uff0c\u7136\u540e\u5199\u5230\u6807\u51c6\u8f93\u51fa\u3002<\/p>\n<p>\u8be5\u7a0b\u5e8f\u662f\u5728UNIX\u4e0b\u7684\u7a0b\u5e8f\uff0c\u4f46\u662f\u8fd9\u4e2a\u7a0b\u5e8f\u6709\u4e24\u4e2a\u95ee\u9898\u3002<\/p>\n<ol>\n<li>\u8be5\u7a0b\u5e8f\u6709\u50cfFigure10.7\u4e00\u6837\u7684\u7455\u75b5\uff1a\u5728\u7b2c\u4e00\u6b21\u8c03\u7528<span style=\"color: #993300;\">alarm<\/span>\u548c\u8c03\u7528<span style=\"color: #993300;\">read<\/span>\u4e4b\u95f4\u6709\u7ade\u4e89\u6761\u4ef6\u3002\u5982\u679c\u5185\u6838\u5728\u8fd9\u4e24\u4e2a\u51fd\u6570\u95f4\u963b\u585e\u8fdb\u7a0b\u7684\u65f6\u95f4\u8d85\u8fc7\u8ba1\u65f6\u5668\u7684\u5468\u671f\uff0c<span style=\"color: #993300;\">read<\/span>\u53ef\u80fd\u6c38\u4e45\u7684\u88ab\u963b\u585e \u3002\u591a\u6570\u8fd9\u79cd\u64cd\u4f5c\u4f7f\u7528\u4e00\u4e2a\u8f83\u957f\u7684\u8ba1\u65f6\u5668\u5468\u671f\uff0c\u4f46\u65e0\u8bba\u600e\u6837\u8fd9\u90fd\u662f\u4e00\u4e2a\u7ade\u4e89\u6761\u4ef6\u3002<\/li>\n<li>\u5982\u679c\u7cfb\u7edf\u8c03\u7528\u81ea\u52a8\u91cd\u542f\uff0c\u5f53<span style=\"color: #993300;\">SIGALRM<\/span>\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u8fd4\u56de\u65f6\uff0c<span style=\"color: #993300;\">read<\/span>\u4e0d\u4f1a\u88ab\u4e2d\u65ad\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u8d85\u65f6\u4e0d\u4f1a\u505a\u4efb\u4f55\u65f6\u4e8b\u3002<\/li>\n<\/ol>\n<table border=\"0\" width=\"100%\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<h5 class=\"docExampleTitle\">Figure 10.10. Calling <tt>read<\/tt> with a timeout<\/h5>\n<pre><span style=\"color: #993300;\">#include \"apue.h\"\r\n\r\nstatic void sig_alrm(int);\r\n\r\nint\r\nmain(void)\r\n{\r\n    int     n;\r\n    char    line[MAXLINE];\r\n\r\n    if (signal(SIGALRM, sig_alrm) == SIG_ERR)\r\n        err_sys(\"signal(SIGALRM) error\");\r\n\r\n    alarm(10);\r\n    if ((n = read(STDIN_FILENO, line, MAXLINE)) &lt; 0)\r\n        err_sys(\"read error\");\r\n    alarm(0);\r\n\r\n    write(STDOUT_FILENO, line, n);\r\n    exit(0);\r\n}\r\n\r\nstatic void\r\nsig_alrm(int signo)\r\n{\r\n    \/* nothing to do, just return to interrupt the read *\/\r\n}\r\n<\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>\u4f8b\u5b50\uff1a<\/h2>\n<p>\u4f7f\u7528<span style=\"color: #993300;\">longjmp<\/span>\u91cd\u5199\u4e0a\u9762\u7684\u4f8b\u5b50\u3002\u4f7f\u7528\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u7528\u62c5\u5fc3\u662f\u5426\u6709\u4e00\u4e2a\u6162\u7cfb\u7edf\u8c03\u7528\u88ab\u4e2d\u65ad\u3002<\/p>\n<p>\u8fd9\u4e2a\u672c\u7248\u4f1a\u5982\u5e0c\u671b\u7684\u90a3\u6837\u5de5\u4f5c\uff0c\u65e0\u8bba\u7cfb\u7edf\u662f\u5426\u91cd\u65b0\u542f\u52a8\u88ab\u4e2d\u65ad\u7684\u7cfb\u7edf\u8c03\u7528\u3002\u5b9e\u9645\u4e0a\uff0c\u8fd9\u4e2a\u7248\u672c\u4ecd\u7136\u5b58\u5728\u548c\u5176\u5b83\u4fe1\u53f7\u4ea4\u4e92\u7684\u95ee\u9898\u3002<\/p>\n<table border=\"0\" width=\"100%\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<h5 class=\"docExampleTitle\">Figure 10.11. Calling <tt>read<\/tt> with a timeout, using <tt>longjmp<\/tt><\/h5>\n<pre><span style=\"color: #993300;\">#include \"apue.h\"\r\n#include &lt;setjmp.h&gt;\r\n\r\nstatic void       sig_alrm(int);\r\nstatic jmp_buf    env_alrm;\r\n\r\nint\r\nmain(void)\r\n{\r\n    int     n;\r\n    char    line[MAXLINE];\r\n\r\n    if (signal(SIGALRM, sig_alrm) == SIG_ERR)\r\n        err_sys(\"signal(SIGALRM) error\");\r\n    if (setjmp(env_alrm) != 0)\r\n        err_quit(\"read timeout\");\r\n\r\n    alarm(10);\r\n    if ((n = read(STDIN_FILENO, line, MAXLINE)) &lt; 0)\r\n        err_sys(\"read error\");\r\n    alarm(0);\r\n\r\n    write(STDOUT_FILENO, line, n);\r\n    exit(0);\r\n}\r\n\r\nstatic void\r\nsig_alrm(int signo)\r\n{\r\n    longjmp(env_alrm, 1);\r\n}\r\n<\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5982\u679c\u6211\u4eec\u60f3\u8bbe\u7f6e\u4e00\u4e2a\u8ba1\u65f6\u5668\u6765\u9650\u7f6eI\/O\u64cd\u4f5c\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528<span style=\"color: #993300;\">longjmp<\/span>\uff0c\u5c31\u50cf\u4e0a\u9762\u663e\u793a\u7684\u90a3\u6837\uff0c\u5b9e\u9645\u4f7f\u7528\u8fc7\u7a0b\u4e2d\u5b83\u5f88\u6709\u53ef\u80fd\u548c\u5176\u5b83\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u7684\u4ea4\u4e92\u3002\u53e6\u4e00\u4e2a\u9009\u62e9\u662f\u4f7f\u7528<span style=\"color: #993300;\">select<\/span>\u6216<span style=\"color: #993300;\">poll<\/span>\u51fd\u6570\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>alarm\u51fd\u6570\u5141\u8bb8\u6211\u4eec\u8bbe\u7f6e\u4e00\u4e2a\u8ba1\u65f6\u5668\uff0c\u8be5\u8ba1\u65f6\u5668\u4f1a\u5728\u672a\u6765\u8d85\u65f6\u5230\u671f\u3002\u5f53\u8ba1\u65f6\u95f4\u903e\u671f\uff0c\u4f1a\u4ea7\u751fSIGALRM\u4fe1\u53f7\u3002\u5982\u679c\u6211\u4eec\u5ffd\u7565\u6216\u4e0d\u6355\u83b7\u8be5\u4fe1\u53f7\uff0c\u5b83\u7684\u9ed8\u8ba4\u52a8\u4f5c\u662f\u7ec8\u6b62\u8fdb\u7a0b\u3002 #&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/www.5x44.cn\/?p=636\"> 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-636","post","type-post","status-publish","format-standard","hentry","category-apue"],"_links":{"self":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/636","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=636"}],"version-history":[{"count":11,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/636\/revisions"}],"predecessor-version":[{"id":648,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/636\/revisions\/648"}],"wp:attachment":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=636"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=636"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=636"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}