Android启动过程中的第一个进程init,在启动过程中会启动两个关键的系统服务进程ServiceManager和Zygote。本文要介绍的就是Zygote进程的启动,Zygote俗称孵化器,专门用于生产(启动)新的进程。Zygote是在Init.rc(aosp/system/core/rootdir)里描述并由init进程启动的。相关代码如下:
service zygote /system/bin/app_process -Xzygote /system/bin --zygote --start-system-server class main socket zygote stream 660 root system onrestart write /sys/android_power/request_state wake onrestart write /sys/power/state on onrestart restart media onrestart restart netd
init.rc文件的语法描述可以参考aosp/system/core/init/readme.txt。
zygote的path路径为/system/bin/app_process,-Xzygote /system/bin --zygote --start-system-server是它的参数,所属class是main,同属同一个class的进程会同时启动或者停止,会启动一个socket名为zygote,重启时将做四个操作。
下面是Zygote进程启动的大致时序图:
Android启动过程中,init进程使用aosp/system/core/init/Init_parser.c解析init.rc脚本文件。
step1,循环启动init.rc文件描述的所有状态不为SVC_DISABLED的service,fork()新进程。
step2,启动Zygote,执行app_main.cpp的main()方法,
// Parse runtime arguments. Stop at first unrecognized option. bool zygote = false; bool startSystemServer = false; bool application = false; const char* parentDir = NULL; const char* niceName = NULL; const char* className = NULL; while (i < argc) {//main的参数匹配 const char* arg = argv[i++]; if (!parentDir) { parentDir = arg; } else if (strcmp(arg, "--zygote") == 0) { zygote = true; niceName = "zygote"; } else if (strcmp(arg, "--start-system-server") == 0) { startSystemServer = true; } else if (strcmp(arg, "--application") == 0) { application = true; } else if (strncmp(arg, "--nice-name=", 12) == 0) { niceName = arg + 12; } else { className = arg; break; } } if (niceName && *niceName) { setArgv0(argv0, niceName); set_process_name(niceName); } runtime.mParentDir = parentDir; if (zygote) { runtime.start("com.android.internal.os.ZygoteInit", startSystemServer ? "start-system-server" : ""); } else if (className) { ...... } else { ...... }
step3,启动AppRuntime,AppRuntime继承自AndroidRuntime,这里需要做的事情有:
/* start the virtual machine */ JniInvocation jni_invocation; jni_invocation.Init(NULL); JNIEnv* env; if (startVm(&mJavaVM, &env) != 0) {//step4,创建JVM return; } onVmCreated(env); /* * Register android functions. */ if (startReg(env) < 0) {//step6,在JVM中注册android本地方法 ALOGE("Unable to register all android natives\n"); return; }
step7~step8,启动ZygoteInit的main()方法,传入一个字符串数组,strArray[0]="com.android.internal.os.ZygoteInit",strArray[1] = "start-system-server"。
step9,registerZygoteSocket()注册服务端socket,这个socket就是前面提到的zygote,在init进程解析init.rc时创建。
public static void main(String argv[]) { try { // Start profiling the zygote initialization. SamplingProfilerIntegration.start(); registerZygoteSocket(); ....... preload(); ....... // Do an initial gc to clean up after startup gc(); ...... if (argv[1].equals("start-system-server")) { startSystemServer();//step11 } else if (!argv[1].equals("")) { throw new RuntimeException(argv[0] + USAGE_STRING); } runSelectLoop(); closeServerSocket(); } catch (MethodAndArgsCaller caller) { caller.run(); } catch (RuntimeException ex) { Log.e(TAG, "Zygote died with exception", ex); closeServerSocket(); throw ex; }
step11,开始启动SystemServer进程,系统启动的关键进程。
private static boolean startSystemServer() throws MethodAndArgsCaller, RuntimeException { ...... /* Hardcoded command line to start the system server */ String args[] = { "--setuid=1000", "--setgid=1000", "--setgroups=1001,1002,1003,1004,1005,1006,1007,1008,1009,1010,1018,1032,3001,3002,3003,3006,3007", "--capabilities=" + capabilities + "," + capabilities, "--runtime-init", "--nice-name=system_server", "com.android.server.SystemServer", }; ZygoteConnection.Arguments parsedArgs = null; int pid; try { ....... /* Request to fork the system server process */ pid = Zygote.forkSystemServer( parsedArgs.uid, parsedArgs.gid, parsedArgs.gids, parsedArgs.debugFlags, null, parsedArgs.permittedCapabilities, parsedArgs.effectiveCapabilities); } catch (IllegalArgumentException ex) { throw new RuntimeException(ex); } /* For child process */ if (pid == 0) { handleSystemServerProcess(parsedArgs);//step12 } return true; }
Zygote.forkSystemServer()中会fork出一个新进程,是即将启动的SystemServer进程,是Zygote的子进程。如果成功fork出新的子进程,这里将会返回两次,一次返回Zygote的pid,值大于0,这里返回执行step29,进入loop,等待ActivityManagerService调用startProcessLocked()启动新的进程,这个过程参考Android4.4
framework分析——startService的创建过程的step12~step35;另一次返回SystemServer的进程id,等于0,将执行step12~step28。
参考:
http://blog.csdn.net/luoshengyang/article/details/6768304
www.cnblogs.com/bastard/archive/2012/08/28/2660389.html
右键复制图片地址,在浏览器中打开即可查看大图。
未完待续,有不对的地方,请指正。