Android4.4 Framework Analysis--zygote process Start-up process

Source: Internet
Author: User

During the first process init during the Android boot process, two critical system service processes ServiceManager and zygote are launched during the boot process. This article is to introduce the start of the zygote process, zygote commonly known as incubators, dedicated to the production (start) of the new process. Zygote is described in Init.rc (Aosp/system/core/rootdir) and started by the Init process. The relevant 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
The syntax description of the init.rc file can be referenced in Aosp/system/core/init/readme.txt.

Zygote's path path is/system/bin/app_process,-xzygote/system/bin--zygote--start-system-server is its argument, and the class is main, The same class process will start or stop at the same time, will start a socket named zygote, restart will do four operations.

The following is a rough sequence diagram of the zygote process startup:


During Android startup, the Init process uses aosp/system/core/init/init_parser.c to parse the init.rc script file.

Step1, the loop-start init.rc file describes all the states that are not svc_disabled service,fork () new processes.

Step2, start Zygote, execute App_main.cpp's main () method,

    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) {//main parameters match the 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=", "n") = = 0) {nicename = arg + 12;            } else {className = arg;        Break        }} if (Nicename && *nicename) {setArgv0 (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) {...} or else {...}
Step3, start appruntime,appruntime inherit from Androidruntime, here are the things to do:

    /* 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 (Startreg (env) < 0) {//STEP6, register the Android native method        Aloge ("Unable to register all Android natives\n") in the JVM; C14/>return;    }

STEP7~STEP8, start Zygoteinit's main () method, pass in an array of strings, strarray[0]= "Com.android.internal.os.ZygoteInit", strarray[1] = "Start-system-server".

Step9,registerzygotesocket () registers the server socket, which is the previously mentioned zygote, 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 a 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;        }

step11, start the systemserver process, and the system starts the critical 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,1032,3001,3002,3003,3006, 3007 ","--capabilities= "+ capabilities +", "+ capabilities,"--runtime-init ","--nice-n        Ame=system_server "," Com.android.server.SystemServer ",};        Zygoteconnection.arguments Parsedargs = null;        int pid; try {.../* Request to fork the system server process */pid = Zygote.forksystemserv ER (parsedargs.uid, Parsedargs.gid, Parsedargs.gids, PARSEDARGS.D Ebugflags, NULL, parsedargs.permittedcapabilities, ParsedarGs.effectivecapabilities);        } catch (IllegalArgumentException ex) {throw new RuntimeException (ex);         }/* for child process */if (PID = = 0) {handlesystemserverprocess (Parsedargs);//STEP12}    return true; }

Zygote.forksystemserver () will fork out a new process, which is the systemserver process that is about to start and is the child of Zygote.If a new sub-process is successfully forked, this will return two times, return the PID of zygote at once, the value is greater than 0, return to execute step29, enter loop, Wait for the Activitymanagerservice call startprocesslocked () to start a new process that references the creation process of the Android4.4 framework analysis--startservice  Step12~step35, and another time to return the process ID of systemserver, equal to 0, will execute step12~step28.


Reference:

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

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


Right-copy the image address and open it in the browser to see a larger image.

Please correct me if there is any wrong place to be continued.


Android4.4 Framework Analysis--zygote process Start-up process

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.