0 · Recall Warm-up
Answer from memory first. Answers are in the Answer Vault at the bottom.
- Is the system-call (trap) instruction privileged? from Lesson 0.1.2
- 2³²/2¹²? from Lesson 0.1.1
1 · The Hook
When a C program calls printf("hi"), the letters eventually reach your screen, but the program never touches the screen itself; it is not allowed to. Somewhere a request crosses from user mode into the kernel. That doorway is a system call. GATE asks what happens during one, and which familiar functions are system calls and which are not.
2 · Predict First
Try the questions first. Open when you are ready to check.
A program calls printf three times, and the C library collects the text and sends it in one go. How many times did the program enter the kernel to print?
Make a guess in your head. The answer opens Section 3.
3 · Core Concept
Predict First — the answer
Once. printf is a library function that buffers text in user mode; only the single underlying write system call enters the kernel.
In plain words
A system call is a program's request to the operating system: "open this file", "give me more memory", "start a new process". The program puts its request in agreed places, executes a special trap instruction, the CPU jumps into the kernel, the kernel does the work, and control returns with an answer.
1. The steps of a system call
- The program calls a library wrapper (e.g.
read(fd, buf, n)). - The wrapper puts the system-call number and the parameters in agreed places.
- It executes the trap instruction: the CPU switches to kernel mode and jumps to the OS's trap handler.
- The kernel looks up the number in the system-call table, runs the matching function, and puts the result in a register.
- The CPU switches back to user mode and the program continues.
Each system call therefore costs two mode switches (user → kernel → user).
2. Passing parameters
In registers (fast, few); in a block of memory whose address is in a register; or on the stack.
3. Kinds of system calls (UNIX examples)
| Kind | Examples |
|---|---|
| Process control | fork, exec, exit, wait, kill |
| File management | open, read, write, close, lseek |
| Device management | ioctl, read/write on devices |
| Information | getpid, time |
| Communication | pipe, shmget, send/recv (sockets) |
4. System calls vs library functions
- System calls: fork, exec, read, write, open, close, exit, brk/sbrk.
- Library functions (run in user mode, may or may not call the kernel): printf (calls write), malloc (sometimes calls brk/mmap), strlen, sqrt (never calls the kernel).
5. Kernel structures (names to recognise)
- Monolithic kernel: all OS services in one big kernel (traditional UNIX, Linux).
- Microkernel: only the essentials in the kernel; file systems and drivers run as user-level servers (more switches, more robust).
4 · Visual
read(fd, buf, 100) step by step
USER MODE KERNEL MODE
--------- -----------
program calls read()
library puts number + params
in registers, executes TRAP ---> trap handler
look up syscall table
sys_read(): copy data
result -> register
program continues <--- return-from-trap (back to user mode)
5 · Worked Examples
Example 1 Easy
Is strlen a system call?
Solution
No: it just counts characters in user memory.
Example 2 Medium
How many mode switches does one system call cause?
Solution
2: user → kernel, then kernel → user.
Example 3 GATE
A program makes 5 system calls. How many times does the CPU switch from user mode to kernel mode because of them?
Solution
5 (and 5 switches back).
6 · GATE Traps
- printf and malloc are library functions; write and brk are the system calls underneath.
- A system call is entered by a trap instruction, not an ordinary function call.
- The trap switches to kernel mode; the return switches back.
- Not every library call reaches the kernel (sqrt never does).
7 · Memory Hook
"Library wraps, trap enters, kernel works, return leaves."
8 · Connect the Dots
Phase 0 is complete. Phase 1 looks at the most important thing the kernel manages: processes, how they are created with the fork system call, and what states they move through.
9 · Self-Test
GATE format. Try every question before opening the vault.
Q1 · MCQ · 1 mark(s) — System calls
Which of these is a system call?
- (A) strlen
- (B) sqrt
- (C) write
- (D) printf
Q2 · NAT · 1 mark(s) — Mode switches
A program performs 7 system calls. Total number of mode switches they cause?
Numerical answer: ________
Q3 · MCQ · 1 mark(s) — Mechanism
A system call transfers control to the kernel using:
- (A) a privileged instruction executed in user mode
- (B) a trap (software interrupt) instruction
- (C) disabling interrupts
- (D) an ordinary function call
Q4 · MSQ · 2 mark(s) — Categories
Which are process-control system calls?
- (A) exec
- (B) fork
- (C) wait
- (D) lseek
Q5 · MCQ · 1 mark(s) — Kernel design
In a microkernel, file systems and device drivers usually run:
- (A) in the BIOS
- (B) on the disk controller
- (C) inside the kernel
- (D) as user-level server processes
10 · Rule Card
- System call: wrapper → parameters + number → trap → kernel handler via system-call table → result → return to user mode (2 mode switches).
- Parameters: registers, memory block, or stack.
- Categories: process (fork, exec, exit, wait), file (open, read, write, close, lseek), device, information, communication.
- printf, malloc, strlen are library functions; write, brk are system calls.
- Monolithic kernel vs microkernel.
11 · Concept Ledger
Unlocked in this lesson. From here on, these terms may be used.
system callsystem-call wrappersystem-call number and tabletrap instructionparameter passingmode switches per system callprocess-control, file, device, information and communication callslibrary function vs system callmonolithic kernelmicrokernel
12 · Answer Vault
Try the questions first. Open when you are ready to check.
🔐 Open the Answer Vault
Recall Warm-up
- No.
- 2²⁰.
Self-Test
Q1 Answer: (C) · 1 mark(s)
It enters the kernel.
- (A) is wrong: Pure user-mode code.
- (B) is wrong: Pure user-mode code.
- (D) is wrong: A library function that calls write.
Re-read: Lesson 0.1.3 — System calls: how programs ask the operating system for help
Q2 Answer: 14 · 1 mark(s)
2 per call.
Re-read: Lesson 0.1.3 — System calls: how programs ask the operating system for help
Q3 Answer: (B) · 1 mark(s)
The trap switches mode safely.
- (A) is wrong: Privileged instructions fail in user mode.
- (C) is wrong: Privileged, so a user program can't do it.
- (D) is wrong: Ordinary calls stay in user mode.
Re-read: Lesson 0.1.3 — System calls: how programs ask the operating system for help
Q4 Answer: (A) (B) (C) · 2 mark(s)
lseek moves a file position.
- (D) is wrong: File management.
Re-read: Lesson 0.1.3 — System calls: how programs ask the operating system for help
Q5 Answer: (D) · 1 mark(s)
Only essentials stay in the kernel.
- (A) is wrong: Unrelated.
- (B) is wrong: Unrelated.
- (C) is wrong: That is a monolithic kernel.
Re-read: Lesson 0.1.3 — System calls: how programs ask the operating system for help