We do IPC between processes using Unix domain sockets, with abstract
socket addresses; we fork off a tiny server that listens on the socket
and forwards messages between the processes. With this approach you'd
have to either implement your own shared semaphore implementation in the
server process, but it does work. You may find it easier to change your
application logic to use message passing rather than semaphores.
Bear in mind that the Dalvik VM doesn't like fork() much, and goes into
conniptions if you try to do *any* work between the fork() and the
exec(). The code we use is as follows:
const char* argv[] = { exe, parameter, NULL };
pid_t pid = vfork();
switch (pid)
{
case -1: /* error */
break;
case 0: /* child */
{
execve(exe, (char* const*) argv, NULL);
_exit(0);
}
default: /* parent */
{
int status;
waitpid(pid, &status, 0);
break;
}
}
This seems to be robust, and appears to work on all the devices we have
access to.
The server process does some setup and then calls daemon(), putting
itself into the background. We can then wait for this with waitpid(),
which allows us to report an error if the server fails to start up.
Actually building the server executable is moderately exciting, using
the 'include $(BUILD_EXECUTABLE)' option in Android.mk (but also be
aware that the MIPS toolchain has bugs and requires special compilation
flags or it produces broken code). We then rename it to a filename like
server.so and put it in the application's lib directory, so that when
the application is installed the server is copied into the real
filesystem on the Android device and becomes runnable.
--
┌─── dg@cowlark.com ───── http://www.cowlark.com ─────
│ "Parents let children ride bicycles on the street. But parents do not
│ allow children to hear vulgar words. Therefore we can deduce that
│ cursing is more dangerous than being hit by a car." --- Scott Adams
Hi!
Exactly, calling fork is bad in a 3rd party application. As a matter
of fact, the code that I am working is a library and not an
application.
Doing some more analysis, I found that the libc.so on the emulator
does contain the posix semaphore ( 'sem_open' etc.l). However, I
could not find the documentation saying that this is part of the
standard ABI of Android. Any one having any idea about this ?
--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to andro...@googlegroups.com.
To unsubscribe from this group, send email to android-ndk...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.
Unfortunately we frequently don't have any choice --- when porting third
party code, rewriting it to be more Android-centric simply isn't an
option. (The library I'm currently working with, for example, took about
a hundred man-years to write.)
Given the commercial realities of actually having to get a product out,
then, we just have to use fork().
(I'd much rather use posix_spawn() if that were available...)
--
┌─── dg@cowlark.com ───── http://www.cowlark.com ─────
│
│ "Never attribute to malice what can be adequately explained by
│ stupidity." --- Nick Diamos (Hanlon's Razor)
Given the commercial realities of actually having to get a product out,
then, we just have to use fork().
Sorry, but I could not understand why to refer SYS-IPC.txt for
'sem_open'. SYS-IPC.txt is all about System V semaphores. However,
sem_open is POSIX version of the semaphore. Is there anything that I
am missing?
Unfortunately we frequently don't have any choice --- when porting third
party code, rewriting it to be more Android-centric simply isn't an
option.