🧠 What is a System Call?
A System Call is the interface between a user program and the Operating System’s kernel.
When a user-space program needs to do something privileged—like read a file, allocate memory, or talk to hardware—it cannot do it directly. It must make a system call, which requests the OS to do it on its behalf.
🔧 Simple Analogy:
Think of the kernel as the bank vault, and the user program as a customer.
You can’t walk into the vault yourself—you request a bank employee (system call) to do it for you.
🔄 Why Do We Need System Calls?
-
User programs run in “user mode” (limited privileges).
-
Kernel runs in “kernel mode” (full access to memory, hardware).
-
To ensure security and stability, user programs must ask the OS before performing sensitive operations.
🧰 Categories of System Calls
| Category | Purpose | Example |
|---|---|---|
| 🗂️ File Management | Open, read, write, close files | open(), read(), write(), close() |
| 🧵 Process Control | Create, kill, wait for processes | fork(), exec(), exit(), wait() |
| 🧠 Memory Management | Allocate/deallocate memory | mmap(), brk() |
| 🌐 Device Management | Access hardware (I/O) | ioctl(), read(), write() |
| 🌍 Information Maintenance | Get system info or time | getpid(), gettimeofday() |
| 🕸️ Communication | Pipes, sockets, IPC | pipe(), socket(), send(), recv() |
📦 How a System Call Works (Step-by-Step)
User Program (e.g., C code)
↓
Calls a library function (e.g., printf → write())
↓
Library function internally invokes a system call (e.g., syscall number for write)
↓
Switch from user mode → kernel mode via software interrupt or syscall instruction
↓
Kernel executes the service
↓
Returns result back to user mode
🔁 This switch from user to kernel mode is known as a context switch.
🔍 Example in C:
#include <unistd.h>
int main() {
write(1, "Hello\n", 6); // System call under the hood
return 0;
}write()is a system call that sends data to the standard output (file descriptor1= stdout).
🔐 Why Not Allow Direct Access?
| Risk | Why It’s a Problem |
|---|---|
| 🔥 Crashes | User code could crash the whole system |
| 🧨 Security | Malicious code could steal data |
| 💣 Stability | Uncontrolled hardware access can brick the system |
So system calls act as a secure, controlled bridge.
🧠 Interview-Ready Definition:
A System Call is a controlled interface provided by the Operating System that allows user programs to request services from the kernel, such as file I/O, process creation, and memory management. It ensures safe access to hardware and privileged operations from user space.