{"id":280,"date":"2016-06-29T10:46:27","date_gmt":"2016-06-29T02:46:27","guid":{"rendered":"http:\/\/www.5x44.cn\/?p=280"},"modified":"2016-06-29T10:46:27","modified_gmt":"2016-06-29T02:46:27","slug":"atexit%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/www.5x44.cn\/?p=280","title":{"rendered":"atexit\u51fd\u6570"},"content":{"rendered":"<p>Section 7.3.\u00a0 Process Termination<\/p>\n<table border=\"0\" width=\"100%\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<table class=\"allBorders\" border=\"1\" rules=\"none\" cellspacing=\"0\" cellpadding=\"5\">\n<tbody>\n<tr>\n<td class=\"docTableCell\" align=\"left\" valign=\"top\">\n<pre>#include &lt;stdlib.h&gt;\r\n\r\nint atexit(void (*<span class=\"docEmphItalicAlt\">func<\/span>)(void));\r\n<\/pre>\n<\/td>\n<\/tr>\n<tr>\n<td class=\"docTableCell\" align=\"right\" valign=\"top\">\n<p class=\"docText\">Returns: 0 if OK, nonzero on error<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>ISO C\u4e00\u4e2a\u8fdb\u7a0b\u6700\u591a\u6ce8\u518c32\u4e2a\u53ef\u4ee5\u81ea\u52a8\u88abexit\u51fd\u6570\u8c03\u7528\u7684\u51fd\u6570\u3002<\/p>\n<p>\u4f20\u9012\u4e00\u4e2a\u51fd\u6570\u6307\u9488\u505a\u4e3aatexit\u7684\u53c2\u6570\uff0c\u5f53\u8be5\u51fd\u6570\u88ab\u8c03\u7528\u65f6\uff0c\u4e0d\u4f20\u9012\u4efb\u4f55\u53c2\u6570\u4e5f\u4e0d\u8fd4\u56de\u4efb\u4f55\u503c\u3002<span style=\"color: #339966;\">exit\u51fd\u6570\u4ee5\u8fd9\u4e9b\u51fd\u6570\u7684\u53cd\u5411\u6ce8\u518c\u987a\u5e8f\u6267\u884c\u3002<\/span><\/p>\n<p>ISO C\u548cPOSIX.1\uff0cexit\u51fd\u6570\u5148\u8c03\u7528exit handler\uff0c\u7136\u540e\u518d\u901a\u8fc7fclose\u5173\u95ed\u6240\u6709\u6253\u5f00\u7684\u6d41\u3002POSIX.1 extends the ISO C standard by specifying that any exit handlers installed will be cleared if the program calls any of the exec family of functions\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 7.3. Example of exit handlers<\/h5>\n<pre>#include \"apue.h\"\r\n\r\nstatic void my_exit1(void);\r\nstatic void my_exit2(void);\r\n\r\nint\r\nmain(void)\r\n{\r\n     if (atexit(my_exit2) != 0)\r\n         err_sys(\"can't register my_exit2\");\r\n\r\n     if (atexit(my_exit1) != 0)\r\n         err_sys(\"can't register my_exit1\");\r\n\r\n     if (atexit(my_exit1) != 0)\r\n         err_sys(\"can't register my_exit1\");\r\n\r\n     printf(\"main is done\\n\");\r\n     return(0);\r\n}\r\n\r\nstatic void\r\nmy_exit1(void)\r\n{\r\n   printf(\"first exit handler\\n\");\r\n}\r\n\r\nstatic void\r\nmy_exit2(void)\r\n{\r\n   printf(\"second exit handler\\n\");\r\n}<\/pre>\n<pre>    $ <span class=\"docEmphStrong\">.\/a.out<\/span>\r\n    main is done\r\n    first exit handler\r\n    first exit handler\r\n    second exit handler<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>Section 7.3.\u00a0 Process Termination #include &lt;stdlib.h&gt; int atexit(void (*fu&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/www.5x44.cn\/?p=280\"> 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":[19,20],"class_list":["post-280","post","type-post","status-publish","format-standard","hentry","category-apue","tag-atexit","tag-c"],"_links":{"self":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/280","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=280"}],"version-history":[{"count":1,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/280\/revisions"}],"predecessor-version":[{"id":281,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/280\/revisions\/281"}],"wp:attachment":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=280"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=280"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=280"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}