[Android]檔案描述符透過Binder傳輸的原理

來源:互聯網
上載者:User

標籤:des   android   blog   http   os   ar   for   檔案   art   

在Linux中,檔案描述符都是屬於進程的,用整數來表示。通過fork,雖然子進程和父進程都是開啟同樣的檔案,但檔案描述符卻是不同的。
同樣的檔案描述符值在不同進程對應不同的檔案描述符值數組。
所以檔案描述符透過Binder來進行傳輸時,不能是簡單的拷貝檔案描述符值。
關鍵是要把對應的檔案結構與一個新的檔案描述符對應起來,這樣另一個進程和原來的進程透過不同的檔案描述符對應同一個檔案。
幸好,開啟檔案的結構struct file是可以在進程間共用的,透過進程a的檔案描述符來擷取struct file,再在進程b中擷取一個新的空檔案描述符,再把它們關聯起來,就實現了檔案描述符在不同進程之間的傳輸。

關鍵函數及結構
int filedescripter;
//透過檔案描述符擷取開啟檔案struct file 結構
strcut file = fget(filedescripter)

//擷取空的檔案描述符
new_fd = task_get_unused_fd_flags(target_proc, O_CLOEXEC);

//建立關聯
task_fd_install(target_proc, new_fd,strcut file);

參考:
《 Android系統匿名共用記憶體Ashmem(Anonymous Shared Memory)在進程間共用的原理分析 》
http://blog.csdn.net/luoshengyang/article/details/6666491

[Android]檔案描述符透過Binder傳輸的原理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.