In Linux, a file descriptor is a process, represented by an integer. by fork, although both the child process and the parent process open the same file, the file descriptor is different.
The same file descriptor values correspond to different file descriptor value arrays in different processes.
Therefore, when the file descriptor is transferred through binder, it cannot be a simple copy of the file descriptor value.
The key is to correspond the file structure to a new file descriptor, so that the other process and the original process through different file descriptors corresponding to the same file.
Fortunately, the structure of open files struct file can be shared between processes, through the file descriptor of process A to obtain a struct file, and then in process B to obtain a new empty file descriptor, and then associate them together, the implementation of the file descriptor between the different processes of transmission.
Key functions and structures
int filedescripter;
To get open file struct file structure through document descriptor
Strcut file = Fget (filedescripter)
Gets the empty file descriptor
NEW_FD = Task_get_unused_fd_flags (Target_proc, o_cloexec);
Establish an association
Task_fd_install (Target_proc, new_fd,strcut file);
Reference:
"An analysis of the principle of anonymous shared memory Ashmem (Anonymous shared memories) sharing between processes in Android system"
http://blog.csdn.net/luoshengyang/article/details/6666491
How the [Android] file descriptor travels through binder