{"id":710,"date":"2017-03-01T11:06:37","date_gmt":"2017-03-01T03:06:37","guid":{"rendered":"http:\/\/www.5x44.cn\/?p=710"},"modified":"2017-03-01T11:06:37","modified_gmt":"2017-03-01T03:06:37","slug":"abort%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/www.5x44.cn\/?p=710","title":{"rendered":"abort\u51fd\u6570"},"content":{"rendered":"<p>\u6211\u4eec\u6ce8\u610f\u5230\u4e4b\u524d\u4f7f\u7528\u7684<span style=\"color: #993300;\">abort<\/span>\u51fd\u6570\u4f1a\u5bfc\u81f3\u7a0b\u5e8f\u975e\u6b63\u5e38\u7ec8\u6b62\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=\"438\" 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;stdlib.h&gt;\r\n\r\nvoid abort(void);\r\n<\/span><\/pre>\n<\/td>\n<\/tr>\n<tr>\n<td class=\"docTableCell\" align=\"right\" valign=\"top\">\n<p class=\"docText\">This function never returns<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u8fd9\u4e2a\u51fd\u6570\u53d1\u6837<span style=\"color: #993300;\">SIGABRT<\/span>\u4fe1\u53f7\u5230\u8c03\u7528\u8fdb\u7a0b\u3002\uff08\u8fdb\u7a0b\u4e0d\u5e94\u8be5\u5ffd\u7565\u8fd9\u4e2a\u4fe1\u53f7\u3002\uff09ISO C\u58f0\u660e\u8c03\u7528<span style=\"color: #993300;\">abort<\/span>\u51fd\u6570\u5c06\u4f1a\u901a\u8fc7\u8c03\u7528<span style=\"color: #993300;\">raise(SIGABRT)<\/span>\u4f20\u9001\u4e00\u4e2a\u672a\u6210\u529f\u7ec8\u6b62\u7684\u901a\u544a\u5230\u4e3b\u673a\u73af\u5883\u3002<\/p>\n<p>ISO C\u8981\u6c42\u5982\u679c\u4fe1\u53f7\u88ab\u6355\u83b7\u5e76\u4e14\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u8fd4\u56de\uff0c<span style=\"color: #993300;\">abort<\/span>\u51fd\u6570\u4ecd\u7136\u4e0d\u4f1a\u8fd4\u56de\u5b83\u7684\u8c03\u7528\u8005\u3002\u5982\u679c\u8fd9\u4e2a\u4fe1\u53f7\u88ab\u6355\u83b7\uff0c\u53ea\u6709\u8c03\u7528\u4e86<span style=\"color: #993300;\">exit<\/span>\uff0c<span style=\"color: #993300;\">_exit<\/span>\uff0c<span style=\"color: #993300;\">_Exit<\/span>\uff0c<span style=\"color: #993300;\">longjmp<\/span>\uff0c\u6216<span style=\"color: #993300;\">siglongjmp<\/span>\u624d\u4f1a\u5bfc\u81f4\u4fe1\u53f7\u6355\u83b7\u51fd\u6570\u4e0d\u80fd\u8fd4\u56de\u3002POSIX.1\u540c\u6837\u6307\u5b9a\u4e86<span style=\"color: #993300;\">abort<\/span>\u53ef\u4ee5\u8986\u76d6\u8fdb\u7a0b\u7684\u4fe1\u53f7\u963b\u585e\u6216\u5ffd\u7565\u3002<\/p>\n<p>\u6355\u83b7<span style=\"color: #993300;\">SIGABRT<\/span>\u4fe1\u53f7\u7684\u4f5c\u7528\u662f\u5728\u8fdb\u7a0b\u7ec8\u6b62\u4e4b\u524d\u6267\u884c\u6e05\u7406\u52a8\u4f5c\u3002\u5982\u679c\u8fdb\u7a0b\u6ca1\u6709\u5728\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u4e2d\u7ec8\u6b62\u5b83\u81ea\u5df1\uff0cPOSIX.1\u58f0\u660e\uff0c\u5f53\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u8fd4\u56de\u65f6\uff0c<span style=\"color: #993300;\">abort<\/span>\u7ec8\u6b62\u8fdb\u7a0b\u3002<\/p>\n<p>ISO C\u7248\u672c\u7684\u8fd9\u4e2a\u51fd\u6570\u6839\u636e\u5177\u4f53\u7684\u5b9e\u73b0\u4e0d\u540c\uff0c\u51b3\u5b9a\u662f\u5426\u5237\u65b0\u8f93\u51fa\u6d41\u548c\u662f\u5426\u5220\u9664\u4e34\u65f6\u6587\u4ef6\u3002POSIX.1\u8981\u6c42\u7684\u66f4\u591a\uff0c\u5e76\u4e14\u8981\u6c42\u5982\u679c\u8c03\u7528<span style=\"color: #993300;\">abort<\/span>\u7ec8\u6b62\u8fdb\u7a0b\uff0c\u90a3\u4e48\u5728\u7ed3\u675f\u8fdb\u7a0b\u524d\uff0c\u53d7\u8be5\u8fdb\u7a0b\u5f71\u54cd\u7684\u6807\u51c6I\/O\u6d41\u5c31\u50cf\u8c03\u7528<span style=\"color: #993300;\">fclose<\/span>\u4e00\u6837\u3002<\/p>\n<p>\uff08<\/p>\n<p>\u65e9\u671f\u7684System V\u7248\u672c\u7684abort\u51fd\u6570\u4ea7\u751f<span style=\"color: #993300;\">SIGIOT<\/span>\u4fe1\u53f7\u3002\u53e6\u5916\uff0c\u5b83\u5f88\u6709\u53ef\u80fd\u88ab\u8fdb\u7a0b\u5ffd\u7565\u6216\u6355\u83b7\uff0c\u5e76\u4ece\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u4e2d\u8fd4\u56de\uff0c\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b<span style=\"color: #993300;\">abort<\/span>\u8fd4\u56de\u5b83\u7684\u8c03\u7528\u8005\u3002<\/p>\n<p>4.3BSD\u4ea7\u751f<span style=\"color: #993300;\">SIGILL<\/span>\u4fe1\u53f7\u3002\u5728\u8fd4\u56de\u8be5\u4fe1\u53f7\u4e4b\u524d\uff0c4.3BSD\u51fd\u6570\u89e3\u9501\u4fe1\u53f7\u5e76\u91cd\u7f6e\u5b83\u7684<span style=\"color: #993300;\">SIG_DFL<\/span>\uff08\u7ec8\u6b62\u5e76\u4ea7\u751f\u4e00\u4e2a<span style=\"color: #993300;\">core<\/span>\u6587\u4ef6\uff09\u3002\u8fd9\u53ef\u4ee5\u9632\u6b62\u8fdb\u7a0b\u5ffd\u7565\u6216\u6355\u83b7\u5b83\u3002<\/p>\n<p>\u5386\u53f2\u4e0a\uff0c<span style=\"color: #993300;\">abort<\/span>\u7684\u5b9e\u73b0\u4e0d\u540c\u4e8e\u4e0e\u4e4b\u4ea4\u4e92\u7684\u6807\u51c6I\/O\u6d41\u3002\u5bf9\u4e8e\u9632\u62a4\u7a0b\u5e8f\u548c\u6539\u5584\u79fb\u690d\uff0c\u5982\u679c\u6211\u4eec\u60f3\u5237\u65b0\u6807\u51c6I\/O\u6d41\uff0c\u90a3\u4e48\u5c31\u8981\u5728\u8c03\u7528<span style=\"color: #993300;\">abort<\/span>\u4e4b\u524d\u624b\u52a8\u5237\u65b0\u3002<\/p>\n<p>\u56e0\u4e3a\u591a\u6570UNIX\u7cfb\u7edf\u4e34\u65f6\u6587\u4ef6\u7684\u5b9e\u73b0\u5728\u521b\u5efa\u6587\u4ef6\u540e\u7acb\u5373\u8c03\u7528\u4e86<span style=\"color: #993300;\">unlink<\/span>\uff0cISO C\u5173\u4e8e\u4e34\u65f6\u6587\u4ef6\u7684\u8b66\u544a\u901a\u5e38\u4e0d\u7528\u5173\u5fc3\u3002<\/p>\n<p>\uff09<\/p>\n<h1>\u4f8b\u5b50<\/h1>\n<p>Figure 10.25\u663e\u793a\u4e86\u4e00\u4e2a\u7531POSIX.1\u5b9e\u73b0\u7684<span style=\"color: #993300;\">abort<\/span>\u51fd\u6570\u7684\u4f8b\u5b50\u3002<\/p>\n<p>\u6211\u4eec\u9996\u5148\u770b\u4e00\u4e0b\u9ed8\u8ba4\u52a8\u4f5c\u662f\u5426\u4f1a\u53d1\u751f\uff1b\u5982\u679c\u53d1\u751f\u4e86\uff0c\u6211\u4eec\u5237\u65b0\u6807\u51c6I\/O\u6d41\u3002\u8fd9\u4e0d\u7b49\u4e8e\u5728\u6240\u6709\u6253\u5f00\u7684\u6d41\u4e0a\u4f7f\u7528<span style=\"color: #993300;\">fclose<\/span>\uff08\u56e0\u4e3a\u5b83\u4ec5\u662f\u5237\u65b0\u5b83\u4eec\u5e76\u6ca1\u6709\u5173\u95ed\u5b83\u4eec\uff09\uff0c\u4f46\u662f\u5f53\u8fdb\u7a0b\u7ec8\u6b62\u65f6\uff0c\u7cfb\u7edf\u5173\u95ed\u6240\u6709\u6253\u5f00\u7684\u6587\u4ef6\u3002\u5982\u679c\u8fdb\u7a0b\u6355\u83b7\u5230\u4e86\u4fe1\u53f7\u5e76\u4e14\u8fd4\u56de\uff0c\u6211\u4eec\u518d\u6b21\u5237\u65b0\u6240\u6709\u7684\u6d41\uff0c\u56e0\u4e3a\u8fdb\u7a0b\u53ef\u80fd\u5df2\u7ecf\u4ea7\u751f\u4e86\u8f93\u51fa\u3002\u552f\u4e00\u6211\u4eec\u4e0d\u53bb\u5904\u7406\u7684\u6761\u4ef6\u662f\u5982\u679c\u8fdb\u7a0b\u6355\u83b7\u5230\u4fe1\u53f7\u5e76\u8c03\u7528<span style=\"color: #993300;\">_exit<\/span>\u6216<span style=\"color: #993300;\">_Exit<\/span>\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4efb\u4f55\u5185\u5b58\u4e2d\u672a\u5237\u65b0\u7684\u6807\u51c6I\/O\u6d41\u7f13\u5b58\u90fd\u4f1a\u88ab\u4e22\u5f03\u3002\u6211\u4eec\u5047\u8bbe\u8fd9\u79cd\u60c5\u51b5\u8c03\u7528\u8005\u4e0d\u60f3\u8ba9\u7f13\u5b58\u5237\u65b0\u3002<\/p>\n<p>\u56de\u5fc6APUE10.9\u8282\uff0c\u5982\u679c\u4f7f\u7528<span style=\"color: #993300;\">kill<\/span>\u4e3a\u8c03\u7528\u8005\u53d1\u9001\u4fe1\u53f7\uff0c\u5e76\u4e14\u5982\u679c\u8fd9\u4e2a\u4fe1\u53f7\u6ca1\u6709\u88ab\u963b\u585e\uff0c\u90a3\u4e48\u8fd9\u4e2a\u4fe1\u53f7\u5728<span style=\"color: #993300;\">kill<\/span>\u8fd4\u56de\u524d\u88ab\u4f20\u9001\u5230\u8fdb\u7a0b\u3002\u6211\u4eec\u963b\u585e\u9664\u4e86<span style=\"color: #993300;\">SIGABRT<\/span>\u4e4b\u5916\u6240\u6709\u4fe1\u53f7\uff0c\u6240\u4ee5\u6211\u4eec\u77e5\u9053\u5982\u679c\u8c03\u7528<span style=\"color: #993300;\">kill<\/span>\u8fd4\u56de\u4e86\uff0c\u90a3\u4e48\u8fdb\u7a0b\u6355\u83b7\u4fe1\u53f7\u5e76\u4e14\u4fe1\u53f7\u6355\u83b7\u51fd\u6570\u8fd4\u56de\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.25. Implementation of POSIX.1 <tt>abort<\/tt><\/h3>\n<pre><span style=\"color: #993300;\">#include &lt;signal.h&gt;\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;unistd.h&gt;\r\n\r\nvoid\r\nabort(void)         \/* POSIX-style abort() function *\/\r\n{\r\n    sigset_t           mask;\r\n    struct sigaction   action;\r\n\r\n    \/*\r\n     * Caller can't ignore SIGABRT, if so reset to default.\r\n     *\/\r\n    sigaction(SIGABRT, NULL, &amp;action);\r\n    if (action.sa_handler == SIG_IGN) {\r\n        action.sa_handler = SIG_DFL;\r\n        sigaction(SIGABRT, &amp;action, NULL);\r\n    }\r\n    if (action.sa_handler == SIG_DFL)\r\n        fflush(NULL);           \/* flush all open stdio streams *\/\r\n\r\n    \/*\r\n     * Caller can't block SIGABRT; make sure it's unblocked.\r\n     *\/\r\n    sigfillset(&amp;mask);\r\n    sigdelset(&amp;mask, SIGABRT);  \/* mask has only SIGABRT turned off *\/\r\n    sigprocmask(SIG_SETMASK, &amp;mask, NULL);\r\n    kill(getpid(), SIGABRT);    \/* send the signal *\/\r\n\r\n    \/*\r\n     * If we're here, process caught SIGABRT and returned.\r\n     *\/\r\n    fflush(NULL);               \/* flush all open stdio streams *\/\r\n    action.sa_handler = SIG_DFL;\r\n    sigaction(SIGABRT, &amp;action, NULL);  \/* reset to default *\/\r\n    sigprocmask(SIG_SETMASK, &amp;mask, NULL);  \/* just in case ... *\/\r\n    kill(getpid(), SIGABRT);                \/* and one more time *\/\r\n    exit(1);    \/* this should never be executed ... *\/\r\n}\r\n<\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>\u6211\u4eec\u6ce8\u610f\u5230\u4e4b\u524d\u4f7f\u7528\u7684abort\u51fd\u6570\u4f1a\u5bfc\u81f3\u7a0b\u5e8f\u975e\u6b63\u5e38\u7ec8\u6b62\u3002 #include &lt;stdlib.h&gt; void abort(void); This fu&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/www.5x44.cn\/?p=710\"> 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-710","post","type-post","status-publish","format-standard","hentry","category-apue"],"_links":{"self":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/710","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=710"}],"version-history":[{"count":5,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/710\/revisions"}],"predecessor-version":[{"id":715,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/710\/revisions\/715"}],"wp:attachment":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=710"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=710"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=710"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}