Detailed explanation of socketpair two-way communication in Android

Many parts of Android involve inter process communication, such as input system. What will be involved in inter process communication?

1. Process: responsible for reading and distributing events 2. Application: responsible for processing input events

What two-way communication will be involved in the above two processes:

1. The process will send an input event. 2. The application will inform that the event has been processed or the app has been closed

Here you may wonder whether the binder system can realize the two-way communication mentioned above?

The answer is No. the binder is divided into server and client. Each time, the client actively sends a request, and the server replies after receiving the request. This disadvantage is that each request can only be initiated unilaterally, and the server cannot actively send data to the client. Naturally, this cannot be called two-way communication.

Therefore, a new method called "socket pair" is introduced here

App obtains two file handles through the socketpair call. Assuming that the two file handles are FD1 and fd2, these two files correspond to two buffers (send_buf and rcv_buf). When a process or thread writes to his send through FD1_ During buf, the socket in the kernel will send_ The data in buf is written to RCV of fd2_ In buf, another thread or process can read fd2 to get the data. On the contrary, the same is true.

However, it also has disadvantages: since the communication is realized by creating a file handle to access the handle, who can see this handle? Only the thread created by the current app or its child process can see these file handles, so it is only applicable to inter thread communication or inter process communication with kinship (parent-child process).

So what if you want to use socket pair to realize two-way communication between arbitrary processes? Suppose there are app1 and app2. If these two apps want to communicate with each other, then app2 needs to get app1's fd2. How can we get it? Fd2 can be transmitted to app2 through binder communication. Of course, it becomes fd3 in app2, so that any process app2 can communicate through socket pair. This article will not explain the implementation of binder for the time being

The procedure and use of "socketpair" are explained below:

The program is very simple. First, use socketpair to get two file handles and set the send / receive buffer, and then create another thread. In the thread, read and write data to the main thread through the file handle. Mian also performs the same operation to realize two-way communication.

Test verification:

Check whether these two threads exist:

We can also modify the program so that the application fork out a child process, and then let the parent-child process realize two-way communication through socket pair. It is relatively simple, which will not be discussed in detail here.

Because sockpair is only applicable to inter thread communication or has affinity, if you want to realize two-way communication between any two processes, you need to use the binder system to transfer FD to another process. Here is a brief process, as follows:

Transfer file handle using binder:

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>