{"id":13969,"date":"2024-07-12T10:39:48","date_gmt":"2024-07-12T02:39:48","guid":{"rendered":"http:\/\/xinyiworld.top\/wordpress_it\/?p=13969"},"modified":"2024-07-12T15:02:11","modified_gmt":"2024-07-12T07:02:11","slug":"zygote-java%e6%ba%90%e7%a0%81%e5%88%86%e6%9e%90","status":"publish","type":"post","link":"http:\/\/xinyiworld.top\/wordpress_it\/?p=13969","title":{"rendered":"Zygote.java\u6e90\u7801\u5206\u6790"},"content":{"rendered":"<p>\u6e90\u7801\u4f4d\u7f6e\uff1a<code>frameworks\/base\/core\/java\/com\/android\/internal\/os\/Zygote.java<\/code><\/p>\n<h2>forkSystemServer\u65b9\u6cd5<\/h2>\n<p><a href=\"https:\/\/www.jianshu.com\/p\/bb96360b4084\">https:\/\/www.jianshu.com\/p\/bb96360b4084<\/a><\/p>\n<pre><code class=\"language-java\">335     public static int forkSystemServer(int uid, int gid, int[] gids, int runtimeFlags,\n336             int[][] rlimits, long permittedCapabilities, long effectiveCapabilities) {\n337         ZygoteHooks.preFork();\n338         \/\/ Resets nice priority for zygote process.\n339         resetNicePriority();\n340         int pid = nativeForkSystemServer(\n341                 uid, gid, gids, runtimeFlags, rlimits,\n342                 permittedCapabilities, effectiveCapabilities);\n343         \/\/ Enable tracing as soon as we enter the system_server.\n344         if (pid == 0) {\n345             Trace.setTracingEnabled(true, runtimeFlags);\n346         }\n347         ZygoteHooks.postForkCommon();\n348         return pid;\n349     }<\/code><\/pre>\n<h3>\u2192nativeForkSystemServer<\/h3>\n<p>nativeForkSystemServer\u65b9\u6cd5\u5b9e\u73b0\u4f4d\u4e8e<code>frameworks\/base\/core\/jni\/com_android_internal_os_Zygote.cpp +1404<\/code>,\u6700\u7ec8\u662f\u8c03\u7528\u4e86c\u8bed\u8a00\u7684fork\u51fd\u6570\u3002<\/p>\n<pre><code class=\"language-cpp\">1404 static jint com_android_internal_os_Zygote_nativeForkSystemServer(\n1405         JNIEnv* env, jclass, uid_t uid, gid_t gid, jintArray gids,\n1406         jint runtime_flags, jobjectArray rlimits, jlong permitted_capabilities,\n1407         jlong effective_capabilities) {\n...\n1417 \n1418   pid_t pid = ForkCommon(env, true,\n1419                          fds_to_close,\n1420                          fds_to_ignore);\n1421   if (pid == 0) {\n1422       SpecializeCommon(env, uid, gid, gids, runtime_flags, rlimits,\n1423                        permitted_capabilities, effective_capabilities,\n1424                        MOUNT_EXTERNAL_DEFAULT, nullptr, nullptr, true,\n1425                        false, nullptr, nullptr);\n1426   } else if (pid &gt; 0) {\n1427       \/\/ The zygote process checks whether the child process has died or not.\n1428       ALOGI(&quot;System server process %d has been created&quot;, pid);\n1429       gSystemServerPid = pid;\n1430       \/\/ There is a slight window that the system server process has crashed\n1431       \/\/ but it went unnoticed because we haven&#039;t published its pid yet. So\n1432       \/\/ we recheck here just to make sure that all is well.\n1433       int status;\n1434       if (waitpid(pid, &amp;status, WNOHANG) == pid) {\n1435           ALOGE(&quot;System server process %d has died. Restarting Zygote!&quot;, pid);\n1436           RuntimeAbort(env, __LINE__, &quot;System server process has died. Restarting Zygote!&quot;);\n1437       }\n1447   }\n1448   return pid;\n1449 }<\/code><\/pre>\n<p>\u4e0a\u9762\u5229\u7528waitpid\u76d1\u542cSystemServer\u5b50\u8fdb\u7a0b\uff0cSystemServer\u6302\u4e86\u5c31\u4f1a\u91cd\u542fzygote\u8fdb\u7a0b\u3002<\/p>\n<pre><code class=\"language-java\"> 313 static void RuntimeAbort(JNIEnv* env, int line, const char* msg) {\n 314   std::ostringstream oss;\n 315   oss &lt;&lt; __FILE__ &lt;&lt; &quot;:&quot; &lt;&lt; line &lt;&lt; &quot;: &quot; &lt;&lt; msg;\n 316   env-&gt;FatalError(oss.str().c_str());\n 317 }<\/code><\/pre>\n<button class=\"simplefavorite-button\" data-postid=\"13969\" data-siteid=\"1\" data-groupid=\"1\" data-favoritecount=\"0\" style=\"\">\u6536\u85cf <i class=\"sf-icon-star-empty\"><\/i><\/button>","protected":false},"excerpt":{"rendered":"<p>\u6e90\u7801\u4f4d\u7f6e\uff1aframeworks\/base\/core\/java\/com\/android\/interna [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1047],"tags":[],"_links":{"self":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/13969"}],"collection":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=13969"}],"version-history":[{"count":5,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/13969\/revisions"}],"predecessor-version":[{"id":13989,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/13969\/revisions\/13989"}],"wp:attachment":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=13969"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=13969"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=13969"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}