SystemServer进程Android用户空间核心的的进程, framework层的很多services都是在SystemServer中进行创建并启动的. SystemServer也是由Zygote进行孵化的. 在将解启动zygote进程一文中知道启动zygote就会进入framework/cmds/app_process/app_main.cpp的main函数中
在init.rc中zygote有四个参数:-Xzygote /system/bin --zygote --start-system-server,各自的意义如下:
-Xzygote:该参数将作为虚拟机启动时所需的参数;
/system/bin:代表虚拟机程序所在目录;
--zygote:指明以ZygoteInit.java类中的main函数作为虚拟机执行入口;
--start-system-server:告诉Zygote进程启动SystemServer进程;
在app_main.cpp的main函数中会使用这些参数。前两个参数主要对虚拟机的设置,后面的参数主要处理systemServer
#if defined(__LP64__) //判断系统为64为还是32为, 进行赋予不同的进程名
static const char ABI_LIST_PROPERTY[] = "ro.product.cpu.abilist64";
static const char ZYGOTE_NICE_NAME[] = "zygote64";
#else
static const char ABI_LIST_PROPERTY[] = "ro.product.cpu.abilist32";
static const char ZYGOTE_NICE_NAME[] = "zygote";
#endif
// Parse runtime arguments. Stop at first unrecognized option. //解析运行时的参数,从解析init.rc中获得
bool zygote = false;
bool startSystemServer = false;
bool application = false;
String8 niceName;
String8 className;
++i; // Skip unused "parent dir" argument.
while (i < argc) {
const char* arg = argv[i++];
if (strcmp(arg, "--zygote") == 0) {
zygote = true; //在zygote模式中启动
niceName = ZYGOTE_NICE_NAME; //设置进程的名字
} else if (strcmp(arg, "--start-system-server") == 0) {
startSystemServer = true; //启动System Server
} else if (strcmp(arg, "--application") == 0) {
application = true; //在application模式启动
} else if (strncmp(arg, "--nice-name=", 12) == 0) {
niceName.setTo(arg + 12); //参数中有进程名,就设置
} else if (strncmp(arg, "--", 2) != 0) {
className.setTo(arg); //设置className
break;
} else {
--i;
break;
}
}
根据上面获取的参数, 知道我们正在zygote mode
// We're in zygote mode.
maybeCreateDalvikCache(); //创建data/dalvik-cache,设置环境
if (startSystemServer) { //startSystemServer为true
args.add(String8("start-system-server"));
}
char prop[PROP_VALUE_MAX];
if (property_get(ABI_LIST_PROPERTY, prop, NULL) == 0) {
LOG_ALWAYS_FATAL("app_process: Unable to determine ABI list from property %s.",
ABI_LIST_PROPERTY);
return 11;
}
String8 abiFlag("--abi-list=");
abiFlag.append(prop);
args.add(abiFlag);