7

Here I want to use System IPCs methods like

  <sys/msg.h>   /* SysV message queues */
  <sys/ipc.h>   /* General IPC definitions */

Here my android NDK code is in C language and I used message queue IPC mechanism for communication for other C application.

So please Let me know is there any way to achieve this IPC goal? How can I implement this IPC mechanism in android NDK code?

frogatto
  • 28,539
  • 11
  • 83
  • 129
user1089679
  • 2,328
  • 8
  • 41
  • 51

1 Answers1

13

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

Yury
  • 20,618
  • 7
  • 58
  • 86
  • So i can Use Unix domain sockets in android ndk Becuase it would be good for communication. – user1089679 Jul 11 '12 at 09:30
  • 2
    No, it's not. It's better to use special Android IPCs like Binder, for instance. I wrote this survey to show that it is possible, but it is not recommended. – Yury Jul 11 '12 at 09:31
  • But How can i communicate this things to C service application which are running on android. Binder is working for communication between Android NDK C code to other C application code? – user1089679 Jul 11 '12 at 11:49
  • Binder is Android IPC mechanism. Thus, yes it can be used to communicate between two native processes. – Yury Jul 11 '12 at 12:19
  • 3
    Android itself uses unix domain sockets in places where binder would add complexity for little useful gain. – Chris Stratton Jul 15 '12 at 00:58