zygote最初的名字: app_process
zygote進(jìn)程是由init進(jìn)程fork出來(lái), 其配置在init.rc
zygote進(jìn)口在app_main.cpp->main()
app_main.cpp
int main(int argc, char* const argv[])
{
AppRuntime runtime(argv[0], computeArgBlockSize(argc, argv));
//判斷輸入?yún)?shù)是否有--start-system-server
if (strcmp(arg, "--start-system-server") == 0) {
startSystemServer = true;
}
//調(diào)用 runtime的start
if (zygote) {
runtime.start("com.android.internal.os.ZygoteInit", args, zygote);
}
}
runtime類型為AppRuntime,
AppRuntime定義在app_main.cpp內(nèi)怯疤, 繼承自AndroidRuntime, start函數(shù)即是執(zhí)行AndroidRuntime的start
main(const char* className,){
if (startVm(&mJavaVM, &env, zygote) != 0) {} //啟動(dòng)Davim虛擬機(jī)
if (startReg(env) < 0) {} //注冊(cè)Jni函數(shù)
//這邊根據(jù)傳進(jìn)來(lái)的className->com.android.internal.os.ZygoteInit來(lái)執(zhí)行ZygoteInit的main,正式進(jìn)入到j(luò)ava世界
env->CallStaticVoidMethod(startClass, startMeth, strArray);
}
ZygoteInit文件main()
public static void main(String argv[]) {
//1. 注冊(cè)socket
zygoteServer.registerServerSocket(socketName);
//2. 預(yù)加載浆洗,類和資源
preload();
//3. start systemserver
if (startSystemServer) {
startSystemServer(abiList, socketName, zygoteServer);
}
//4.進(jìn)入循環(huán)監(jiān)聽(tīng)
zygoteServer.runSelectLoop(abiList);
}
1. 注冊(cè)socket, registerServerSocket, 注冊(cè)IPC服務(wù)器端,通過(guò)socket方式
2. 預(yù)加載, preload();
static void preload() {
preloadClasses(); //預(yù)加載類
preloadResources(); //預(yù)加載資源
preloadOpenGL();
preloadSharedLibraries();
preloadTextResources();
}
接下來(lái)著重看下preloadClasses和preloadResources
preloadClasses: preloaded-classes文件通過(guò)工具生成集峦,加載時(shí)間超過(guò)1250的加入到這個(gè)list.
private static void preloadClasses() {
//Preloaded_Classes存儲(chǔ)與system中伏社,可以通過(guò)cat查看,在工程中的位置framework/base
is = new FileInputStream(PRELOADED_CLASSES);//PRELOADED_CLASSES = "/system/etc/preloaded-classes";
}
preloadResource->加載framework-res.apk的資源
- 啟動(dòng)system server
SystemServer是Android Service駐留的進(jìn)程塔淤, 是framework的核心摘昌, 如果systemServer死掉,zygote會(huì)自殺高蜂。
ZygoteInit.java
private static boolean startSystemServer(String abiList, String socketName, ZygoteServer zygoteServer){
//參數(shù)第焰,UID = 1000, Gid = 1000
String args[] = {
"--setuid=1000",
"--setgid=1000",
"--setgroups=",
"--capabilities=" + capabilities + "," + capabilities,
"--nice-name=system_server",
"--runtime-args",
"com.android.server.SystemServer",
};
//調(diào)用 Zygote的fork函數(shù)來(lái)啟動(dòng)SystemServer進(jìn)程,返回進(jìn)程ID
pid = Zygote.forkSystemServer(
parsedArgs.uid, parsedArgs.gid,
parsedArgs.gids,
parsedArgs.debugFlags,
null,
parsedArgs.permittedCapabilities,
parsedArgs.effectiveCapabilities);
/* For child process */
if (pid == 0) {//判斷如果pid = 0,則處于子進(jìn)程
handleSystemServerProcess(parsedArgs);
}
}
Zygote fork函數(shù)
public static int forkSystemServer(int uid, int gid, int[] gids, int debugFlags){
int pid = nativeForkSystemServer(
uid, gid, gids, debugFlags, rlimits, permittedCapabilities, effectiveCapabilities);
}
- 進(jìn)入循環(huán)監(jiān)聽(tīng)妨马, APP進(jìn)程也通過(guò)zygote進(jìn)行孵化出來(lái)
調(diào)用ZygoteServer.runSelectLoop調(diào)用進(jìn)入循環(huán)挺举,這個(gè)函數(shù)里面會(huì)啟動(dòng)一個(gè)while無(wú)限循環(huán)
ZygoteConnection代表客戶端
Zygote總結(jié)
(1)從app_main->main()入口進(jìn)入,調(diào)用Appruntime的start()
(2)startVm創(chuàng)建虛擬機(jī)烘跺, startReg注冊(cè)JNI
(3)調(diào)用ZygoteInit->main
(4)在 zygoteInit中注冊(cè)socket, 預(yù)加載湘纵, 啟動(dòng)systemServer;
(5)最后進(jìn)入循環(huán)loop,等待客戶端通信~