Common communication mechanisms in Android and communication mechanisms in Linux

Source: Internet
Author: User

Handler

Handler is a messaging mechanism in the Android system that works against multithreaded scenarios. The message of a process is passed to the B thread for asynchronous message processing. In many cases, an action message in a worker thread that needs to update the UI is passed to the UI main thread, which implements the update UI operation.
Because the worker thread and the main thread are shared address spaces, that is, the handler instance object Mhandler is on a shared memory heap between threads, and the worker thread and the main thread use the object directly, just pay attention to the multi-threaded synchronization problem. The working system adds a message to its member variable MessageQueue through Mhandler, and the main thread is always in the loop, and when a new message is received, it is distributed to the appropriate Handlermessage method in accordance with a certain rule.
the core of handler-to-thread communication with processes is the shared memory space between threads, and different reserved addresses have different address spaces, and handler can not be used to achieve interprocess communication. 1, as shown in the handler message Communication architecture diagram.
In the figure, first handler sends a message through SendMessage () to the MessageQueue queue, Looper continuously extracts a message that reaches the trigger condition via Looper (), and sends the message to target for processing And then distributed through Displatchmessage () to handlermessage processing.
When you add a message to MessageQueue, the character is written to the pipeline, which wakes the loop thread back, and if there is no message in MessageQueue and the idle state is processed, the method in the Idlehandler interface is executed. Often used to do some clean-up work. About the priority of message distribution:

    • Message callback Distribution: Message Callback.run () has the highest precedence.
    • Handler Callback Distribution: Handler.mCallback.handleMesage (msg), priority.
    • Handler Default distribution: Handler.handlermessage (msg) with the lowest priority.
Binder Overview

Android in general each application for a process, and involves the communication between each application, namely interprocess communication, the most IPC mechanism used in Android is the binder mechanism. First we introduce the principle of IPC mechanism, 2 shows the IPC mechanism from the perspective of process. As you can see, each Android process can only run in its own virtual address space. For a 4G virtual address space, assume that the user space is 3G and the kernel space is 1G (modifiable). The data in the user space of the process is not shareable and can be shared in the memory space. The client process wants the server process to send information, which is done using the kernel space shareable mechanism.

    • From the IPC perspective: Binder is a cross-process communication method in Android that is unique to the Android system.
    • From Android driver layer: Binder can also be understood as a virtual physical device, the device driver is/dev/binder.
    • From Android native layer: Binder is a bridge that creates service manager as well as Bpbinder/bbinder model, everyone with binder drive.
    • From Android Frameworks layer: Binder is a bridge for various managers (Activitymanager, WindowManager) and corresponding xxxmanagerservice.
    • From the app layer: Binder is the medium for client and server-side process communication, and when Bindservicer, the server returns a Binder object that contains the service-side business call, through which the binder object The client can obtain services provided by the server (both ordinary and aidl-based services) or data.
Architecture

3, as binder at different levels of Android location and correlation diagram, is a C/s architecture communication mechanism. In the kernel layer, binder can be seen as a driver with the same driver architecture as other drivers. Native Layer ServiceManager will start a binder daemon, ServiceManager function is simple, including access to services, registration services, most of the peers through the presence of Bpbinder and Bbinder. The binder logic of the framework layer is based on the native layer architecture, and the core logic is given to the native layer for processing. However, it is necessary to note that the binder logic of the frameworks layer is based on the native layer architecture, and the core logic is given to the native layer for processing. However, it is important to note that the ServiceManager function of the frameworks layer ServiceManager and native layer does not correspond, and the final implementation is done by Binderproxy to the native layer.

Socket

Socket communication is also based on the C/S architecture, but it is simpler than binder. First, let's review the knowledge of the TCP/IP protocol. TCP/IP protocol is a four-tier architecture: Application layer, Transport layer, network layer, network interface layer. There are also TCP and UDP two protocols in the transport layer. A socket is an abstraction between the application layer and the transport layer in the work and TCP/IP protocol. The Android system is also divided into stream sockets and datagram sockets. wherein the stream socket uses the TCP protocol as its end-to-end protocol, providing a trustworthy byte stream is the service; The datagram sockets use the UDP protocol to provide the data packaging and sending service. Scenarios for using socket communication in an Android system include:

    • Zygote:fork new process, System_server sends fork to Zygote the new process request is to use socket communication.
    • INSTALLD: User installs the App daemon, the PMS installs the application to send the socket communication to INSTALLD to complete the installation process.
    • ADBD: Used to service ADB operations.
    • LOGCATD: For service Logcat operation
    • Vold: The storage class daemon, used to service event handling for storage devices such as USD, SDcard, and so on. (This process communication has been modified to binder implementation in Android9.0)
