The third day of the android startup Century

Source: Internet
Author: User

Reprinted from: http://www.cnblogs.com/hangxin1940/archive/2011/10/22/2221451.html

 

On the third day, Google said that the Garden of Eden (the Linux World) was isolated, so Adam (ADM) and Eve (Eve) were created, called zygote and system_server.

-- Xxx

 

The next day, init ran out. The most important thing for the Android system is to start zygote and system-server. Who is Adam and who is Eve?

From the analysis of init. RC

1 service zygote /system/bin/app_process -Xzygote /system/bin --zygote --start-system-server

-- Start-system-server is only a parameter.

 

Source code analysis

Ah, originally, Eve (zygote) used its own rib (fork) to create Adam (system_server )! What? Why didn't Adam create Eve !? The world of Android is a world of ascupational reproduction. zygote (Eve) was a fertilized egg at the beginning .......

 

Eve birthnote (Zygote Startup Process)

Step 1: fertilization (Rename)

From init. RC, we can see that the app_process program is started. After the process is started, change its name to zygote, even if it is fertilized ..

 

Step 2: implantation (Submit to appruntime to start JavaVM)

At this time, a VM will be created, which is the ancestor of the android Java World, and various parameters will be assigned when the VM is started. For example, we all know that by default, the memory loaded by an application cannot exceed 16 Mb. this parameter is set here, And the heapsize is 16 Mb. After that, the VM is started. Oh, it should be Dalvik.

After it is started, JNI functions will be registered for Java classes. In the Android world, C and Java are intertwined.

Step 3: Grow the umbilical cord (Java World)

After registering various JNI functions, you can call Java code at the C level of zygote.

This is the first time that we have entered the Java World. It callsCom. Android. Internal. OS. zygoteinitMain method.

The Java World is not isolated

1,It first registers a socket to make itself a server, that is, the IPC communication server. This is the greatness of Android, cleverly utilizing all the features of Linux. We will talk about zygote in the future.

2,Then pre-load the class and some Android resources. Over 1 K Java classes need to be loaded, and the loading time is also more than 1250 of the subtle classes, Android framework loading time-consuming classes are loaded here, this is also the reason why Android boot is slow, but is it easy to get tired and able to do everything, and it will be convenient to use it in the future, isn't it? Of course, the android system is like a mass production car, and all kinds of performance is not the best but the most stable. The majority of enthusiasts change Rom like a modified car. They need to be proficient in all kinds of knowledge from gear groups to ECUs, in order to achieve the perfect combination of hardware and software in order to exert the maximum effect, more .. To customize the ROM and reduce the boot time, you must rely on the Nb level and good insight.

3,Start systemserver. That is, the system_server process.

As I said just now, this system_server is ADM, and the first piece of meat of Eve fell down. Similarly, a system_server process was split from the zygote process by using the Linux fork mechanism. Working together, Adam and Eve worked together to build a beautiful Android world for us.

What does system_server do.

 4,Create a thread and transfer it to the socket listening mode. Each APK runs in Android as a separate Linux Process, which is split by zygote. Currently, zygote listens for requests passed in by activityservice through system_server using socket to split the process. At the end of the thread, disable the socket listener that was previously opened.

Now, what Eve should do is basically done, and we will wait for Adam to make her fertilized again ..... -_-!!!!

 

Here we will describe the process of childbirth for each activity! (Activity Startup Process)

Step 1 a mortal asks God for a child:

Start an activity, that is, startactivity. This activity is attached to an unstarted process. It is like opening an android program when it is just started.

Step 2 the great god received the following wishes:

Actvitymanagerservice receives startactvity, sorts out various parameters, such as APK registration, and sends this pray to Eve in the Garden of Eden.

Step 3 The Garden of Eden received a child request:

