Zygote (app_process) Correlation Analysis 2, zygoteapp_process
In the previous article, we analyzed the startup process from init. c to Zygote (app_process.
Analysis of frameworks/base/cmds/app_process/app_main.cpp is started today.
service zygote /system/bin/app_process -Xzygote /system/bin --zygote --start-system-server
The above content will be used in app_main.cpp.
/** Start zygote by/system/bin/app_process-Xzygote/system/bin -- zygote -- start-system-server *. Therefore, what is stored in argc = 5 * argv is these five parameters argv [0] = "/system/bin/app_process ", argv [1] = "-Xzygote ".... */int main (int argc, const char * const argv []) {... appRuntime runtime ;... // This function returns 1, indicating that only the-Xzytote parameter is processed. The so-called processing is to add this parameter to the mOptions variable of the runtime object. Int I = runtime. addVmArguments (argc, argv); // Next arg is parent directory if (I <argc) {runtime. mParentDir = argv [I ++];} // Next arg is startup classname or "-- zygote" // Process command line arguments if (I <argc) {arg = argv [I ++]; if (0 = strcmp ("-- zygote", arg) {bool startSystemServer = (I <argc )? Strcmp (argv [I], "-- start-system-server") = 0: false; setArgv0 (argv0, "zygote"); set_process_name ("zygote"); runtime. start ("com. android. internal. OS. zygoteInit ", startSystemServer);} else {set_process_name (argv0); runtime. mClassName = arg; // Remainder of args get passed to startup class main () runtime. mArgC = argc-I; runtime. mArgV = argv + I; LOGV ("App process is starting with pid = % d, class = % s. \ n ", getpid (), runtime. getClassName (); runtime. start () ;}} else {LOG_ALWAYS_FATAL ("app_process: no class name or -- zygote supplied. "); fprintf (stderr," Error: no class name or -- zygote supplied. \ n "); app_usage (); return 10 ;}
The Code shows that the start function of AppRuntime is called to start zygote.
AppRuntime inherits from AndroidRuntime analysis: (zygote enters the java World)
Will call the start function of AndroidRuntime. cpp.
In this function, the main action is relatively simple.
Start ():
Set the root directory "/system" startVM () 1. Call the jni vm to create a function. 2. Set the heapsize of the virtual machine to 16 MB by default. 1. register the JNI function. 2. Call the main function of the com. android. internal. OS. ZygoteInit class through JNI. ZygoteInit related 1. Find the main function of the zygoteInit class (java class) 2. Call CallStaticVoidMethod () to enter the java World (main () of ZygoteInit. java ())
Let's take a look at the Code:
Void start (const char * className, const char * options) {// start the virtual machine JNIEnv * env; if (startVm (& mJavaVM, & env )! = 0) {return;} // register JNI if (starregulatory (env) <0) {return ;} // jni calls the java main method jmethodID startMeth = env-> GetStaticMethodID (startClass, "main", "([Ljava/lang/String;) V "); // call jni To Find Out ZygoteInit class jclass startClass = env-> FindClass (className );
// Run the main function of the ZygoteInit class
Env-> CallStaticVoidMethod (startClass, startMeth, strArray );}
In the next section, we will continue to analyze the main function of the com. android. internal. OS. ZygoteInit class.