当前位置:网站首页>Binder explanation of Android interview notes

Binder explanation of Android interview notes

2022-06-25 10:23:00 HeartCircle

Binder Eight questions

1. Why do interviews ask Biner?

1.1 Binder What is it? ?

Activity, Service And so on AMS  Interact , These cross process communications are   adopt  Binder To complete .

 Look at... From three perspectives Binder:
	 Mechanism : A mechanism for cross process communication 

	 drive : Virtual physical device driver 

	 application layer :Binder It's a that can initiate communication Java class 

1.2 Why use multiple processes ?

1.2.1 Because the memory allocated by the virtual machine to each process is limited , Can break through Memory limit

 Why does the maximum memory of the current machine already have 16G 了 , But when you load a big picture , Still will OOM, Because of   The memory allocated by the virtual machine to each process is limited 

1.2.2 Independent process be conducive to Maintain the stability of long connections ( push )

1.2.3 Avoid memory leaks : independent webview process Block problems caused by memory leaks

1.2.4 Isolate risk , For unstable functions, separate processes run , Avoid main process collapse

1.3 system service typewriting , alarm clock , SMS , Telephone … It's all multiprocessing

AMS,WMS,PMS

 If these services are not multiprocessing , So each of these app  Integrate these system services , Will lead to  app  Become big 

2.Binder What are the advantages ? With the traditional IPC Mechanism contrast

2.1 Conventional IPC Mechanism – Linux

 Traditional communication mechanism : The Conduit ,  Shared memory , socket, Message queue 

ProcessA ,B signal communication 
	1. ProcessA  adopt   system call  copy_from_user , The information  copy Into the   Kernel space 

	2. ProcessB  adopt   system call  copy_to_user,  The information  copy  Into the  ProcessB User space 

	3.  Communication complete ,  two copy

2.2 Binder And Tradition IPC Comparison of the figure

 Insert picture description here

 performance :Binder It only needs copy once   -- mmap

 characteristic : be based on C/S  framework , Easy to use 

 Security : For each APP Distribute UID( Identity recognition ), Mark each process with UID The label of    Support real name and   anonymous 

 Real name is   system service  eg:AMS  obtain   adopt  getSystemService("activity"),  Get services by name , You need to register with SM Medium 

 Anonymity is   Self created Services 

2.3 Process space partition

Memory partition diagram :

 Insert picture description here

2.2.1  Progress score   User space  +  Kernel space , The user space of the two processes is process isolated , The user space and kernel space of the same process are also isolated ( adopt API call )

2.2.2  The kernel space of the two processes is the real point   Of the same physical space 

2.2.3  All processes   Of   Kernel space , Are mapped in the same physical space , Virtual space   and   Physical space   It is one-to-one correspondence through coordinates  

2.2.4  What we say   User space   and   Kernel space    All are   Virtual space 

3.Binder How to do it once copy Of ? mmap

ProcessA ,B signal communication 
	1. ProcessA  adopt   system call  copy_from_user , take   Information  copy Into the   Kernel space 

	2.  then , The system will   Information   Deposit in   Kernel space 

	3. ProcessB  Of   Part of the user space   and   Some kernel space   Point to the same piece   Physical space   ( With tradition IPC Different places , Tradition IPC It won't be right   Virtual space mapping processing )

	4.  stay ProcessB When you need to get information , Direct to   From the physical space , Just use it directly , No need to... Again copy

4.MMAP Explain the principle of ?

Linux By associating a virtual memory area with an object on disk , To initialize this   Contents of the virtual memory area    This process is called memory mapping  (memory mapping)

 This process is done by the driver    java layer  mmap Method  --》  Driver layer  binder_mmap

e.g. 
    1. Users cannot operate directly , Of documents , Because users are app Inside is a virtual space , The files are on the hard disk , It's physical space .
    2. Use mmap  Can be   Virtual spatial   Part of the memory space   Mapping to   Real room space 
    3. So as to achieve   Users write data in the virtual space , It's equivalent to   Really operating   Files on disk 

4.1 Source code How to do mapping

 Insert picture description here

5.Binder How mechanisms cross processes ?

 Insert picture description here

 Through a copy

6. describe AIDL Generated Java Class details ?

1. ( Remote services ) The server defines the interface , The client initiates a method call  e.g. addPerson

2.  The client will automatically generate  Proxy.java class , Server side Stub Will come true  addPerson Method 

3.  First it will generate   Two bags  ( Packets used by clients to send data  data  And packets used to receive data from the server reply)

4.  secondly   client   Will pass  mRemote.transact(int, data, reply, flag), Communicate with the server   ( here   The client thread hangs ,  Generally, it is synchronous communication )

5.  Then it comes to the server Stub Medium onTransact (code, data, reply, flags) Method ,  adopt code  Differentiate between different methods 

6.  Finally called   Server implementation   The specific methods , If there is   The return value of , Write to reply In bag 

6.1 How about the server know Which method is called ?

 If there are multiple methods in the interface , Client pass  transact(int,  The first parameter , To specify the method name to call , Not used string String , Save a space 

7. Four components The underlying communication mechanism

adopt binderService As an example to explain , What did you do in the process , Finally, if the connection is successful, a IBinder

7.1 How the client obtain Server side IBinder object

 Insert picture description here

  1. The client and SM communicate (SM Is constant Handler – Namely 0, Easy to find ), Get... Through real name AMS Of IBinder
    2. adopt AMS Of IBinder And AMS communicate , request BindService
    3. that AMS Will execute scheduleBindService To Remote service onBind Method , Get the remote service IBinder object

7.2 How the server will IBinder Object returns

 Insert picture description here

  1. The same as the client , Remote server Need to get to AMS Of IBinder object
  2. adopt AMS Of IBinder Object and the AMS communicate , Publish your own ( Remote service )IBinder The object is to AMS
  3. adopt AMS Of IBinder Object will be served remotely ibinder object Return to the client (c.conn.connected(r.name.service)-- Will call Service onServiceConnected Method )

7.3 General process

 Insert picture description here

7.4 bindService The whole process There are several cross process ?

 The client and  ServiceManager  Also belong to   Cross process 

 in total   Across  6 Time   process , Just to get IBinder object , After getting it , The client and server only need to communicate through this IBinder Object 

7.4 AMS and Binder In the same process ?

 be not in ,AMS At the system level , and Binder In the driver layer 

8. Why? Intent Can't deliver big data ?

https://wanandroid.com/wenda/show/13775
Ask every day | Activity And Fragment Those things ,“ No problem with it , I have to go , You broke down ?”

mmap Allocated memory :(1M - 8K) This does not include Data header

8.1 If you deliver Data larger than this size , It must break down

8.2 Some students pass 520 k I snapped , Why? ?

 If   To transfer data   It's synchronous , that  mmap size yes  1M - 8K

 If   To transfer data   It's asynchronous , that  mmap size yes  (1M - 8K)/2

8.2 Some students pass 300 k I snapped , Why? ?

 It may be that part of the space has been used , Then apply for space  >  The remaining mmap Size 

Reference material

  1. In Tencent class , Enjoy the source code analysis course provided by
原网站

版权声明
本文为[HeartCircle]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202200544307755.html