Introduction to Operating Systems
What an operating system does, user and kernel mode, system calls, interrupts and kernel designs.
An operating system (OS) is the software layer between application programs and the hardware. It has two main jobs:
- Resource manager: share the CPU, memory, disks and devices among many programs, fairly and safely.
- Extended machine: hide messy hardware details behind clean abstractions such as processes, files and sockets.
User mode and kernel mode
The CPU has (at least) two modes, selected by a mode bit:
- Kernel mode (supervisor mode): every instruction is allowed, including privileged instructions such as changing the page table, disabling interrupts, starting I/O or halting the CPU.
- User mode: privileged instructions are forbidden. If a user program tries one, the CPU raises an exception and the OS takes over.
This separation stops a buggy or malicious program from crashing the whole machine or reading another program's memory.
System calls
A program in user mode asks the OS for a service (read a file, create a process, send data) through a system call. The call executes a special trap instruction that switches the CPU to kernel mode and jumps to a fixed OS entry point.
Common system calls in Unix-like systems: fork, exec, exit, wait (processes); open, read, write, close (files); mmap (memory); socket, send (networking).
Interrupts
An interrupt is a signal that makes the CPU pause what it is doing and run an interrupt handler:
| Kind | Caused by | Example |
|---|---|---|
| Hardware interrupt | a device | disk finished a read, key pressed, timer tick |
| Trap (software interrupt) | an instruction, on purpose | system call |
| Exception (fault) | an instruction, by error | divide by zero, page fault, illegal instruction |
The timer interrupt is what lets the OS take the CPU back from a program that never gives it up: this is the basis of preemptive multitasking.
Kinds of operating systems
| Type | Idea |
|---|---|
| Batch | run jobs one after another with no interaction |
| Multiprogramming | keep several jobs in memory; when one waits for I/O, run another (better CPU use) |
| Time-sharing (multitasking) | switch between programs so quickly that every user gets a fast response |
| Real-time | must meet deadlines (hard real-time: missing one is a failure) |
| Distributed | several connected machines appear as one system |
Kernel designs
- Monolithic kernel: the whole OS (file systems, drivers, scheduling) runs in kernel mode as one program. Fast, but a bug anywhere can crash everything. Linux is monolithic (with loadable modules).
- Microkernel: only the essentials (scheduling, memory, message passing) run in kernel mode; file systems and drivers are user-mode servers. More robust, but extra message passing costs time. Examples: MINIX 3, QNX.
- Hybrid: a mix of both, for example Windows NT and macOS (XNU).
GATE-style practice questions
Which of the following instructions should be privileged (allowed only in kernel mode)? (Select all that apply.)
Show answer and explanation
Answer: A, B, DDisabling interrupts, changing the page table and directly starting device I/O could let a program take over the machine or read other programs' data, so they must be privileged. Reading the clock is harmless and is allowed in user mode.
A user program executes a system call. Which sequence describes what happens?
Show answer and explanation
Answer: BThe trap instruction is the controlled way into the kernel: it changes the mode bit and jumps to a fixed entry point, so user code can never choose where in the kernel to start.
Which statement about multiprogramming is true?
Show answer and explanation
Answer: AMultiprogramming keeps several jobs in memory so that the CPU is not idle while a job waits for I/O. It works on a single CPU. Deadlines are the concern of real-time systems.