Android7.0系统进入Zygote进程

本文详细介绍了Android系统中Zygote进程如何孵化SystemServer。从Zygote的启动参数到虚拟机设置,再到ZygoteInit类的main函数执行,揭示了Zygote如何进入Java世界并启动SystemServer,以及Zygote进程在Android系统中的作用,特别是类和资源的预加载如何提高应用启动速度。
摘要由CSDN通过智能技术生成

    SystemServer进程Android用户空间核心的的进程, framework层的很多services都是在SystemServer中进行创建并启动的. SystemServer也是由Zygote进行孵化的. 在将解启动zygote进程一文中知道启动zygote就会进入framework/cmds/app_process/app_main.cpp的main函数中

init.rczygote有四个参数:-Xzygote /system/bin --zygote --start-system-server,各自的意义如下:

-Xzygote:该参数将作为虚拟机启动时所需的参数;

/system/bin:代表虚拟机程序所在目录;

--zygote:指明以ZygoteInit.java类中的main函数作为虚拟机执行入口;

--start-system-server:告诉Zygote进程启动SystemServer进程;

app_main.cppmain函数中会使用这些参数。前两个参数主要对虚拟机的设置,后面的参数主要处理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);

  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值