Detailed explanation of the communication between Linux and android process and android Binder mechanism

There are many ways to communicate between processes, and different ways are suitable for different scenarios.


Five different forms of IPC

1. Message delivery (pipe, FIFO and message queue)

2. Synchronization (mutexes, condition variables, read-write locks, file and record locks, semaphores)

3. Shared memory (anonymous and named)

4. Remote procedure call (Solaris door and Sun RPC and Android Binder)

5. Programs that communicate through computer networks, using TCP/IP protocol suite API (inter-network IPC)

1. Pipe

Features: One-way, with a "read" end and a "write" end, with limited capacity.

2.Signal real-time semaphore


3.Trace

The above three are only suitable for the communication between the parent process and the child thread or sibling process

4.S ystem V's message queue (Message )


5. System V shared memory (ShareMessage)

Steps to implement shared memory

1. Create a shared memory area

2. Map memory area

3. Access to the shared memory area

4. Inter-process communication

5. Cancel the memory mapping area

6. Delete the shared memory area

6. Semaphore of System V

Semaphore and PV primitives were invented by Dijkstra, and they are also one of the most widely used mutual exclusion methods.

The execution process of P operation

  • Semaphore S is decremented by 1
  • If S is still >=0 at this time. Explain that the shared resource is allowed to access at this time, so the caller returns directly, and then starts to operate the shared resource
  • Otherwise, it is necessary to wait for others to actively release resources. In this case, the caller will be added to the waiting sequence until it is subsequently awakened.
  • When someone releases the shared resource, the related (depending on the situation) object in the waiting queue will be awakened, and the object has the right to access the resource.
V operation execution process
  • Semaphore S increases by 1
  • At this time, if S>0, it means that there is no waiter who wants to access the resource, so return directly
  • Otherwise, the V operation must wake up the relevant objects in the waiting queue, which corresponds to the last step in the P operation.

7. Socket IPC communication based on TCP/IP


8. Sync

There are two types of synchronization defined by the Posix thread standard: Mutex, read-write lock and condition variable.

Mutex is interpreted as a mutual exclusion in Chinese.

The difference between it and Semaphore:

If multiple variables are allowed to be accessed at the same time, it is called Counting Semaphores; for only allowing values ​​of 0 or 1, it is called Binary Semaphore. The latter has the same properties as Mutex.

Mutex usually has shared control over an exclusive resource, either the resource is occupied (locked) or can be accessed (unlocked).

  There is no essential difference between the two

Guan Cheng: It is actually an extension and extension of Semaphore, a synchronization method with simpler control

9.Unix Domain Socket

UDS is specifically proposed for inter-process communication within a single machine, and is sometimes called an IPC socket. The actual implementation mechanism does not depend on the TCP/IP protocol family.

  1. The server monitors IPC requests
  2. The client initiates an IPC application
  3. Both parties successfully established an IPC connection
  4. The client sends data to the server to prove that the IPC communication is valid


10. What is the difference between System V message queue, semaphore, and shared memory and Posix message queue, semaphore, and shared memory?


11.Sync mechanism in android

Mutex

Condition: is the implementation class of condition variables in the android system,

Barrier: A model based on Mutex and Condition at the same time,

Android binder 

Socket as a common interface, transmission efficiency is low , a large overhead, low-speed communication is mainly used in the inter-process communication across the network and the native process.

Message queues and pipelines adopt a store -and- forward approach, that is, data is first copied from the sender's buffer to the buffer opened by the kernel, and then from the kernel buffer to the receiver's buffer, at least twice.

Although the shared memory does not need to be copied, the control is complicated and difficult to use.

Binder only needs to be copied once

Traditional IPC does not have any security measures and completely relies on upper-layer protocols to ensure it.

At this time, android needs to establish a new IPC mechanism to meet the communication requirements of the system

Binder provides remote call (Remote Procedure Cails) feature that allows Java or C ++ object instance to migrate from one process to another, transparently implementation calls cross-process oriented to like thinking the introduction of inter-process communication turn into by for a number Binder of object reference tone with the pair of object -side method , and its unique Laid lies Binder of object it is that one can cross process references the object , its entity is located in a process, and a reference to it, but All over the various processes of the system. Most lure people is this quote and java in references as both can be strong class type, can also be a weak class type, andCan be transferred from one process to another process, so that everyone can access the same Server , it is like the one pair of like or references to other assign as a reference. Binder mold paste the course of the border , short of inter-process communication process, the whole systems seem to run on the same face of the elephant in the program. All kinds of Binder of elephant and dotted with references seemed stuck to access various applications of glue , which is Binder in Ying Wen in the original meaning 

Main components: Server, Client, ServiceManager and Binder driver

1. Binder driver:

It is in the kernel is the core of communications, responsible for between processes Binder establish communications, Binder transfer between processes of delivery , Binder reference count management, data packet transfer process between the delivery and cross each other and a series of underlying support. 

2.ServiceManager(SMgr)

And DNS Similarly , SMgr action is character-shaped formula Binder name turn of to Client in of the Binder references that have Client can be by Binder name eligible obtained on Server in Binder referenced entity. 

3.Server

The Server creates a Binder entity, the Client sends data to the Server after obtaining the entity reference, and the Server makes corresponding processing after receiving the data.

4.Client

Server to SMgr registered a Binder entities and their names later, Client you can name obtained was that Binder 's references . 

 The process of completing a communication

1. The client process calls the Stub interface

2. Stub is packaged according to the requirements of the system

3. The kernel completes the specific interaction with the server, and it is responsible for sending the client's data to the server kernel

4. The server Stub unpacks and calls the process matching the data packet

5. Process execution operation

6. The server returns to the client in the reverse process of the above steps


Guess you like

Origin blog.csdn.net/u013741019/article/details/48448801