開門見山
所謂Zygote崖瞭,就如同它的名字“受精卵”一樣狂巢,它是Android 系統(tǒng)所有進(jìn)程的源頭,所有的進(jìn)程都是由zygote進(jìn)程fork出來的书聚。而zygote進(jìn)程是由init進(jìn)程fork出來的唧领。關(guān)于init進(jìn)程這里不做進(jìn)一步擴(kuò)展,我們只需要知道Android系統(tǒng)啟動的時候雌续,會啟動init進(jìn)程斩个,然后init進(jìn)程會創(chuàng)建zygote進(jìn)程。
Zygote 的初始化
其實(shí)zygote的初始化流程并不在zygote.java類中驯杜,而在ZygoteInit.java中受啥。我們先可以看ZygoteInit的main方法:
public static void main(String argv[]) {
ZygoteServer zygoteServer = new ZygoteServer();
// Mark zygote start. This ensures that thread creation will throw
// an error.
ZygoteHooks.startZygoteNoThreadCreation();
// Zygote goes into its own process group.
try {
Os.setpgid(0, 0);
} catch (ErrnoException ex) {
throw new RuntimeException("Failed to setpgid(0,0)", ex);
}
final Runnable caller;
try {
// Report Zygote start time to tron unless it is a runtime restart
if (!"1".equals(SystemProperties.get("sys.boot_completed"))) {
MetricsLogger.histogram(null, "boot_zygote_init",
(int) SystemClock.elapsedRealtime());
}
String bootTimeTag = Process.is64Bit() ? "Zygote64Timing" : "Zygote32Timing";
TimingsTraceLog bootTimingsTraceLog = new TimingsTraceLog(bootTimeTag,
Trace.TRACE_TAG_DALVIK);
bootTimingsTraceLog.traceBegin("ZygoteInit");
RuntimeInit.enableDdms();
boolean startSystemServer = false;
String socketName = "zygote";
String abiList = null;
boolean enableLazyPreload = false;
for (int i = 1; i < argv.length; i++) {
if ("start-system-server".equals(argv[i])) {
startSystemServer = true;
} else if ("--enable-lazy-preload".equals(argv[i])) {
enableLazyPreload = true;
} else if (argv[i].startsWith(ABI_LIST_ARG)) {
abiList = argv[i].substring(ABI_LIST_ARG.length());
} else if (argv[i].startsWith(SOCKET_NAME_ARG)) {
socketName = argv[i].substring(SOCKET_NAME_ARG.length());
} else {
throw new RuntimeException("Unknown command line argument: " + argv[i]);
}
}
if (abiList == null) {
throw new RuntimeException("No ABI list supplied.");
}
zygoteServer.registerServerSocketFromEnv(socketName);
// In some configurations, we avoid preloading resources and classes eagerly.
// In such cases, we will preload things prior to our first fork.
if (!enableLazyPreload) {
bootTimingsTraceLog.traceBegin("ZygotePreload");
EventLog.writeEvent(LOG_BOOT_PROGRESS_PRELOAD_START,
SystemClock.uptimeMillis());
preload(bootTimingsTraceLog);
EventLog.writeEvent(LOG_BOOT_PROGRESS_PRELOAD_END,
SystemClock.uptimeMillis());
bootTimingsTraceLog.traceEnd(); // ZygotePreload
} else {
Zygote.resetNicePriority();
}
// Do an initial gc to clean up after startup
bootTimingsTraceLog.traceBegin("PostZygoteInitGC");
gcAndFinalize();
bootTimingsTraceLog.traceEnd(); // PostZygoteInitGC
bootTimingsTraceLog.traceEnd(); // ZygoteInit
// Disable tracing so that forked processes do not inherit stale tracing tags from
// Zygote.
Trace.setTracingEnabled(false, 0);
Zygote.nativeSecurityInit();
// Zygote process unmounts root storage spaces.
Zygote.nativeUnmountStorageOnInit();
ZygoteHooks.stopZygoteNoThreadCreation();
if (startSystemServer) {
Runnable r = forkSystemServer(abiList, socketName, zygoteServer);
// {@code r == null} in the parent (zygote) process, and {@code r != null} in the
// child (system_server) process.
if (r != null) {
r.run();
return;
}
}
Log.i(TAG, "Accepting command socket connections");
// The select loop returns early in the child process after a fork and
// loops forever in the zygote.
caller = zygoteServer.runSelectLoop(abiList);
} catch (Throwable ex) {
Log.e(TAG, "System zygote died with exception", ex);
throw ex;
} finally {
zygoteServer.closeServerSocket();
}
// We're in the child process and have exited the select loop. Proceed to execute the
// command.
if (caller != null) {
caller.run();
}
}
雖然main方法非常的長,但是我們只關(guān)注幾個比較重要的流程
1 注冊socket
zygoteServer.registerServerSocketFromEnv(socketName);
通過這個我們可以看到鸽心,其他進(jìn)程和zygote的通訊其實(shí)就是通過socket進(jìn)行通訊的滚局,比如我們在上一篇Activity 啟動流程分析中可以看到,ActivityManagerService通知zygote fork目標(biāo)進(jìn)程就是采用的socket顽频。
2 創(chuàng)建SystemServer
if (startSystemServer) {
Runnable r = forkSystemServer(abiList, socketName, zygoteServer);
// {@code r == null} in the parent (zygote) process, and {@code r != null} in the
// child (system_server) process.
if (r != null) {
r.run();
return;
}
}
在這里我們可以看到藤肢,zygote其實(shí)懶的很。zygote把systemServer fork 出來后就不管了冲九,就相當(dāng)于zygote是一個大老板谤草,而systemServer是一位秘書。如果某些進(jìn)程需要和Zygote進(jìn)行通訊的時候莺奸,其實(shí)是通過SystemServer做中轉(zhuǎn)丑孩,由SystemServer和Zygote進(jìn)行通訊。
在這里灭贷,如果由同學(xué)對SystemServer的啟動流程感興趣的話温学,可以看我之前的另一篇文章 Android SystemServer 啟動流程
3開啟循環(huán)監(jiān)聽socket
caller = zygoteServer.runSelectLoop(abiList);
runSelectLoop()方法里面由一個while(true),也即是開啟一個循環(huán)不停的去監(jiān)聽當(dāng)前有沒有ZygoteConnect甚疟。
總結(jié)
Zygote分析起來其實(shí)比較簡單仗岖,結(jié)合前面的兩篇文章我們就可以把完整的流程給分析出來