These are the notes for CS Fundamentals for Placements, episode 15, the first episode of the operating systems part of the course, the biggest placement topic of all. You invent the operating system one problem at a time: the OS as resource manager and extended machine, the kernel and the shell, user mode vs kernel mode, the mode bit, privileged instructions, system calls and the trap, the OS services, hardware vs software interrupts and exceptions, and every type of OS: batch, multiprogramming, multitasking/time-sharing, multiprocessing, real-time (hard and soft), distributed, embedded and mobile. Every term is explained in plain words the first time it appears, and there is a full glossary at the end. All outputs below are real: the C programs were compiled with gcc -O2 (gcc 13.3) and run on Linux x86-64 on 29 October 2026.
A quick refresher from episodes 1 and 6: the CPU runs instructions (tiny steps like add, load, jump) one after another; main memory (RAM) holds running programs and their data; I/O devices are the keyboard, screen, disk, network card; registers are tiny storage slots inside the CPU; an interrupt (episode 6) makes the CPU pause and run a special routine. In C, printf prints formatted text, #include brings in a library's declarations, and main is where the program starts.
The six problems
| Problem | What it teaches |
|---|---|
| Many programs, one machine | Operating system, resource manager, extended machine |
| A program halts the CPU | User mode, kernel mode, mode bit, privileged instructions, kernel |
| So how does printf print? | System call, trap, the system-call round trip, OS services |
| Events that can't wait | Interrupt, hardware interrupt, software interrupt, exception, timer interrupt |
| Who reads my commands? | Shell, command interpreter, CLI vs GUI, kernel vs shell |
| Different machines, different needs | Batch, multiprogramming, time-sharing/multitasking, multiprocessing, real-time (hard/soft), distributed, embedded, mobile |
Problem 1: many programs, one machine (what an OS is)
Right now a laptop may be running a browser, a music player, a chat app and a compiler, all sharing one CPU, one memory, one disk and one screen. If each program grabbed the hardware for itself, they would overwrite each other's memory, fight over the disk and freeze the screen.
Real-life example: the restaurant manager. Many customers, one kitchen. The manager (1) decides who sits where and which chef cooks what, fairly, and (2) gives customers a simple menu so they never deal with gas pipes and ovens.
An operating system (OS) is the software layer between programs and hardware. It has two big jobs:
| Job | Meaning | Example |
|---|---|---|
| Resource manager | shares the hardware (CPU time, memory, disk space, devices) between programs fairly and safely | the browser and the compiler both get CPU time; neither can read the other's memory |
| Extended machine (abstraction) | hides ugly hardware details behind simple ideas | a program opens notes.txt instead of reading raw disk sectors (the fixed-size blocks, often 512 bytes or 4 KB, a disk really stores) |
Other abstractions the OS invents: a file (instead of sectors), a process (a running program, episode 17), virtual memory (each program sees its own clean memory, episode 33), a socket (instead of raw network packets).
Problem 2: a program halts the CPU (user mode vs kernel mode)
If the OS is the manager, what stops a rude program from ignoring it? hlt is a real x86 instruction that halts the processor:
#include <stdio.h>
int main() {
printf("Trying to stop the CPU...\n");
fflush(stdout);
__asm__ volatile ("hlt"); // a privileged instruction
printf("Done!\n");
return 0;
}
Output:
Trying to stop the CPU...
Segmentation fault
(In the SyntaxPilot compiler the result shows as Runtime Error (SIGSEGV) with Segmentation fault.) Done! never printed. The CPU refused to run hlt for an ordinary program, raised a fault, and the OS ended the program. (fflush(stdout) forces the first line out before the crash; __asm__ lets C include a raw CPU instruction.)
Two modes and the mode bit
- The CPU has a mode bit, a single bit of hardware state that says who is running right now.
- User mode (mode bit = 1): ordinary programs: our C code, browsers, games, even the shell.
- Kernel mode (mode bit = 0), also called supervisor, system or privileged mode: the core of the OS, with full control of the hardware.
Privileged instructions are instructions the CPU will only execute in kernel mode. If user-mode code tries one, the CPU raises an exception and the OS takes over (usually killing the program).
| Privileged (kernel mode only) | Not privileged (any mode) |
|---|---|
halt the CPU (hlt) |
add, subtract, compare |
turn interrupts on/off (cli/sti) |
read/write your own variables |
| talk directly to I/O devices | call your own functions |
| set the timer | read the current time (via the OS) |
| change memory-protection settings, switch the mode bit | jump, loop |
The kernel is the part of the OS that runs in kernel mode: the core that manages the CPU, memory and devices and stays in memory the whole time the computer is on.
Problem 3: so how does printf print? (system calls)
Talking to devices is privileged, and the screen is a device. So printf must ask the kernel. Here is that request, made directly:
#include <unistd.h>
#include <sys/syscall.h>
int main() {
// ask the kernel to write 18 bytes to the screen (file 1)
write(1, "Hello via write()\n", 18);
// the same request, made by its raw number
syscall(SYS_write, 1, "Hello via syscall()\n", 20);
return 0;
}
Output:
Hello via write()
Hello via syscall()
- A system call (syscall) is a request from a program to the kernel to do something privileged on its behalf: read or write a file, create a process, allocate memory, send network data, get the time.
write(1, text, length)is the C library's thin wrapper for thewritesystem call. File number 1 is standard output (normally the screen); 0 is standard input, 2 is standard error. These numbers are file descriptors.syscall(SYS_write, ...)makes the same system call by its number, with no wrapper.printfitself formats text into a buffer and then makes awritesystem call.
The round trip: trap in, work, return
Real-life example: the bank token. You can't walk into the vault. You hand a token at the counter (one guarded door), the clerk inside checks and does the work, and you get a receipt and go back to your seat.
- The program puts the system-call number and its arguments in registers.
- It runs a trap instruction (on x86-64 it's literally named
syscall). A trap is a deliberate jump into the kernel: the CPU switches the mode bit to 0 and jumps to a fixed kernel entry point, so a program can't choose where it lands. - The kernel looks up the number in its system-call table and runs the handler, after checking the request (is this file yours? is this memory yours?).
- The kernel puts the result (for
write, the number of bytes written; −1 on error) in a register, switches the mode bit back to 1, and returns to the instruction after the trap.
This is why a system call is slower than an ordinary function call: it changes modes, saves and restores state, and the kernel validates everything.
#include <stdio.h>
#include <unistd.h>
int main() {
printf("My process id: %d\n", getpid()); // a system call
printf("My parent's id: %d\n", getppid()); // another one
return 0;
}
Output (the numbers change from run to run):
My process id: 5850
My parent's id: 5844
getpid() returns this program's process ID (PID), its unique number while it runs; getppid() returns its parent's PID, the program that started it. Both are system calls.
OS services
| Service | What the OS does | Example system calls (Linux) |
|---|---|---|
| Program execution | load, run and end programs | fork, execve, exit |
| I/O operations | let programs use devices safely | read, write, ioctl |
| File system | create, read, write, delete files and folders | open, close, mkdir, unlink |
| Communication | let programs exchange data (same machine or network) | pipe, socket, sendto |
| Error detection | notice and handle hardware and program errors | kills a program on a bad instruction |
| Resource allocation | hand out CPU time, memory, devices | mmap, brk |
| Protection and security | keep programs and users apart; check permissions | chmod, setuid |
| Accounting | record who used how much | CPU time per process |
The system calls themselves (categories, API vs ABI, POSIX) are covered in episode 16.
Problem 4: events that can't wait (interrupts and exceptions)
Sometimes the program doesn't ask; something just happens. With 0 typed into the input box:
#include <stdio.h>
int main() {
int plates = 10, guests;
scanf("%d", &guests); // guests = 0
printf("Sharing %d plates...\n", plates);
fflush(stdout);
printf("%d each\n", plates / guests);
return 0;
}
Output:
Sharing 10 plates...
Floating point exception
(In the SyntaxPilot compiler: Runtime Error (SIGFPE).) The CPU can't divide an integer by zero, so it stopped at that instruction and jumped into the kernel by itself. The kernel sent the program the signal SIGFPE ("floating-point exception", a historic name used for integer division errors too), whose default action is to end the program. (A signal is a small notification the kernel sends to a process; more in episode 19.) scanf reads the guest count from input so the compiler can't work out the division in advance.
Three ways into the kernel
An interrupt, in general, is a signal that makes the CPU pause what it is doing, save its place, run a piece of kernel code (an interrupt handler, or interrupt service routine, ISR), and then resume.
| Hardware interrupt | Software interrupt (trap) | Exception (fault) | |
|---|---|---|---|
| Caused by | a device: key press, disk finished, network packet, timer | the program, on purpose | the CPU, when an instruction fails |
| When | any moment: asynchronous (not tied to the current instruction) | exactly at the trap instruction: synchronous | exactly at the failing instruction: synchronous |
| Example | you press a key | syscall for write |
divide by zero, privileged instruction, bad memory access |
| Real-life | the doorbell rings while you cook | you call the waiter | you burn your hand and must stop |
The CPU finds the right handler through the interrupt vector table (on x86, the interrupt descriptor table): a table of handler addresses indexed by interrupt number, set up by the kernel at boot.
The timer interrupt keeps the OS in charge. The kernel programs a hardware timer to interrupt the CPU many times a second. Even if a program runs while(1); forever, the timer fires, the CPU enters the kernel, and the OS can switch to another program. Setting that timer is privileged, so no program can turn it off.
Some books use "exception" for all synchronous events and split them into traps (deliberate, like system calls), faults (fixable, like a page fault, after which the instruction is retried) and aborts (unrecoverable hardware errors).
Problem 5: who reads my commands? (kernel vs shell)
#include <stdlib.h>
int main() {
// hand a command line to the shell, a normal program
system("echo Hello from the shell; uname -s");
return 0;
}
Output:
Hello from the shell
Linux
system() starts a shell and hands it a command line. The shell ran echo, then uname -s, which asks the kernel for its name: Linux. The shell is an ordinary user-mode program.
- A shell is a command interpreter: it reads a command, works out what you mean, and makes system calls to do it (for example
forkandexecto start a program). - A CLI (command line interface) shell is one you type into, like
bashorsh. A GUI (graphical user interface) shell is the desktop of windows and icons you click. - Layers, like a fruit: hardware at the centre, the kernel around it, then system programs (shell, file manager, compilers) and applications in user mode.
| Kernel | Shell | |
|---|---|---|
| Runs in | kernel mode | user mode |
| Job | manage CPU, memory, devices, files | read commands and ask the kernel to run them |
| Always in memory? | yes | only while running |
| Replaceable? | not while running | yes, you can install and switch shells |
"The OS" usually means the kernel plus its system programs, including the shell.
Problem 6: different machines, different needs (types of OS)
The types appeared one after another, each fixing the previous one's problem.
| Type | Idea | Problem it fixes | Problem it leaves |
|---|---|---|---|
| Batch OS | users hand in jobs; similar jobs are grouped into a batch and run one after another, with no interaction | a human loading each job by hand | the CPU sits idle while a job waits for slow I/O; long wait for results |
| Multiprogramming | keep several jobs in memory; when one waits for I/O, the CPU switches to another | idle CPU | users still can't interact; one job could hog the CPU |
| Multitasking / time-sharing | switch between programs every few milliseconds (a time slice) using the timer interrupt, so each user feels the machine is theirs | slow response for interactive users | switching costs time (context switches, episode 17) |
| Multiprocessing | one OS runs programs on several CPUs (or cores) at the same time | one CPU's limit | sharing memory between CPUs safely (synchronization, episodes 24-27) |
| Distributed | many computers on a network cooperate and look like one system to the user | one machine's limits; single point of failure | network delays and partial failures |
| Real-time | guarantees a response within a deadline | unpredictable timing | must be designed for worst cases |
| Embedded | small OS inside a device built for one job | running a dedicated device with tiny memory | limited features |
| Mobile | OS for phones and tablets: touch, battery, sensors, cameras, radios | desktop OSes don't fit phones | battery and security limits |
- Degree of multiprogramming = the number of programs held in memory at the same time.
- Multiprogramming vs multitasking: multiprogramming switches when a job waits; multitasking also switches when a time slice runs out, which keeps response fast.
- Hard real-time: missing a deadline is a failure (an airbag controller, a pacemaker, an aircraft's flight control). Soft real-time: a late result is worse but still acceptable (a video call dropping a frame, live audio).
- Embedded example: FreeRTOS describes itself as a "Real-time operating system for microcontrollers and small microprocessors" (freertos.org). A microcontroller is a small computer (CPU, memory and I/O) on a single chip.
- Mobile example: Android runs on a modified Linux kernel: its own source documentation says "The Android Linux kernel includes enhancements to the Linux 2.6 kernel" (Android source docs).
- A modern laptop OS is multiprogrammed, time-shared and multiprocessing all at once.
Common mistakes (and the real output)
| Mistake | Real output | Why | Fix |
|---|---|---|---|
Running hlt from a normal program |
Segmentation fault |
hlt is privileged: kernel mode only |
ask the kernel through a system call |
plates / guests with guests = 0 |
Floating point exception (SIGFPE) |
the CPU raises an exception; the kernel ends the program | check guests != 0 first |
write(1, "Hello\n", 3) with the wrong length |
prints only Hel |
write sends exactly the bytes you ask for |
pass the real length (strlen) |
| Saying "the shell is the kernel" | (interview: wrong) | the shell is a user-mode program | kernel = core in kernel mode; shell = command interpreter |
| Saying "a system call is a normal function call" | (interview: wrong) | it traps into kernel mode and back | trap → mode switch → handler → return |
| Mixing up multiprogramming and multitasking | (interview: wrong) | multitasking adds time slices | multitasking = multiprogramming + timer-driven switching |
Mini project: watch your own system calls
On any Linux machine with strace installed (sudo apt install strace), save this as hello.c:
#include <stdio.h>
#include <unistd.h>
int main() {
printf("pid %d says hello\n", getpid());
return 0;
}
Then run gcc hello.c -o hello && strace ./hello. strace prints every system call the program makes. Near the end you will find a getpid() line and a write(1, "pid ... says hello\n", ...) line: proof that printf ends in a write system call. Everything before them is the loader setting up the program (episode 16). Count the system calls with strace -c ./hello.
Practice
1. Print the value write() returns.
Solution
#include <stdio.h>
#include <unistd.h>
int main() {
ssize_t n = write(1, "Hello\n", 6);
printf("write returned %zd\n", n);
return 0;
}
Output:
Hello
write returned 6
write returns the number of bytes written, or −1 on error.
2. Make the divide demo safe.
Solution
if (guests == 0) {
printf("No guests, nothing to share\n");
return 0;
}
printf("%d each\n", plates / guests);
3. Name three privileged instructions and why each must be privileged.
Solution
Halt the CPU (one program would freeze everything); disable interrupts (a program could silence the timer and keep the CPU forever); direct device I/O (a program could read another user's disk or overwrite the screen); also changing memory-protection settings (it could read other programs' memory).
4. Classify: an ATM, a pacemaker, a video game console, a cluster of servers acting as one database.
Solution
ATM: embedded (often soft real-time). Pacemaker: embedded, hard real-time. Game console: multitasking with soft real-time needs (a late frame is a glitch, not a disaster). Server cluster: distributed.
5. Hardware interrupt, software interrupt or exception? (a) a network packet arrives, (b) getpid(), (c) reading memory at address 0, (d) the timer fires.
Solution
(a) hardware interrupt. (b) software interrupt (trap). (c) exception (a fault: segmentation fault). (d) hardware interrupt.
Quick quiz
Which of these can a normal program not do directly, in user mode?
A. add two numbers B. call its own function C. turn off interrupts D. read a local variable
Answer
C. Turning interrupts off is a privileged instruction. If a user program could do it, it could stop the timer interrupt and keep the CPU forever.
Interview questions
- What is an operating system? Software between programs and hardware that manages resources (CPU, memory, devices, files) and provides abstractions (files, processes) so programs don't deal with raw hardware.
- What is the kernel? The core of the OS that runs in kernel mode, always in memory, managing the CPU, memory, devices and system calls.
- Kernel vs shell? The kernel manages hardware in kernel mode; the shell is a user-mode command interpreter that asks the kernel to run your commands.
- Why do we need user mode and kernel mode? Protection: a buggy or malicious program must not be able to halt the CPU, read others' memory or control devices directly.
- What is the mode bit? A hardware bit that says whether the CPU is in kernel mode (0) or user mode (1); it decides whether privileged instructions are allowed.
- What is a system call? Walk through one. A program's request to the kernel. Arguments go in registers, a trap instruction switches to kernel mode and jumps to a fixed entry, the kernel validates and runs the handler from the system-call table, then returns the result and switches back to user mode.
- Why is a system call slower than a function call? Mode switch, saving/restoring registers, validation of arguments, and possible cache effects.
- Interrupt vs trap vs exception? Hardware interrupt: from a device, asynchronous. Trap (software interrupt): deliberate, for system calls. Exception: the CPU signals a failing instruction (divide by zero, page fault), synchronous.
- Why is the timer interrupt important? It returns control to the OS regularly, so no program can monopolise the CPU; it makes time-sharing possible.
- Multiprogramming vs multitasking vs multiprocessing? Multiprogramming: several programs in memory, switch on I/O wait. Multitasking: also switch on a time slice for fast response. Multiprocessing: several CPUs running programs in parallel.
- Hard vs soft real-time? Hard: a missed deadline is a failure (pacemaker). Soft: late is degraded but tolerated (video streaming).
- What are the services an OS provides? Program execution, I/O, file system, communication, error detection, resource allocation, protection/security, accounting.
Glossary
| Term | Meaning in plain words | Example |
|---|---|---|
| Operating system (OS) | Software between programs and hardware that shares the hardware and hides its details | Linux |
| Resource manager | The OS's job of sharing CPU, memory, disks and devices fairly and safely | two programs both get CPU time |
| Extended machine | The OS's job of offering a simpler machine built from abstractions | files instead of disk sectors |
| Abstraction | A simple idea that hides complex details | a "file" |
| Sector | The fixed-size block a disk really reads and writes | 512 bytes or 4 KB |
| Kernel | The core of the OS, running in kernel mode, always in memory | Linux is a kernel |
| Shell | A command interpreter: reads your commands and asks the kernel to run them | sh, bash |
| Command interpreter | A program that reads and carries out commands | the shell |
| CLI | Command line interface: you type commands | a terminal |
| GUI | Graphical user interface: windows and icons | a desktop |
| User mode | CPU mode for ordinary programs; privileged instructions are blocked | our C program |
| Kernel mode | CPU mode for the kernel with full hardware control (supervisor/privileged mode) | the write handler |
| Mode bit | Hardware bit telling the CPU which mode it is in (0 = kernel, 1 = user) | flipped by a trap |
| Privileged instruction | An instruction allowed only in kernel mode | hlt, disable interrupts |
| Segmentation fault | The program was stopped for an illegal memory access or forbidden operation (signal SIGSEGV) | our hlt demo |
| System call | A program's request to the kernel to do something privileged | write, getpid |
| Wrapper (libc) | A C library function that sets up and makes a system call for you | write() |
| Trap | A deliberate instruction that switches to kernel mode and jumps to a fixed kernel entry | x86-64 syscall |
| System-call table | The kernel's table mapping system-call numbers to handlers | SYS_write → write handler |
| File descriptor | A small number a process uses to refer to an open file or device | 1 = standard output |
| Standard output | The default place a program's output goes, file descriptor 1 | the screen |
| Process | A running program | our demo while it runs |
| Process ID (PID) | A process's unique number | getpid() |
| Parent process | The process that started another | getppid() |
| OS services | The functions an OS provides to programs and users | file system, I/O |
| Interrupt | A signal that makes the CPU pause, run kernel code, and resume | a key press |
| Interrupt handler (ISR) | The kernel code run for an interrupt | keyboard handler |
| Interrupt vector table | Table of handler addresses indexed by interrupt number | set up at boot |
| Hardware interrupt | An interrupt from a device; asynchronous | disk finished reading |
| Software interrupt | An interrupt a program causes on purpose (a trap) | a system call |
| Exception | An event the CPU raises when an instruction fails; synchronous | divide by zero |
| Synchronous | Happening at a specific instruction, caused by it | an exception |
| Asynchronous | Happening at any moment, independent of the current instruction | a hardware interrupt |
| Signal | A small notification the kernel sends to a process | SIGFPE, SIGSEGV |
| SIGFPE | Signal for an arithmetic error such as integer divide by zero | our divide demo |
| Timer interrupt | A periodic hardware interrupt that returns control to the OS | time-sharing |
| Batch OS | Runs grouped jobs one after another with no interaction | early computer centres |
| Multiprogramming | Several programs in memory; switch when one waits for I/O | CPU stays busy |
| Degree of multiprogramming | Number of programs in memory at once | 5 jobs loaded |
| Multitasking / time-sharing | Switching between programs every time slice so all feel responsive | a laptop |
| Time slice (quantum) | The short time a program runs before the OS may switch | a few milliseconds |
| Multiprocessing | One OS running programs on several CPUs at once | a multi-core laptop |
| Distributed OS | Many networked computers working as one system | a server cluster |
| Real-time OS (RTOS) | An OS that guarantees responses within deadlines | FreeRTOS |
| Hard real-time | Missing a deadline is a failure | pacemaker |
| Soft real-time | Missing a deadline degrades quality but is tolerated | video call |
| Embedded OS | A small OS inside a single-purpose device | washing machine |
| Microcontroller | A small computer (CPU, memory, I/O) on one chip | inside a washing machine |
| Mobile OS | An OS for phones and tablets | Android |
Summary
- An operating system sits between programs and hardware: a resource manager (shares CPU, memory, devices) and an extended machine (files, processes instead of raw hardware).
- The CPU's mode bit separates user mode (programs) from kernel mode (the kernel). Privileged instructions like
hltonly work in kernel mode; our program was killed with a segmentation fault. - Programs ask the kernel through system calls: a trap switches to kernel mode at a fixed entry, the kernel checks and works, then returns.
printfends in awritesystem call. - The kernel is entered by hardware interrupts (devices, asynchronous), software interrupts/traps (system calls) and exceptions (failing instructions, synchronous). The timer interrupt keeps the OS in charge.
- The shell is a user-mode command interpreter (CLI or GUI); the kernel is the core.
- OS types evolved: batch → multiprogramming → time-sharing/multitasking → multiprocessing; plus distributed, real-time (hard, soft), embedded and mobile.
Next episode: OS structure, system calls and booting: monolithic vs microkernel, the kinds of system calls, API vs ABI, POSIX, and what happens from the power button to the login screen.