Mobile OS Architecture

[!NOTE] This module explores the core principles of Mobile OS Architecture, deriving solutions from first principles and hardware constraints to build world-class, production-ready expertise.

1. The Computer You Touch

Mobile Operating Systems (Android, iOS) are not just “small desktops”. They operate under radically different constraints:

  1. Battery: The CPU must sleep 99% of the time.
  2. Memory: No Swap space (Flash storage wears out). When RAM is full, apps are killed, not paged out.
  3. Security: Every app is untrusted and sandboxed by default.

2. Android Internals (Linux-Based)

Android uses the Linux Kernel for threading and memory management, but everything above it is custom.

The Zygote: Instant App Launch

In Linux, fork() is fast, but loading Java classes is slow. Android boots a process called Zygote at startup. It preloads thousands of common Java classes (Buttons, Views).

  • App Launch: Zygote forks itself. The new app inherits the preloaded memory (Copy-on-Write).
  • Result: Apps start in milliseconds.

The Low Memory Killer (LMK)

Desktop Linux uses OOM Killer as a last resort. Android uses LMK proactively. It keeps a “Least Recently Used” (LRU) list of apps. When RAM gets tight, it kills the oldest background app to free space for the foreground app.


3. Binder IPC: The Nervous System

Standard Linux IPC (Pipes, Sockets) is too slow or too heavy for the thousands of messages Android sends (Intents, Content Providers). Binder is a custom kernel driver that facilitates high-performance IPC.

  • Mechanism: It uses shared memory (mmap).
  • Zero-Copy (Almost): Data is copied once from the Sender to the Receiver’s buffer in the Kernel. (Standard pipes copy twice: User A → Kernel → User B).

4. Interactive: Binder IPC Transaction

Visualize how App A sends data to App B through the Binder Driver.

App A (Client)
User Space
Kernel (Binder Driver)
Mapped to App B's Address Space
App B (Server)
User Space
Ready.

5. iOS Security (The Walled Garden)

iOS is built on Darwin (XNU Kernel - Mach + BSD). Unlike Android’s open filesystem approach, iOS enforces strict Sandboxing.

  • Entitlements: An app cannot do anything (access Camera, GPS, Contacts) unless it is signed with a specific entitlement by Apple.
  • Sandbox: An app can only read/write to its own Documents/ folder. It cannot see other apps or the system files.

6. Code Example: IPC Transaction

Go
Java
package main

import "fmt"

// Go uses Channels for local IPC.
// This simulates the "One-Way" message passing of Binder.

func main() {
    // The "Binder Driver" (Channel)
    binderDriver := make(chan string)

    // App B (Service) - Waiting for transactions
    go func() {
        for msg := range binderDriver {
            fmt.Printf("[App B] Received Transaction: %s\n", msg)
            // Process...
        }
    }()

    // App A (Client)
    fmt.Println("[App A] Sending Intent...")
    binderDriver <- "START_ACTIVITY: com.example.camera"

    // In real Binder, A blocks until B handles it (Synchronous)
    // or returns immediately (One-way).
    fmt.Println("[App A] Done.")
}
// Android Interface Definition Language (AIDL) generates this code.
// This is what happens under the hood.

import android.os.Binder;
import android.os.IBinder;
import android.os.Parcel;
import android.os.RemoteException;

public class MyBinderService extends Binder {

    // Transaction ID
    static final int TRANSACTION_add = IBinder.FIRST_CALL_TRANSACTION + 0;

    @Override
    protected boolean onTransact(int code, Parcel data, Parcel reply, int flags)
            throws RemoteException {

        switch (code) {
            case TRANSACTION_add:
                data.enforceInterface("com.example.ICalculator");
                // Read args from the "Parcel" (Marshalled data)
                int a = data.readInt();
                int b = data.readInt();

                // Execute logic
                int result = a + b;

                // Write reply
                reply.writeNoException();
                reply.writeInt(result);
                return true;
        }
        return super.onTransact(code, data, reply, flags);
    }
}

7. Summary

  • Power & Memory: The dictators of Mobile OS design.
  • Zygote: Forking pre-warmed processes for speed.
  • Binder: The high-performance, capability-based IPC mechanism.
  • Sandbox: Strict isolation to protect user data in an ecosystem of untrusted apps.