Android系統(tǒng)有哪些進(jìn)程
在Linux系統(tǒng)啟動時,會讀取init.rc,里面配置了一些需要啟動的進(jìn)程善炫。注意:SystemServer進(jìn)程不在init.rc里,因為SystemServer進(jìn)程是由zygote啟動的兜挨。
如下所示:
service zygote /system/bin/app_process ...
service servicemanager /system/bin/servicemanager ...
service sufaceflinger /system/bin/suerfaceflinger ...
service media /system/bin/mediaserver ...
...
Zygote是怎么啟動的?
- init進(jìn)程fork出zygote進(jìn)程
- 啟動虛擬機(jī)眯分,注冊JNI函數(shù)
- 預(yù)加載系統(tǒng)資源(常用類拌汇、主題資源、JNI函數(shù)弊决、共享庫等)
- 啟動SystemServer
private static boolean startSystemServer(...){
String args[] = {
...
"com.android.server.SystemServer",
};
int pid = Zygote.forkSystemServer(...);
if(pid==0){
handleSystemServerProcess(parsedArgs);
}
return true;
}
......
void handleSystemServerProcess(Arguments parsedArgs){
RuntimeInit.zygoteInit(parsedArgs.targetSdkVersion,parsedArgs.remainingArgs,...);
}
......
void zygoteInit(String[] argv,...){
commonInit(); //常規(guī)初始化
nativeZygoteInit(); //調(diào)用native的onZygoteInit担猛,啟動binder。
applicationInit(argv,...); //調(diào)用Java類(SystemServer)的main函數(shù)
}
......
virtual void onZygoteInit(){
sp<ProcessState> proc = ProcessState::self();
proc->startThreadPool();
}
......
void applicationInit(...){
invokeStaticMain(args,...);//調(diào)用Java類(SystemServer)的main函數(shù)
}
......
SystemServer.java
public static void main(String[] args){
new SystemServer().run();
}
......
private void run(){
Looper.prepareMainLooper(); //創(chuàng)建主線程looper
System.loadLibrary("android_servers"); //加載native層的SystemServer代碼
createSystemContext(); //創(chuàng)建系統(tǒng)上下文
startBootstrapServices(); //啟動引導(dǎo)服務(wù)
startCoreServices(); //啟動核心服務(wù)
startOtherServices(); //啟動其他服務(wù)
Looper.loop(); //開啟循環(huán)
}
- 進(jìn)入Socket Loop
boolean runOnce(){
String[] args = readArgumentList();
int pid = Zygote.forkAndSpecialize();
if(pid==0){
//in child process
handleChildProc(args,...);
//should never get here, the child is expected to either throw ZygoteInit.MethodAndArgsCaller or exec().
return true;
}else{
return handleParentProc(pid,...);
}
}
系統(tǒng)服務(wù)是怎么發(fā)布丢氢,讓應(yīng)用程序可見?
void publisBinderService(String name,IBinder service){
publishBinderService(name,service,false);
}
void publishBinderService(String name,IBinder service,...){
ServiceManager.addService(name,service,allowIsolated);
}
系統(tǒng)服務(wù)運(yùn)行在什么線程傅联?
工作線程:AMS、PMS疚察、PackageManagerService
還有一些運(yùn)行在公用的線程中蒸走,如在DisplayThread,FgThread,IoThread,UiThread線程中運(yùn)行。
binder線程:應(yīng)用接收到binder調(diào)用后貌嫡,啟動線程比驻。
系統(tǒng)服務(wù)的互相依賴是如何解決的?
- 分批啟動
AMS, PMS, PKMS - 分階段啟動
階段1岛抄,階段2别惦,階段3,……
桌面的啟動
public void systemReady(final Runnable goingCallback){
……
//啟動桌面程序的Launcher類
startHomeActivityLocked(mCurrentUserId,"systemReady");
……
}
//Launcher(Activity)的onCreate方法中會創(chuàng)建一個LoaderTask
mLoaderTask = new LoaderTask(mApp.getContext(),loadFlags);
//在LoaderTask中會去向Package ManagerSevice查詢已安裝應(yīng)用夫椭,然后將應(yīng)用圖標(biāo)顯示在桌面掸掸,當(dāng)用戶點擊圖標(biāo)時,Launcher就會啟動應(yīng)用程序蹭秋。
mPm.queryIntentActivitiesAsUser();