{"id":313,"date":"2007-05-05T18:09:00","date_gmt":"2007-05-05T16:09:00","guid":{"rendered":"http:\/\/yesybl.org\/blogcn\/2007\/05\/05\/313\/"},"modified":"2007-05-05T18:09:00","modified_gmt":"2007-05-05T16:09:00","slug":"%e5%83%b5%e5%b0%b8%e8%bf%9b%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/www.yesybl.com\/?p=313","title":{"rendered":"\u50f5\u5c38\u8fdb\u7a0b"},"content":{"rendered":"<p>\u4ea7\u751f\u50f5\u5c38\u8fdb\u7a0b\u7684\u60c5\u51b5\u662f\uff1a\u5b50\u8fdb\u7a0b\u5728\u7236\u8fdb\u7a0b\u8c03\u7528wait()\u51fd\u6570\u4e4b\u524d\u7ed3\u675f\u3002<\/p>\n<p>\u4ee5\u4e0b\u4ee3\u7801\u53ef\u4ee5\u4ea7\u751f\u50f5\u5c38\u8fdb\u7a0b\uff1a<\/p>\n<pre lang=\"c\">\n#include &lt;stdlib.h&gt;\n#include &lt;sys\/types.h&gt;\n#include &lt;unistd.h&gt;\nint main ()\n{\n  pid_t child_pid;\n  \/* Create a child process. *\/\n  child_pid = fork ();\n  if (child_pid &gt; 0) {\n    \/* This is the parent process. Sleep for a minute. *\/\n    sleep (60);\n  }\n  else {\n    \/* This is the child process. Exit immediately. *\/\n    exit (0);\n  }\n  return 0;\n}\n<\/pre>\n<p>\u50f5\u5c38\u8fdb\u7a0b\u7684\u7236\u8fdb\u7a0b\u7ed3\u675f\u540e\u4f1a\u81ea\u52a8\u7ee7\u627f\u4e3a\u7236\u8fdb\u7a0b\u662finit\u7684\u5b50\u8fdb\u7a0b\uff0cinit\u5177\u6709\u81ea\u52a8\u6e05\u9664\u50f5\u5c38\u8fdb\u7a0b\u7684\u80fd\u529b\u3002\u56e0\u6b64\uff0c\u5f53\u50f5\u5c38\u8fdb\u7a0b\u7684\u4eb2\u592b\u8fdb\u7a0b\u7ec8\u7ed3\u540e\uff0c\u50f5\u5c38\u8fdb\u7a0b\u4e5f\u4f1a\u7acb\u5373\u88ab\u6e05\u9664\u3002<\/p>\n<p>\u50f5\u5c38\u8fdb\u7a0b\u7684\u6d88\u9664\uff0c\u53ef\u4ee5\u5229\u7528SIGCHLD\u4fe1\u53f7\uff0c\u56e0\u4e3a\u5f53\u4e00\u4e2a\u5b50\u8fdb\u7a0b\u7ec8\u6b62\u7684\u65f6\u5019\uff0cLinux\u4f1a\u5411\u5b83\u7684\u7236\u8fdb\u7a0b\u53d1\u9001SIGCHLD\u4fe1\u53f7\u3002<\/p>\n<p>\u4ee5\u4e0b\u4ee3\u7801\u63a9\u9970\u5982\u4f55\u5229\u7528\u6b64\u65b9\u6cd5\u9632\u6b62\u50f5\u5c38\u8fdb\u7a0b\uff1a<\/p>\n<pre lang=\"c\">\n#include &lt;signal.h&gt;\n#include &lt;string.h&gt;\n#include &lt;sys\/types.h&gt;\n#include &lt;sys\/wait.h&gt;\nsig_atomic_t child_exit_status;\nvoid clean_up_child_process (int signal_number)\n{\n  \/* Clean up the child process. *\/\n  int status;\n  wait (&amp;status);\n  \/* Store its exit status in a global variable.  *\/\n  child_exit_status = status;\n}\nint main ()\n{\n  \/* Handle SIGCHLD by calling clean_up_child_process. *\/\n  struct sigaction sigchld_action;\n  memset (&amp;sigchld_action, 0, sizeof (sigchld_action));\n  sigchld_action.sa_handler = &amp;clean_up_child_process;\n  sigaction (SIGCHLD, &amp;sigchld_action, NULL);\n  \/* Now do things, including forking a child process.  *\/\n  \/* ... *\/\n  return 0;\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<a href=\"https:\/\/www.yesybl.com\/?p=313\" rel=\"bookmark\" title=\"Permalink to \u50f5\u5c38\u8fdb\u7a0b\"><p>\u4ea7\u751f\u50f5\u5c38\u8fdb\u7a0b\u7684\u60c5\u51b5\u662f\uff1a\u5b50\u8fdb\u7a0b\u5728\u7236\u8fdb\u7a0b\u8c03\u7528wait()\u51fd\u6570\u4e4b\u524d\u7ed3\u675f\u3002 \u4ee5\u4e0b\u4ee3\u7801\u53ef\u4ee5\u4ea7\u751f\u50f5\u5c38\u8fdb\u7a0b\uff1a #include &lt;stdlib.h&gt; #include &lt;sys\/types.h&gt; #include &lt;unistd.h&gt; int main () { pid_t child_pid; \/* Create a child process. *\/ child_pid = fork (); if (child_pid &gt; 0) { \/* This is the parent process. Sleep for a minute. *\/ sleep (60); } else { \/* This is the child process. Exit immediately. *\/ exit [&hellip;]<\/p>\n<\/a>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pa4z28-53","_links":{"self":[{"href":"https:\/\/www.yesybl.com\/index.php?rest_route=\/wp\/v2\/posts\/313"}],"collection":[{"href":"https:\/\/www.yesybl.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yesybl.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yesybl.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yesybl.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=313"}],"version-history":[{"count":0,"href":"https:\/\/www.yesybl.com\/index.php?rest_route=\/wp\/v2\/posts\/313\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yesybl.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=313"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yesybl.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=313"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yesybl.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=313"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}