標籤:arc one nfs 目的 檔案的 處理序間通訊 led ann server
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
10.3、android輸入系統_必備Linux編程知識_任意進程雙向通訊(scoketpair+binder)