Android7.0启动SystemServer进程

本文详细解析了Android系统中SystemServer进程的创建过程,从ZygoteInit的forkSystenServer开始,经过JNI调用到Zygote.cpp的fork函数,再到ForkAndSpecializeCommon中的系统服务初始化,最终由RuntimeInit负责启动SystemServer,完成Android系统的启动流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在分析Android系统进入zygote进程一文中知道SystemServer是系统中非常核心的进程

 

SystemServer在ZygoteInit中进行创建,并且启动起来的.代码位置frameworks/base/core/java/com/android/internal/os/ZygoteInit.java

            if (startSystemServer) {    //根据init中传来的参数可知startSystemServer为true
                startSystemServer(abiList, socketName);    //启动SystemServer
            }

    /**
     * Prepare the arguments and fork for the system server process.
     */
    private static boolean startSystemServer(String abiList, String socketName)
            throws MethodAndArgsCaller, RuntimeException {
    //.......
        /* Hardcoded command line to start the system server */
        String args[] = {         //准备启动System Server所需要的参数
            "--setuid=1000",
            "--setgid=1000",
            "--setgroups=1001,1002,1003,1004,1005,1006,1007,1008,1009,1010,1018,1021,1032,3001,3002,3003,3006,3007,3009,3010",
            "--capabilities=" + capabilities + "," + capabilities,
            "--nice-name=system_server",   //进程的名字为system_server
            "--runtime-args",
            "com.android.server.SystemServer",    //包名
        };
        ZygoteConnection.Arguments parsedArgs = null;

        int pid;

        try {
            parsedArgs = new ZygoteConnection.Arguments(args);    //通过ZygoteConnection对参数进行封装
            ZygoteConnection.applyDebuggerSystemProperty(parsedArgs);
            ZygoteConnection.applyInvokeWithSystemProperty(parsedArgs);

            /* Request to fork the system server process */ 
            pid = Zygote.forkSystemServer(    //请求孵化SystemServer进程, 将创建的进程号赋值给pid
                    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) {            //pid=0为zygote的子进程
            if (hasSecondZygote(abiList)) {
                waitForSecondaryZygote(socketName);  //等待zygote第二阶段
            }

            handleSystemServerProcess(parsedArgs);    //运行SystemServer,之后SystemServer就与Zygote分道扬镳,在自己的进程中运行
        }

        return true;
    }
下面将分两部分讲解进入SystemServer流程:

1.创建SystemServer进程

2.运行SystemServer进程

创建SystemServer进程

Zygote调用forkSystenServer函数来进行创建SystemServer进程,具体代码位置frameworks/base/core/java/com/android/internal/os/Zygote.java

    /* @return 0 if this is the child, pid of the child
     * if this is the parent, or -1 on error.
     */
    public static int forkSystemServer(int uid, int gid, int[] gids, int debugFlags,
            int[][] rlimits, long permittedCapabilities, long effectiveCapabilities) {
        VM_HOOKS.preFork();      //将所有守护进程停止运行
        int pid = nativeForkSystemServer(     //调用native函数孵化进程
                uid, gid, gids, debugFlags, rlimits, permittedCapabilities, effectiveCapabilities);
        // Enable tracing as soon as we enter the system_server.
        if (pid == 0) {
            Trace.setTracingEnabled(true);   //进入SystemServer进程,可以输出trace
        }
        VM_HOOKS.postForkCommon();   //重新运行各个守护进程
        return pid;
    }

    native private static int nativeForkSystemServer(int uid, int gid, int[] gids, int debugFlags,
            int[][] rlimits, long permittedCapabilities, long effectiveCapabilities);

通过JNI调用到native函数中,

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值