· 这是《Android 系统源代码情景分析》 一书中第 11 章, Zygote 和 System 进程的启动过程,的读书摘要;
Zygote 进程是由 Android 系统的第一个进程脚本 init 启动的;
Zygote 进程是通过复制自身的方式来创建 System 进程和应用程序的;
Android 系统中,所有的应用程序进程,以及用来运行系统关键服务的 System 进程都是 Zygote 进程负责创建的;
1、reigsterZyogteSocket()
创建一个 Server 段的本地 Socket,用来等待 ActivityManagerService 请求 Zygote 创建新的应用程序进程;
2、startSystemServer()
启动 System 进程,以便它可以将系统关键服务启动起来;
方法 Zygote.forkSystemServer() 创建子进程也就是 Android 系统的 System 进程;
System 进程的用户 ID 和用户组 ID 均为 1000, 并且它还具有用户组1001~1010, 1018以及3001~3003 的权限;
方法 handleSystemServiceProcess(...) 启动 System 进程
3、runSelectLoop()
等待 ActivityManagerService 请求 Zygote 进程创建新的应用程序进程;
至此, Zygote 进程的启动已经完成。
private void run() { ... // Prepare the main looper thread (this thread). // 将线程的优先级设为前台线程,不能后台取消, 启动 主线程的 Looper android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_FOREGROUND); android.os.Process.setCanSelfBackground(false); Looper.prepareMainLooper(); // Initialize native services. System.loadLibrary("android_servers"); nativeInit(); // Check whether we failed to shut down last time we tried. // This call may not return. performPendingShutdown(); // Initialize the system context. // 创建化系统的 context createSystemContext(); // Create the system service manager. // 创建 系统服务管理 mSystemServiceManager = new SystemServiceManager(mSystemContext); LocalServices.addService(SystemServiceManager.class, mSystemServiceManager); // Start services. // 启动服务 try { startBootstrapServices(); startCoreServices(); startOtherServices(); } catch (Throwable ex) { Slog.e("System", "******************************************"); Slog.e("System", "************ Failure starting system services", ex); throw ex; } // Loop forever. // 进入 Looper Looper.loop(); ... }
// 创建系统的 context private void createSystemContext() { ActivityThread activityThread = ActivityThread.systemMain(); mSystemContext = activityThread.getSystemContext(); mSystemContext.setTheme(android.R.style.Theme_DeviceDefault_Light_DarkActionBar); }
/** * 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() { // Wait for installd to finish starting up so that it has a chance to // create critical directories such as /data/user with the appropriate // permissions. We need this to complete before we initialize other services. Installer installer = mSystemServiceManager.startService(Installer.class); // Activity manager runs the show. // Activity 管理 mActivityManagerService = mSystemServiceManager.startService(ActivityManagerService.Lifecycle.class).getService(); mActivityManagerService.setSystemServiceManager(mSystemServiceManager); mActivityManagerService.setInstaller(installer); // Power manager needs to be started early because other services need it. // Native daemons may be watching for it to be registered so it must be ready // to handle incoming binder calls immediately (including being able to verify // the permissions for those calls). // 电池服务管理 mPowerManagerService = mSystemServiceManager.startService(PowerManagerService.class); // Now that the power manager has been started, let the activity manager // initialize power management features. mActivityManagerService.initPowerManagement(); // Display manager is needed to provide display metrics before package manager // starts up. // 显示服务 mDisplayManagerService = mSystemServiceManager.startService(DisplayManagerService.class); // We need the default display before we can initialize the package manager. mSystemServiceManager.startBootPhase(SystemService.PHASE_WAIT_FOR_DEFAULT_DISPLAY); ... // Start the package manager. // 启动 package 管理 mPackageManagerService = PackageManagerService.main(mSystemContext, installer, mFactoryTestMode != FactoryTest.FACTORY_TEST_OFF, mOnlyCore); mFirstBoot = mPackageManagerService.isFirstBoot(); mPackageManager = mSystemContext.getPackageManager(); ServiceManager.addService(Context.USER_SERVICE, UserManagerService.getInstance()); // Initialize attribute cache used to cache resources from packages. AttributeCache.init(mSystemContext); // Set up the Application instance for the system process and get started. // 启动 ActivityMangerService mActivityManagerService.setSystemProcess(); }
/** * Starts some essential services that are not tangled up in the bootstrap process. */ private void startCoreServices() { // Manages LEDs and display backlight. // 启动屏幕灯光 mSystemServiceManager.startService(LightsService.class); // Tracks the battery level. Requires LightService. // 启动电池服务 mSystemServiceManager.startService(BatteryService.class); // Tracks application usage stats. mSystemServiceManager.startService(UsageStatsService.class); mActivityManagerService.setUsageStatsManager(LocalServices.getService(UsageStatsManagerInternal.class)); // Update after UsageStatsService is available, needed before performBootDexOpt. mPackageManagerService.getUsageStatsIfNoPackageUsageInfo(); // Tracks whether the updatable WebView is in a ready state and watches for update installs. // 更新 WebView mSystemServiceManager.startService(WebViewUpdateService.class); }