源码位置:frameworks/base/core/java/com/android/internal/os/Zygote.java
forkSystemServer方法
https://www.jianshu.com/p/bb96360b4084
335 public static int forkSystemServer(int uid, int gid, int[] gids, int runtimeFlags,
336 int[][] rlimits, long permittedCapabilities, long effectiveCapabilities) {
337 ZygoteHooks.preFork();
338 // Resets nice priority for zygote process.
339 resetNicePriority();
340 int pid = nativeForkSystemServer(
341 uid, gid, gids, runtimeFlags, rlimits,
342 permittedCapabilities, effectiveCapabilities);
343 // Enable tracing as soon as we enter the system_server.
344 if (pid == 0) {
345 Trace.setTracingEnabled(true, runtimeFlags);
346 }
347 ZygoteHooks.postForkCommon();
348 return pid;
349 }
→nativeForkSystemServer
nativeForkSystemServer方法实现位于frameworks/base/core/jni/com_android_internal_os_Zygote.cpp +1404
,最终是调用了c语言的fork函数。
1404 static jint com_android_internal_os_Zygote_nativeForkSystemServer(
1405 JNIEnv* env, jclass, uid_t uid, gid_t gid, jintArray gids,
1406 jint runtime_flags, jobjectArray rlimits, jlong permitted_capabilities,
1407 jlong effective_capabilities) {
...
1417
1418 pid_t pid = ForkCommon(env, true,
1419 fds_to_close,
1420 fds_to_ignore);
1421 if (pid == 0) {
1422 SpecializeCommon(env, uid, gid, gids, runtime_flags, rlimits,
1423 permitted_capabilities, effective_capabilities,
1424 MOUNT_EXTERNAL_DEFAULT, nullptr, nullptr, true,
1425 false, nullptr, nullptr);
1426 } else if (pid > 0) {
1427 // The zygote process checks whether the child process has died or not.
1428 ALOGI("System server process %d has been created", pid);
1429 gSystemServerPid = pid;
1430 // There is a slight window that the system server process has crashed
1431 // but it went unnoticed because we haven't published its pid yet. So
1432 // we recheck here just to make sure that all is well.
1433 int status;
1434 if (waitpid(pid, &status, WNOHANG) == pid) {
1435 ALOGE("System server process %d has died. Restarting Zygote!", pid);
1436 RuntimeAbort(env, __LINE__, "System server process has died. Restarting Zygote!");
1437 }
1447 }
1448 return pid;
1449 }
上面利用waitpid监听SystemServer子进程,SystemServer挂了就会重启zygote进程。
313 static void RuntimeAbort(JNIEnv* env, int line, const char* msg) {
314 std::ostringstream oss;
315 oss << __FILE__ << ":" << line << ": " << msg;
316 env->FatalError(oss.str().c_str());
317 }
0 条评论