posted 5 years ago in Dev Platform category by Ahn Joonseok
In our previous article Android, at a glance we have covered how Android has appeared, explained its software structure and what has to be paid attention when developing apps for this platform. In this article, I will go further and discuss about the Binder, the core subsystem of the complex and huge Android platform. I will explain about different components provided in Binder, how application processes communicate with system processes, and how data is shared.
As hardware performance of smartphones, a so-called "PC in my hand," improves, they are taking over the roles of PCs. With the trend, mobile OS has also been enhanced to support multitasking and process a variety of multimedia tasks using multi-core CPUs. Most mobile operating systems are based on PC operating systems. Keeping the architecture of the existing PC OS, the kernel gets partially modified to meet the requirements of the mobile environment, and necessary functions are added for optimization. Window Phone based on MS Windows OS, iOS based on Mac OS X, Google Android based on Linux Kernel, are the most popular in this category.
Smartphone - A PC in My Hand ... PC OS in My Hand!
If you have questions like, "What will be modified if the PC OS is embedded into the smartphone, and how and why?", the open source Android platform can provide your with a pretty good answer. The Android platform is the result of the latest software technology; it provides a lot of information via its developers community and allows users to directly modify its open source code.
Android has inherited powerful base systems from Linux Kernel such as the memory management, multitasking and file management. In addition, it has lowered the entry barrier by providing various development tools used to develop Java applications based on the Dalvik VM. Since the base systems are implemented in C++, they come with highly productive code. Actually, the Android developers say that one of Android's benefits compared to the other embedded systems is C++.
Thus, Android is a platform which embraces numerous technologies like Linux Kernel, C++, Java, Dalvik VM, etc.
Figure 1: Android Platform Structure.
Whatever, it is a Linux Process!
Android provides the process-unit component model. In other words, system services which provide an application or a camera feature created in Java on Eclipse, or system services which are responsible for screen display, all such Android components are eventually expressed as Linux processes.
Figure 2: Process Management of Linux Kernel.
Since Android runs based on Linux memory, process and file management, the system service is also isolated by Linux process for protection. The system process is written in Java code as well as C++ native code, so the services that run in Dalvik VM, such as Wi-Fi, Location and Activity Service, are included here.
To support mobile devices such as smartphones, all of the default system functions of Android are provided as the server process type. In other words, to use the functions such as SurfaceFlinger or AudioFlinger, a request should be made as a separate process that runs on the user mode.
Figure 3: Android System Services and Applications Run by Linux Process.
For example, when an application that I have created calls APIs to get the location information provided by the Android SDK, the application sends a request to the Linux process that provides the Location service internally and gets the response. Also, it interoperates with the camera service when a camera is used.
Figure 4: Application Process Which Calls Location and Camera Services.
As all system services are provided as a server process, a mechanism to send requests and responses to other processes is necessary. In Android it is called the Binder. Android uses functions provided by other processes through Binder.
Figure 5: Binder, Android's Communication Infrastructure between Processes.
Then, why has a new mechanism been developed, rather than using Inter Process Communication (IPC), such as sockets and pipes provided by Linux? It is because of performance. As we have discussed above, all system functions of Android are provided as a server process, so an optimized communication method between processes is required, and Binder is the result of this consideration. Binder refers to a kernel memory which is shared between all processes to minimize the overhead caused by memory copy. In addition, it provides the Remote Procedure Call (RPC) framework written in C++ for high productivity.
The system functions are provided as server processes where requests and responses are received through the Binder mechanism. "What are the benefits of the Android platform structure?" you would ask.
- Easy to expand or remove functions: It is easy to add a new system service or remove an existing function.
- Easy to port: Porting to a new processor requires few changes. A toolchain for porting is provided.
- Easy to test: Tests are limited by the component unit, so it is not necessary to test the entire services, and more strict tests are available.
- Support for distribution system: Process communication is based on the Binder, so it guarantees transparency in location between components.
Such benefits are very similar to those of Microkernel structure OS.
Let's discuss more about the Binder.
Binder that Binds All Functions
The Binder mechanism has started from a simple idea. "Let requests and responses be written in an area where all processes can share and let each process refer to the memory address." So, the kernel space is used for it.
Figure 6: Kernel Space Shared by All Processes.
A Binder Driver is implemented to use the kernel space. The role of the Binder driver is to convert the memory address that each process has mapped with the memory address of the kernel space for reference.
Figure 7: Binder Driver Configuration.
The Binder driver can be used by the
ioctl() system function, which is a standard method in Linux. The mechanism is called Binder IPC.
Figure 8: Transfer Structure of User Data between Processes through Binder Driver.
There is a C++ framework that processes the data transferred using the Binder IPC and makes Remote Procedure Call (RPC). It is used to create a system service. Then, a process can use functions provided by other processes as if their own.
Figure 9: C++ Framework that Translates Data Translated to RPC through Binder Driver.
In this short article I have tried to provide only simple information despite the vast technology behind Android. I hope it will be helpful to you in understanding the Android platform.
To wrap up, Binder is the base of the Android platform. We have reviewed three components of Binder: Driver, IPC and RPC. The Binder Driver will be included in the main version of the Linux kernel. The following summarizes the description of Android and Binder.
- Android is a PC OS based on Linux Kernel and is optimized for smartphones.
- Android has a process-unit component model and provides system functions as server processes. For a functional mesh-up of processes, it provides Binder.
- Android Binder uses the kernel memory reference to support communication between processes, optimized for mobile devices.
By Joonseok Ahn, Game Common Development Team, NHN Corporation.