How applications interact with kernel operations
When an application (running in user space) needs to access hardware or perform a privileged operation (e.g., reading a file, opening a network connection), it must "trigger" the kernel via system calls (syscalls). Here’s how this process works:
Step-by-Step Process
Opening a file (open())
Writing to the screen (write())
Allocating memory (mmap())
Creating a process (fork()).
int fd = open("file.txt", O_RDONLY); // This triggers the open syscall.
The syscall instruction (e.g., syscall on x86-64, svc on ARM) triggers a software interrupt, forcing the CPU to switch from user mode (unprivileged) to kernel mode (privileged).
The CPU saves the app’s current state (registers, stack) to resume later.
The kernel’s syscall handler identifies the requested service (e.g., open) via a syscall number (e.g., 2 for open on Linux).
The kernel validates the request (e.g., checks permissions, ensures parameters are safe).
It performs the privileged operation (e.g., accesses the disk driver to open the file).
The kernel places results (e.g., a file descriptor) in registers/memory accessible to the app.
The CPU switches back to user mode and resumes the app’s execution.
Recommended by LinkedIn
Key Mechanisms
1. Syscall Table
The kernel maintains a table mapping syscall numbers to corresponding kernel functions. For example:
2. Hardware Support
3. Context Switching
Switching between user and kernel mode involves:
Example: Writing to a File
Why This Matters
Real-World Analogy
Imagine a library visitor (app) needing a restricted book (hardware access). They ask the librarian (kernel) to fetch it. The librarian checks permissions, retrieves the book, and hands it over. The visitor never enters the restricted storage area.
Tools to Observe Syscalls
By design, syscalls are the only safe gateway for apps to interact with the kernel and hardware. This architecture underpins modern operating systems’ security and reliability.
Very insightful 🙏 series David Zhu