Android startup-a deep understanding of zygote 2

Source: Internet
Author: User

In the previous section, the http://www.bkjia.com/kf/201203/123062.html talked about using Zygote. forkSystemServer to generate the SystemServer process using fork. What did the process do?

The following describes the handleSystemServerProcess process.

The code snippet is as follows:

/* Request to fork the system server process */

Pid = Zygote. forkSystemServer (

ParsedArgs. uid, parsedArgs. gid,

ParsedArgs. gids, debugFlags, null );

/* For child process */

If (pid = 0 ){

HandleSystemServerProcess (parsedArgs );

}

-->

Private static void handleSystemServerProcess (

ZygoteConnection. Arguments parsedArgs)

{

// Disable socket inherited from Zygote

CloseServerSocket ();

// Call the zygoteInit function.

RuntimeInit. zygoteInit (parsedArgs. remainingArgs );

}

RuntimeInit. java @ frameworks \ base \ core \ java \ com \ android \ internal \ OS

Public static final void zygoteInit (String [] argv ){

// Call the JNI function here.

ZygoteInitNative ();

// Call the main function of startClass com. android. server. SystemServer.

InvokeStaticMain (startClass, startArgs );

}

-->

ZygoteInitNative @ frameworks \ base \ core \ jni

Call onZygoteInit @ frameworks \ base \ cmds \ app_process \ app_main.cpp

Virtual void onZygoteInit ()

{

Sp <ProcessState> proc = ProcessState: self ();

If (proc-> supportsProcesses ()){

LOGV ("App process: starting thread pool. \ n ");

Proc-> startThreadPool (); // start the thread to establish binder Communication

}

}

OK, this process is clear. Next we will focus on how the native system_server process starts?

Main @ frameworks \ base \ services \ java \ com \ android \ server \ SystemServer. java

Public static void main (String [] args ){

System. loadLibrary ("android_servers ");

Init1 (args );

}

Here init1 is a native function:

System_init @ frameworks \ base \ cmds \ system_server \ library \ system_init.cpp

Extern "C" status_t system_init ()

{

LOGI ("Entered system_init ()");

// Start the SurfaceFlinger

SurfaceFlinger: instantiate ();

Runtime-> callStatic ("com/android/server/SystemServer", "init2 ");

...

}

Here we call

Init2 @ frameworks \ base \ services \ java \ com \ android \ server \ SystemServer. java

Public static final void init2 (){

Thread thr = new ServerThread ();

Thr. setName ("android. server. ServerThread ");

Thr. start ();

}

A ServerThread thread is started and its start () function is called.

Run () function of the ServerThread

Public void run (){

Slog. I (TAG, "Entropy Service ");

ServiceManager. addService ("entropy", new EntropyService ());

Slog. I (TAG, "Power Manager ");

Power = new PowerManagerService ();

ServiceManager. addService (Context. POWER_SERVICE, power );

...

// Finally, it enters the loop message loop to process the message ....

Logoff. loop ();

Slog. d (TAG, "System ServerThread is exiting! ");

}

Oh, it turns out that many important system services are added to ServiecManager in sequence, and the entire SystemServer process has been started.

 


To sum up, the important execution process is as follows:



From andyhuabing's column

Related Article

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.