Introduction
An operating system (OS) is the program that controls the hardware and gives every other program a cleaner, safer machine to run on. It plays two roles at once. As a resource manager it shares the CPU, memory, disks and devices among competing programs and keeps them from damaging each other. As an extended machine it hides awkward hardware behind abstractions: processes instead of raw CPU registers, address spaces instead of physical RAM, files instead of disk sectors.
User programs cannot be trusted with the hardware directly, so the hardware itself provides at least two privilege levels. Ordinary code runs in user mode; the OS kernel runs in kernel mode. A system call is the controlled doorway between them: the only way a user program can ask the kernel to do something privileged, such as reading a file, creating a process or allocating memory.
The protection mechanism (dual mode, privileged instructions, traps and interrupts), the system-call path from a C library function to a kernel handler and back, the main categories of system calls, and the classic ways of structuring a kernel are the base that every later OS topic relies on.
Prerequisites
- I/O Interface and Interrupts: how a hardware interrupt saves the program counter and jumps to a handler through a vector table.
- Basic C: functions, return values, the idea of a library.
Core concepts
Dual-mode operation
The CPU keeps a mode bit in a status register. In the convention used by Silberschatz, Galvin and Gagne, mode bit 0 means kernel mode and 1 means user mode; other architectures use privilege rings (x86 has rings 0 to 3, with the kernel in ring 0 and applications in ring 3). The numeric encoding is an architecture convention (0 or 1, or a ring number); the rule that matters is:
Privilege rule. An instruction that could let one program harm another program or the OS is privileged. Executing a privileged instruction in user mode does not run it: the hardware raises an exception and transfers control to the kernel, which normally terminates the program.
| Instruction or action | Privileged? | Why |
|---|---|---|
| Disable or enable interrupts | Yes | A user could keep the CPU forever |
| Load the timer | Yes | Same reason: the timer is how the OS takes the CPU back |
| Direct I/O instructions, device registers | Yes | Devices are shared |
| Load the page-table base or relocation and limit registers | Yes | Would defeat memory protection |
| Switch from user mode to kernel mode directly | Yes | That is the whole protection |
| Halt the processor | Yes | Stops everyone |
| Read the time of day clock | No | Reading leaks nothing dangerous |
| Execute the trap (system call) instruction | No | It is the sanctioned way into the kernel |
| Ordinary arithmetic, loads and stores to the program's own memory | No | Protected by the MMU, not by privilege |
Reading the clock versus setting the timer. Reading the clock is harmless; setting the timer is privileged. The two are easy to confuse.
Traps, exceptions and interrupts
Traps, exceptions and interrupts transfer control to designated handlers, often through vectors/tables. An event from user mode enters kernel mode; an event already in kernel mode need not change privilege. System-call dispatch-table lookup is distinct from hardware handler entry. They differ in cause.
| Event | Cause | Synchronous with the instruction stream? | Example |
|---|---|---|---|
| Hardware interrupt | A device signals the CPU | No (asynchronous) | Disk transfer complete, timer tick, key press |
| Exception (fault) | The current instruction failed | Yes | Division by zero, page fault, privileged instruction in user mode |
| Trap (software interrupt) | The program deliberately requests a service | Yes | syscall on x86-64, svc on ARM, int 0x80 on old 32-bit Linux |
A page fault is a fault that the kernel usually repairs; the faulting instruction is then restarted. A division by zero is usually not repairable and ends the process with a signal. A system-call trap returns to the instruction after the trap.
The timer
The kernel sets a hardware timer before giving the CPU to a user program. When it expires it interrupts, and the kernel runs again even if the program is in an infinite loop. A timer guarantees periodic regain of control and enables quantum-based round robin even for a compute-bound infinite loop. Device interrupts can also cause event-driven preemption without a timer; periodic time slicing in Round Robin and Multilevel Queue Scheduling needs the periodic timer mechanism.
How a system call works
From library function to kernel and back
Programmers rarely issue the trap themselves. They call an API function, such as POSIX read() in the C library, and the library wrapper does the machine-level work.
user program: n <- read(fd, buf, 100)
C library wrapper:
put the system-call number for read into a register
put fd, buf, 100 into argument registers
execute the trap instruction (user -> kernel mode)
kernel trap handler:
save the user registers
check the number and index the system-call table
call the kernel's read routine; validate fd and buf
(may block the process until the disk data arrives)
store the result or an error code
restore registers, return-from-trap (kernel -> user mode)
C library wrapper:
on error set errno and return -1, else return the byte countMode switch versus context switch. An ordinary returning system call by a user-mode thread costs two privilege transitions (user to kernel, kernel to user). A nonreturning exit call has no return-to-user transition for that caller; count fork parent/child continuations only when the question includes them. It causes a context switch (a different process gets the CPU) only if the call blocks, for example a read() that must wait for the disk, or if the kernel decides to schedule someone else on the way out. "Every system call causes a context switch" is false.
Passing parameters
| Method | How | Limitation |
|---|---|---|
| Registers | Arguments in CPU registers | Few registers |
| Memory block | Arguments in a block; its address in one register | One extra memory access per argument |
| Stack | Program pushes arguments; kernel pops them | Kernel must validate the user stack |
Linux on x86-64 passes up to six arguments in registers. Whatever the method, the kernel must check every user pointer before use, because a malicious program can pass an address belonging to the kernel.

