5. System Calls in Operating Systems

1. What is a System Call?
Definition:
A system call is a request made by a user program to the OS kernel to perform tasks that require privileged hardware access (e.g., file creation, memory allocation).
Why Needed?
User applications run in user mode (restricted access).
Kernel runs in kernel mode (full hardware access).
System calls bridge this gap via the System Call Interface (SCI).
Analogy:
User Mode = Customer at a restaurant (orders food).
Kernel Mode = Chef in the kitchen (executes orders).
System Call = Waiter (delivers order to kitchen).
2. How System Calls Work
Step-by-Step Flow:
User Request: e.g.,
mkdir Movies
(CLI) or "New Folder" (GUI).Trigger SCI: Command reaches the System Call Interface.
Mode Switch: Software interrupt switches CPU to kernel mode.
Kernel Execution: Kernel’s C code creates the folder on disk.
Return to User Mode: Confirmation sent back to user space.
Example:
$ mkdir Movies # User command
↓
SCI maps to `sys_mkdir()` in kernel
↓
Kernel creates folder → Returns success message
3. Types of System Calls
Category | Purpose | Examples (Linux/Windows) |
Process Control | Create/end processes | fork() , exit() , wait() |
File Management | Read/write files | open() , read() , chmod() |
Device Management | Control hardware (printers, USB) | ioctl() , read() , write() |
Information | Get system data (time, process ID) | getpid() , time() , alarm() |
Communication | IPC (pipes, shared memory) | pipe() , shmget() , mmap() |
4. Key Examples Explained
(A) Process Control: fork()
What? Creates a child process (clone of parent).
Code:
#include <unistd.h> int main() { fork(); // Creates child process printf("Hello OS!\n"); return 0; }
Output:
Hello OS! // Parent Hello OS! // Child
(B) File Management: open()
& write()
What? Creates/writes to a file.
Terminal Demo:
$ echo "Hello" > file.txt # Uses `open()` and `write()` internally.
(C) Communication: pipe()
What? Creates a channel for inter-process communication.
Use Case:
- Process A writes to pipe → Process B reads from it.
5. Practical Demo: From Terminal
Creating a Script (Hello.sh
)
Write Script:
#!/bin/bash echo "Hello CodeHelp" sleep 10 # Uses `sleep()` system call
Run Script:
$ chmod +x Hello.sh # `chmod()` system call $ ./Hello.sh # `fork()` + `exec()`
Monitor Process:
$ ps -a # `getpid()` system call $ kill -9 [PID] # `kill()` system call
6. Why System Calls Matter
Security: Prevents apps from directly accessing hardware.
Abstraction: Simplifies complex operations (e.g.,
mkdir
vs. manual disk allocation).Standardization: Uniform interface across applications.
7. Interview Questions
How does
mkdir
work internally?- Triggers
sys_mkdir()
in kernel via SCI.
- Triggers
Difference between
fork()
andexec()
?fork()
clones a process;exec()
replaces it with a new program.
What happens during a mode switch?
- CPU saves user mode state → Switches to kernel mode via interrupt.
8. Homework
Explore
man
pages:$ man 2 kill # System call documentation $ man 2 open
Experiment:
Use
strace
to trace system calls:$ strace ./Hello.sh
Key Takeaways
System calls = Gateway between user apps and kernel.
5 Types: Process, File, Device, Info, Communication.
Real-World Use: Every CLI/GUI action (e.g., file creation) relies on system calls.
Subscribe to my newsletter
Read articles from Tanish Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
