{"id":202597,"date":"2020-10-21T09:18:02","date_gmt":"2020-10-21T01:18:02","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=202597"},"modified":"2020-10-08T16:18:57","modified_gmt":"2020-10-08T08:18:57","slug":"pause-linux","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/pause-linux.html","title":{"rendered":"\u4ec0\u4e48\u662f\u65f6\u5e8f\u7ade\u6001?"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u4ec0\u4e48\u662f\u65f6\u5e8f\u7ade\u6001?\u5c06\u540c\u4e00\u4e2a\u7a0b\u5e8f\u6267\u884c\u4e24\u6b21\uff0c\u6b63\u5e38\u60c5\u51b5\u4e0b\uff0c\u524d\u540e\u4e24\u6b21\u6267\u884c\u5f97\u5230\u7684\u7ed3\u679c\u5e94\u8be5\u662f\u4e00\u6837\u7684\u3002\u4f46\u7531\u4e8e\u7cfb\u7edf\u8d44\u6e90\u7ade\u4e89\u7684\u539f\u56e0\uff0c\u524d\u540e\u4e24\u6b21\u6267\u884c\u7684\u7ed3\u679c\u6709\u53ef\u80fd\u5f97\u5230\u4e0d\u4e00\u6837\u7684\u7ed3\u679c\uff0c\u8fd9\u4e2a\u73b0\u8c61\u5c31\u662f\u65f6\u5e8f\u7ade\u6001\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

pause\u51fd\u6570<\/strong><\/div>\n

\u51fd\u6570\u539f\u578b\uff1a<\/p>\n

int\u00a0pause(void);<\/pre>\n

\u51fd\u6570\u4f5c\u7528\uff1a<\/p>\n

\u8fdb\u7a0b\u8c03\u7528pause\u51fd\u6570\u65f6\uff0c\u4f1a\u9020\u6210\u8fdb\u7a0b\u4e3b\u52a8\u6302\u8d77(\u5904\u4e8e\u963b\u585e\u72b6\u6001\uff0c\u5e76\u4e3b\u52a8\u653e\u5f03CPU)\uff0c\u5e76\u4e14\u7b49\u5f85\u4fe1\u53f7\u5c06\u5176\u5524\u9192\u3002<\/p>\n

\u8fd4\u56de\u503c\uff1a<\/p>\n

\u6211\u4eec\u77e5\u9053\uff0c\u4fe1\u53f7\u7684\u5904\u7406\u65b9\u5f0f\u6709\u4e09\u79cd\uff1a1. \u9ed8\u8ba4\u52a8\u4f5c;2. \u5ffd\u7565\u5904\u7406;3. \u6355\u6349\u3002\u8fdb\u7a0b\u6536\u5230\u4e00\u4e2a\u4fe1\u53f7\u540e\uff0c\u4f1a\u5148\u5904\u7406\u54cd\u5e94\u4fe1\u53f7\uff0c\u518d\u5524\u9192pause\u51fd\u6570\u3002\u4e8e\u662f\u6709\u4e0b\u9762\u51e0\u79cd\u60c5\u51b5\uff1a<\/p>\n

    \n
  • \u5982\u679c\u4fe1\u53f7\u7684\u9ed8\u8ba4\u5904\u7406\u52a8\u4f5c\u662f\u7ec8\u6b62\u8fdb\u7a0b\uff0c\u5219\u8fdb\u7a0b\u5c06\u88ab\u7ec8\u6b62\uff0c\u4e5f\u5c31\u662f\u8bf4\u4e00\u6536\u5230\u4fe1\u53f7\u8fdb\u7a0b\u5c31\u7ec8\u6b62\u4e86\uff0cpause\u51fd\u6570\u6839\u672c\u5c31\u6ca1\u6709\u673a\u4f1a\u8fd4\u56de;<\/li>\n
  • \u5982\u679c\u4fe1\u53f7\u7684\u9ed8\u8ba4\u5904\u7406\u52a8\u4f5c\u662f\u5ffd\u7565\uff0c\u5219\u8fdb\u7a0b\u5c06\u76f4\u63a5\u5ffd\u7565\u8be5\u4fe1\u53f7\uff0c\u76f8\u5f53\u4e8e\u6ca1\u6536\u5230\u8fd9\u4e2a\u4fe1\u53f7\uff0c\u8fdb\u7a0b\u7ee7\u7eed\u5904\u4e8e\u6302\u8d77\u72b6\u6001\uff0cpause\u51fd\u6570\u4e0d\u8fd4\u56de;<\/li>\n
  • \u5982\u679c\u4fe1\u53f7\u7684\u5904\u7406\u52a8\u4f5c\u662f\u6355\u6349\uff0c\u5219\u8fdb\u7a0b\u8c03\u7528\u5b8c\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u4e4b\u540e\uff0cpause\u8fd4\u56de-1\uff0cerrno\u8bbe\u7f6e\u4e3aEINTR\uff0c\u8868\u793a\u201c\u88ab\u4fe1\u53f7\u4e2d\u65ad\u201d\u3002<\/li>\n
  • pause\u6536\u5230\u7684\u4fe1\u53f7\u4e0d\u80fd\u88ab\u5c4f\u853d\uff0c\u5982\u679c\u88ab\u5c4f\u853d\uff0c\u90a3\u4e48pause\u5c31\u4e0d\u80fd\u88ab\u5524\u9192\u3002<\/li>\n<\/ul>\n
    \u56e0\u4e3aalarm\u51fd\u6570\u53ef\u4ee5\u5728\u8bbe\u5b9a\u7684\u65f6\u95f4\u4e4b\u540e\u53d1\u9001SIGALRM\u4fe1\u53f7\uff0cpause\u51fd\u6570\u53c8\u53ef\u4ee5\u5c06\u8fdb\u7a0b\u6302\u8d77\u7b49\u5f85\u4fe1\u53f7\uff0c\u5219\u4e8c\u8005\u7ed3\u5408\u53ef\u4ee5\u81ea\u5df1\u5199\u4e00\u4e2asleep\u51fd\u6570\uff0c\u5982\u4e0b\uff1a\r\n#include\u00a0<unistd.h>\r\n\u00a0#include\u00a0<signal.h>\r\n\u00a0#include\u00a0<stdio.h>\r\n\u00a0\r\n\u00a0void\u00a0sig_alrm(int\u00a0signo)\r\n\u00a0{\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\/*\u00a0nothing\u00a0to\u00a0do\u00a0*\/\r\n\u00a0}\r\n\u00a0\r\nunsigned\u00a0int\u00a0mysleep(unsigned\u00a0int\u00a0nsecs)\r\n{\r\n\u00a0\u00a0\u00a0\u00a0unsigned\u00a0int\u00a0unslept;\r\n\u00a0\r\n\u00a0\u00a0\u00a0\u00a0signal(SIGALRM,\u00a0&sig_alrm);\r\n\u00a0\u00a0\u00a0\u00a0unslept\u00a0=\u00a0alarm(nsecs);\r\n\u00a0\u00a0\u00a0\u00a0pause();\r\n\u00a0\r\n\u00a0\u00a0\u00a0\u00a0return\u00a0unslept;\r\n}\r\n\u00a0\r\n\u00a0\r\nint\u00a0main(void)\r\n{\r\n\u00a0\u00a0\u00a0\u00a0while(1){\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0mysleep(2);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0printf(\"Two\u00a0seconds\u00a0passed\\n\");\r\n\u00a0\u00a0\u00a0\u00a0}\r\n\u00a0\r\n\u00a0\u00a0\u00a0\u00a0return\u00a00;\r\n}\r\n<\/pre>\n
    \u65f6\u5e8f\u7ade\u6001\u524d\u5bfc\u4f8b<\/strong><\/div>\n

    \u5728\u8bb2\u65f6\u5e8f\u7ade\u6001\u5177\u4f53\u73b0\u8c61\u4e4b\u524d\uff0c\u6211\u4eec\u5148\u6765\u770b\u4e00\u4e2a\u751f\u6d3b\u4e2d\u5e38\u89c1\u7684\u573a\u666f\uff1a<\/p>\n

    \u60f3\u5348\u776110\u5206\u949f\uff0c\u4e8e\u662f\u5b9a\u4e86\u4e2a10\u5206\u949f\u7684\u95f9\u949f\uff0c\u5e0c\u671b10\u5206\u949f\u540e\u95f9\u949f\u5c06\u81ea\u5df1\u53eb\u9192\u3002<\/p>\n

    \u6b63\u5e38\u60c5\u51b5\uff1a\u5b9a\u597d\u95f9\u949f\uff0c\u5348\u7761\uff0c10\u5206\u949f\u540e\u95f9\u949f\u53eb\u9192\u81ea\u5df1;<\/p>\n

    \u5f02\u5e38\u60c5\u51b5\uff1a\u5b9a\u597d\u95f9\u949f\uff0c\u8eba\u4e0b\u7761\u89c92\u5206\u949f\uff0c\u88ab\u540c\u5b66\u53eb\u9192\u53bb\u6253\u7403\uff0c\u6253\u4e8620\u5206\u949f\u540e\u56de\u6765\u7ee7\u7eed\u7761\u89c9\u3002\u4f46\u5728\u6253\u7403\u671f\u95f4\uff0c\u95f9\u949f\u65e9\u5c31\u54cd\u8fc7\u4e86\uff0c\u5c06\u4e0d\u4f1a\u518d\u5524\u9192\u81ea\u5df1\u3002<\/p>\n

    \u8fd9\u4e2a\u4f8b\u5b50\u4e0e\u4e4b\u540e\u8981\u8bb2\u7684\u65f6\u5e8f\u7ade\u6001\u6709\u5f88\u5927\u7684\u76f8\u4f3c\u4e4b\u5904\u3002<\/p>\n

    ##\u65f6\u5e8f\u7ade\u6001\u95ee\u9898\u5206\u6790<\/strong><\/span><\/div>\n

    \u6211\u4eec\u518d\u56de\u8fc7\u5934\u6765\u770b\u4e0a\u9762\u6240\u5199\u7684mysleep\u7a0b\u5e8f\u3002\u8fd9\u4e2a\u51fd\u6570\u6709\u53ef\u80fd\u662f\u4e0b\u9762\u7684\u65f6\u5e8f\uff1a<\/p>\n

      \n
    • SIGALRM\u9ed8\u8ba4\u52a8\u4f5c\u662f\u7ec8\u6b62\u8fdb\u7a0b\uff0c\u56e0\u6b64\u6211\u4eec\u8981\u5c06\u5176\u6355\u6349\uff0c\u5bf9SIGALRM\u6ce8\u518c\u4fe1\u53f7\u5904\u7406\u51fd\u6570;<\/li>\n
    • \u8c03\u7528alarm(1)\u51fd\u6570\u5b9a\u65f61\u79d2\u949f;<\/li>\n
    • alarm(1)\u8c03\u7528\u7ed3\u675f\uff0c\u5b9a\u65f6\u5668\u5f00\u59cb\u8ba1\u65f6\u3002\u5c31\u5728\u8fd9\u65f6\uff0c\u8fdb\u7a0b\u5931\u53bbCPU\uff0c\u8fdb\u5165\u5c31\u7eea\u6001\u7b49\u5f85CPU(\u76f8\u5f53\u4e8e\u88ab\u540c\u5b66\u53eb\u9192\u53bb\u6253\u7403)\u3002\u5931\u53bbCPU\u7684\u65b9\u5f0f\u6709\u53ef\u80fd\u662f\u5185\u6838\u8c03\u5ea6\u4e86\u4f18\u5148\u7ea7\u66f4\u9ad8\u7684\u8fdb\u7a0b\u53d6\u4ee3\u4e86\u5f53\u524d\u8fdb\u7a0b\uff0c\u4f7f\u5f97\u5f53\u524d\u8fdb\u7a0b\u65e0\u6cd5\u83b7\u5f97CPU;<\/li>\n
    • \u6211\u4eec\u77e5\u9053\uff0calarm\u51fd\u6570\u5982\u679c\u91c7\u7528\u81ea\u7136\u5b9a\u65f6\u6cd5\u7684\u8bdd\uff0c\u5b9a\u65f6\u5668\u5c06\u4e00\u76f4\u8ba1\u65f6\uff0c\u4e0e\u8fdb\u7a0b\u72b6\u6001\u65e0\u5173\u3002\u4e8e\u662f\uff0c1\u79d2\u540e\uff0c\u95f9\u949f\u5b9a\u65f6\u65f6\u95f4\u5230\uff0c\u5185\u6838\u5411\u5f53\u524d\u8fdb\u7a0b\u53d1\u9001SIGALRM\u4fe1\u53f7\u3002\u9ad8\u4f18\u5148\u7ea7\u8fdb\u7a0b\u5c1a\u672a\u6267\u884c\u5b8c\u6bd5\uff0c\u5f53\u524d\u8fdb\u7a0b\u4ecd\u7136\u65e0\u6cd5\u83b7\u5f97CPU\uff0c\u7ee7\u7eed\u5904\u4e8e\u5c31\u7eea\u6001\uff0c\u4fe1\u53f7\u65e0\u6cd5\u5904\u7406(\u5904\u4e8e\u672a\u51b3\u72b6\u6001);<\/li>\n
    • \u4f18\u5148\u7ea7\u9ad8\u7684\u8fdb\u7a0b\u6267\u884c\u5b8c\u6bd5\uff0c\u5f53\u524d\u8fdb\u7a0b\u83b7\u5f97CPU\u8d44\u6e90\uff0c\u5185\u6838\u8c03\u5ea6\u56de\u5f53\u524d\u8fdb\u7a0b\u6267\u884c\u3002SIGALRM\u4fe1\u53f7\u9012\u8fbe\uff0c\u5e76\u88ab\u8fdb\u7a0b\u5904\u7406;<\/li>\n
    • \u4fe1\u53f7\u5904\u7406\u5b8c\u6bd5\u540e\uff0c\u8fd4\u56de\u5f53\u524d\u4e3b\u63a7\u6d41\u7a0b\uff0c\u5e76\u8c03\u7528pause()\u51fd\u6570\uff0c\u6302\u8d77\u7b49\u5f85alarm\u51fd\u6570\u53d1\u9001\u7684SIGALRM\u4fe1\u53f7\u5c06\u81ea\u5df1\u5524\u9192;<\/li>\n
    • \u4f46\u5b9e\u9645SIGALRM\u4fe1\u53f7\u5df2\u7ecf\u5904\u7406\u5b8c\u6bd5\uff0cpause()\u51fd\u6570\u6c38\u8fdc\u4e0d\u4f1a\u7b49\u5230\u3002<\/li>\n<\/ul>\n
      ##\u89e3\u51b3\u65f6\u5e8f\u7ade\u6001\u95ee\u9898<\/strong><\/span><\/div>\n

      \u901a\u8fc7\u4ee5\u4e0a\u65f6\u5e8f\u5206\u6790\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u51fa\uff0c\u9020\u6210\u65f6\u5e8f\u7ade\u6001\u7684\u539f\u56e0\u5c31\u662fSIGALRM\u4fe1\u53f7\u5728\u8fdb\u7a0b\u5931\u53bbCPU\u7684\u65f6\u5019\u5c31\u5df2\u7ecf\u53d1\u9001\u8fc7\u6765\u3002\u4e3a\u4e86\u9632\u6b62\u8fd9\u4e2a\u73b0\u8c61\u51fa\u73b0\uff0c\u6211\u4eec\u53ef\u4ee5\u5148\u5c06\u8be5\u4fe1\u53f7\u963b\u585e\uff0c\u5c06\u5176\u201c\u6293\u4f4f\u201d\uff0c\u518d\u5728\u89e3\u9664\u963b\u585e\u7684\u65f6\u5019\u7acb\u523b\u8c03\u7528pause\u51fd\u6570\u6302\u8d77\u7b49\u5f85\u3002\u8fd9\u6837\u5373\u4f7f\u5728\u8c03\u7528alarm\u5c31\u5931\u53bbCPU\uff0c\u4e5f\u53ef\u4ee5\u5728\u8fdb\u7a0b\u91cd\u65b0\u83b7\u5f97CPU\u65f6\u5c06\u6293\u5230\u7684SIGALRM\u4fe1\u53f7\u91cd\u65b0\u201c\u653e\u51fa\u6765\u201d\uff0c\u5e76\u5c06\u4e4b\u540e\u7684pause\u51fd\u6570\u5524\u9192\u3002<\/p>\n

      \u4f46\u5728\u89e3\u9664\u963b\u585e\u4e0epause\u7b49\u5f85\u6302\u8d77\u4fe1\u53f7\u4e4b\u95f4\uff0c\u8fd8\u662f\u6709\u53ef\u80fd\u5931\u53bbCPU\uff0c\u9664\u975e\u5c06\u8fd9\u4e24\u4e2a\u6b65\u9aa4\u505a\u6210\u4e00\u4e2a\u201c\u539f\u5b50\u64cd\u4f5c\u201d\u3002Linux\u7cfb\u7edf\u63d0\u4f9b\u7684sigsuspend\u51fd\u6570\u5c31\u5177\u5907\u8fd9\u4e2a\u529f\u80fd\u3002\u6240\u4ee5\uff0c\u5728\u65f6\u5e8f\u8981\u6c42\u6bd4\u8f83\u4e25\u683c\u7684\u573a\u5408\u4e0b\u90fd\u5e94\u8be5\u4f7f\u7528sigsuspend\u51fd\u6570\uff0c\u800c\u975epause\u51fd\u6570\u3002<\/p>\n

      \u51fd\u6570\u539f\u578b\uff1a<\/p>\n

      int\u00a0sigsuspend(const\u00a0sigset_t\u00a0*mask);<\/pre>\n

      \u51fd\u6570\u4f5c\u7528\uff1a\u6302\u8d77\u7b49\u5f85\u4fe1\u53f7;<\/p>\n

      \u51fd\u6570\u53c2\u6570\uff1amask\uff0c\u4f20\u5165\u53c2\u6570\uff0csigsuspend\u51fd\u6570\u8c03\u7528\u671f\u95f4\uff0c\u8fdb\u7a0b\u4fe1\u53f7\u5c4f\u853d\u5b57\u7531\u53c2\u6570mask\u6307\u5b9a\u3002<\/p>\n

      \u5177\u4f53\u7528\u6cd5\uff1a\u53ef\u5c06\u67d0\u4e2a\u4fe1\u53f7(\u5982SIGALRM)\u4ece\u4e34\u65f6\u4fe1\u53f7\u5c4f\u853d\u5b57mask\u4e2d\u5220\u9664\uff0c\u4e5f\u5c31\u662f\u5728\u8c03\u7528sigsuspend\u51fd\u6570\u65f6\u5bf9\u8be5\u4fe1\u53f7\u89e3\u9664\u5c4f\u853d\uff0c\u7136\u540e\u6302\u8d77\u7b49\u5f85\u4fe1\u53f7\u3002\u4f46\u6211\u4eec\u6b64\u65f6\u5df2\u7ecf\u6539\u53d8\u4e86\u8fdb\u7a0b\u7684\u4fe1\u53f7\u5c4f\u853d\u5b57\uff0c\u6240\u4ee5\u8c03\u7528\u5b8csigsuspend\u51fd\u6570\u4e4b\u540e\uff0c\u5e94\u5c06\u8fdb\u7a0b\u7684\u4fe1\u53f7\u5c4f\u853d\u5b57\u6062\u590d\u539f\u6837\u3002<\/p>\n

      #include\u00a0\r\n\u00a0#include\u00a0\r\n\u00a0#include\u00a0\r\n\r\n\u00a0void\u00a0sig_alrm(int\u00a0signo)\r\n\u00a0{\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\/*\u00a0nothing\u00a0to\u00a0do\u00a0*\/\r\n\u00a0}\r\n\r\nunsigned\u00a0int\u00a0mysleep(unsigned\u00a0int\u00a0nsecs)\r\n{\r\n\u00a0\u00a0\u00a0\u00a0struct\u00a0sigaction\u00a0newact,\u00a0oldact;\r\n\u00a0\u00a0\u00a0\u00a0sigset_t\u00a0newmask,\u00a0oldmask,\u00a0suspmask;\r\n\u00a0\u00a0\u00a0\u00a0unsigned\u00a0int\u00a0unslept;\r\n\r\n\u00a0\u00a0\u00a0\u00a0\/\/1.\u4e3aSIGALRM\u8bbe\u7f6e\u6355\u6349\u51fd\u6570\uff0c\u4e00\u4e2a\u7a7a\u51fd\u6570\r\n\u00a0\u00a0\u00a0\u00a0newact.sa_handler\u00a0=\u00a0sig_alrm;\r\n\u00a0\u00a0\u00a0\u00a0sigemptyset(&newact.sa_mask);\r\n\u00a0\u00a0\u00a0\u00a0newact.sa_flags\u00a0=\u00a00;\r\n\u00a0\u00a0\u00a0\u00a0sigaction(SIGALRM,\u00a0&newact,\u00a0&oldact);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\/\/2.\u8bbe\u7f6e\u963b\u585e\u4fe1\u53f7\u96c6\uff0c\u963b\u585eSIGALRM\u4fe1\u53f7\r\n\u00a0\u00a0\u00a0\u00a0sigemptyset(&newmask);\r\n\u00a0\u00a0\u00a0\u00a0sigaddset(&newmask,\u00a0SIGALRM);\r\n\u00a0\u00a0\u00a0sigprocmask(SIG_BLOCK,\u00a0&newmask,\u00a0&oldmask);\u00a0\u00a0\u00a0\/\/\u4fe1\u53f7\u5c4f\u853d\u5b57\u00a0mask\r\n\r\n\u00a0\u00a0\u00a0\u00a0\/\/3.\u5b9a\u65f6n\u79d2\uff0c\u5230\u65f6\u540e\u53ef\u4ee5\u4ea7\u751fSIGALRM\u4fe1\u53f7\r\n\u00a0\u00a0\u00a0\u00a0alarm(nsecs);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\/*4.\u6784\u9020\u4e00\u4e2a\u8c03\u7528sigsuspend\u4e34\u65f6\u6709\u6548\u7684\u963b\u585e\u4fe1\u53f7\u96c6\uff0c\r\n\u00a0\u00a0\u00a0\u00a0\u00a0*\u00a0\u00a0\u5728\u4e34\u65f6\u963b\u585e\u4fe1\u53f7\u96c6\u91cc\u89e3\u9664SIGALRM\u7684\u963b\u585e*\/\r\n\u00a0\u00a0\u00a0\u00a0suspmask\u00a0=\u00a0oldmask;\r\n\u00a0\u00a0\u00a0\u00a0sigdelset(&suspmask,\u00a0SIGALRM);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\/*5.sigsuspend\u8c03\u7528\u671f\u95f4\uff0c\u91c7\u7528\u4e34\u65f6\u963b\u585e\u4fe1\u53f7\u96c6suspmask\u66ff\u6362\u539f\u6709\u963b\u585e\u4fe1\u53f7\u96c6\r\n\u00a0\u00a0\u00a0\u00a0\u00a0*\u00a0\u00a0\u8fd9\u4e2a\u4fe1\u53f7\u96c6\u4e2d\u4e0d\u5305\u542bSIGALRM\u4fe1\u53f7,\u540c\u65f6\u6302\u8d77\u7b49\u5f85\uff0c\r\n\u00a0\u00a0\u00a0\u00a0\u00a0*\u00a0\u00a0\u5f53sigsuspend\u88ab\u4fe1\u53f7\u5524\u9192\u8fd4\u56de\u65f6\uff0c\u6062\u590d\u539f\u6709\u7684\u963b\u585e\u4fe1\u53f7\u96c6*\/\r\n\u00a0\u00a0\u00a0\u00a0sigsuspend(&suspmask);\r\n\r\n\u00a0\u00a0\u00a0\u00a0unslept\u00a0=\u00a0alarm(0);\r\n\u00a0\u00a0\u00a0\u00a0\/\/6.\u6062\u590dSIGALRM\u539f\u6709\u7684\u5904\u7406\u52a8\u4f5c\uff0c\u547c\u5e94\u524d\u9762\u6ce8\u91ca1\r\n\u00a0\u00a0\u00a0\u00a0sigaction(SIGALRM,\u00a0&oldact,\u00a0NULL);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\/\/7.\u89e3\u9664\u5bf9SIGALRM\u7684\u963b\u585e\uff0c\u547c\u5e94\u524d\u9762\u6ce8\u91ca2\r\n\u00a0\u00a0\u00a0\u00a0sigprocmask(SIG_SETMASK,\u00a0&oldmask,\u00a0NULL);\r\n\r\n\u00a0\u00a0\u00a0\u00a0return(unslept);\r\n}\r\n\r\nint\u00a0main(void)\r\n{\r\n\u00a0\u00a0\u00a0\u00a0while(1){\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0mysleep(2);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0printf(\"Two\u00a0seconds\u00a0passed\\n\");\r\n\u00a0\u00a0\u00a0\u00a0}\r\nreturn\u00a00;\r\n}<\/pre>\n
      ##\u53ef\u91cd\u5165\u51fd\u6570\/\u4e0d\u53ef\u91cd\u5165\u51fd\u6570<\/strong><\/span><\/div>\n

      \u4e00\u4e2a\u51fd\u6570\u5728\u88ab\u8c03\u7528\u6267\u884c\u671f\u95f4\u5c1a\u672a\u8c03\u7528\u7ed3\u675f\u7684\u65f6\u5019\uff0c\u7531\u4e8e\u67d0\u79cd\u65f6\u5e8f\uff0c\u8be5\u51fd\u6570\u53c8\u88ab\u91cd\u590d\u8c03\u7528\uff0c\u8fd9\u79cd\u60c5\u51b5\u79f0\u4e3a\u300c\u91cd\u5165\u300d\u3002\u5982\u679c\u4ece\u4fe1\u53f7\u5904\u7406\u7a0b\u5e8f\u8fd4\u56de\uff0c\u5219\u7ee7\u7eed\u6267\u884c\u8fdb\u7a0b\u65ad\u70b9\u5904\u7684\u6b63\u5e38\u6307\u4ee4\u5e8f\u5217\uff0c\u4ece\u91cd\u65b0\u6062\u590d\u5230\u65ad\u70b9\u91cd\u65b0\u6267\u884c\u7684\u8fc7\u7a0b\u4e2d\uff0c\u51fd\u6570\u6240\u4f9d\u8d56\u7684\u73af\u5883\u6ca1\u6709\u53d1\u751f\u6539\u53d8\uff0c\u5c31\u8bf4\u8fd9\u4e2a\u51fd\u6570\u662f\u53ef\u91cd\u5165\u7684\uff0c\u53cd\u4e4b\u5c31\u662f\u4e0d\u53ef\u91cd\u5165\u7684\u3002<\/p>\n

      \u5982\u679c\u8981\u5c06\u51fd\u6570\u505a\u6210\u53ef\u91cd\u5165\u51fd\u6570\uff0c\u5219\u51fd\u6570\u5185\u4e0d\u80fd\u542b\u6709\u5168\u5c40\u53d8\u91cf\u53castatic\u53d8\u91cf\uff0c\u4e5f\u4e0d\u80fd\u4f7f\u7528malloc\u3001free\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

      \u51fd\u6570\u539f\u578b\uff1a int\u00a0pause(void); \u51fd\u6570\u4f5c\u7528\uff1a \u8fdb\u7a0b\u8c03\u7528pause\u51fd\u6570\u65f6\uff0c\u4f1a\u9020\u6210\u8fdb\u7a0b\u4e3b\u52a8\u6302\u8d77(\u5904\u4e8e\u963b […]<\/p>\n","protected":false},"author":323,"featured_media":202541,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-202597","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/202597","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/323"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=202597"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/202597\/revisions"}],"predecessor-version":[{"id":202622,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/202597\/revisions\/202622"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/202541"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=202597"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=202597"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=202597"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}