10.3、android输入系统_必备Linux编程知识_任意进程双向通信(scoketpair+binder)

3. 任意进程间通信(socketpair_binder)

进程每执行一次open打开文件,都会在内核中有一个file结构体表示它;

对每一个进程在内核中都会有一个task_struct表示进程,这个结构体内部有个files_struct结构体,这个结构体里面有个fdtble结构体,这个结构体里有个struct file **fd,fd就是个数组,fd[open时返回的句柄]就保存的对应文件的file结构体

因此不同进程的文件句柄只在本进程中有含义,如果想要在进程外面使用这个文件句柄,需要让外面进程的fd[任何句柄都可以]指向需要获得的目的进程的file

这里使用binder来传输文件句柄:

(1)APP1  open(file)得到fd1;

(2)通过binder驱动,根据fd1得到file:files->fdt->fd[fd1]

(3)从APP2的files->fdt->fd取出空项fd2,让fd[fd2]指向该file

(4)APP1通过fd1,APP2通过fd2就可以访问同一个file文件了,fd1和fd2不一样

取出APP_0004_Binder_CPP_App V4来修改:
第一次:
git clone https://github.com/weidongshan/APP_0004_Binder_CPP_App.git

更新:
git pull origin

取出指定版本:
git checkout v4 // 兼容APP_0005_Binder_JAVA_App

参考代码:
frameworks\base\core\jni\android_view_InputChannel.cpp (用binder传文件句柄)
server端写fd: android_view_InputChannel_nativeWriteToParcel
parcel->writeDupFileDescriptor
client端读fd: android_view_InputChannel_nativeReadFromParcel
int rawFd = parcel->readFileDescriptor();
int dupFd = dup(rawFd);

frameworks\native\libs\binder\Parcel.cpp

支持传输文件句柄的程序 v5:
第一次:
git clone https://github.com/weidongshan/APP_0004_Binder_CPP_App.git

更新:
git pull origin

取出指定版本:
git checkout v5 // v5, use binder to transfer file descriptor

编译:
把 APP_0004_Binder_CPP_App 放入 /work/android-5.0.2/frameworks/testing

cd /work/android-5.0.2/
. setenv
lunch //选择单板
mmm frameworks/testing/APP_0004_Binder_CPP_App
cp /work/android-5.0.2/out/target/product/tiny4412/system/bin/test_* /work/nfs_root/android_fs/

测试:
su
busybox mount -t nfs -o nolock,vers=2 192.168.1.123:/work/nfs_root /mnt
logcat HelloService:* GoodbyeService:* TestService:* *:S &
echo asfsdfasdf > 1.txt
./test_server 1.txt &
./test_client readfile

原文地址:https://www.cnblogs.com/liusiluandzhangkun/p/9161570.html

时间: 2024-08-30 00:10:47

10.3、android输入系统_必备Linux编程知识_任意进程双向通信(scoketpair+binder)的相关文章

10.1、android输入系统_必备Linux编程知识_inotify和epoll

1. inotify和epoll 怎么监测键盘接入与拔出? (1)hotplug机制:内核发现键盘接入/拔出==>启动hotplug进程==>发消息给输入系统 (2)inotify机制:输入系统使用inotify来监测目录/dev/input android使用inofity机制 当插入多个键盘时,系统怎么知道哪个键盘被按下? android下使用epoll,可以同时监控多个文件,当文件发生改变,其会知道谁变化了 参考代码:frameworks\native\services\inputfli

10.5 android输入系统_Reader线程_使用EventHub读取事件和核心类及配置文件_实验_分析

4. Reader线程_使用EventHub读取事件 使用inotify监测/dev/input下文件的创建和删除 使用epoll监测有无数据上报 细节: a.fd1 = inotify_init("/dev/input") b.假设input下已经有了event0和event1 fd2 = open("/dev/input/event0") fd3= open("/dev/input/event1") c.使用epoll_wait监测fd1.f

10.6 android输入系统_Dispatcher线程_总体框架

图解Android - Android GUI 系统 (5) - Android的Event Input System - 漫天尘沙 - 博客园.htm // 关注里面的Dispatcher处理流程http://www.cnblogs.com/samchen2009/p/3368158.html Dispatcher线程框架: 分发 问:发什么?发给谁? Dispatcher流程如下: 获得事件: (1)放入队列前先稍加处理:分类(Global输入/System输入/User输入).处理紧急事件

10.8 android输入系统_实战_使用GlobalKey一键启动程序

11. 实战_使用GlobalKey一键启动程序参考文章:Android 两种注册.发送广播的区别http://www.jianshu.com/p/ea5e233d9f43 [Android]动态注册广播接收器 http://blog.csdn.net/etzmico/article/details/7317528 Android初学习 - 在BroadcastReceiver中启动Activity的问题 http://blog.csdn.net/cnmilan/article/details/

《深入理解Android 卷III》第五章 深入理解Android输入系统

<深入理解Android 卷III>即将公布.作者是张大伟.此书填补了深入理解Android Framework卷中的一个主要空白.即Android Framework中和UI相关的部分.在一个特别讲究颜值的时代,本书分析了Android 4.2中WindowManagerService.ViewRoot.Input系统.StatusBar.Wallpaper等重要"颜值绘制/处理"模块 第5章 深入理解Android输入系统(节选) 本章主要内容: ·  研究输入事件从设

Android输入系统(4)——InputStage

一.两个线程启动过程 SystemService.java 启动 InputManagerService 服务 Service: InputManagerService.java JNI: com_android_server_input_InputManagerService.cpp InputManagerService(Context context) /*调用的第一个本地函数*/ mPtr = nativeInit(this, mContext, mHandler.getLooper()

Android输入系统(6)——多点触摸处理

1. 多触摸和单触摸的Mapper不同 InputReader::addDeviceLocked(nsecs_t when, int32_t deviceId) InputDevice* device = createDeviceLocked(deviceId, controllerNumber, identifier, classes); //键盘的Mapper if (classes & (INPUT_DEVICE_CLASS_KEYBOARD | INPUT_DEVICE_CLASS_DP

10.10 android输入系统_APP获得并处理输入事件流程

APP对fd/InputChannel的注册过程: new WindowInputEventReceiver extends InputEventReceiver//InputEventReceiver类的dispatchInputEvent函数会调用onInputEvent onInputEvent函数在收到事件后被调用//被父类的dispatchInputEvent调用 在InputEventReceiver的构造函数中调用nativeInit nativeInit//从java进入C++

[Android] 输入系统(三):加载按键映射

映射表基本概念 由于Android调用getEvents得到的key是linux发送过来的scan code,而Android处理的是类似于KEY_UP这种统一类型的key code,因此需要有映射表把scan code转换成key code.映射表在板子上的位置是/system/usr/keylayout/xxx.kl,先看一下映射表是什么样子的,下面截选了一段. key 2 1 key 3 2 key 4 3 key 5 4 key 6 5 key 7 6 key 8 7 key 9 8 k