Several other communication mechanism pipelines

Linux supports one of the first UNIX IPC mechanisms, characterized by:

    • The pipe is half-duplex, the data can only flow in one direction, it is recommended to use two pipes when both sides need to communicate.
    • Can only be used between parent-child processes or sibling processes.
    • Separate form a separate file system: The pipeline is a file for the process at both ends of the pipe, but it is not an ordinary file, it is not a file system, but a separate file system, and each time it does not read the data from the buffer.
    • Data readout and writing; a process that writes to the pipeline is read out by the process at the other end of the pipeline. The content that is written is added to the end of the pipeline buffer every time, and the data is read from the buffer header each time.
Message Queuing

Message Queuing provides a way to send a block of data from one process to another. Each block of data is considered to contain a type, and the receiving process can independently accept data structures that contain different types. Avoid problems with the synchronization and blocking of clear pipelines by sending messages. Message Queuing has a maximum length limit for messages sent.

Shared memory

Shared memory, as the name implies, allows two unrelated processes to access the same logical memory. Shared memory is a very effective way to share and pass between two running processes. Memory that is shared between different processes is usually scheduled as the same piece of physical memory. Processes can talk about the same section of shared memory linked to their own address space. All processes have access to the addresses in the shared memory.
It is important to note that the shared memory does not use the synchronization mechanism, that is, when process a writes to the shared memory process, the shared memory is not locked, and process B can still read the data in the shared memory. So often we need to use other mechanisms to synchronize access to shared memory.

Signal Volume

To prevent multiple processes from synchronizing access to a shared resource and causing a series of problems, we need a method that can typically generate tokens to authorize, at any moment, only a critical area that points to thread access code. A critical region is a code that performs data updates that requires exclusive execution. Semaphores can provide such an access mechanism, which means that the semaphore is used to coordinate the process's access to resource sharing.
A semaphore is a special variable whose access is atomic and allows only the process to wait and send information to it. The simplest semaphore is a variable of only 0 and 1. This is also the most common form of semaphores. This is the binary signal volume . The amount of a semaphore that can take multiple positive integers is called a generic semaphore .

Signal

The signal is an event that occurs when UNIX and Linux systems respond to certain conditions, and the process that receives the signal takes some action in response. usually the signal is generated by an error . But they can also be a form of inter-process communication or modification behavior that is explicitly sent to another process by one process. signal generation is called generating and receiving is called signal capture .

Comparison of several IPC mechanisms in Android
name Advantages Disadvantages Applicable Scenarios
Bundle Simple to use Only data types supported by bundles can be transferred Inter-process communication between components
File sharing Simple to use Not suitable for concurrent scenarios, and cannot do instant communication between processes No concurrent access scenarios, exchanging simple data in real-time scenarios
Aidl Powerful, support for one-to-many concurrent communication, support real-time communication Use complex, need to handle thread synchronization One-to-many communications with RPC requirements
Message Functions in general, support one-to-many serial communication, support real-time communication Cannot handle high concurrency scenarios, RPC is not supported, data is transmitted through the message process, so only data types supported by the bundle can be transferred Low concurrent one-to-many instant communication, no RPC requirements, or no RPC requirements to return results
ContentProvider Powerful in data access, supports one-to-many concurrent data sharing, and extends other operations through the call method Can be understood as constrained aidl, which primarily provides CRUD operations for data sources Data sharing between multiple processes
Socket Powerful, can use network to transmit byte stream, support one-to-many concurrent real-time communication Implementation details are a bit cumbersome and do not support direct RPC Network data exchange, or a small number of inter-process exchanges

Common communication mechanisms in Android and communication mechanisms in Linux

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.