· 這是《Android 系統源代碼情景分析》 一書中第 11 章, Zygote 和 System 進程的啓動過程,的讀書摘要;
一、Zygote 進程的過程
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 進程的啓動已經完成。
二、System 進程啓動過程
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);
}