🧠 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

CategoryPurposeExample
🗂️ File ManagementOpen, read, write, close filesopen(), read(), write(), close()
🧵 Process ControlCreate, kill, wait for processesfork(), exec(), exit(), wait()
🧠 Memory ManagementAllocate/deallocate memorymmap(), brk()
🌐 Device ManagementAccess hardware (I/O)ioctl(), read(), write()
🌍 Information MaintenanceGet system info or timegetpid(), gettimeofday()
🕸️ CommunicationPipes, sockets, IPCpipe(), 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 descriptor 1 = stdout).

🔐 Why Not Allow Direct Access?

RiskWhy It’s a Problem
🔥 CrashesUser code could crash the whole system
🧨 SecurityMalicious code could steal data
💣 StabilityUncontrolled 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.