1. Android system startup process
The boot process of the Android system involves multiple stages and components. The following is a brief description of the general Android system boot process:
-
Bootloader (bootloader): When you turn on the system, the system first loads the bootloader (bootloader), which is a small program whose main function is to initialize the hardware and load the boot image of the operating system. A bootloader is usually located on a device's storage medium, such as flash memory, and is fixed in a specific location by hardware.
-
Kernel boot: The bootloader loads the operating system kernel (kernel). The kernel is the core of the operating system and is responsible for managing hardware, memory, processes, and device drivers. Android devices typically use the Linux kernel.
-
init process start:
init
After the kernel starts, a user space process named is started . The main task of this process is to initialize the user space andinit.rc
start system services and applications according to the rules defined in the configuration file. -
init.rc configuration parsing:
init
The process parsesinit.rc
the configuration file, which defines a series of initialization steps, system services, and property settings to be performed when the system starts. -
Zygote startup:
init.rc
Among the services defined in , there will be a servicezygote
named .zygote
A process is an incubator for Android applications, a template for new application processes. -
System Server Startup:
init.rc
Another important service defined in issystem_server
.system_server
It is the core service of the Android system, responsible for starting and managing various components of the system, such as Activity Manager, Package Manager, Notification Manager, etc. -
Application startup: Once
system_server
started, the Android system is ready to run applications normally. The user interface is loaded and the user can interact with the device. Applications can be triggered by user actions or background events.
2. About the init.rc file
The purpose of the init.rc file is to start core services, set properties, and execute some initialization scripts when the system boots. These services and attributes play an important role in the operation of the entire system.
-
Service Definition: The most important part of the init.rc file is the definition of the service (Service). A service represents a component in the system, which can be a background process, daemon process, or other system-level tasks. Each service is defined by a name and a set of attributes, including startup command, startup level, dependencies, etc. Here is an example service definition:
service servicename /system/bin/executable arg1 arg2 { class classname user username seclabel u:r:servicename:s0 oneshot }
Here,
servicename
is the name of the service,/system/bin/executable
is the binary executable to execute, andarg1 arg2
is the parameter passed to the executable.class
Defines the type of service, such asmain
, ,zygote
,media
etc.user
The running user of the service is specified,seclabel
and the security label is defined,oneshot
indicating that the service will only run once at boot time. -
Property setting: The init.rc file is also used to set system properties. Properties are key-value pairs used to control the behavior and configuration of the system. Attributes can be shared between different components of the system, affecting process running, permission control, etc. For example:
setprop ro.build.type user
setprop persist.sys.language en
-
These properties can be accessed through the system property service (property_service) in the Android system.
-
Startup levels and dependencies: Services in the init.rc file can be ensured to start at the correct time and order by specifying startlevels and dependencies. The startup level (on boot phases) defines when to start the service, such as
early-init
,init
, ,system-server
etc. Dependencies can ensure that some services start before others. For example:on early-init start servicename
This code indicates that the service
early-init
is started during the stageservicename
.
3. About Zygote
Zygote is a special process in the Android system that acts as an application incubator for creating new application processes.
-
Creation of Zygote process: During the Android system startup process, the Zygote process is started by the init process (the first user space process). The Zygote process preloads and initializes commonly used classes, resources, and states at system startup to reduce the startup time of subsequent application processes.
-
Shared state and resources: Zygote process loads many commonly used classes and resources during initialization, which can be shared between application processes, thus speeding up the startup of new application processes. This sharing includes preloaded classes, system resources, parts of the virtual machine state, and more.
-
Application Incubation: When a user starts a new application, the Android system does not create a new process for the application, but creates a new application process by copying the Zygote process. This reduces memory usage and startup time. The new application process is called an "app process" and it inherits the state and resources of the Zygote process.
-
Application initialization: In the application process, the classes and resources in the Zygote process are already preloaded, which allows the application to start faster. The application process executes its own
main()
methods to initialize the application's components, resources, and business logic. -
Application Isolation: Although application processes inherit some state from the Zygote process, they are still isolated from each other. Each application runs in its own process and cannot directly access memory or resources from each other. This helps improve the security and stability of the application.