{"id":548,"date":"2016-10-25T15:31:26","date_gmt":"2016-10-25T07:31:26","guid":{"rendered":"http:\/\/www.5x44.cn\/?p=548"},"modified":"2016-12-01T15:27:18","modified_gmt":"2016-12-01T07:27:18","slug":"%e5%ad%a4%e7%ab%8b%e8%bf%9b%e7%a8%8b%e7%bb%84%ef%bc%88orphaned-process-groups%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.5x44.cn\/?p=548","title":{"rendered":"\u5b64\u7acb\u8fdb\u7a0b\u7ec4\uff08Orphaned Process Groups\uff09"},"content":{"rendered":"<p>\u4e4b\u524d\u6211\u4eec\u8bf4\u8fc7\u5982\u679c\u4e00\u4e2a\u8fdb\u7a0b\u7684\u7236\u8fdb\u7a0b\u7ec8\u6b62\u4e86\uff0c\u90a3\u4e48\u5b83\u4f1a\u201c\u5b64\u7acb\u201d\u5e76\u4e14\u7531<span style=\"color: #993300;\">init<\/span>\u8fdb\u7a0b\u7ee7\u627f\u7ba1\u7406\u3002\u73b0\u5728\u6211\u4eec\u770b\u4e00\u4e0b\u6240\u6709\u80fd\u88ab\u201c\u5b64\u7acb\u201d\u7684\u8fdb\u7a0b\u7ec4\uff0c\u8fd8\u6709POSIX.1\u5982\u4f55\u5904\u7406\u8fd9\u79cd\u72b6\u6001\u3002<\/p>\n<h2>\u4f8b\u5982\uff1a<\/h2>\n<p>\u8003\u8651\u4e00\u4e0b\u8fd9\u79cd\u60c5\u51b5\uff0c\u4e00\u4e2a\u8fdb\u7a0b<span style=\"color: #993300;\">fork<\/span>\u4e00\u4e2a\u5b50\u8fdb\u7a0b\uff0c\u7136\u540e\u7ec8\u6b62\u3002\u867d\u7136\u8fd9\u6ca1\u6709\u4ec0\u4e48\u4e0d\u6b63\u5e38\u7684\uff08\u5b83\u53ef\u80fd\u53d1\u751f\u5728\u4efb\u4f55\u65f6\u5019\uff09\uff0c\u4f46\u5f53\u7236\u8fdb\u7a0b\u7ec8\u6b62\u65f6\uff0c\u5982\u679c\u5b50\u8fdb\u7a0b\u5df2\u7ecf\u505c\u6b62\uff08\u4f7f\u7528\u4efb\u52a1\u63a7\u5236\uff09\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1f\u5b50\u8fdb\u7a0b\u8981\u5982\u4f55\u7ee7\u7eed\u8fd0\u884c\uff0c\u8fd8\u6709\u5982\u4f55\u8ba9\u5b50\u8fdb\u7a0b\u77e5\u9053\u81ea\u5df1\u5df2\u7ecf\u6210\u201c\u5b64\u513f\u201d\u4e86\u4e48\uff1f\u4e0b\u56fe\u663e\u793a\u4e86\u8fd9\u4e2a\u72b6\u6001\uff1a\u7236\u8fdb\u7a0b\u5df2\u7ecffork\u4e86\u4e00\u4e2a\u5b50\u8fdb\u7a0b\uff0c\u5b50\u8fdb\u7a0b\u505c\u6b62\uff0c\u4e4b\u540e\u7236\u8fdb\u7a0b\u6253\u7b97\u9000\u51fa\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-550\" src=\"http:\/\/blog.5x44.cn\/wp-content\/uploads\/2016\/10\/figure9.10.jpg\" alt=\"figure9-10\" width=\"579\" height=\"362\" srcset=\"https:\/\/www.5x44.cn\/wp-content\/uploads\/2016\/10\/figure9.10.jpg 579w, https:\/\/www.5x44.cn\/wp-content\/uploads\/2016\/10\/figure9.10-300x188.jpg 300w, https:\/\/www.5x44.cn\/wp-content\/uploads\/2016\/10\/figure9.10-432x270.jpg 432w\" sizes=\"auto, (max-width: 579px) 100vw, 579px\" \/><\/p>\n<p>\u4e0b\u9762\u7684\u7a0b\u5e8f\uff08FIGURE 9.11\uff09\u6f14\u793a\u4e86\u8fd9\u79cd\u60c5\u51b5\u3002\u8fd9\u4e2a\u7a0b\u5e8f\u6709\u4e00\u4e9b\u65b0\u529f\u80fd\u3002\u8fd9\u91cc\u6211\u4eec\u5047\u5b9a\u7528\u7684SHELL\u652f\u6301\u4efb\u52a1\u63a7\u5236\u3002\u56de\u5fc6\u4e4b\u524d\u7684\u7ae0\u8282\uff0cSHELL\u628a\u524d\u53f0\u8fdb\u7a0b\u7ec4\u653e\u8fdb\u5b83\u81ea\u5df1\u7684\u8fdb\u7a0b\u7ec4\uff08\u672c\u4f8b\u662f6099\uff09\u4e2d\uff0c\u5e76\u4e14SHELL\u7559\u5728\u5b83\u81ea\u5df1\u7684\u8fdb\u7a0b\u7ec4\uff082837\uff09\u4e2d\u3002\u5b50\u8fdb\u7a0b\u4ece\u5b83\u7684\u7236\u8fdb\u7a0b\u90a3\u91cc\u7ee7\u627f\u4e86\u8fdb\u7a0b\u7ec4\uff086099\uff09\u3002\u7136\u540e<span style=\"color: #993300;\">fork<\/span>\uff0c<\/p>\n<table border=\"0\" width=\"100%\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<h5 class=\"docExampleTitle\">Figure 9.11. Creating an orphaned process group<\/h5>\n<pre><span style=\"color: #993300;\">#include \"apue.h\"\r\n#include &lt;errno.h&gt;\r\n\r\nstatic void\r\nsig_hup(int signo)\r\n{\r\n    printf(\"SIGHUP received, pid = %d\\n\", getpid());\r\n}\r\n\r\nstatic void\r\npr_ids(char *name)\r\n{\r\n    printf(\"%s: pid = %d, ppid = %d, pgrp = %d, tpgrp = %d\\n\",\r\n        name, getpid(), getppid(), getpgrp(), tcgetpgrp(STDIN_FILENO));\r\n    fflush(stdout);\r\n}\r\n\r\nint\r\nmain(void)\r\n{\r\n     char     c;\r\n     pid_t    pid;\r\n\r\n     pr_ids(\"parent\");\r\n     if ((pid = fork()) &lt; 0) {\r\n         err_sys(\"fork error\");\r\n     } else if (pid &gt; 0) {   \/* parent *\/\r\n         sleep(5);       \/*sleep to let child stop itself *\/\r\n         exit(0);        \/* then parent exits *\/\r\n     } else {            \/* child *\/\r\n         pr_ids(\"child\");\r\n         signal(SIGHUP, sig_hup);    \/* establish signal handler *\/\r\n         kill(getpid(), SIGTSTP);    \/* stop ourself *\/\r\n         pr_ids(\"child\");    \/* prints only if we're continued *\/\r\n         if (read(STDIN_FILENO, &amp;c, 1) != 1)\r\n             printf(\"read error from controlling TTY, errno = %d\\n\",\r\n                 errno);\r\n         exit(0);\r\n     }\r\n}\r\n<\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<ul>\n<li>\u7236\u8fdb\u7a0b\u4f11\u77205\u79d2\u3002\u8fd9\u662f\u6211\u4eec\u8ba9\u5b50\u8fdb\u7a0b\u5728\u7236\u8fdb\u7a0b\u4e4b\u524d\u6267\u884c\u7684\u4e00\u79cd\u65b9\u6cd5\u3002\uff08imperfect\uff09<\/li>\n<li>\u5b50\u8fdb\u7a0b\u4e3a\u6302\u8d77\u4fe1\u53f7\uff08<span style=\"color: #993300;\">SIGHUP<\/span>\uff09\u5efa\u7acb\u4fe1\u53f7\u53e5\u67c4\u3002\u8fd9\u80fd\u8ba9\u6211\u4eec\u770b\u5230<span style=\"color: #993300;\">SIGHUP<\/span>\u662f\u5426\u5df2\u7ecf\u53d1\u9001\u5230\u5b50\u8fdb\u7a0b\u3002<\/li>\n<li>\u5b50\u8fdb\u7a0b\u4f7f\u7528<span style=\"color: #993300;\">kill<\/span>\u7a0b\u5e8f\u53d1\u9001\u505c\u6b62\u4fe1\u53f7\uff08<span style=\"color: #993300;\">SIGTSTP<\/span>\uff09\u3002\u8fd9\u91cc\u505c\u6b62\u5b50\u8fdb\u7a0b\u7c7b\u4f3c\u4e8e\u6211\u4eec\u4f7f\u6709\u7ec8\u7aef\u7684\u6302\u8d77\u5b57\u7b26\uff08Ctrl+Z\uff09\u505c\u6b62\u524d\u53f0\u4efb\u52a1\u3002<\/li>\n<li>\u5f53\u7236\u8fdb\u7a0b\u7ed3\u675f\uff0c\u5b50\u8fdb\u7a0b\u53d8\u6210\u201c\u5b64\u5c9b\u201d\uff0c\u6240\u4ee5\u5b50\u8fdb\u7a0b\u7684\u7236\u8fdb\u7a0b\u53d8\u62101\u2014\u2014<span style=\"color: #993300;\">init<\/span>\u8fdb\u7a0b\u7684ID\u3002<\/li>\n<li>\u73b0\u5728\uff0c\u5b50\u8fdb\u7a0b\u662f<em>\u5b64\u5c9b\u8fdb\u7a0b\u7ec4<\/em>\u7684\u6210\u5458\u4e86\u3002POSIX.1\u5b9a\u4e49\u7684\u72ec\u7acb\u8fdb\u7a0b\u7ec4\u662f\uff1aone in which the parent of every member is either itself a member of the group or is not a member of the groups session\u3002\u6362\u79cd\u8bf4\u6cd5\u5c31\u662f\uff1a\u53ea\u8981\u8fdb\u7a0b\u7ec4\u4e2d\u7684\u8fdb\u7a0b\u5b83\u6709\u4e00\u4e2a\u5728\u540c\u4e00\u4f1a\u8bdd\u4e0d\u540c\u8fdb\u7a0b\u7ec4\u7684\u7236\u8fdb\u7a0b\uff0c\u90a3\u4e48\u5b83\u5c31\u4e0d\u662f\u5b64\u5c9b\u8fdb\u7a0b\u7ec4\u3002\u5982\u679c\u8fdb\u7a0b\u7ec4\u4e0d\u662f\u201c\u5b64\u5c9b\u201d\uff0c\u90a3\u4e48\u5b83\u7684\u7236\u8fdb\u7a0b\u5c31\u6709\u673a\u4f1a\u91cd\u65b0\u542f\u52a8\u5df2\u7ecf\u505c\u6b62\u7684\u8fdb\u7a0b\u3002\u5728\u8fd9\u91cc\uff0c\u8fdb\u7a0b\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u8fdb\u7a0b\u7684\u7236\u8fdb\u7a0b\uff08\u4e5f\u5c31\u662f\u8fdb\u7a0b1\u662f\u8fdb\u7a0b6100\u7684\u7236\u8fdb\u7a0b\uff09\u90fd\u5c5e\u4e8e\u5176\u5b83\u4f1a\u8bdd\u3002<\/li>\n<li>\u56e0\u4e3a\u5f53\u7236\u8fdb\u7a0b\u7ec8\u6b62\u65f6\uff0c\u8fdb\u7a0b\u7ec4\u53d8\u6210\u201c\u5b64\u5c9b\u201d\uff0c\u6240\u4ee5POSIX.1\u9700\u8981\u6bcf\u4e00\u4e2a\u8fdb\u7a0b\uff0c\u8be5\u8fdb\u7a0b\u662f\u65b0\u8fd1\u7684\u5df2\u7ecf\u505c\u6b62\uff08\u4e00\u4e2a\u6302\u8d77\u4fe1\u53f7\u201c<span style=\"color: #993300;\">SIGHUP<\/span>\u201d\u8ddf\u7740\u4e00\u4e2a\u7ee7\u7eed\u4fe1\u53f7\u201c<span style=\"color: #993300;\">SIGCONT<\/span>\u201d\uff09\u7684\u5b64\u5c9b\u8fdb\u7a0b\u7ec4\uff08\u5c31\u50cf\u6211\u4eec\u7684\u5b50\u8fdb\u7a0b\uff09\u3002<\/li>\n<li>\u8fd9\u80fd\u5f15\u8d77\u5728\u8fdb\u7a0b\u5904\u7406\u6302\u8d77\u4fe1\u53f7\u540e\uff0c\u5b50\u8fdb\u7a0b\u7ee7\u7eed\u3002\u5bf9\u4e8e\u6302\u8d77\u4fe1\u53f7\u9ed8\u8ba4\u52a8\u4f5c\u662f\u7ec8\u6b62\u8fdb\u7a0b\uff0c\u6240\u4ee5\u6211\u4eec\u5fc5\u987b\u63d0\u4f9b\u4fe1\u53f7\u53e5\u67c4\uff0c\u7528\u4e8e\u6355\u6349\u4fe1\u53f7\u3002\u56e0\u6b64\u6211\u4eec\u5e0c\u671b\u5728<span style=\"color: #993300;\">sig_hup<\/span>\u51fd\u6570\u4e2d\u7684<span style=\"color: #993300;\">printf<\/span>\u51fd\u6570\u5728<span style=\"color: #993300;\">pr_ids<\/span>\u51fd\u6570\u4e2d\u7684<span style=\"color: #993300;\">printf<\/span>\u4e4b\u524d\u663e\u793a\u3002<\/li>\n<\/ul>\n<p>\u8fd9\u662f\u4e0a\u9762\u51fd\u6570\u7684\u8f93\u51fa\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   parent: pid = 6099, ppid = 2837, pgrp = 6099, tpgrp = 6099\r\n   child: pid = 6100, ppid = 6099, pgrp = 6099, tpgrp = 6099\r\n   $ SIGHUP received, pid = 6100\r\n   child: pid = 6100, ppid = 1, pgrp = 6099, tpgrp = 2837\r\n   read error from controlling TTY, errno = 5\r\n<\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u6ce8\u610f\uff1a\u6765\u81ea\u5b50\u8fdb\u7a0b\u7684shell\u8f93\u51fa\u63d0\u793a\uff0c\u56e0\u4e3a\u4e24\u4e2a\u8fdb\u7a0b\u90fd\u662f\u6211\u4eec\u767b\u9646shell\u6253\u5f00\u7684\uff0c\u5e76\u4e14\u5b50\u8fdb\u7a0b\u5199\u5230\u7ec8\u7aef\u3002\u5c31\u50cf\u6211\u4eec\u5e0c\u671b\u7684\u90a3\u6837\uff0c\u5b50\u8fdb\u7a0b\u7684\u7236\u8fdb\u7a0bID\u53d8\u6210\u4e861\u3002<\/p>\n<p>\u5728\u5b50\u8fdb\u7a0b\u4e2d\u8c03\u7528<span style=\"color: #993300;\">pr_ids<\/span>\u540e\uff0c\u7a0b\u5e8f\u8bd5\u56fe\u4ece\u6807\u51c6\u8f93\u5165\u8bfb\u53d6\u6570\u636e\u3002\u5c31\u50cf\u4e4b\u524d\u770b\u5230\u7684\u90a3\u6837\uff0c\u5f53\u540e\u53f0\u8fdb\u7a0b\u7ec4\u8bd5\u56fe\u4ece\u5b83\u7684\u63a7\u5236\u7ec8\u7aef\u4e2d\u8bfb\u53d6\u65f6\uff0c\u4f1a\u4ea7\u751f\u4e00\u4e2a<span style=\"color: #993300;\">SIGTTIN<\/span>\u4fe1\u53f7\u7ed9\u540e\u53f0\u8fdb\u7a0b\u7ec4\u3002\u4f46\u662f\u5728\u8fd9\u91cc\u6211\u4eec\u6709\u4e00\u4e2a\u5b64\u5c9b\u8fdb\u7a0b\u7ec4\uff1b\u5982\u679c\u5185\u6838\u4f7f\u7528\u8fd9\u4e2a\u4fe1\u53f7\u505c\u6b62\u8fdb\u7a0b\uff0c\u90a3\u4e48\u8fdb\u7a0b\u7ec4\u4e2d\u7684\u8be5\u8fdb\u7a0b\u53ef\u80fd\u6c38\u8fdc\u90fd\u4e0d\u80fd\u6062\u590d\u8fd0\u884c\u4e86\u3002POSIX.1\u6307\u5b9a\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c<span style=\"color: #993300;\">read<\/span>\u4f1a\u8fd4\u56de\u4e00\u4e2a<span style=\"color: #993300;\">errno<\/span>\u4e3a<span style=\"color: #993300;\">EIO<span style=\"color: #000000;\">\u7684\u9519\u8bef<\/span><\/span>\uff08\u5728\u672c\u7cfb\u7edf\u4e0a\u5b83\u7684\u503c\u662f5\uff09\u3002<\/p>\n<p>\u6700\u540e\uff0c\u6ce8\u610f\u4e00\u4e0b\u5f53\u7236\u8fdb\u7a0b\u7ec8\u6b62\u65f6\uff0c\u6211\u4eec\u7684\u5b50\u8fdb\u7a0b\u5df2\u7ecf\u88ab\u653e\u5230\u540e\u53f0\u8fdb\u7a0b\u7ec4\u4e2d\uff0c\u56e0\u4e3a\u7236\u8fdb\u7a0b\u662f\u88abshell\u5f53\u505a\u524d\u53f0\u8fdb\u7a0b\u6267\u884c\u7684\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e4b\u524d\u6211\u4eec\u8bf4\u8fc7\u5982\u679c\u4e00\u4e2a\u8fdb\u7a0b\u7684\u7236\u8fdb\u7a0b\u7ec8\u6b62\u4e86\uff0c\u90a3\u4e48\u5b83\u4f1a\u201c\u5b64\u7acb\u201d\u5e76\u4e14\u7531init\u8fdb\u7a0b\u7ee7\u627f\u7ba1\u7406\u3002\u73b0\u5728\u6211\u4eec\u770b\u4e00\u4e0b\u6240\u6709\u80fd\u88ab\u201c\u5b64\u7acb\u201d\u7684\u8fdb\u7a0b\u7ec4\uff0c\u8fd8\u6709POSIX.1\u5982\u4f55\u5904\u7406\u8fd9\u79cd\u72b6\u6001\u3002&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/www.5x44.cn\/?p=548\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-548","post","type-post","status-publish","format-standard","hentry","category-apue"],"_links":{"self":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/548","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=548"}],"version-history":[{"count":13,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/548\/revisions"}],"predecessor-version":[{"id":562,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/548\/revisions\/562"}],"wp:attachment":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=548"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=548"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=548"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}