[FrameWork] ①System service and application process related

system service

1. Zygote function, startup process, working principle

  • 1.1 Role:

    • Start SystemServer
    • Incubation application process
  • 1.2 Start-up process

    • The init process (the first process in the user space after Linux starts), first loads the startup configuration file init.rc, the configuration file contains the system services to be started

    • Zygote is one of the services to be started, and other services such as ServiceManager are also started in this way

    • Two ways to start the process

      • fork+handle
      • fork+execve
  • 1.3 What did Zygote do after it was launched

    • The Native World of Zygote (C++)
      • Start the Android virtual machine: JNI_CreateJavaVM
      • Register Android JNI function
      • Enter the java world
    • Zygote's Java world
      • Preload Resources
      • Start System Server, fork out the process of System Server
      • Enter the loop and wait for the socket message
  • 1.4 Matters needing attention

    • zygote fork needs single thread
    • zygote's IPC (inter-process communication mechanism) does not use binder
  • problem

    • Why not let SystemServer do the incubating application process and design a Zygote?

    • Why doesn't Zygote's IPC communication mechanism use binder? Will there be any problems if we use binder?

2. Android system startup

  • init.rc system process, as shown in the figure

  • 2.1 Zygote launch review

    • The init process forks the zygote process
    • Start the virtual machine, register the jni function, and prepare for entering the world of java
    • Preload system resources
    • Start SystemServer, SystemServer runs a lot of system services
    • Enter the Socket Loop, continue to receive and process Socket messages
  • 2.2 How to start SystemServer

    • zygote fork creates the SystemServer process

    • As shown in Figure 7

  • 2.3 How to start the system service

    • How to publish system services to make the application visible?
      • Register the system service binder to ServiceManager
    • On what thread does the system service run?
      • DisplayThread is responsible for displaying
      • FgThread foreground task
      • loThread IO task
      • UiThread It is worth mentioning that this is a child thread
      • binder thread
    • problem:
      • Why don't all system services run in the binder thread?
      • Why don't all system services run in their own private worker threads?
      • How to choose between running on the binder thread and running on the worker thread?
  • 2.4 How to solve the interdependence of system service startup

    • Start in batches
      • Start the basic service first, AMS PMS PKMS
      • After starting some of the upper services
    • Start in stages
  • 2.5 Launching the desktop

    • After the AMS is ready, the SystemReady function is called, and the STARThomeActivity function is called internally
    • PMS queries the installed applications and displays them on the desktop
    • LauncherActivity of the application will be launched after clicking the application
    • As shown in Figure 8

Answer points

Talk about the startup process of the Android system

  • Short answer: first start zygote, then start SystemServer, and then start the desktop
  • Answering skills:
    • Answer how to start zygote: init->zygote->native world->java world
    • Answer how to start SystemServer
    • Answer how the system service is started

3. How to add a system service

  • Key words

    • Convenience
    • open
    • IPC cross-process communication
  • How to use system services

    • Get service based on service name
    • The principle is shown in Figure 10
  • How to register system services

    • addService incoming service name and Binder object
    • Picture 11
  • When is the system service registered

    • Registered when SystemServer starts
    • Picture 12
  • Independent process system service

    • Take surfaceflinger as an example, this is a system service implemented by native
    • Regardless of whether it is a service of an independent process or a service of SystemServer, it needs to be registered with the ServiceManager
    • Figure 13
  • Start the binder mechanism

    • Open the binder driver
    • Map memory, allocate buffer
    • Start the binder thread and enter the binder loop

Answer points

  • Key points for answering questions
    • Why add system services: for others
    • How to let others use: abandon the binder mechanism ipc communication, register and open it, so that other callers can use
    • Specifically what to do
  • Add system service timing: Register when SystemServer starts
  • What to do on the server
    • Enable binder mechanism
    • Service initialization work
    • Binder registered to ServiceManager
  • What to do on the application side
    • System service call method Context.getSystemSevice
    • If you add a service yourself, it must be consistent with the system service call, and you need to register serviceFeature for the service

4. What is the difference between system service and bind application service

The difference in startup mode

  • Most system services run in SystemServer, such as AMS WMS PMS

    • Figure 14
  • Application Service: It will eventually be transferred to AMS, AMS is responsible for the management and scheduling of Service, and the application side is responsible for the startup and loading of servcie

    • Figure 15

The difference in registration method

  • System service: the binder entity object is registered in the ServiceManager, and only the system service can be registered in the ServiceManager
  • Application service: Different from system service, application service needs to apply bindService, and it needs AMS to request it
    • Figure 16

Difference in usage

  • system service:

    • Figure 17
  • Application Service

    • Figure 18

5. Starting and working principle of ServiceManager

What is the startup process of ServiceManager?

  • Start process
  • Enable the binder mechanism: open the binder driver, map memory
  • Publish your own service:
  • Wait and respond to requests: loop, read request processing request

How to get the binder object of ServiceManager?

  • Create a BpBinder based on the value of handle 0

How to add service to ServiceManager?

How to get service from ServiceManager?

  • Get the binder object of ServiceManager first
  • Then initiate a getService call

Application process related

1. How does the application process start?

Investigation point:

  • Understand how processes start under Linux
  • Familiar with the basic process of application process startup
  • Deeply understand the principle of application process startup

When did the triggered process start?

  • Find the process of the component
  • If it is not started, call the startProcessLocked of AMS to initiate the start

