system_service 进程由zygote进程fork来


基于:http://blog.csdn.net/jianguo_liao19840726/article/details/16112993


ZygoteInit.java的main中提到的startSystemServer(); 

 public static void main(String argv[]) {
            registerZygoteSocket();  
            preloadClasses();      
            preloadResources();    
            if (argv[1].equals("true")) {
                startSystemServer();
            }
            if (ZYGOTE_FORK_MODE) {
                runForkMode();
            } else {
                runSelectLoopMode();
            }
            closeServerSocket();
    }

进入 startSystemServer(); 方法

    /**
     * Prepare the arguments and fork for the system server process.
     */
    private static boolean startSystemServer()
            throws MethodAndArgsCaller, RuntimeException {
        /* Hardcoded command line to start the system server */
        String args[] = {
            "--setuid=1000",
            "--setgid=1000",
            "--setgroups=1001,1002,1003,1004,1005,1006,1007,1008,1009,1010,1018,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;

            /* Request to fork the system server process */
            pid = Zygote.forkSystemServer(
                    parsedArgs.uid, parsedArgs.gid,
                    parsedArgs.gids, debugFlags, null,
                    parsedArgs.permittedCapabilities,
                    parsedArgs.effectiveCapabilities);
        } catch (IllegalArgumentException ex) {
            throw new RuntimeException(ex);
        }

        /* For child process */
        if (pid == 0) {
            handleSystemServerProcess(parsedArgs);
        }

        return true;
    }

从上面的注释拉看是fork一个进程出来进程名nice-name=system_server,主要是执行了如下代码

/* Request to fork the system server process */
            pid = Zygote.forkSystemServer(

当之进程fork成功后,将执行如下:

/* For child process */
        if (pid == 0) {
            handleSystemServerProcess(parsedArgs);
        }

从上面来看,我们总结下:

1、pid = Zygote.forkSystemServer(,创建了system_service子进程

2、子进程fork成功后,执行handleSystemServerProcess(parsedArgs);




你可能感兴趣的:(system_service 进程由zygote进程fork来)