Android system startup learning record

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:

  1. 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.

  2. 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.

  3. 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 and init.rcstart system services and applications according to the rules defined in the configuration file.

  4. init.rc configuration parsing: init The process parses init.rcthe configuration file, which defines a series of initialization steps, system services, and property settings to be performed when the system starts.

  5. Zygote startup:init.rc Among the services defined in , there will be a service zygotenamed . zygoteA process is an incubator for Android applications, a template for new application processes.

  6. System Server Startup:init.rc Another important service defined in is system_server. system_serverIt 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.

  7. Application startup: Once system_serverstarted, 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.

  1. 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, servicenameis the name of the service, /system/bin/executableis the binary executable to execute, and arg1 arg2is the parameter passed to the executable. classDefines the type of service, such as main, , zygote, mediaetc. userThe running user of the service is specified, seclabeland the security label is defined, oneshotindicating that the service will only run once at boot time.

  2. 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
  1. These properties can be accessed through the system property service (property_service) in the Android system.

  2. 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-serveretc. Dependencies can ensure that some services start before others. For example:

    on early-init
        start servicename
    

    This code indicates that the service early-initis started during the stage servicename.

3. About Zygote

Zygote is a special process in the Android system that acts as an application incubator for creating new application processes.

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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.

Guess you like

Origin blog.csdn.net/weixin_53324308/article/details/132451373