Interview questions related to FrameWork

Refer to learning video: Framework interview collection, big factory old drivers will take you to clear up the difficulties of the framework

1, Knowledge points about zygote

1. Who started zygote?
Answer: init rc

2. Why zygote?
A: if there is no zygote, if the app wants to start a process, it will call the system api to help us open it. The app will fork itself. As a result, the app developer does not follow the corresponding specifications. If the memory space of an app is 50M, open the system api through the fork() function to make the memory reach 500M, which will cause a problem.
zygote process is to manage the start process. Starting a process is a dangerous operation. Starting a process requires a lot of resources and cpu

3. Why do you need socket instead of binder?
A: in order to prevent the api of app hook zygote corresponding to the opening process, google engineers can't open the opening process.
1) Security (binder communication is very hook)
2) Standing at the design angle * c/s model)

4. How does the launcher notify the zygote process of creating an app?
A: the launcher first obtains the ams reference, that is, the binder reference, and calls startActivity() to send information to the zygote process through socket. After the zygote process receives the message, it creates the process through the fork() function, opens the main() entry function of the ActivityThread in the process, loads the application and reads the ativity.

2, Knowledge points related to PMS

1. What is PMS service for? Is PMS related to our installation speed and startup speed?

PMS: PackageManagerService
Install apk, manage apk and load Activity in advance.
PMS is related to our installation speed and startup speed. PMS will traverse the / data/app installation directory. The more apps are installed, the lower the execution efficiency and the longer the time consumption

2. Why PMS is needed

A: 1) each time you open an app, you need to traverse the / data/app directory, and you can locate the jump app through the package name;
2) Get androidmanifest.IO operation xml;
3) dom parsing traverses androidmaniftest XML, find the entry Activity whose attribute in intent filterl is category android:name = "android.intent.category.LAUNCHER";
4) Load entry Activity.

3. Who started PMS? Is it a separate process? If not, in which process?

A: Yes, it is_ The server process is started. It is not a separate process. It is started by SystemServer calling the main() method of PMS.
system_ The parent process of server is zygote, and PMS and AMS run in system_server process.

SystemServer.java

public static void main(String[] args) {
    new SystemServer().run();
}

SystemServer.java

private void run() {
	... ...
	 // Start services.
     try {
         traceBeginAndSlog("StartServices");
         startBootstrapServices();
         startCoreServices();
         startOtherServices();
         SystemServerInitThreadPool.shutdown();
     } catch (Throwable ex) {
         Slog.e("System", "******************************************");
         Slog.e("System", "************ Failure starting system services", ex);
         throw ex;
     } finally {
         traceEnd();
     }
	... ...
}

SystemServer.java

/**
* Starts the small tangle of critical services that are needed to get the system off the
* ground.  These services have complex mutual dependencies which is why we initialize them all
* in one place here.  Unless your service is also entwined in these dependencies, it should be
* initialized in one of the other functions.
*/
private void startBootstrapServices() {
	... ...
	ActivityTaskManagerService atm = mSystemServiceManager.startService(
        ActivityTaskManagerService.Lifecycle.class).getService();
    mActivityManagerService = ActivityManagerService.Lifecycle.startService(
            mSystemServiceManager, atm);
    mActivityManagerService.setSystemServiceManager(mSystemServiceManager);
    mActivityManagerService.setInstaller(installer);
	... ...
	try {
        Watchdog.getInstance().pauseWatchingCurrentThread("packagemanagermain");
        mPackageManagerService = PackageManagerService.main(mSystemContext, installer,
                mFactoryTestMode != FactoryTest.FACTORY_TEST_OFF, mOnlyCore);
    } finally {
        Watchdog.getInstance().resumeWatchingCurrentThread("packagemanagermain");
    }
	... ... 
}

SystemServiceManager.java

