Android and Linux systems rely on various communication methods for processes and threads to interact. Linux provides several IPC (Inter-Process Communication) mechanisms such as pipes, message queues, shared memory, sockets, semaphores, and signals. Android adds its own Binder IPC mechanism, which is widely used for communication between system components and apps. The Zygote process uses sockets for communication, while higher-level services (System Server, Media Server, Apps) primarily use Binder. For communication within the same process, especially between threads, Android uses the Handler message mechanism.
Overview
- Binder: Main Android IPC for cross-process communication (system/app level).
- Socket: Used for specific system daemons and framework-to-native communication.
- Handler: For thread-to-thread messaging inside a single process (mainly UI updates).
- Know the difference: Binder/Socket = inter-process; Handler = inter-thread.
Binder
- What is Binder?
- Android’s main IPC mechanism, crucial for system and app development.
- Follows a Client/Server model, with ServiceManager managing services.
- How it works:
- Server registers service with ServiceManager.
- Client queries ServiceManager for the service.
- Client uses the service via Binder driver (/dev/binder).
- Key Components:
- Client, Server, ServiceManager, Binder Driver.
- Binder is complex; best understood by reading source code.
- Used for: Communication between Android system components and apps, across process boundaries.
Socket
- What is Socket?
- A simpler C/S communication method, often used between framework and native layers.
- Examples of Socket Usage:
- Zygote (process creation)
- installd (app installation)
- lmkd (low memory killer)
- adbd (ADB service)
- logcatd (log service)
- vold (storage daemon)
- Why use Socket?
- Simpler than Binder; preferred for certain system daemons.
Handler
- What is Handler?
- Mechanism for communication between threads within the same process.
- Composed of Handler, MessageQueue, Message, and Looper.
- How it works:
- Worker thread sends a message via Handler to the main thread’s MessageQueue.
- Main thread processes messages in a loop, dispatching them to handleMessage().
- Key Points:
- Handler is for intra-process (not inter-process) communication.
- Used for updating UI from worker threads.
- Only works when threads share memory (i.e., within the same process).
Other IPC Mechanisms
- Linux also provides: Pipes, message queues, shared memory, signals, semaphores.
- Android uses: Process.killProcess() (signal mechanism) and others as needed.