android 定时server,Android系统启动(四)-SystemServer篇

从上篇的Zygote学习中,了解到Zygote进程最后启动了SyetemServer进程,那么这篇文章就来分析下SystemServer。

一、SystemServer启动流程

c6f464457f4c?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

这个过程在zygote进程中,主要包括这么几个内容:

1.1 Zygote fork SystemServer

1.2 当system_server进程创建失败时,将会重启zygote进程:

当kill system_server进程后,只重启zygote64和system_server,不重启zygote;

当kill zygote64进程后,只重启zygote64和system_server,也不重启zygote;

当kill zygote进程,则重启zygote、zygote64以及system_server。

1.3 Zygote通过信号处理函数SigChldHandler监听所有子进程的存亡

static void SigChldHandler(int /*signal_number*/) {

pid_t pid;

int status;

//Zygote监听所有子进程的存亡

while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {

//某一个子进程挂了

if (WIFSIGNALED(status)) {

if (WTERMSIG(status) != SIGKILL) {

ALOGI("Process %d exited due to signal (%d)", pid, WTERMSIG(status));

}

}

//如果挂掉的是SystemServer

if (pid == gSystemServerPid) {

ALOGE("Exit zygote because system server (%d) has terminated", pid);

kill(getpid(), SIGKILL); //Zygote自杀

}

}

}

二、SystemServer启动后的工作

c6f464457f4c?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

关键流程说明:

ZygoteInit.startSystemServer()

fork 子进程 system_server,进入 system_server 进程。

ZygoteInit.handleSystemServerProcess()

设置当前进程名为“system_server”,创建 PathClassLoader 类加载器。

RuntimeInit.zygoteInit()

重定向 log 输出,通用的初始化(设置默认异常捕捉方法,时区等),初始化 Zygote -> nativeZygoteInit()。

app_main::onZygoteInit()

proc->startThreadPool(); 启动Binder线程池,这样就可以与其他进程进行通信。

ZygoteInit.main()

开启 DDMS 功能,preload() 加载资源,预加载 OpenGL,调用 SystemServer.main() 方法。

SystemServer.main()

先初始化 SystemServer 对象,再调用对象的 run() 方法。

SystemServer.run()

createSystemContext

startBootstrapServices();

startCoreServices();

startOtherServices();

Looper.loop();

三、系统服务

startBootstrapServices:引导服务

服务

作用

Installer

系统安装apk时的一个服务类,启动完成Installer服务之后才能启动其他的系统服务

ActivityManagerService

负责四大组件的启动、切换、调度。

PowerManagerService

计算系统中和Power相关的计算,然后决策系统应该如何反应

LightsService

管理和显示背光LED

DisplayManagerService

用来管理所有显示设备

UserManagerService

多用户模式管理

SensorService

为系统提供各种感应器服务

PackageManagerService

用来对apk进行安装、解析、删除、卸载等等操作

startCoreServices:核心服务

服务

作用

BatteryService

管理电池相关的服务

UsageStatsService

收集用户使用每一个APP的频率、使用时常

WebViewUpdateService

WebView更新服务

startOtherServices:其他服务

服务

作用

CameraService

摄像头相关服务

AlarmManagerService

全局定时器管理服务

InputManagerService

管理输入事件

WindowManagerService

窗口管理服务

VrManagerService

VR模式管理服务

BluetoothService

蓝牙管理服务

NotificationManagerService

通知管理服务

DeviceStorageMonitorService

存储相关管理服务

LocationManagerService

定位管理服务

AudioService

音频相关管理服务

….

四、SystemServiceManager分析

从名字就很明显知道SystemServiceManager是系统服务管理类,简单了解两个方法:

4.1 startService函数

很简单,就是通过反射创建服务对象,并调用其onStart( )方法启动。

4.2 startBootPhase函数

Systemserver会将设备启动分成几个阶段,每个阶段service通过继承SytemService实现的onBootPhase方法来做一些对应的操作,相当于一个回调。

c6f464457f4c?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值