/**
  * Creates and starts a system service. The class must be a subclass of
  * {@link com.android.server.SystemService}.
  *
  * @param serviceClass A Java class that implements the SystemService interface.
  * @return The service instance, never null.
  * @throws RuntimeException if the service fails to start.
  */
 @SuppressWarnings("unchecked")
 public <T extends SystemService> T startService(Class<T> serviceClass) {
     try {
         final String name = serviceClass.getName();
         Slog.i(TAG, "Starting " + name);
         Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "StartService " + name);

         // Create the service.
         if (!SystemService.class.isAssignableFrom(serviceClass)) {
             throw new RuntimeException("Failed to create " + name
                     + ": service must extend " + SystemService.class.getName());
         }
         final T service;
         try {
			// Build an object through reflection, add this object to return, and start service
             Constructor<T> constructor = serviceClass.getConstructor(Context.class);
             service = constructor.newInstance(mContext);   
         } catch (InstantiationException ex) {
             throw new RuntimeException("Failed to create service " + name
                     + ": service could not be instantiated", ex);
         } catch (IllegalAccessException ex) {
             throw new RuntimeException("Failed to create service " + name
                     + ": service must have a public constructor with a Context argument", ex);
         } catch (NoSuchMethodException ex) {
             throw new RuntimeException("Failed to create service " + name
                     + ": service must have a public constructor with a Context argument", ex);
         } catch (InvocationTargetException ex) {
             throw new RuntimeException("Failed to create service " + name
                     + ": service constructor threw an exception", ex);
         }

         startService(service);
         return service;
     } finally {
         Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
     }
 }

  build a service object through reflection, return the object, and start the service through startService. Therefore, AMS and SystemServer are in the same process.

PackageManagerService.java

public static PackageManagerService main(Context context, Installer installer,
       boolean factoryTest, boolean onlyCore) {
    // Self-check for initial settings.
    PackageManagerServiceCompilerMapping.checkProperties();

    PackageManagerService m = new PackageManagerService(context, installer,
            factoryTest, onlyCore);
    m.enableSystemUserPackages();
    ServiceManager.addService("package", m);
    final PackageManagerNative pmn = m.new PackageManagerNative();
    ServiceManager.addService("package_native", pmn);
    return m;
}

It just calls an ordinary main() method.

4. What is the purpose of PMS scanning and why PMS needs to be designed like this?

A: PMS scanning only parses each apk file, parses the AndroidManifest file inside, and parses the file to generate a javabean. The javabean contains all the tag attribute fields in the AndroidManifest

PackageParser.java

 @UnsupportedAppUsage
 public final ArrayList<Permission> permissions = new ArrayList<Permission>(0);
 @UnsupportedAppUsage
 public final ArrayList<PermissionGroup> permissionGroups = new ArrayList<PermissionGroup>(0);
 //The activity refers to an internal class, just a java bean. public final static class Activity extends Component<ActivityIntentInfo> implements Parcelable
 @UnsupportedAppUsage
 public final ArrayList<Activity> activities = new ArrayList<Activity>(0);  
 @UnsupportedAppUsage
 public final ArrayList<Activity> receivers = new ArrayList<Activity>(0);
 @UnsupportedAppUsage
 public final ArrayList<Provider> providers = new ArrayList<Provider>(0);
 @UnsupportedAppUsage
 public final ArrayList<Service> services = new ArrayList<Service>(0);
 @UnsupportedAppUsage
 public final ArrayList<Instrumentation> instrumentation = new ArrayList<Instrumentation>(0);

 @UnsupportedAppUsage
 public final ArrayList<String> requestedPermissions = new ArrayList<String>();

3, Knowledge points about AMS

1. What is the relationship between AMS and PMS? Are they in the same process?

Answer: PMS is used for package management and AMS is used for life cycle management; The two complement each other. PMS comes first and AMS comes second. Only PMS manages, caches and scans first, and then it will enter the life cycle management of AMS.
AMS and PMS are in the same process_ Server.

2. How does the system store AMS service objects and how does the application layer get AMS applications?

3. What is the relationship between AMS and servicemanage? Let's talk about the app startup process

4. Describe in detail the relationship between AMS and ActivityThread and its interaction mechanism.

Keywords: Framework

Added by radiators on Wed, 19 Jan 2022 17:32:08 +0200