Application launch

  • After AMS initiates a request to zygote to start the application process, it will return the process pid to AMS
  • After the application process is started, I personally tell AMS to start it myself
  • ActivityThread is not a thread, it is just a class
  • Figure 20

answer

  • When did the application process start
    • Start application components, such as Activity and Service will first determine whether the process where the application is started, if it is not started, it will start the process first
  • Who initiated the process
    • AMS initiates a request to zygote and communicates via socket
  • Zygote forks the application process and executes the main function of the entry of ActivityThread. The java class name of the entry function is sent to zygote through socket by AMS
  • After the process is started, it reports to AMS, and the registration of Application Thread is finished. Only when it reports to AMS, the process is available.
  • Figure 22

2. How does the application enable the binder mechanism

Investigation point :

  • Understand what binder is used for?
    • -> Cross-process communication
  • Where is the binder mechanism used in the application?
    • -> System service, broadcast, start Activity, everything related to application components basically involves the binder mechanism
  • What is the general startup process of the application?
    • -> AMS initiates a request to zygote -> zygote forks the application process -> Report to AMS after the process is started, and register Application Thread -> Process is available
  • How does a process enable the binder mechanism?
    • Solutions to the following questions

answer

  • First answer the start timing of the binder: after the application process is started, the binder is supported
  • Then answer the following 4 points
    • Open the binder driver
    • Map memory, allocate buffer
    • Register binder thread
    • Enter the binder loop

3. Talk about the understanding of Application

Investigation point :

  • Understand the role of Application (primary)
  • Familiar with Application's class inheritance and life cycle (intermediate)
  • Deeply understand the principle of Application initialization (advanced)

Role :

  • Save global variables in the application process
  • Since Application is earlier than the other four components, some initialization operations, SDK init, etc. can be performed.
  • Provide application context
  • Note: Application is related to process, there are several processes, there are several applications

Inheritance : Application-ContextWrapper

Life cycle :

  • start up
    • Constructor new Application()
    • attchBaseContext
    • onCreate
  • end
    • onTerminate(), only useful in the simulator, not in the scope of this topic

How to initialize the application

  • ActivityThread.attch()->

  • Figure 24

  • to sum up:

    • As shown in the figure above, first call the ActivityThread.attch() function, and finally create an Application instance through reflection
    • Then the context application.attchBaseContext() will be prepared
    • Finally, the application Oncreate function is called
    • The overall process is shown in Figure 25
  • note:

    • Do not perform time-consuming operations in the life cycle of the Application, because this may block the UI thread of the application (be careful in the code process, and time-consuming operations in Application onCreate such as the time-consuming initialization operation of the SDK should be avoided)
    • The bug of static variables in Application
      • Example
        • Declare the variable static String name in Application
        • Initialize the value of name in MainActivity
        • Get the value of name in TestActivity
      • Question: Suppose the application process is killed, when switching back to the application, the system will rebuild the application and restore TestActivity. At this time, the name is not initialized, so the name is null, which will cause some exceptions.

4. Talk about the understanding of Context

A good article: https://juejin.cn/post/6864346705081401352

Inspection point

  • Understand the role of Context

    • Is the context of the application component
    • Access specific resource class
    • Application layer call: start Activity, send broadcast, receive intents
    • Figure 26
  • In-depth understanding of the Context difference between different application components, class inheritance relationship

  • Familiar with the Context initialization process

How many contexts are there in the application? What is the difference between different Context?

The number of Context in the Application (there are multiple Application contexts under multiple processes) + the number of Activity's Context + the number of Service's Context. The difference is that the direct Activity inherits from ContextThemeWrapper because of the UI, and indirectly inherits ContextWrapper, while Service And Application directly inherits ContextWrapper

  • Application: related to the startup of the application process (as mentioned in the previous question)
    • Inheritance: Application ← ContextWrapper ← Context
    • Calling sequence, →attachBaseContext → onCreate
    • ContextWrapper contains a Context, the calls are delegated to him
  • Activity:
    • Figure 27

    • Inheritance: Activity ← ContextThemeWrapper ← ContextWrapper ← Context

    • Calling sequence, → attachBaseContext → onCreate

  • Service:
    • Figure 28

    • Inheritance: Application ← ContextWrapper ← Context

  • Broadcast without Context
    • Broadcast is an abstract class, it does not directly or indirectly inherit from Context
    • Context in onReceive, if it is dynamically registered, then the context is the context for registering broadcast; if it is statically registered, it is a ContextWrapper with application as mBase (will be discussed later)
  • ContentProvider has no Context,
    • The Context member variable is passed in when the ContentProvider is initialized, and the Context of the Application is passed. The initialization of the ContentProvider, that is, onCreate is called before the Applicaiton onCreate
    • Figure 29

What is the difference between this and getBaseContext in Activity?

  • this returns Activity to point to itself
  • getBaseContext returns mBase in ContextWrapper

What is the difference between getApplication and getApplicationContext?

  • All return the Applicaiton object
  • getApplicationContext is an abstract function in Context, getApplication is unique to Activity and Service, and cannot be used in other places such as broadcasting

The structure of application components, the calling sequence of onCreate and attachBaseContext?

  • First call the component's constructor, then call attachBaseContext to give the context, and then call onCreate

If you want to know more about Android-related knowledge, you can click into my [ GitHub project: https://github.com/733gh/GH-Android-Review-master ] to check it out for yourself , and there are many records in it. Android knowledge points. Finally, please give me your support! ! !

Guess you like

Origin blog.csdn.net/dongrimaomaoyu/article/details/114591781