Tuesday, September 17, 2013

Shared Memory in Android Applications


The common mechanism in android to share data between processes is the binder. We can also use other IPC like unix domain sockets, pipes etc.
The main problem with the above is performance. In some cases we need to share memory between processes to prevent the system from coping data to and from the kernel
Android ashmem
One feature that google added to linux kernel is ashmem. We can create a new region using MemoryFile class or ashmem_create_region on native layer but, for security reasons, we cant open an existing region

The solution is to create a service that creates the region and return it to the client via file descriptor

The Service AIDL


interface IShmService
{
ParcelFileDescriptor getFD(in String name);
void setNum(int pos,int num);
int getNum(int pos);
}


Service Native code



static jint getFD(JNIEnv *env, jobject thiz, jstring path)
{
const char *name = (env)->GetStringUTFChars(path,NULL);

jint fd = open("/dev/ashmem",O_RDWR);

ioctl(fd,ASHMEM_SET_NAME,name);

ioctl(fd,ASHMEM_SET_SIZE,4096);


map = (int *)mmap(0,4096,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
map[0]=99;
map[10]=88;

(env)->ReleaseStringUTFChars(path,name);

return fd;

}

The service java code



public ParcelFileDescriptor getFD(String name) throws RemoteException {
try {
int fd = ShmLib.getFD(name);
ParcelFileDescriptor ret=ParcelFileDescriptor.fromFd(fd);
return ret;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

The client java code


ParcelFileDescriptor p = service.getFD("samp");
ShmClientLib.init(p.getFd());


The java native code


static void init(JNIEnv *env, jobject thiz, jint fd)
{
map = (int *)mmap(0,4096,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
}




Using External Toolchain


One option to write software for the native layer is to use external toolchain. You can download codesourcery toolcahin for example (Lite edition is free) and use it to compile your code. The only note is that we need to link it statically (makes the binary file bigger):

arm-none-linux-gnueabi-gcc -static -o myapp myapp.c 

then push it to the device using adb

adb push ./myapp /data/myapp

The benefits you get from using an external toolchain are:

  • You can use the same binary file in linux and android systems
  • You can use features that are not included in google bionic c library like priority inheritance futex or SYS V mailbox etc. 

Embedded Android


Android is not just a smartphone/tablet OS, it is a great platform for embedded systems especially systems that currently based on linux
when using android as embedded linux alternative we can enjoy the many benefits that the operating system provides us and in fact there is no disadvantages because you can write code that runs in the lower layers exactly as written for Linux
The product must not have a touch screen, it can be "faceless" and you can still take advantage of many infrastructures that google wrote to make the product "time to marker" shorter
Android Stack based on the following layers (bottom up)

  1. Linux kernel
  2. Bionic - C library
  3. Native layer
  4. Frameworks 
  5. Applications

Linux kernel

Android is based on linux kernel with some changes. Currently most of them merged to the formal linux kernel (kernel.org). 
Google added some security features, IPC mechanisms and power management  features
I will get into more details in a future post
In general, if we want to add drivers, file systems, network modules or any other kernel feature its the same as it done on linux

Bionic - C library

Google wrote the c library from scratch. The library interface is the same as glibc but the implementation is much better and fit to embedded devices


Native Layer

This layer is the same as user space layer on linux. It contains native libraries (.so files), daemons and native applications.
Writing code for this layer is the same as writing a linux application and its not connected to any android or java restriction

Frameworks

This layer contains all android services, we can use this layer to connect the java applications with the native layer in a secure way


Application

This is the top layer, we can write code in java and get access to many features, great GUI and more.
We can add code in C/C++ or even write a full C/C++ application 
 More details in all my future posts

Wednesday, January 14, 2009

ASP.NET 3.5 slides and examples

Slides from the open day
Part 1
Part 2

Examples

for help and other learning materials
visit our site: Bina

Sunday, December 23, 2007

C# vs. Java

I found this article today,
Its a good comparison between C# and Java

for help and other learning materials
visit our site: Bina