The Android operating system is implemented based on Linux, but the core value of Android is not Linux. Therefore, the Android kernel does not refer to Linux. This book is not a book about Linux. This is like Apple's operating system iOS is implemented based on Unix, but the core value of iOS is not Unix.
So what is the Android kernel and its core value?
We have heard that the most frequently used words in the Android kernel should be "Android Framework" and "Dalvik Virtual Machine". Then, from the perspective of the internal operating mechanism, what role does the two core parts play, how can we work together? After understanding this, we also understand the core value of Android, that is, the Android kernel.
From the process perspective, the Android runtime environment is shown in:
After the Linux kernel is started, the status of the system is basically the same as that of a common Linux system. rc file, you can specify the program to be executed after the kernel is started, and the program started after this is the difference between the Android system and the common Linux application system.
Init. an important process started in rc is called the zygote process. As shown in the red border in the figure, the English word zygote indicates "fertilized eggs". This book calls it a "seed process ", from the process perspective, the seed process is just a Linux process. It is the same level as the process produced by a C program that only contains the main () function, as shown in.
The program running in the seed process is basically the essence of the Android kernel, which mainly completes two tasks. The first thing is to load a piece of program code, which is written in C language. This Code serves only to execute the bytecode compiled by the Java compiler, this code is the legendary Java virtual machine, known as the Dalvik Virtual Machine in Android.
The second thing must be based on the first thing, that is, when the Dalvik virtual machine code Initialization is complete, it is executed from the main () function named ZygoteInit. java class. How does the Dalvik Virtual Machine know which Jar package the Java class ZygoteInit belongs? In fact, the directory location information of this Jar package is exactly in init. the configuration in rc is not directly specified, but uses a flag. When the flag is "zygote, the Dalvik Virtual Machine obtains the Jar package of the ZygoteInit class from the hard-coded string. The Jar package is exactly the framework. jar.
The next thing is simple and complex. The so-called simplicity means that the things done by the main () function in the ZygoteInit class have little to do with Linux itself. Theoretically, you can implement any simple function in this main () function; the so-called complexity refers to the fact that the main () function has just started the core functions of Android.
In the main () function of the ZygoteInit class, first load some class files. These classes will be used as the classes shared by all other Apk programs. Then, a Socket server will be created, this server will be used to start a new process through Socket.
The reason this process is called a "Seed" process is that when its internal Socket server receives a request to start a new Apk process, it will use a Linux system to call folk () the function copies a new process from itself. The new process and Zygote process share the loaded classes in the framework. jar.
The concept of the Android kernel is analyzed from the process perspective. The following describes the meaning of the Android kernel from the perspective of the graphic user interface. As we all know, the Linux Kernel provides the following functions:
L Process Scheduling
L Memory Management
L Driver Model
These functions have nothing to do with the user interface. Generally, the kernel only outputs some status information through the USB interface or RS232 serial port, which is far from enough for the window operating system, the most important thing is that the operating system should provide a set of user interface subsystems, including how to create and delete windows and how users interact with windows. At the same time, a set of interface libraries should be provided, this allows third-party developers to quickly develop some window applications based on the interface library. This is the core content of Android. Most of the Code for completing these functions is in the framework. jar file. The Dalvik virtual machine is just an environment for executing these function code.
Therefore, if you consider a graphical user interface, the internal relationship of an Android application is shown in:
First, the internal modules can be divided into three parts:
The first part is the Linux driver. This module abstracts the standard Linux driver into the hardware interface defined by Android, thus maintaining the independence of the Android kernel code. That is, when the Linux driver changes, you only need to modify the adaptation layer, you do not need to modify the Android kernel code. The driver is also called the hardware abstraction layer (Harware Abstarction Layout ).
The second part is the Framework server, which processes the input messages and transmits the messages to the window management service thread (WmS). The WmS is based on the hierarchies of all current application windows, decide the window to which the message should be distributed. In addition to WmS, WmS also contains a Core Thread component, namely, the Activity management thread (AmS). Activity is a program fragment defined in Android, this segment can be understood as "programs that can be Dynamically Loaded". That is, when the application thread starts, different activities can be loaded based on user operations.
The third part is the Apk application client. The client of each Apk application is executed from the main () function in the ActivityThread class, which is identical to a common Java program. After ActivityThread is started, will report to AmS "I have started, please tell me which Activity fragment to execute", AmS will communicate through Inter
Process Communication) to notify ActivityThread of the Activity to be loaded, so that ActivityThread executes the specified Activity, in the Activity, various functions in the Framework are called to add and delete windows.
Android Kernel Analysis is a thorough analysis of the above process.