read() call: the library wrapper executes a trap, the mode bit changes from user (1) to kernel (0), the kernel indexes the system-call table and runs the handler, and return-from-trap restores user mode.Library calls that are not system calls
strlen, sqrt and qsort run entirely in user mode. printf formats in user mode and calls write() only when the stdio buffer must be flushed. malloc usually carves blocks out of memory it already owns and calls the kernel (brk or mmap on Linux) only when it needs more.
Categories of system calls
| Category | POSIX examples | Typical use |
|---|---|---|
| Process control | fork, exec family, exit, wait, kill | Process trees in Process Creation and fork() |
| File management | open, read, write, close, lseek, unlink | File descriptors, file offsets |
| Device management | ioctl, read and write on device files | Devices treated as files |
| Information maintenance | getpid, time, alarm, sleep | |
| Communication | pipe, shmget, mmap, socket, send, recv | Inter-Process Communication |
| Protection | chmod, chown, umask | Permission bits |
Worked examples
Counting system calls and mode switches
A program writes 10,000 bytes to a file.
This simplified comparison counts only write calls, their two user/kernel transitions and their overhead; it excludes open, close and any other system calls in both versions.
- Version A calls
write(fd, &c, 1)once per byte: 10,000 system calls, so 20,000 mode switches. - Version B uses
fputcwith a fully buffered stream and a 4,096-byte buffer, then closes the stream. The library callswrite()when the buffer fills (after 4,096 and 8,192 bytes) and once more at close for the remaining 1,808 bytes: system calls and 6 mode switches.
If each counted write call costs an assumed overhead of 1 microsecond beyond the copying work, version A spends 10 ms in overhead and version B spends 3 microseconds. The difference is the reason stdio buffering exists.
Which instructions trap?
A user-mode program executes, in order: an add, a read of the time-of-day clock, a load of the timer register, and a trap instruction. The first two run normally. The timer load is privileged and raises an exception, so control goes to the kernel; with the usual policy the process is terminated and the trap instruction is never reached. If instead the program had skipped the timer load, the trap would enter the kernel legitimately.
Operating-system structures
| Structure | Idea | Strength | Weakness |
|---|---|---|---|
| Simple or monolithic | The whole kernel is one program in one address space; components call each other directly | Fast: a service is a function call | A bug anywhere can crash everything; hard to maintain |
| Layered | Layer uses only layers below it; layer 0 is hardware | Easy to verify layer by layer | Hard to order layers; each crossing costs time |
| Microkernel | Kernel keeps only IPC, basic scheduling and memory; drivers, file systems and servers run in user mode and talk by messages | Reliability, a crashed server can be restarted; portability | Message passing and extra mode switches cost performance |
| Loadable modules | Core kernel plus modules linked in at run time | Flexibility without message overhead | Modules still run in kernel mode |
| Hybrid | Mixes the above | Practical compromise | No single clean model |
Linux is monolithic with loadable modules. MINIX 3 and QNX are microkernels. Windows and macOS are usually described as hybrids.
Microkernel cost. In a microkernel, a file read is a message from the application to the file server, possibly another message to the disk driver, and replies back, each crossing the kernel. A monolithic kernel serves the same read with one trap and in-kernel function calls. More mode switches, not more privilege, is why microkernels are slower.
Related material. A virtual machine monitor (hypervisor) runs whole operating systems as guests. A guest kernel believes it is in kernel mode, but its privileged instructions trap to the hypervisor, which emulates them. This is the trap mechanism applied one level down.
Important properties and observations
- The trap instruction is unprivileged; what it reaches is privileged.
- Kernel mode is a property of the CPU, not of a process. The same process runs in user mode most of the time and in kernel mode while the kernel serves its system calls.
- A system call is synchronous: the caller does not continue until the call returns (or, for non-blocking calls, returns an error or partial result immediately).
- Interrupts can occur while the kernel is running; kernels mask some interrupts briefly to protect their own data structures.
Common misconceptions.
- "The OS runs all the time." It runs only on traps, exceptions and interrupts, then gives the CPU back.
- "
printfis a system call." It is a library function that may make zero, one or severalwrite()calls. - "Microkernels put drivers in the kernel." They move drivers out to user space.
GATE examination perspective
- Privileged instruction lists. Mark an instruction privileged if misuse could break isolation or keep the CPU: timer, interrupts, I/O, memory-management registers, mode change, halt. Reading the clock and the trap instruction are not privileged.
- What happens on a system call. Mode switch to kernel through a trap and a dispatch table; the kernel stack is used; the call may or may not block. Mode switch is certain, context switch is not.
- Trap versus interrupt. Trap and fault are synchronous and caused by the running instruction; a device interrupt is asynchronous.
- Counting kernel entries. Count system calls, not library calls; buffered I/O reduces the count to when the stream is flushed only when full and at close.
- Structure trade-offs. Monolithic is fast but fragile; microkernel is robust but pays message-passing and mode-switch overhead.
Summary
Sources
- A. Silberschatz, P. B. Galvin, G. Gagne, Operating System Concepts, 10th ed., Wiley: Chapter 1 (1.4 Operating-System Operations, dual mode and the timer), Chapter 2 (2.3 System Calls, 2.8 Operating-System Structure).
- A. S. Tanenbaum, H. Bos, Modern Operating Systems, 4th ed., Pearson: Section 1.6 System Calls, Section 1.7 Operating System Structure (monolithic, layered, microkernels, virtual machines).