Thursday, 4 February 2016

Android Booting process


Android as we know uses a Linux kernel underneath and as all operating systems it has a boot sequence. Android has following stack -

You can see complete details in one of my previous posts - 

In this process we will see how booting happens in Android.

Android Boot Sequence

To represent the entire process in a picture it would be something like below - 

  1. When you power on your Android device Boot ROM which is hardwired code at a predefine location in your ROM starts executing. This loads your Bootloader code in your RAM and starts executing.
  2. Bootloader is a process that starts before Android OS is loaded. Bootloader code itself is not a part of Android operating system. This code will be customized by the OEMs to put in their restrictions. This program sets up necessary things to run the kernel like memory, clock, network etc.
  3. Next kernel is launched. As the kernel launches, is starts to setup cache, protected memory, scheduling and loads drivers. System can now use virtual memory and launch user space processed. When the kernel finishes the system setup , it looks for “init” in the system files and launch it as the initial user space process.
  4.  init process is the parent process of all processed. This can be found at location  -
    • <android source>/system/core/init
    It is the 1st user process that starts. It has two resposibilities -.
    1. Mounts directories like /sys , /dev or /proc and
    2. Runs init.rc file located at - <android source>/system/core/rootdir/init.rc
    This is a script that describes the system services, file system and other parameters that need to be set up. If you refer above picture init process initializes zygote, runtime and daemon processed. At this point you should see the Android logo on your screen. 
  5. As we know each Java process runs in a separate JVM. However in an handheld system like that of Android both memory footprint and startup time must be considered. So JVM with some customizations needed for Android is called Dalvik VM that runs Android applications.  Also initi process which starts up Zygote process preloads and initializes core libraries needed for the VM. For each new app a new VM is forked from zygote and app is run in it's sandboxed environment. Zygote provides pre warmed up VM instance to the app to run thereby reducing the startup time.
  6. After complete above steps, runtime request Zygote to launch system servers. System Servers are written in native and java both. The system server is the first java component to run in the system. It will start system services like Power Manager, Activity Manger, Telephony Service etc.
  7. Once System Services up and running in memory, Android has completed booting process, At this time “ACTION_BOOT_COMPLETED” standard broadcast action will fire.
 When an app starts new VM is forked from zygote and app is started in it (sanboxed).

NOTE : When zygote does a fork on receiving a command it uses copy-on-write technique. Memory is copied only when the new process tries to modify it.

Also the core libraries that zygote loads on startup are read only and cannot be modified. So they are not copied over but shared with new forked processes.

All of these led to quick startup and less memory footprint.

NOTE : Zygote isn't really bound up with Dalvik, it's just an init process. Zygote is the method Android uses to start apps. Rather than having to start each new process from scratch, loading the whole system and the Android framework afresh each time you want to start an app, it does that process once, and then stops at that point, before Zygote has done anything app-specific. Then, when you want to start an app, the Zygote process forks, and the child process continues where it left off, loading the app itself into the VM.

Related Links

No comments:

Post a Comment

t> UA-39527780-1 back to top