π§ What are System Calls in Linux?
π Visual Understanding



β‘ Simple Definition
A system call is a way for a program (user space) to request services from the Linux kernel.
π Because:
-
Apps cannot directly access hardware
-
Only the kernel has full control
So apps say:
βKernel, please do this for meβ
π Real-Life Analogy
Think of it like this:
-
π¨βπ» You (user/app)
-
π§βπ³ Waiter (system call interface)
-
π¨βπ³ Kitchen (kernel)
π You donβt go into the kitchen directly
π You place an order via waiter β kitchen executes
π§ Why System Calls Exist
Without system calls:
-
Apps could directly access hardware β (dangerous)
-
System would crash easily β
-
No security β
π System calls ensure:
-
β Security
-
β Stability
-
β Controlled access
π§± How System Calls Work (Step-by-Step)
Example: cat file.txt
-
You run command β
cat -
catneeds to read file -
It makes a system call β
read() -
Kernel:
-
Checks permissions
-
Reads file from disk
- Kernel returns data β app prints it
π₯ Flow:
User App β System Call β Kernel β Hardware β Response β App
π Types of System Calls (Important for Interviews)
1. File Management
open(),read(),write(),close()
π Used when:
-
Reading logs
-
Writing files
2. Process Management
fork(),exec(),exit(),wait()
π Example:
- Running any command β new process created
3. Device Management
- Interacting with hardware (disk, printer, etc.)
4. Information Management
getpid(),getuid()
π Used to get system/process info
5. Communication (IPC)
- Pipes, shared memory, sockets
π Used in:
-
Microservices
-
Network apps
π§ Important Concepts
πΉ User Mode vs Kernel Mode
| Mode | Description |
|---|---|
| User Mode | Apps run here (restricted) |
| Kernel Mode | Full access (danger zone) |
π System call = switch from user mode β kernel mode
πΉ Context Switching
-
CPU switches between user & kernel
-
Slight overhead (important in performance tuning)
π₯ DevOps-Level Examples
1. Reading Logs
cat /var/log/syslogπ Internally:
open()βread()βclose()
2. Running a Command
lsπ Internally:
fork()βexec()
3. Network Call
curl google.comπ Uses:
socket(),connect(),send(),recv()
π§ͺ How to SEE System Calls (VERY IMPORTANT)
Use:
strace lsπ Output shows:
- All system calls used by
ls
Example:
open("/etc/ld.so.cache", O_RDONLY)
read(3, ...)
π‘ This is how real debugging is done