Android4.4 framework analysis -- Start Process of Zygote Process

Source: Internet
Author: User

Android4.4 framework analysis -- Start Process of Zygote Process

The first process init In the Android startup process starts two key system service processes: ServiceManager and Zygote. This article describes how to start a Zygote process. Zygote is also known as an incubator and is used to produce (start) new processes. Zygote is described in Init. rc (aosp/system/core/rootdir) and started by the init process. The related code is as follows:

service zygote /system/bin/app_process -Xzygote /system/bin --zygote --start-system-server    class main    socket zygote stream 660 root system    onrestart write /sys/android_power/request_state wake    onrestart write /sys/power/state on    onrestart restart media    onrestart restart netd
For the syntax description of the init. rc file, see aosp/system/core/init/readme.txt.

The path of zygote is/system/bin/app_process.-Xzygote/system/bin -- zygote -- start-system-server is its parameter and Its class is main, processes of the same class will start or stop at the same time. A socket named zygote will be started, and four operations will be performed upon restart.

The following figure shows the general sequence of Zygote process startup:


During Android startup, The init process uses aosp/system/core/init/Init_parser.c to parse the init. rc Script file.

Step 1: cyclically start all the services in the state not SVC_DISABLED described in the init. rc file and the new fork () process.

Step 2: Start Zygote and execute the main () method of app_main.cpp,

// Parse runtime arguments. stop at first unrecognized option. bool zygote = false; bool startSystemServer = false; bool application = false; const char * parentDir = NULL; const char * niceName = NULL; const char * className = NULL; while (I <argc) {// The main parameter matches const char * arg = argv [I ++]; if (! ParentDir) {parentDir = arg;} else if (strcmp (arg, "-- zygote") = 0) {zygote = true; niceName = "zygote ";} else if (strcmp (arg, "-- start-system-server") = 0) {startSystemServer = true;} else if (strcmp (arg, "-- application ") = 0) {application = true;} else if (strncmp (arg, "-- nice-name =", 12) = 0) {niceName = arg + 12 ;} else {className = arg; break;} if (niceName & * niceName) {set Argv0 (argv0, niceName); set_process_name (niceName);} runtime. mParentDir = parentDir; if (zygote) {runtime. start ("com. android. internal. OS. zygoteInit ", startSystemServer? "Start-system-server": "");} else if (className) {...} else {......}
Step 3: Start AppRuntime. AppRuntime inherits from AndroidRuntime. The following things need to be done:

/* Start the virtual machine */JniInvocation jni_invocation; jni_invocation.Init (NULL); JNIEnv * env; if (startVm (& mJavaVM, & env )! = 0) {// step4, create JVM return;} onVmCreated (env);/** Register android functions. */if (starregulatory (env) <0) {// step6, register the android local method ALOGE ("Unable to register all android natives \ n") in JVM; return ;}

Step 7 ~ Step 8: Start the main () method of ZygoteInit and input a string array, strArray [0] = "com. android. internal. OS. zygoteInit ", strArray [1] =" start-system-server ".

Step 9: registerZygoteSocket () registers the server socket. This socket is the zygote mentioned earlier. It is created when the init process parses init. rc.


   public static void main(String argv[]) {        try {            // Start profiling the zygote initialization.            SamplingProfilerIntegration.start();            registerZygoteSocket();            .......            preload();            .......            // Do an initial gc to clean up after startup            gc();            ......            if (argv[1].equals("start-system-server")) {                startSystemServer();//step11            } else if (!argv[1].equals("")) {                throw new RuntimeException(argv[0] + USAGE_STRING);            }            runSelectLoop();            closeServerSocket();        } catch (MethodAndArgsCaller caller) {            caller.run();        } catch (RuntimeException ex) {            Log.e(TAG, "Zygote died with exception", ex);            closeServerSocket();            throw ex;        }

Step 11: Start the SystemServer process and the key processes started by the system.

  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,1032,3001,3002,3003,3006,3007",            "--capabilities=" + capabilities + "," + capabilities,            "--runtime-init",            "--nice-name=system_server",            "com.android.server.SystemServer",        };        ZygoteConnection.Arguments parsedArgs = null;        int pid;        try {            .......            /* Request to fork the system server process */            pid = Zygote.forkSystemServer(                    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) {            handleSystemServerProcess(parsedArgs);//step12        }        return true;    }

In Zygote. forkSystemServer (), fork generates a new process, which is the system server process to be started and a sub-process of Zygote. If a new sub-process is successfully fork, it will be returned twice here, And the pid of Zygote will be returned at one time. The value is greater than 0. Here, step29 is returned to go To the loop and wait for ActivityManagerService to call startProcessLocked () start a new process. For details about this process, refer to Android4.4 framework analysis-startService creation process. Step 12 ~ Step 35; the other time returns the SystemServer process id, which is equal to 0 ~ Step 28.


Refer:

Http://blog.csdn.net/luoshengyang/article/details/6768304

Www.cnblogs.com/bastard/archive/2012/08/28/2660389.html


Right-click the image address and open it in the browser to view the large image.

Not yet resumed. please correct me if there is anything wrong.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.