设为首页 加入收藏

TOP

Java 世界的盘古和女娲 —— Zygote(一)
2019-09-30 16:44:20 】 浏览:148
Tags:Java 世界 盘古 女娲 Zygote

本文基于 Android 9.0 , 代码仓库地址 : android_9.0.0_r45

文中源码链接:

Zygote.java

ZygoteInit.java

ZygoteServer.java

ZygoteConnection.java

RuntimeInit.java

仔细看看下面这张 Android 体系图,找一下 Zygote 在什么地方。

上图来自 Gityuan 博客

纵观整个 Android 体系结构,底层内核空间以 Linux Kernel 为核心,上层用户空间以 C++/Java 组成的 Framework 层组成,通过系统调用来连接用户空间和内核空间。而用户空间又分为 Native 世界和 Java 世界,通过 JNI 技术进行连接。Native 世界的 init 进程是所有用户进程的祖先,其 pid 为 1 。init 进程通过解析 init.rc 文件创建出 Zygote 进程,Zygote 进程人如其名,翻译成中文就是 受精卵 的意思。它是 Java 世界的中的第一个进程,也是 Android 系统中的第一个 Java 进程,颇有盘古开天辟地之势。

Zygote 创建的第一个进程就是 System ServerSystem Server 负责管理和启动整个 Java Framework 层。创建完 System Server 之后,Zygote 就会完全进入受精卵的角色,等待进行无性繁殖,创建应用进程。所有的应用进程都是由 Zygote 进程 fork 而来的,称之为 Java 世界的女娲也不足为过。

Zygote 的启动过程是从 Native 层开始的,这里不会 Native 层作过多分析,直接进入其在 Java 世界的入口 ZygoteInit.main() :

public static void main(String argv[]) {
    ZygoteServer zygoteServer = new ZygoteServer();

    // Mark zygote start. This ensures that thread creation will throw
    // an error.
    ZygoteHooks.startZygoteNoThreadCreation();

    // Zygote goes into its own process group.
    // 设置进程组 ID
    // pid 为 0 表示设置当前进程的进程组 ID
    // gid 为 0 表示使用当前进程的 PID 作为进程组 ID
    try {
        Os.setpgid(0, 0);
    } catch (ErrnoException ex) {
        throw new RuntimeException("Failed to setpgid(0,0)", ex);
    }

    final Runnable caller;
    try {
        ......
        RuntimeInit.enableDdms(); // 启用 DDMS

        boolean startSystemServer = false;
        String socketName = "zygote";
        String abiList = null;
        boolean enableLazyPreload = false;
        for (int i = 1; i < argv.length; i++) { // 参数解析
            if ("start-system-server".equals(argv[i])) {
                startSystemServer = true;
            } else if ("--enable-lazy-preload".equals(argv[i])) {
                enableLazyPreload = true;
            } else if (argv[i].startsWith(ABI_LIST_ARG)) {
                abiList = argv[i].substring(ABI_LIST_ARG.length());
            } else if (argv[i].startsWith(SOCKET_NAME_ARG)) {
                socketName = argv[i].substring(SOCKET_NAME_ARG.length());
            } else {
                throw new RuntimeException("Unknown command line argument: " + argv[i]);
            }
        }

        if (abiList == null) {
            throw new RuntimeException("No ABI list supplied.");
        }

        // 1. 注册服务端 socket,这里的 IPC 不是 Binder 通信
        zygoteServer.registerServerSocketFromEnv(socketName); 
        // In some configurations, we avoid preloading resources and classes eagerly.
        // In such cases, we will preload things prior to our first fork.
        if (!enableLazyPreload) {
            bootTimingsTraceLog.traceBegin("ZygotePreload");
            EventLog.writeEvent(LOG_BOOT_PROGRESS_PRELOAD_START,
                SystemClock.uptimeMillis());
            preload(bootTimingsTraceLog); // 2. 预加载操作
            EventLog.writeEvent(LOG_BOOT_PROGRESS_PRELOAD_END,
                SystemClock.uptimeMillis());
            bootTimingsTraceLog.traceEnd(); // ZygotePreload
        } else {
            Zygote.resetNicePriority(); // 设置线程优先级为 NORM_PRIORITY (5)
        }

        // Do an initial gc to clean up after startup
        gcAndFinalize(); // 3. 强制进行一次垃圾收集

        Zygote.nativeSecurityInit();

        // Zygote process unmounts root storage spaces.
        Zygote.nativeUnmountStorageOnInit();

        ZygoteHooks.stopZygoteNoThreadCreation();

        if (startSystemServer) {
            // 4. 启动SystemServer 进程
            Runnable r = forkSystemServer(abiList, socketName, zygoteServer); 

            // {@code r == null} in the parent (zygote) process, and {@code r != null} in the
            // child (system_server) process.
            if (r != null) {
                r.run(); // 由 RuntimeInit.java 中的 MethodAndArgsCaller 反射调用SystemServer 的 main() 方法
                return;
            }
        }

        Log.i(TAG, "Accepting
首页 上一页 1 2 3 4 5 6 7 下一页 尾页 1/7/7
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇欢迎加入强哥的 Android 开发交流.. 下一篇Android.mk语法说明

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目