What Are the Ipc Mechanisms Available in the Android Os

What are the IPC mechanisms available in the Android OS?

There are three types of IPC mechanism in Android:

  1. Intents (along with Bundles)
  2. Binders
  3. ASHMEM (Anonymous Shared Memory) - The main difference between Linux shared memory and this shared memory is, in Linux other processes can't free the shared memory but here if other processes require memory this memory can be freed by Android OS.

IPC mechanisms concepts

IPC is inter-process communication mechanisms in OS is large discussion concept so, I think here we can't cover all this,

Some Low Level stuff:

The IPC mechanism discussed here is at the lowest level—all other inter-CPU IPC mechanisms use it as the base. For example, a TCP/IP connection through the ARM11 processor to another processor ends up going through this IPC mechanism. Diagnostic messages are another example of messages that rely on this low-level IPC.

The IPC mechanism is implemented with two sides—a "client side," which faces the kernel and provides a callback-based style of interface, and a "CPU side," which provides the interface to the other CPUs.

  • The CPU side is implemented as a shared-memory interface, with
    interrupts and a "doorbell" mechanism. At the highest level, to send
    messages from the ARM11 to another CPU, the message content is placed
    in a buffer in shared memory and a hardware port is tickled to
    indicate to the other CPU that data is available.

  • In the reverse direction, the data is placed into shared memory by
    the other CPU and a hardware interrupt is triggered on the ARM11.
    This hardware interrupt causes the ARM11 to examine the shared
    memory's buffer, retrieve the message and route it to the client.

But to more specific to Android:

IPC in android, It describes the mechanism how different types of android components are communicated.

Android implements a few key tools used to communicate with or coordinate between programs securely. These mechanisms give Android applications the ability to run processes in the background, offer services consumed by other applications, safely share relational data, start other programs, and reuse components from other applications safely.

  • Much of the interprocess communication (IPC) that occurs on Android
    is done through the passing around of a data structures called
    Intents. These are collections of information that have a few
    expected properties the system can use to help figure out where to
    send an Intent if the developer wasn’t explicit. The Action property
    expresses what the Intent is for (the Intent.ACTION_VIEW action
    indicates that the data is to be displayed to the user, for example).
    The data property is an optional URI and could point to a file,
    contact, web page, phone number, and so on. Intents also potentially
    have a collection of key/value pairs called extras, as well as flags,
    components, and other more advanced features.

  • Each of these IPC mechanisms uses Intents in some capacity and is
    probably somewhat familiar to most Android developers. However,
    because using these safely is key to Android security,

1) Intents are messages which components can send and receive. It is a universal mechanism of passing data between processes. With help of the intents one can start services or activities, invoke broadcast receivers and so on.

2) Bundles are entities the data is passed through. It is similar to the serialization of an object, but much faster on android. Bundle can be got from intent via the getExtras() method.

3) Binders are the entity which allows activities and services obtain a reference to another services. It allows not simply send messages to services but directly invoke methods on them.

For more info look at:

  1. Grasping Android's IPC mechanism

  2. Android’s Securable IPC Mechanisms

Can we use System IPC in android NDK code

One year ago I wrote a survey about this topic. Here is a part of it:

2 Unix IPC mechanisms

Unix IPC mechanisms include:

  • Unix signals.
  • Unix pipes.
  • Unix domain sockets.

At the same time Unix System-V IPC mechanisms are not included in Android.
Bionic intentionally does not provide support for System-V IPCs mechanisms, like the
ones provided by semget(), shmget(), msgget(). The reason for this is to avoid denial-
of-service [11].

2.1 Unix signals

One of the best explanations how unix signals work we can find in wikipedia: “A
signal is a limited form of inter-process communication used in Unix, Unix-like, and
other POSIX-compliant operating systems. Essentially it is an asynchronous notifica-
tion sent to a process in order to notify it of an event that occurred. When a signal is
sent to a process, the operating system interrupts the process’s normal flow of execu-
tion. Execution can be interrupted during any non-atomic instruction. If the process has
previously registered a signal handler, that routine is executed. Otherwise the default
signal handler is executed.”
It seems that unix signals are rearly used in Android programming. Moreover,
some signals have been disabled in debug mode. However, it is a possible IPC
mechanism.

2.2 Unix pipes

Pipe is a communication mechanism that allows to connect directly the output stream
of one process with the input stread of another process. There are two types of unix
pipes: named pipes and unnamed pipes. It seems that in Android programming only
named pipes are used. In this case two processes interact using a special file that
connects the output and input. It should be mentioned that pipe is one-direction way
of communication, i.e. one process is always reader and the second is always writer.
The communication file has to be created on Linux filesystem, because sdcard’s FAT32
does not allow to create pipe. Here is an example how a named unix pipe can be created
and used in Android (In Russian). The source code for this example can be found here.

2.3 Unix domain sockets

Unix domain sockets, on the contrary of unix pipes, allow to tranfer information in
both ways: from server to client and from client to server. Unix domain sockets
and unix pipes use file system as address name space. That means that server and
client use special file to establish communication. Considering Android there are two
classes that are used to program unix domain sockets: LocalServerSocket and
LocalSocket. All the implementation can be built around these two classes and it is
not required to use native code to make a unix domain socket. A simple example how
to use unix domain sockets is shown here.

[11] Android ndk documentation. NDK documentation for android-ndk-r6b

Which Linux IPC technique to use?

I would go for Unix Domain Sockets: less overhead than IP sockets (i.e. no inter-machine comms) but same convenience otherwise.

Which kind of inter process communication (ipc) mechanism should I use at which moment?

Long story short:

  • Use lock files, mutexes, semaphores and barriers when processes compete for a scarce resource. They operate in a similar manner: several process try to acquire a synchronisation primitive, some of them acquire it, others are put in sleeping state until the primitive is available again. Use semaphores to limit the amount of processes working with a resource. Use a mutex to limit the amount to 1.

  • You can partially avoid using synchronisation primitives by using non-blocking thread-safe data structures.

  • Use signals, queues, pipes, events, messages, unix sockets when processes need to exchange data. Signals and events are usually used for notifying a process of something (for instance, ctrl+c in unix terminal sends a SIGINT signal to a process). Pipes, shared memory and unix sockets are for transmitting data.

  • Use sockets for networking (or, speaking formally, for exchanging data between processes located on different machines).

Long story long: take a look at Modern Operating Systems book by Tanenbaum & Bos, namely IPC chapter. The topic is vast and can't be completely covered within a list or a paper.



Related Topics



Leave a reply



Submit