综上所述,系统层次关于启动最核心的部分是zygote(即app_process)和system server,zygote它负责最基本的虚拟机的建立,以支持各个应用程序的启动,而system server用于管理android后台服务,启动步骤及顺序。 10. 参考 http://blog.csdn.net/basonjiang_sz/category/648399.aspx Android 启动过程详解 Android从Linux系统启动有4个步骤; (1) init进程启动 (2) Native服务启动 (3) System Server,Android服务启动 (4) Home启动 总体启动框架图如: 第一步:initial进程(system/core/init) init进程,它是一个由内核启动的用户级进程。内核自行启动(已经被载入内存,开始运行,并已初始化所有的设备驱动程序和数据结构等)之后,就通过启动一个用户级程序init的方式,完成引导进程。init始终是第一个进程. Init.rc Init.marvell.rc Init进程一起来就根据init.rc和init.xxx.rc脚本文件建立了几个基本的服务: servicemanamger zygote 。。。 最后Init并不退出,而是担当起property service的功能。 1.1脚本文件 init@System/Core/Init Init.c: parse_config_file(Init.rc) @parse_config_file(Init.marvel.rc) 解析脚本文件:Init.rc和Init.xxxx.rc(硬件平台相关) Init.rc是Android自己规定的初始化脚本(Android Init Language, System/Core/Init/readme.txt) 该脚本包含四个类型的声明: Actions Commands Services Options. 1.2 服务启动机制 我们来看看Init是这样解析.rc文件开启服务的。 (1)打开.rc文件,解析文件内容@ system/core/init/init.c 将service信息放置到service_list中。@ system/core/init parser.c (2)restart_service()@ system/core/init/init.c service_start execve(…).建立service进程。 第二步 Zygote Servicemanager和zygote进程就奠定了Android的基础。Zygote这个进程起来才会建立起真正的Android运行空间,初始化建立的Service都是Navtive service.在.rc脚本文件中zygote的描述: service zygote /system/bin/app_process -Xzygote /system/bin --zygote --start-system-server 所以Zygote从main(…)@frameworks/base/cmds/app_main.cpp开始。 (1) main(…)@frameworks/base/cmds/app_main.cpp 建立Java Runtime runtime.start("com.android.internal.os.ZygoteInit", startSystemServer); (2) runtime.start@AndroidRuntime.cpp 建立虚拟机 运行:com.android.internal.os.ZygoteInit:main函数。 (3)main()@com.android.internal.os.ZygoteInit//正真的Zygote。 registerZygoteSocket();//登记Listen端口 startSystemServer(); 进入Zygote服务框架。 经过这几个步骤,Zygote就建立好了,利用Socket通讯,接收ActivityManangerService的请求,Fork应用程序。 第三步 System Server startSystemServer@com.android.internal.os.ZygoteInit在Zygote上fork了一个进程: com.android.server.SystemServer.于是SystemServer@(SystemServer.java)就建立了。Android的所有服务循环框架都是建立SystemServer@(SystemServer.java)上。在SystemServer.java中看不到循环结构,只是可以看到建立了init2的实现函数,建立了一大堆服务,并AddService到service Manager。 main() @ com/android/server/SystemServer { init1(); } Init1()是在Native空间实现的(com_andoird_server_systemServer.cpp)。我们一看这个函数就知道了,init1->system_init() @System_init.cpp 在system_init()我们看到了循环闭合管理框架。 { Call "com/android/server/SystemServer", "init2" ….. ProcessState::self()->startThreadPool(); IPCThreadState::self()->joinThreadPool(); } init2()@SystemServer.java中建立了Android中所有要用到的服务。 这个init2()建立了一个线程,来New Service和AddService来建立服务 第三步 Home启动 在ServerThread@SystemServer.java后半段,我们可以看到系统在启动完所有的Android服务后,做了这样一些动作: (1) 使用xxx.systemReady()通知各个服务,系统已经就绪。 (2) 特别对于ActivityManagerService.systemReady(回调) Widget.wallpaper,imm(输入法)等ready通知。 Home就是在ActivityManagerService.systemReady()通知的过程中建立的。下面是ActivityManagerService.systemReady()的伪代码: systemReady()@ActivityManagerService.java resumeTopActivityLocked() startHomeActivityLocked();//如果是第一个则启动HomeActivity。 startActivityLocked(。。。)CATEGORY_HOME Android Booting: init is the first process after kernel started. The corresponding source code lies in: device/system/init. It does the following tasks step by step: 1.Initialize log system. 2.Parse /init.rc and /init.%hardware%.rc. 3. Execute early-init action in the two files parsed in step 2. 4. Device specific initialize. For example, make all device node in /dev and download firmwares. 5. Initialize property system. Actually the property system is working as a share memory. Logically it looks like a registry under Windows system. 6. Execute init action in the two files parsed in step 2. 7. Start property service. 8. Execute early-boot and boot actions in the two files parsed in step 2. 9. Execute property action in the two files parsed in step 2. 10. Enter into an indefinite loop to wait for device/property set/child process exit events. For example, if an SD card is plugined, init will receive a device add event, so it can make node for the device. Most of the important process is forked in init, so if any of them crashed, init will receive a SIGCHLD then translate it into a child process exit event, so in the loop init can handle the process exit event and execute the commands defined in *.rc(it will run command onrestart). The .rc file is a script file defined by Android. The default is device/system/rootdir/init.rc. We can take a loot at the file format(device/system/init/readme.txt is a good overall introduction of the script). Basically the script file contains actions and services. Actions Actions are named sequences of commands. Actions have a trigger which is used to determine when the action should occur. When an event occurs which matches an action's trigger, that action is added to the tail of a to-be-executed queue (unless it is already on the queue). Each action in the queue is dequeued in sequence and each command in that action is executed in sequence. Init handles other activities (device creation/destruction, property setting, process restarting) "between" the execution of the commands in activities. Actions take the form of: on ... Services Services are programs which init launches and (optionally) restarts when they exit. Services take the form of: service [ ]*