Home > Uncategorized > The Android IPC system

The Android IPC system

The information below comes from a number of sources, including my own experiments with the Android IPC and some disparate internet sources.

The overall architecture of the Android IPC system is shown in the diagram below. It consists of four major blocks; one in kernel space, and the other three in user space. The dashed lines represent the logical RPC calls. The solid lines represent the actual data flow.

  • BinderDriver: This is the core of IPC system. It passes data between a ServiceProvider(s) and a ServiceUser(s). This kernel component is provided by Android.
  • ServiceProvider: Provides some kind of service. It parses the received RPC data from the BinderDriver and does the real work. Application developers will either make use of existing service providers (such as the Camera or AudioFlinger), or in some cases will write their own.
  • ServiceManager: This is a special singleton ServiceProvider that provides service manager services for other service providers. This component is provided by Android.
  • ServiceUser: This is the client. It remote calls the ServiceProvider by generating an RPC and sending it to the BinderDriver. Application developers typically write their own ServiceUser as part of their application.

Here is a typical flow of events for a fictitious MultServiceProvider (a service provider that multiplies two numbers for a client) and a MultServiceUser client which doesn’t know how to do multiplication (maybe because the numbers are quaternions) and needs to use the MultServiceProvider:

  1. ServiceManager runs first (at power-up) and registers a special node (node O) with the BinderDriver.
  2. The MultServiceProvider gets an IServiceManager proxy object for the special node O by calling the global defaultServiceManager() function.
  3. The MultServiceProvider then calls defaultServiceManager()->addService("Multiplier", new MultServiceProvider()) to add itself as a service provider and then waits in an infinite loop for someone to request its services. The addService RPC call is routed to the ServiceManager through the BinderDriver.
  4. The BinderDriver notices that the RPC is for the ServiceManager to add a new service, so besides routing the RPC to the ServiceManager it generates another node (let’s call it node M), for the new MultServiceProvider.
  5. The ServiceManager reads the data from the BinderDriver and processes the IServiceManager::addService RPC call.
  6. The MultServiceUser client process gets an IServiceManager proxy object for the special node O (again by using defaultServiceManager()).
  7. The client does an IServiceManager::getService("Multiplier") RPC call to get the MultServiceProvider. This call is routed to the ServiceManager through the BinderDriver.
  8. The ServiceManager reads the RPC data from the BinderDriver, processes the IServiceManager::getService request and returns back the node representing the MultServiceProvider.
  9. MultServiceUser calls MultServiceProvider::multiply(a, b). This call is routed to  the MultServiceProvider by the BinderDriver.
  10. The MultServiceProvider handles the MultServiceProvider::multiply RPC call and sends the product of the 2 numbers in a reply to the BinderDriver.
  11. The BinderDriver routes the reply back to the client.
  12. The client reads the data from the BinderDriver which contains the result of “a * b”.

In a future post I hope to discuss the whole architecture in more detail, with concrete code examples for how to use IBinder, IInterface, BBinder, BpInterface, BnInterface, etc… to create a ServiceProvider and a ServiceUser all in native C++ code on Android.

Categories: Uncategorized Tags:
  1. Harsha
    February 16th, 2011 at 12:08 | #1

    Nice article. HIghly simplified. Eagerly waiting for a sample that shows the same in the native code in android

  2. Riaz Ur Rahaman
    August 16th, 2011 at 23:38 | #2

    Nice article. Waiting for the continuation of this article.

  3. Ganesh Kumar Ramamoorthy
    October 19th, 2011 at 07:24 | #3

    Is it possible to provide the simple example to understand the concept….

    It will be highly helpful.. PLs help ….

    Thanks.,
    Ganesh kumar R.

  4. Oliver
    July 4th, 2012 at 00:44 | #4

    Nice article. BTW, did you write the article with concrete code examples for how to use IBinder, IInterface, BBinder, BpInterface, BnInterface, etc… to create a ServiceProvider and a ServiceUser all in native C++ code on Android. ?

    Thanks
    Oliver

  5. Gabriel Burca
    July 6th, 2012 at 00:38 | #5

    I just posted sample code to go with this post to GitHub: https://github.com/gburca/BinderDemo

  6. Artum
    July 8th, 2013 at 02:15 | #6

    Hi, it seems that this model of IPC is only the “one way”. (The client triggers the service)
    Is there any way add to async. call back from service to client? (For e.g. if my service is doing work that take some time, I would like that service will be able to call client once the work is done)

  7. Harvey
    January 13th, 2015 at 17:04 | #7

    Thanks for the post. BTW You use “Node O”, but the OH should be a ZERO, i.e. “Node 0”.

  8. aaa
    September 30th, 2015 at 04:59 | #8

    the best tutorial ever !

  9. vinaykumar
    October 23rd, 2017 at 04:03 | #9

    Hi,

    http://ebixio.com/blog/2012/07/07/using-android-ipc-binders-from-native-code/

    This website is not opening. could you please share the contents of above url.

    • Gabriel Burca
      January 21st, 2019 at 11:52 | #10

      Fixed the issue. The article is now available.

  1. No trackbacks yet.