---
title: "System call; read() execution"
subject: "Operating System"
chapter: 1
key: "semester_6/operating_system:system-call-read-example"
source: https://www.bctengineer.com/q/semester_6/operating_system/system-call-read-example
site: BCT Engineering
---

# System call; read() execution

Operating System · Chapter 1: Introduction to Operating System

- Asked in: 2075 Baisakh, 6 marks; year not given
- Video: [System Calls](https://www.youtube.com/watch?v=lhToWeuWWfw) — Neso Academy
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_6/operating_system/system-call-read-example
- Whole chapter: https://www.bctengineer.com/md/semester_6/operating_system/chapter-1

## Question

What is a system call? Illustrate the execution of the system call read() to read a file.

## Short answer

A system call is a user program's request to the kernel for privileged service, using a library procedure and a TRAP. For read(), parameters are pushed, the system-call number is placed in a register, TRAP switches to kernel; the dispatcher invokes the handler, which performs I/O and returns the byte count.

## Answer

A **system call** is a request made by a user program to the operating system kernel to perform a privileged service. The call is made through a library procedure that executes a TRAP instruction to enter kernel mode.

#### Execution of `count = read(fd, buffer, nbytes)`

The call reads `nbytes` bytes from the file with descriptor `fd` into `buffer` and returns the number of bytes read (-1 on error). The steps (Tanenbaum) are:

1. The program pushes the parameters (`fd`, `buffer`, `nbytes`) onto the stack (steps 1-3).
2. The program calls the **library procedure** `read` (step 4).
3. The library procedure puts the **system-call number** for `read` in a register (step 5).
4. It executes a **TRAP** instruction, switching from user mode to kernel mode and jumping to a fixed address in the kernel (step 6).
5. The kernel's **dispatcher** examines the system call number and uses it to index a table of pointers to the handlers (step 7).
6. The **read handler** runs: it checks the parameters, finds the file, and starts the disk transfer, blocking the process if necessary (step 8).
7. When the work is done, control returns to the library procedure after the TRAP instruction (step 9).
8. The library procedure returns to the user program in the normal way, with the result as its return value (step 10).
9. The user program cleans up the stack by incrementing the stack pointer (step 11) and continues.

```text
 User space:  program --> read() library
                              | TRAP
 -----------------------------+------------
 Kernel space:  dispatcher -> table lookup
                              |
                         sys_read handler
                              |
                  disk driver / file system
                              |
                    return to library --> program
```

The user-mode to kernel-mode switch keeps the hardware protected, because user code cannot directly run privileged instructions.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. In read(fd, buffer, nbytes), what happens before the TRAP?
   A. The kernel dispatcher first examines the call number
   B. The disk transfer starts
   C. Parameters are pushed on the stack, the library procedure is called and puts the system-call number in a register
   D. The stack is cleaned up
   Answer: C. The user side prepares parameters and the call number, then executes TRAP. (Key point: Parameters first)

2. What does the TRAP instruction do?
   A. Reads the disk block directly
   B. Returns the result to the program
   C. Pushes parameters onto the stack
   D. Switches from user mode to kernel mode and jumps to a fixed kernel address
   Answer: D. TRAP is the controlled entry into the kernel. (Key point: Role of TRAP)

3. How does the kernel dispatcher find the read handler?
   A. It uses the system-call number to index a table of handler pointers
   B. It searches the file system for the name read
   C. It asks the user for the handler
   D. It uses the file descriptor as the handler address
   Answer: A. The call number indexes the system-call table. (Key point: Dispatcher)

4. After the handler finishes, how does control come back?
   A. The kernel jumps directly to the program's next line without mode change
   B. Control returns to the library procedure after the TRAP, which returns to the program; the program then cleans up the stack
   C. The program restarts from main()
   D. The disk driver calls the program directly
   Answer: B. Library procedure returns the result, then the user program increments the stack pointer. (Key point: Return path)

## Related in Operating System

- [System call; monolithic and VM architecture](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Asystem-call-architecture) — Operating System, chapter 1
- [OS abstraction, extended machine, system call](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Aos-abstraction-system-call) — Operating System, chapter 1
- [Extended machine, kernel, system call](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Aextended-machine-kernel-syscall) — Operating System, chapter 1
- [File system layout, i-node, backup](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Afs-layout-inode-backup) — Operating System, chapter 5
- [File system layout; i-node vs linked list](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Afs-layout-inode-vs-linked-list) — Operating System, chapter 5
- [Linked list file system; block size impact](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Afs-linked-list-block-size) — Operating System, chapter 5

## Related in other subjects

- [Student file read/write and modify by roll](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Astudent-file-modify-roll) — Object Oriented Programming, chapter 8
- [Manufacturing execution system and automation](https://www.bctengineer.com/question.md?key=semester_4%2Fcomputer_aided_design%3Amanufacturing-execution-system-automation) — Computer Aided Design, chapter 6
- [Google File System architecture](https://www.bctengineer.com/question.md?key=semester_8%2Fbig_data_technologies%3Agfs-architecture) — Big Data Technologies, chapter 2, asked 2×
- [File system handling of kernel](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3Akernel-file-system-handling) — Embedded System, chapter 6
