Full text:Tryenough.com/android-ipc…

Android development process communication, all Android applications rely on binder as the underlying communication mechanism. Linux doesn’t have a binder mechanism for interprocess communication, so why create it in Android? With that question in mind, read on.

Process related concepts in Linux


Linux divides system memory into user space and kernel space:

User space: Normal applications run in user space and cannot use specific system functions, access hardware directly, or access kernel space directly. Kernel space: The core software of the system runs at a high level of privilege. It resides in the protected memory space and has full access to the hardware device.

User programs can only run in user space, and the only way that user space can access kernel space is through system calls.

Linux user programs and processes

In Linux, all user programs execute as processes. Parent-child relationships exist between processes to represent multiple synchronization tasks initiated by the same user program.

All processes form a tree structure with init as the root, because the Linux kernel does not provide system calls to create new processes directly. All the remaining processes are created by the init process through the fork mechanism. The new process is duplicated by the old process, which is called fork. Fork is a system call.

Each process has its own space allocated in memory (memory space, including stack, heap, global static area, text constant area, program code area). Processes isolate resources from each other:

  • Process isolation is to protect the execution of processes without interfering with each other.
  • Process isolation uses A virtual address space, that is, the virtual address of process A is different from that of process B, which prevents process A from writing data to process B.

Full text:Tryenough.com/android-ipc…

Process A and process B cannot communicate directly because of process isolation.

However, in development, it is inevitable to encounter process communication (for example, when an application fails to pass data between processes, etc.).

Interprocess Communication (IPC)

Although different processes cannot communicate directly in user space, they share a kernel space. Obviously, when a user process wants to communicate with another user process, it can do so through kernel space.

There are several common ways to communicate between processes in Linux:

  • 1. Pipes and named pipes: Pipes can be used for communication between related processes. Named pipes overcome the limitation that pipes do not have names and therefore allow communication between unrelated processes in addition to the functions of pipes.

  • 2. Signal: The Signal is a complex communication method used to notify the receiving process of the occurrence of a certain event. In addition to inter-process communication, the process can also send signals to the process itself. In addition to supporting sigAL, Linux also supports SIGAction, a signal function whose semantics conform to Posix.1 standard (in fact, this function is based on BSD, BSD in order to achieve a reliable signal mechanism, and can unify the external interface, Reimplementing signal with sigAction);

  • 3. Message queue: A Message queue is a link table of messages, including Posix Message queue system V Message queue. A process with sufficient permissions can add messages to the queue, and a process with read permissions can read messages from the queue. Message queue overcomes the disadvantages of signal carrying less information, pipe carrying only unformatted byte stream and limited buffer size.

  • 4. Shared memory: Enables multiple processes to access the same memory space, which is the fastest form of IPC available. It is designed for the low efficiency of other communication mechanisms. Often used in conjunction with other communication mechanisms such as semaphores to achieve synchronization and mutual exclusion between processes.

  • 5. Semaphore: Mainly used as a means of synchronization between processes and different threads of the same process.

  • 6. Socket: a more general interprocess communication mechanism, which can be used for interprocess communication between different machines. Originally developed by the BSD branch of Unix systems, it is now generally portable to other Unix-like systems: Both Linux and System V variants support sockets.

Full text:Tryenough.com/android-ipc…

The above six are all kernel applications:

Process A initiates A request to the program in the kernel, which in turn forwards the request to process B, thus achieving interprocess communication.

Binder in Android was born

Android system through Linux dynamic loadable kernel module, add a kernel module running in the kernel space, the user process through this module as a bridge, you can complete communication. That’s what we’ll cover later: Binder drivers.

Google implemented the interprocess communication protocol by adding a new kernel module, then used binder drivers to call the new kernel module to provide interfaces for the upper layer applications, and finally encapsulated the interface in the framework layer to provide Java API call interfaces.

Binder is an interprocess communication protocol for Android.

  • Binder delivers high transmission efficiency and operability on mobile devices.
  • The Binder mechanism is a good implementation of the client-server architecture.
  • Binder mechanisms provide high security.
    • The traditional method does not make strict authentication for the identity of the communication parties, only to set up on the upper protocol;
    • For example, if the Socket communication IP address is manually filled by the client, it can be forged.
    • The Binder mechanism improves security by supporting identity verification of communication parties from the protocol itself.

Thanks for reading, and this series will continue to write about specific uses of process communication in Android. Welcome to come and watch.

Full text:Tryenough.com/android-ipc…

Recommended reading

Android development art exploration