那么再往下看 ZygoteInit 类的 main 函数,其中的一段 MethodAndArgsCaller 代码为
if (argv[1].equals("true")) { startSystemServer(); } else if (!argv[1].equals("false")) { throw new RuntimeException(argv[0] + USAGE_STRING); }调用了 startSystemServer () ,这个函数启动了一个子进程来作为 SystemServer 的载体,
1. 它首先指定 SystemServer 进程的参数 ;
2. 根据指定的参数来创建 SystemServer 进程;
3. 调用 handleSystemServerProcess 启动第一步指定进程参数过程中指定的类,此时为“ com.Android.server.SystemServer ” ,启动的这个进程在 ps 查看后显示为” system_server ” 。
private static boolean startSystemServer() throws MethodAndArgsCaller, RuntimeException { /* Hardcoded command line to start the system server */ String args[]; String ashmem_size = System.getProperty("gralloc.ashmem_size"); if ((null != ashmem_size) && (0 != ashmem_size.length())) { args = new String[] { "--setuid=1000", "--setgid=1000", "--setgroups=1001,1002,1003,1004,1005,1006,1007,1008,1009,1010,3001,3002,3003", "--capabilities=130104352,130104352", "--rlimit=8,", "--runtime-init", "--nice-name=system_server", "com.android.server.SystemServer", }; args[4] = args[4].concat(ashmem_size); args[4] = args[4].concat(","); args[4] = args[4].concat(ashmem_size); } else { args = new String[] { "--setuid=1000", "--setgid=1000", "--setgroups=1001,1002,1003,1004,1005,1006,1007,1008,1009,1010,3001,3002,3003", "--capabilities=130104352,130104352", "--runtime-init", "--nice-name=system_server", "com.android.server.SystemServer", }; } ZygoteConnection.Arguments parsedArgs = null; int pid; try { parsedArgs = new ZygoteConnection.Arguments(args); /* * Enable debugging of the system process if *either* the command line flags * indicate it should be debuggable or the ro.debuggable system property * is set to "1" */ int debugFlags = parsedArgs.debugFlags; if ("1".equals(SystemProperties.get("ro.debuggable"))) debugFlags |= Zygote.DEBUG_ENABLE_DEBUGGER; int[][] rlimits = new int[0][0]; if (parsedArgs.rlimits != null) { rlimits = parsedArgs.rlimits.toArray(rlimits); } /* Request to fork the system server process */ pid = Zygote.forkSystemServer( parsedArgs.uid, parsedArgs.gid, parsedArgs.gids, debugFlags, rlimits, parsedArgs.permittedCapabilities, parsedArgs.effectiveCapabilities); } catch (IllegalArgumentException ex) { throw new RuntimeException(ex); } /* For child process */ if (pid == 0) { handleSystemServerProcess(parsedArgs); } return true; }