Android Startup Process

Source: Internet
Author: User

The Android startup process can be divided into two stages: the first stage is the Linux Startup, and the second stage is the Android startup. Next we will take a look at the specific process.

The first step is Linux Startup. I want to skip this part. It is nothing more than Linux Bootloader, Kernel, Driver, and so on. The only thing to mention here is ServiceManager, which is the service manager, this process is started before Android is loaded. this configuration item is displayed in rc:

Service servicemanager/system/bin/servicemanager

ServiceManager is the service management daemon of the Binder and is the core of the Binder. It uses the Binder driver for IPC management. We will not detail the IPC communication mechanism here. In the APP and Framework, the ServiceManager. java used by the application communicates with this daemon through Proxy.

 

Next, we will describe the startup of Android in detail. Let's take a look at the configuration in init. rc.

Service zygote/system/bin/app_process-Xzygote/system/bin -- zygote -- start-system-server

That is, after linux is started, it starts the zygote service process, which is exactly like its name: incubator, which is the incubator for all Android applications.

 

Let's take a look at the app_process code:

Frameworks/base/cmds/app_process/app_main.cpp

The main () function has the following code:

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 );

}

We can track AndroidRuntime, where the code is located:

Frameworks/base/core/jni/AndroidRuntime. cpp

The start () function has the following code:

/* Start the virtual machine */

If (startVm (& mJavaVM, & env )! = 0)

Goto bail;

......

Env-> CallStaticVoidMethod (startClass, startMeth, strArray );

That is, the virtual machine is started first, and then the zygoteInit function is called using JNI.

 

Continue to trace the main () function of frameworks/base/core/java/com/android/internal/OS/ZygoteInit. java. The Code is as follows:

If (argv [1]. equals ("true ")){

StartSystemServer ();

} Else if (! Argv [1]. equals ("false ")){

Throw new RuntimeException (argv [0] + USAGE_STRING );

}

 

Log. I (TAG, "Accepting command socket connections ");

 

If (ZYGOTE_FORK_MODE ){

RunForkMode ();

} Else {

RunSelectLoopMode ();

}

The first part is to start the system service, and the last part is a condition judgment, but ZYGOTE_FORK_MODE is assigned false. Therefore, the runSelectLoopMode () function of the else branch is implemented. In this function, in fact, in an infinite loop, the zygoteConnection class is used to process messages through socket. It is used to fork a new zygote, so as to implement a virtual machine mechanism for each process in the most lightweight way.

 

Continue to check startSystemServer (). The Code is located:

Frameworks/base/services/java/com/android/server/systemserver. java

In its main () function, the native function init1 (args) is called and tracked

Frameworks/base/services/jni/com_android_server_systemService.cpp, and then

Frameworks/base/cmds/system_server/library/system_init.cpp

The system_init () function has the following code:

If (strcmp (propBuf, "1") = 0 ){

// Start the SurfaceFlinger

SurfaceFlinger: instantiate ();

}

AndroidRuntime * runtime = AndroidRuntime: getRuntime ();

 

LOGI ("System server: starting Android services./n ");

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

SurfaceFlinger is instantiated, and then the init2 () function of SystemServer is called using the callStatic () function at runtime. This function is located:

Frameworks/base/services/java/com/android/server/SystemServer. java

The code is:

Public static final void init2 (){

Slog. I (TAG, "Entered the Android system server! ");

Thread thr = new ServerThread ();

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

Thr. start ();

}

In this ServerThread, we can see the familiar Android service, such as the startup of the WallpaperService service:

Try {

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

Wallpaper = new WallpaperManagerService (context );

ServiceManager. addService (Context. WALLPAPER_SERVICE, wallpaper );

} Catch (Throwable e ){

Slog. e (TAG, "Failure starting Wallpaper Service", e );

}

Finally, call the systemReady () function of each service to notify the system that it is ready.

 

At this point, the START process of the system has ended. Two diagrams are used to illustrate the problem:

 

It can be seen from this that after linux init starts several daemon processes, it starts Android runtime and zygote, zygote and then starts the virtual machine, system service, and system service after it starts the local service, A number of Android services have been started, and ServiceManager registration has been completed. Finally, the system has been started. Shows the process space of the system:

It can be seen that the zygote incubator implements the virtual machine at the minimum cost in the write-time replication mode for each process.

 

Well, I believe that through this series of source code tracking, we can have a clearer understanding of the Android startup process.


By Kang chunhui

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.