Is Adam willing to contact Eve directly? Haha, all services are started by systemserver, and systemserver is in charge of the binder, and will certainly handle this notification first. Systemserver sends a fork request to zygote through the IPC Mechanism of socket. At this time, the native world is returned from the Java World. Adam (system_server) fertilized Eve (zygote .... -_-!!! Android boom is inseparable from this kind of startup activity

Step 4: Generate EVE:

After splitting, fork generates a new process and returns it to the Java World, which is managed by activitymanagerservice. Here, let this process call activitythread. actvitythread is main, and the main of the APK program.

In linuix, the fork child process inherits all the information of the parent process, which is equivalent to one copy, but becomes another process. Since all information is inherited, Dalvik is inherited, which explains why an android program has a VM process.

 

Adam birthnote (Systemserver Startup Process)

As mentioned above, zygote starts and fork systemserver on the Java layer, that is, the first piece of meat dropped from Eve.

Systemserver first closes the socket inherited from the parent process due to fork.

Step 1: Native layer Initialization

Here, the native method is called through JNI, and the Native world is returned.

First, initialize the skia engine, which is an image engine that encapsulates various painting operations so that the screen can display things.

Then, start the binder, which is the core of Android IPC.

 

Step 2: Change the name and initialize the Java layer.

After the name is changed, the VM process is officially named system_server.

Java layer initialization, that is, callingCom. Android. server. systemserverMain. Interestingly, after obtaining the main method of the systemserver class through reflection, an exception is thrown instead of being called directly. This exception contains the main method.

Let's look back at zygote's Java World. The code that started system_server is in try. Catch intercepts the exception mentioned above and runs the main method.

 

Why?

Let's take a look at the sample program:

01 public class Method_test {
02  
03     public static void main(String[] args) {
04         ClassA ca=new ClassA();
05         ca.start();    
06     }
07  
08 }
09  
10 class ClassA{
11  
12     public ClassA() {
13     }
14     public void start(){
15         System.out.println("Start to call method");
16         Method_A();
17     }
18      
19     public void Method_A(){
20         System.out.println("Method A is called");
21         Method_B();
22              
23     }
24     public void Method_B(){
25         System.out.println("Method B is called");
26         Method_C();
27     }
28     public void Method_C(){
29         System.out.println("Method C is called");
30         Method_final();
31     }
32      
33     public void Method_final(){
34         System.out.println("The final method is called");
35          
36     }
37 }

 

The above code simulates a scenario where method A calls Method B and method B calls method C again... A ()-> B ()-> C ()-> final (), there may be more complex content in final, and nothing will be done after final. In real cases, there are more call levels between a () and final.

The above call stack is like this:

 

In this case, on some platforms where resources are scarce, and, indeed, they are not very good. We hope that the level of the previous call will disappear when final is executed, the variables used in the past disappear, and we have a clean world, and we don't have to jump out after the execution.

In this case, try catch has a major effect. Check the Code:

01 public class Method_test {
02  
03     public static void main(String[] args) {
04         ClassA ca=new ClassA();
05         ca.start();    
06     }
07  
08 }
09  
10 class ClassA{
11  
12     public ClassA() {
13     }
14     public void start(){
15         System.out.println("Start to call method");
16         try{
17             Method_A();
18         }catch (RuntimeException e) {
19             // Execute the target method after an exception is caught
20             Method_final();
21         }
22     }
23      
24     public void Method_A(){
25         System.out.println("Method A is called");
26         Method_B();
27              
28     }
29     public void Method_B(){
30         System.out.println("Method B is called");
31         Method_C();
32     }
33     public void Method_C(){
34         System.out.println("Method C is called");
35         // An exception is thrown here.
36         throw new RuntimeException();
37     }
38      
39     public void Method_final(){
40         System.out.println("The final method is called");
41          
42     }
43 }

Compared with the previous one, we changed the call of final () to start (), directly let C () throw an exception, and execute final () in catch ().

The call stack is as follows:

 

The design idea of Android is really fierce. Through this analysis, I learned another skill ....

 

Systemservice-> main

1. Load the native library and call init1 ()

Here we have created several native services, such as servicemanger and surfaceflinger, and added the current thread to the communication Army of the binder.

2. Call init2 () in the Java World through JNI ()

In init1 (), the init2 () in the Java World is called ().

Here we start various services, such as entropyservice, powermanagerservice, batteryservice, and windowmanager.

It will also summon the helldog, namely watchdog, to keep an eye on some important services to prevent them from degrading. The responsibility of the guard dog is to stare at some important services. If they get down, they will kill Adam and let init restore it again with full blood.

Finally, it enters the message loop to schedule cross-thread access in the Android world. Google often calls sub-threads to update the UI thread through handler, the messages sent in Handler are distributed here.

Severe recommendationDeng fanpingThe Preface 《In-depth understanding of anroid volume I", I feel that this is the best book I have ever seen involving the framework. It is mainly easy to understand (in fact, it is still limited)

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.