Chapter 1: Introduction

Operating Systems
What is an operating system?User and kernel modeExtended machine Resource managerHistoryProcessor and registers Memory hierarchyMulticore chipsPolling, interrupts, DMA Kinds of OSPolicy and mechanismFrom program to process
Sercan KÜLCÜ · Giresun University · Computer Engineering

Introduction

Learning Outcomes

Definition and place

Explain where the OS sits between hardware and applications, and its two basic roles (extended machine, resource manager).

Protection

Explain kernel mode and user mode and the controlled transitions between them (system call, interrupt, exception).

Historical development

Describe which problems batch processing, multiprogramming and time sharing solved.

Hardware basics

Explain how the processor and registers, the memory hierarchy and multicore chips shape the OS.

I/O methods

State the key difference between polling, interrupts and DMA; tell an interrupt from a trap.

Concepts

Explain, with examples, the kinds of OS, the policy–mechanism split and how a program becomes an executable.

Introduction

What Is an Operating System?

  • A modern computer has a multicore processor, gigabytes of memory, an SSD, a network card, a GPU and dozens of devices.
  • No application developer can know every detail of this hardware and program it directly.
  • Operating system (OS): the software layer that manages the hardware and gives applications a simple, safe and common interface.
  • Kernel: the privileged part that is always resident in memory; the "operating system" in the narrow sense.
  • Users interact with the computer through a shell (command line) or a GUI (graphical user interface).
DesktopWindows 11, macOS, Linux distributions (Ubuntu, Fedora), ChromeOS
MobileAndroid, iOS / iPadOS, HarmonyOS
Server and cloudLinux, Windows Server, FreeBSD
Embedded and IoTZephyr, FreeRTOS, Contiki-NG, VxWorks, QNX

Introduction

Components of a Computer System

UsermodeKernelmodeHardwareWeb browserE-mail clientMusic playerShell / GUI (user-interface program)Operating system (kernel)ProcessorMemorySSD/DiskNetwork cardKeyboard/MouseDisplayPrinter
Hardware: processor, memory, storage and I/O devices. Software: the OS running in kernel mode and everything that runs in user mode; even the shell/GUI is an ordinary user program.

Introduction

Shell and GUI

● ● ●Terminal: bash$ ls ~/notesch1.md ch2.md$ ps aux | grep firefoxsercan 4211 2.1 firefox$ uname -srLinux 6.12.8$ _Desktop (GUI)DocumentsSettingsBrowserFile Explorerwindow · icon · menu · pointer
  • Shell: a command-line interface (CLI) · bash, zsh, fish, PowerShell, cmd.exe.
  • GUI (graphical user interface): visual interaction with windows, icons, menus and a pointer (WIMP) · Windows Explorer, macOS Finder, GNOME, KDE Plasma.
  • Neither is part of the kernel: both are user-mode programs that request kernel services through system calls. So they can be replaced and customized, and several can run at once.

Introduction

Shell or GUI?

Shell

  • Automation with scripts: repeating a task a thousand times is easy.
  • Pipelines: combine small tools, as in ps aux | grep ssh | wc -l.
  • Light on resources; remote server administration over SSH.
  • Does specific tasks quickly and precisely.

GUI

  • Intuitive and discoverable: no commands to memorize.
  • Natural for visual content (photos, maps, design).
  • Reaches a wide audience; accessibility tools.
  • Touch and voice interfaces belong to this family too.
They are not rivals but complements: the GUI for everyday work, the shell for repetitive and remote work. Both use the same kernel services; the kernel does not know which interface a request came from.

Introduction

Device Manager

  • A Windows administrative tool that lists the hardware devices attached to the computer and their drivers (devmgmt.msc, an MMC snap-in).
  • Shows a device's type, vendor, driver version and the resources it uses (interrupt, memory range).
  • Update or roll back a driver, disable a device; problem devices are flagged with a warning icon.
  • The real work is done by the Plug and Play (PnP) manager and device drivers in the kernel; the tool is just a view of them.
  • Linux equivalents: lspci, lsusb, lsblk, lsmod, /sys and udev.
COMPUTER-01Disk drivesNVMe SSD 1 TBDisplay adaptersGPU (discrete)Network adaptersWi-Fi 7Ethernet 2.5 GbEUSB controllersUSB root hub (USB 3.2)Other devicesUnknown device!

Position and Modes

Where the Operating System Sits

  • The OS is the interface between hardware (the physical parts: CPU, RAM, disk) and software (code describing the work the computer should do).
  • Applications reach the hardware not directly but through the system calls the OS provides.
UsersApplications (browser, office, games, compiler)System-call interfaceOperating system (kernel)Hardware (CPU, RAM, SSD, devices)• controls how software uses the hardware• optimizes hardware utilization• provides security and isolation

Position and Modes

Kernel Mode and User Mode

  • The processor has a mode bit / privilege level: x86 rings 0–3, ARMv8 EL0–EL3, RISC-V U/S/M.
  • Kernel mode: every instruction and all of memory are available; privileged instructions such as I/O, disabling interrupts or changing the page table are allowed.
  • User mode: privileged instructions are forbidden; trying one makes the processor raise an exception (trap), and the kernel may kill the process.
  • The shell and GUI are the lowest level of user-mode software.
  • User code enters the kernel only through controlled gates: system call, interrupt, exception.
Ring 0kernelRing 1Ring 2Ring 3 · applicationsx86: rings 1–2 are unused in practice

Position and Modes · Animation

Mode Switch Step by Step: the read() System Call

User modeKernel modeUser processn = read(fd, buf, 100);libc: read() wrapperrax=0, rdi=fd, …; syscallSystem-call table[0] → sys_read()sys_read(): checkarguments, copy datatrap (syscall)sysret (return)mode bit1user

The process runs in user mode (mode bit = 1). It wants to read a file but cannot touch the disk itself.

User modeKernel modeUser processn = read(fd, buf, 100);libc: read() wrapperrax=0, rdi=fd, …; syscallSystem-call table[0] → sys_read()sys_read(): checkarguments, copy datatrap (syscall)sysret (return)mode bit1user

The read() wrapper in the C library puts the call number (x86-64 Linux: read = 0, in rax) and the arguments in registers and executes syscall.

User modeKernel modeUser processn = read(fd, buf, 100);libc: read() wrapperrax=0, rdi=fd, …; syscallSystem-call table[0] → sys_read()sys_read(): checkarguments, copy datatrap (syscall)sysret (return)mode bit0kernel

Trap: the hardware sets the mode bit to 0, saves the return address and jumps to a fixed kernel entry point. The process cannot choose where it lands.

User modeKernel modeUser processn = read(fd, buf, 100);libc: read() wrapperrax=0, rdi=fd, …; syscallSystem-call table[0] → sys_read()sys_read(): checkarguments, copy datatrap (syscall)sysret (return)mode bit0kernel

The kernel uses the number to find sys_read() in the system-call table, validates the arguments (does buf really belong to the process?) and does the work.

User modeKernel modeUser processn = read(fd, buf, 100);libc: read() wrapperrax=0, rdi=fd, …; syscallSystem-call table[0] → sys_read()sys_read(): checkarguments, copy datatrap (syscall)sysret (return)mode bit1user

The result (bytes read) goes into rax; sysret sets the mode bit back to 1 and the process continues after read().

Position and Modes

Physical Devices, Microarchitecture, Machine Language

BankingReservationsBrowserCompilerEditorShellOperating systemMachine language (ISA)MicroarchitecturePhysical devicesApplicationSystemHardwareSoftware
Physical devicesChips, memory modules, SSDs, I/O devices.
MicroarchitectureThe processor's internal design: datapath, registers, ALU, caches, control unit.
Machine language (ISA)Binary instructions the hardware executes directly: x86-64, ARM64, RISC-V.
The OS's jobDrivers turn requests from above into low-level commands the device understands.

A compiler translates a program into machine language; the OS does not. The OS loads and runs the compiled program and mediates its access to the hardware.

Roles of the OS

The OS as an Extended Machine

  • Software built on top of the hardware that makes the hardware easier to use.
  • Makes the hardware's capabilities usable and hides its messy details.
  • Keeps applications from touching the hardware directly, for both convenience and protection.
  • Example: writing a block to an SSD involves NVMe queues, LBAs, error codes… The application just says write(fd, buf, n).
  • In Tanenbaum's words, the OS turns ugly hardware into beautiful abstractions.
Application programsBeautiful interfaceopen, read, write, fork …Operating systemUgly interfaceregisters, interrupts, timingHardware

Roles of the OS

Abstractions and Roles

HardwareOS abstraction
Processor (CPU)Process and thread
Memory (RAM)Address space (virtual memory)
Storage (SSD/disk)File and directory
Network cardSocket
Display, keyboardWindow, terminal

Each abstraction makes a limited, shared hardware resource appear private to each program.

Hardware designerDesigns the ISA, the devices and their interfaces.
Kernel developerImplements the abstractions on the hardware.
Application developerUses the abstractions through system calls and APIs.
End userSees only the applications and the interface.

Roles of the OS · Animation

The Journey of a write() Call

Applicationwrite(fd, "Hello", 5)C library (libc)write() wrapper → syscallSystem-call interfaceuser → kernel modeVFS (virtual file system)fd → open file → inodeFile system (ext4, NTFS, APFS)file offset → block addressPage cache + block layerdirty page, I/O queueDevice driver (NVMe)NVMe write command, DMASSD controllerwrites a flash pageuser mode ↑kernel mode ↓

The application supplies only a file descriptor (fd), a buffer and a length; it does not even know what kind of disk it is.

Applicationwrite(fd, "Hello", 5)C library (libc)write() wrapper → syscallSystem-call interfaceuser → kernel modeVFS (virtual file system)fd → open file → inodeFile system (ext4, NTFS, APFS)file offset → block addressPage cache + block layerdirty page, I/O queueDevice driver (NVMe)NVMe write command, DMASSD controllerwrites a flash pageuser mode ↑kernel mode ↓

The libc wrapper puts the arguments in registers and enters the kernel with syscall.

Applicationwrite(fd, "Hello", 5)C library (libc)write() wrapper → syscallSystem-call interfaceuser → kernel modeVFS (virtual file system)fd → open file → inodeFile system (ext4, NTFS, APFS)file offset → block addressPage cache + block layerdirty page, I/O queueDevice driver (NVMe)NVMe write command, DMASSD controllerwrites a flash pageuser mode ↑kernel mode ↓

VFS: the fd is resolved through the process's open-file table to a file object and its i-node. One common layer for all file systems.

Applicationwrite(fd, "Hello", 5)C library (libc)write() wrapper → syscallSystem-call interfaceuser → kernel modeVFS (virtual file system)fd → open file → inodeFile system (ext4, NTFS, APFS)file offset → block addressPage cache + block layerdirty page, I/O queueDevice driver (NVMe)NVMe write command, DMASSD controllerwrites a flash pageuser mode ↑kernel mode ↓

The file system maps the position in the file (the offset) to a block address on disk, allocating a new block if needed.

Applicationwrite(fd, "Hello", 5)C library (libc)write() wrapper → syscallSystem-call interfaceuser → kernel modeVFS (virtual file system)fd → open file → inodeFile system (ext4, NTFS, APFS)file offset → block addressPage cache + block layerdirty page, I/O queueDevice driver (NVMe)NVMe write command, DMASSD controllerwrites a flash pageuser mode ↑kernel mode ↓

Data usually goes into the page cache first and write() returns at once; the disk write happens later (fsync() for durability).

Applicationwrite(fd, "Hello", 5)C library (libc)write() wrapper → syscallSystem-call interfaceuser → kernel modeVFS (virtual file system)fd → open file → inodeFile system (ext4, NTFS, APFS)file offset → block addressPage cache + block layerdirty page, I/O queueDevice driver (NVMe)NVMe write command, DMASSD controllerwrites a flash pageuser mode ↑kernel mode ↓

The driver builds an NVMe command; the SSD fetches the data from memory by DMA, writes it to flash and raises an interrupt when done.

Roles of the OS

The OS as a Resource Manager

  • Lets several programs run at the same time, and allocates and protects resources such as the CPU, memory and I/O devices.
  • Goal: use resources efficiently and share them fairly among programs.
  • Top-down view: it provides abstractions to applications. Bottom-up view: it manages the pieces of a complex system.
  • Resources are multiplexed in two ways: in time (taking turns) and in space (dividing into parts).
Questions: what if several programs want to print at once? How is each process's resource usage measured? How do we keep fairness and efficiency as resources multiply?
Time multiplexing: one CPUABCABCAtimeSpace multiplexing: memoryOSABCfreePrinter → time · disk blocks → space

Roles of the OS · Animation

When Several Programs Print at Once

Word processorPDF viewerBrowserPrinteroutput gets mixed!

Three programs want to print almost at once, and there is only one printer. If all of them wrote to it directly, the pages would get interleaved.

Word processorPDF viewerBrowserspool queue (disk)job 1: report.pdfjob 2: photo.jpgjob 3: note.txtPrinter

Programs write their jobs not to the printer but to the OS's spool queue on disk, and go straight back to work.

Word processorPDF viewerBrowserspool queue (disk)job 1: report.pdfjob 2: photo.jpgjob 3: note.txtSpoolerserviceCUPS / Print SpoolerPrinter

The only process that uses the printer is the spooler (Linux: CUPS, Windows: Print Spooler); this exclusive ownership prevents mixing.

Word processorPDF viewerBrowserspool queue (disk)job 1: report.pdfjob 2: photo.jpgjob 3: note.txtSpoolerserviceCUPS / Print SpoolerPrinternow: report.pdf

The spooler usually sends jobs in arrival order (FCFS); an administrator can apply priorities or pause jobs. Job 1 is printing now.

Word processorPDF viewerBrowserspool queue (disk)job 1: report.pdfjob 2: photo.jpgjob 3: note.txtSpoolerserviceCUPS / Print SpoolerPrinternow: photo.jpg

Job 1 is done; job 2 is next. While the slow printer worked, the programs and the CPU did other things: I/O overlaps with computation.

Roles of the OS · Step through the code

Code Trace: The Spool Queue

char *queue[4];
int in = 0, out = 0, count = 0;
void enqueue(char *file) {   /* programs */
  queue[in] = file;
  in = (in + 1) % 4;
  count++;
}
void spooler(void) {
  while (count > 0) {
    send_to_printer(queue[out]);
    queue[out] = NULL;   /* free slot */
    out = (out + 1) % 4;
    count--;
  }
}
Spool queue (on disk)[0][1][2][3]inoutcount = 0PrinteridlePrinted:

The queue has 4 slots and is circular: in points to the next free slot, out to the first job to print. Empty queue: count = 0.

Spool queue (on disk)a.pdf[0][1][2][3]inoutcount = 1PrinteridlePrinted:

Program 1 calls enqueue("a.pdf"): the file goes into slot [0], in = 1, count = 1. The program returns to work without waiting for the printer.

Spool queue (on disk)a.pdf[0]b.txt[1][2][3]inoutcount = 2PrinteridlePrinted:

enqueue("b.txt") → [1], in = 2, count = 2.

Spool queue (on disk)a.pdf[0]b.txt[1]c.doc[2][3]inoutcount = 3PrinteridlePrinted:

enqueue("c.doc") → [2], in = 3, count = 3.

Spool queue (on disk)a.pdf[0]b.txt[1]c.doc[2]d.png[3]inoutcount = 4PrinteridlePrinted:

enqueue("d.png") → [3]; in = (3 + 1) % 4 = 0: it wraps around to the start. The queue is full: count = 4, in = out.

Spool queue (on disk)a.pdf[0]b.txt[1]c.doc[2]d.png[3]inoutcount = 4Printera.pdfPrinted:

The spooler runs: count = 4 > 0. The oldest job, queue[out] = a.pdf, goes to the printer (FCFS).

Spool queue (on disk)[0]b.txt[1]c.doc[2]d.png[3]inoutcount = 3Printera.pdfPrinted:a.pdf

The slot is freed (queue[0] = NULL): out = 1, count = 3. The printer is slow; meanwhile the programs do other work.

Spool queue (on disk)[0][1]c.doc[2]d.png[3]inoutcount = 2Printerb.txtPrinted:a.pdfb.txt

count = 3 > 0: b.txt is sent; out = 2, count = 2.

Spool queue (on disk)[0][1][2]d.png[3]inoutcount = 1Printerc.docPrinted:a.pdfb.txtc.doc

count = 2 > 0: c.doc is sent; out = 3, count = 1.

Spool queue (on disk)[0][1][2][3]inoutcount = 0Printerd.pngPrinted:a.pdfb.txtc.docd.png

count = 1 > 0: d.png is sent; out = 0, count = 0.

Spool queue (on disk)[0][1][2][3]inoutcount = 0PrinteridlePrinted:a.pdfb.txtc.docd.png

out has wrapped to 0 as well; count = 0 → the loop ends. Since only the spooler touched the printer, the output was not mixed. Concurrent count++ / count-- is a race, so real queues are protected by a lock.

Roles of the OS

Measuring Resource Usage

ResourceMetricLinuxWindows
CPUUser / kernel-mode CPU time, utilization percentage/proc/[pid]/stat, top, timeTask Manager, Performance Monitor
MemoryResident set (RSS), virtual size, working set; allocation/free pattern/proc/[pid]/status, smem"Working set" column
I/OBytes read/written, number of system calls and I/O requests/proc/[pid]/io, iotop, strace -cResource Monitor
$ grep -E 'VmRSS|Threads' /proc/4211/status
VmRSS:    412360 kB
Threads:  57
The numbers come from the kernel: it updates per-process counters on every context switch, system call and page fault.

Roles of the OS

Fairness and Efficiency

More resources (many cores, large memory) bring performance and responsiveness, but make sharing them fairly and efficiently harder.

SchedulingWhich process runs when, and on which core? Linux 6.6 (2023) replaced CFS with the EEVDF scheduler.
Prioritizationnice values, Windows priority classes, real-time classes.
Quotas and limitsLinux cgroups v2 (containers), ulimit, disk quotas, Windows Job objects.
Trade-offThroughput ↔ response time ↔ fairness: improving one can hurt another.
The right approach depends on the workload: throughput on a server, response time on a desktop, meeting deadlines in a real-time system.

Roles of the OS

Three Big Ideas: Four Small Experiments

$./print X & ./print Y & ./print Z &[1] 3101 [2] 3102 [3] 3103XYZZXY…print X“the CPU is mine”print Y“the CPU is mine”print Z“the CPU is mine”really one CPU, taking turns:XYZXYZXYZtimea few ms per slice; the OS decideswho runs when

Virtualizing the CPU: on one core, three programs seem to run at once. Through time sharing the OS gives each the illusion of its own CPU; who runs next is policy (scheduling), how the switch happens is mechanism (context switch).

$setarch -R ./counter & setarch -R ./counter &(4012) p = 0x5555555592a0(4013) p = 0x5555555592a0(4012) *p = 1(4013) *p = 1(4013) *p = 2(4012) *p = 2…process 4012virtual address space0x5555555592a0process 4013virtual address space0x5555555592a0physicalmemoryframe 17frame 42same virtual address, different physical frame

Virtualizing memory: two processes print the same address, yet their values grow independently. Each process sees its own address space; the OS maps them to different physical frames. ASLR is turned off (setarch -R) to see the same address; otherwise addresses change on every run.

$./incr 1000result: 2000$./incr 1000000result: 1318442$./incr 1000000result: 1407715(expected: 2000000)T1: counter++ N timesT2: counter++ N timescountercounter++ is really three instructions:loadaddstoreif the other thread cuts in, an incrementis lost: the result is below 2N

Concurrency: two threads each increment a shared counter N times. For small N the result looks right; for large N it is below 2N and differs on every run, because counter++ is not atomic (sample output).

$./save$cat note.txthello disk$sudo reboot… (the system shuts down and boots again)$cat note.txthello diskprogram: open, write, closeOS: file systemdevice driver: I/O requestSSD / disk: persistentblock choice, metadata, ordering: all in the OS

Persistence: DRAM is volatile; to keep data across a reboot we write it to disk through a file system. The program only calls open, write, close; choosing blocks, metadata and talking to the device are the OS's job.

History

A History of Operating Systems

1945–55Vacuum tubesNo OS; plugboards1st gen.1955–65TransistorsBatch systems2nd gen.1965–80IntegratedcircuitsMultiprogramming,time sharing3rd gen.1980–Personal computersMS-DOS, Mac, Windows,Linux4th gen.1990–Mobile devicesSymbian, iOS,Android5th gen.2000–Virtualizationand the cloudHypervisors, containers,IoTtoday
  • Each generation follows a leap in hardware technology (tube → transistor → integrated circuit → microprocessor).
  • Each generation asks the same question again: how do we make better use of the expensive resource (CPU, memory, human time)?
  • The last row is not one of Tanenbaum's generations; it is added to help explain today's systems.

History

The First and Second Generations

1st generation (1945–55): vacuum tubes
  • Room-sized, slow, failure-prone machines (ENIAC, 1945).
  • The same group of engineers designed, built, programmed and maintained them.
  • Programming in machine language or with plugboards; punched cards in the early 1950s.
  • No programming languages and no operating system; used mostly for numerical tables.
2nd generation (1955–65): transistors and batch systems
  • Transistors replaced tubes: smaller, more reliable, more energy-efficient.
  • Mainframes in air-conditioned rooms; an operator between programmer and machine.
  • Batch processing: jobs are collected on tape and a small monitor program runs them one after another, automatically; the operator only carries tapes.
  • One job in memory: the CPU idles during I/O; no interaction.
Batch processing aimed to eliminate the manual changeover time between jobs. It was unsuitable for interactive and real-time use.

History · Animation

An Early Batch System

Cardreader1401Introductiontape7094Outputtape1401PrinterSystemtapeoperator carriesoperator carries(a)(b)(c)(d)(e)(f)

(a) Programmers bring their card decks to the 1401's card reader.

Cardreader1401Introductiontape7094Outputtape1401PrinterSystemtapeoperator carriesoperator carries(a)(b)(c)(d)(e)(f)

(b) The cheap 1401 reads a batch of jobs and writes them to the input tape (keeping slow card reading off the expensive machine).

Cardreader1401Introductiontape7094Outputtape1401PrinterSystemtapeoperator carriesoperator carries(a)(b)(c)(d)(e)(f)

(c) The operator carries the input tape to the expensive 7094.

Cardreader1401Introductiontape7094Outputtape1401PrinterSystemtapeoperator carriesoperator carries(a)(b)(c)(d)(e)(f)

(d) The 7094 runs the jobs one by one under the monitor on the system tape and writes the output to the output tape.

Cardreader1401Introductiontape7094Outputtape1401PrinterSystemtapeoperator carriesoperator carries(a)(b)(c)(d)(e)(f)

(e) The operator carries the output tape back to the 1401.

Cardreader1401Introductiontape7094Outputtape1401PrinterSystemtapeoperator carriesoperator carries(a)(b)(c)(d)(e)(f)

(f) The 1401 prints the output (offline printing). The expensive 7094 never had to read cards or print.

History

IBM 1401, 7094 and System/360

MachineYearFeatures
IBM 14011959Business data processing; character-oriented and relatively cheap. Also used for I/O work such as reading cards, copying to tape and printing. One of the most common computers of the 1960s.
IBM 70941962Scientific computing; 36-bit words, an architecture entirely different from the 1401. Ran batch jobs under IBSYS / FMS. Time sharing was tried on a modified 7094 at MIT with CTSS.
System/3601964A family sharing one architecture from small to large models. Its OS, OS/360, had multiprogramming and spooling; a huge, bug-ridden project of millions of lines (Brooks, The Mythical Man-Month).
The 1401 and 7094 did not run under OS/360, which was written for the System/360 family announced in 1964. Nor was the 7094 a bigger 1401; it was a separate machine that complemented it.

History

A Typical FMS Job

$ENDProgram data$RUN$LOADFORTRAN program$FORTRAN$JOB, 10,7710802, STUDENT
  • FMS = Fortran Monitor System: a batch monitor program for the IBM 709/7094.
  • Control cards starting with $ tell the monitor what to do: start the job, load the compiler, link the program, run it, finish.
  • These cards are the ancestors of today's shell scripts and IBM JCL.
Do not confuse FMS here with the "Flexible Manufacturing System" of factory automation; the two are unrelated.

History

Third Generation: ICs and Multiprogramming

  • Integrated circuits (1965–80): compatible families such as System/360; smaller, cheaper, more reliable.
  • Multiprogramming: memory is split into partitions, one job per partition. When a job waits for I/O, the CPU switches to another job.
  • On one CPU the jobs interleave: not true parallelism but concurrency.
  • Protection hardware is needed so jobs cannot touch each other's memory.
  • Spooling: cards go straight to disk; when a job finishes, the next one is loaded from disk.
  • Time sharing: CTSS (MIT, 1961) → MULTICS → UNIX (Bell Labs, 1969) → BSD, POSIX, MINIX, Linux (1991).
Job 3Job 2Job 1Operating systemmemory partitionsThree jobs in memory

History · Animation

Multiprogramming: Why the CPU Stays Busy

UniprogrammedCPUI/OMultiprogrammedCPUI/O (A)I/O (B)A: CPU 2 · I/O 4 · CPU 2B: CPU 3 · I/O 3 · CPU 10123456789101112131415

Two jobs (time units): A = CPU 2, I/O 4, CPU 2 · B = CPU 3, I/O 3, CPU 1. Total CPU demand: 8 units.

UniprogrammedCPUI/OMultiprogrammedCPUI/O (A)I/O (B)AABBABidleidle0123456789101112131415done at t=15 · CPU 53%

Uniprogrammed: first A (t = 0–8), then B (t = 8–15). The CPU is idle during I/O: utilization 8 / 15 ≈ 53%.

UniprogrammedCPUI/OMultiprogrammedCPUI/O (A)I/O (B)AABBABidleidleABA0123456789101112131415done at t=15 · CPU 53%

Multiprogrammed: when A starts I/O at t = 2, the CPU switches to B at once (t = 2–5). A's I/O proceeds in the background.

UniprogrammedCPUI/OMultiprogrammedCPUI/O (A)I/O (B)AABBABidleidleABAABB0123456789101112131415done at t=15 · CPU 53%

B starts I/O at t = 5 (5–8). A's I/O ends at t = 6: the CPU idles only from 5 to 6; A runs 6–8, B runs 8–9.

UniprogrammedCPUI/OMultiprogrammedCPUI/O (A)I/O (B)AABBABidleidleABAABB0123456789101112131415done at t=15 · CPU 53%done at t=9 · CPU 89%

The same two jobs finish in 9 units instead of 15; CPU utilization is 8 / 9 ≈ 89%. The gain comes from overlapping CPU and I/O.

History

Fourth Generation: Personal Computers

YearDevelopment
1974CP/M for the Intel 8080 (Gary Kildall): the first widely used disk-based OS for microcomputers.
1981The IBM PC and MS-DOS (derived from 86-DOS); a BASIC interpreter in ROM.
1973–84GUI ideas from Xerox PARC → Apple Lisa (1983) and Macintosh (1984): a user-friendly interface.
1985–2000Windows 1.0 … 3.x were graphical environments on top of MS-DOS; Windows 95/98/ME were still MS-DOS based.
1993–Windows NT, written from scratch → 2000, XP, 7, 10, 11: the core of today's Windows.
1991–Linus Torvalds's Linux kernel; in 2001 the UNIX-based Mac OS X (Darwin/XNU) → today's macOS.

Rapidly falling prices put computers in everyone's hands; the OS's goal shifted from "use the machine efficiently" to "make life easy for the user".

History

Fifth Generation: Mobile and Today

Mobile devices (1990–)

  • 1990s: PDAs, Palm OS; 2000s: Symbian, BlackBerry OS.
  • 2007 iOS (iPhone), 2008 Android (on the Linux kernel).
  • Windows Phone launched in 2010; development stopped in 2017.
  • Today Android and iOS share the market; touch interfaces, power management and permission models stand out.
Virtualization and the cloudHypervisors (KVM, Hyper-V, Xen); many virtual machines on one server.
ContainersDocker, Kubernetes: isolated environments sharing one kernel (namespaces, cgroups).
Heterogeneous hardwareCPU + GPU + NPU; hybrid processors with big and little cores.
SecurityMemory-safe languages: Rust support in the kernel since Linux 6.1.

Hardware

Components of a Personal Computer

CPUMemoryDisplaycontrollerKeyboardcontrollerUSBcontrollerDiskcontrollerMMUBusMonitorKeyboardPrinter (USB)SSD / diskDevices →
  • The CPU, memory and I/O devices communicate over a bus; each device has a controller.
  • The MMU (memory-management unit) sits inside the CPU and translates virtual addresses to physical ones.
  • Real systems use point-to-point links (PCIe) instead of one shared bus, but the conceptual model is the same.

Hardware

Processor and Registers

  • The computer's "brain": the processor (CPU) executes every instruction.
  • The basic cycle: fetch, decode, execute.
  • Memory is slow, so variables and intermediate results are kept in registers: load (memory → register), store (register → memory).
  • Each processor family has its own instruction set: an x86-64 program does not run directly on ARM.
  • Performance: clock rate, core count, cache size, pipeline depth, memory bandwidth.
  • On a context switch (changing processes) the OS saves and restores all registers.
Registerx86-64ARM64
Program counter: next instructionRIPPC
Stack pointer: top of the stackRSPSP
PSW: condition bits, mode, interrupt enableRFLAGSPSTATE
General purpose16 of them31 of them
PC0x1008SP0x7FF0PSWmode, condition, interruptR0 … R15intermediate resultsMemory0x1004instruction0x1008instruction0x100Cinstruction…0x7FF0stack top0x7FF8stack

Hardware

Multicore Chips

(a) Shared L2Core 1L1Core 2L1Core 3L1Core 4L1Shared L2(b) Private L2 per coreCore 1L2Core 2L2Core 3L2Core 4L2(c) Today: private L1+L2, shared L3Core 1L1L2Core 2L1L2Core 3L1L2Core 4L1L2Shared L3 (LLC)
MulticoreSeveral independent CPUs on one chip; the OS sees and schedules each as a separate processor.
SMT / Hyper-ThreadingOne core runs two hardware threads; the OS sees two "logical CPUs", but they share resources.
Hybrid coresPerformance (P) and efficiency (E) cores (ARM big.LITTLE, Intel 2021+): the scheduler must know which to pick.

For the OS this means problems of parallelism, synchronization and cache coherence.

Hardware

Memory Hierarchy

RegistersCache L1/L2/L3Main memory (DRAM)SSD (NVMe)Hard disk (HDD)Archive: tape, cloudfastcostlylargecheap
LevelAccess (approx.)Capacity (typical)
Registers< 1 ns< 1 KB
L1 cache~1 ns32–64 KB
L2 / L33–20 ns1–100+ MB
DRAM60–100 ns8–64 GB
NVMe SSD20–100 µs0.5–4 TB
HDD5–10 ms2–24 TB

DRAM and above are volatile (lost on power-off); SSDs and HDDs are persistent. Figures are rough 2026 magnitudes. Each level acts as a cache for the one below: thanks to locality, most accesses are served by the fast upper levels.

I/O

Three I/O Methods

Every I/O device has a controller; the kernel's device driver writes commands to the controller's registers and reads its status. There are three ways to learn that the work is done:

MethodKey difference
PollingThe CPU reads the status register in a loop until the device is ready; it can do nothing else while waiting (busy waiting).
InterruptThe device signals the CPU at the end of each unit of work; in between, the CPU runs another process.
DMA (direct memory access)The controller moves the whole block to memory itself; the CPU steps in only at the start (programming it) and the end (one interrupt).
An interrupt from a device and a trap caused by the executing instruction itself (divide by zero, page fault, system call) enter the kernel the same way: the processor switches to kernel mode and jumps to the handler in the table.

Kinds of OS

Kinds of Operating Systems (1)

MainframeVery high I/O capacity, reliability and availability; batch, transaction processing and time sharing. Banks, airlines. IBM z/OS, z/VM, Linux on Z, Unisys MCP, Fujitsu BS2000.
ServerWeb, database, file and mail services for many users over a network. Data centers, cloud, virtualization (KVM, Hyper-V), containers. Linux, Windows Server, FreeBSD.
MultiprocessorSeveral cores/sockets; runs work in parallel and deals with synchronization and NUMA. Today every general-purpose OS is in this class. Linux, Windows, macOS.
PersonalSingle user, a good GUI, multitasking; browsing, office work, games, file management. Windows 11, macOS, Linux distributions, ChromeOS.

Kinds of OS

Kinds of Operating Systems (2)

Mobile (handheld)Touch interface, battery and thermal management, sensors, app store and permission model. Android, iOS/iPadOS, HarmonyOS. (Windows Phone ended in 2017.)
EmbeddedCars, appliances, network gear, aircraft; users usually do not install software, and the system is fixed in flash. Embedded Linux (Yocto), Zephyr, FreeRTOS, QNX, VxWorks.
Sensor nodeA few tens of KB of RAM, years on a battery, event-driven design, low-power radio (802.15.4, BLE). IoT and M2M networks. Contiki-NG, RIOT, Zephyr; TinyOS is now historical.
Smart cardVery limited CPU and memory; authentication, payment, cryptography, secure storage. Bank cards, SIM/eSIM, ID cards. Java Card, MULTOS.
1 KB1 MB1 GB1 TBtypical RAM (log scale) →smart cardsensor nodeembeddedmobilepersonalservermainframe

Kinds of OS

Real-Time Operating Systems

Hard: brake control2 ms of work every 5 msSoft: video framedeadline ~16.7 mslate → frame droppeddeadline0 ms5 ms10 ms15 ms20 ms
  • What matters is predictability, not speed: a job must finish before its deadline.
  • Hard: missing a deadline is a disaster (brakes, airbags, flight control, industrial robots).
  • Soft: an occasional miss is tolerated; quality drops (audio/video streaming, games).
  • Examples: VxWorks (Mars rovers), QNX (in-car systems), FreeRTOS, Zephyr.
  • Linux's PREEMPT_RT patches were merged into mainline in 2024 (6.12); the old RTLinux project has ended.
  • Other uses: audio, video, motion and sensor processing, train control, defense.

Basic Concepts

Policy and Mechanism

  • Policy: what will be done?
  • Mechanism: how will it be done?
  • Separating the two is a key design principle: when policy decisions change later, the mechanism need not be rewritten → maximum flexibility.
Example: on Linux the scheduling class can be changed with sched_setscheduler() and the I/O scheduler through /sys at run time; the kernel code stays the same.
Mechanism (fixed kernel code)timer interrupt → context switchslice = 4 msslice = 10 mspolicy plugs inreplaceable
MechanismPolicy
Timer interruptHow many ms should a time slice be?
Priority schedulerWhich process gets high priority?
File permission bits (rwx)Who gets which permission?
Page-replacement machineryWhich page gets evicted?
cgroupsHow much CPU/memory for which container?

Striking these balances takes creativity as much as engineering.

Basic Concepts

MS-DOS and FreeBSD

(a) MS-DOS at bootfree memorycommand interpreterkernel(b) MS-DOS running a programfree memoryprocessinterpreter (resident part)kernel(c) FreeBSD: many processesprocess Dfree memoryprocess Cinterpreterprocess Bkernel

MS-DOS: single-tasking, one memory area. The shell (COMMAND.COM) is loaded at boot; a program is loaded over its transient part, and when the program ends the shell reloads itself from disk. No multitasking, no protection.

FreeBSD (a UNIX derivative): multitasking. When a user logs in, a shell starts; it creates a new process with fork() and loads the program into it with exec(), then waits for it or (with &) runs it in the background and keeps taking commands.

Basic Concepts · Animation

Building an Executable

code.cPreprocessorcode.iCompilercode.sAssemblercode.oLinkerprog (ELF)Loaderprocesslibc.so, other.o

The source file code.c: human-readable C code.

code.cPreprocessorcode.iCompilercode.sAssemblercode.oLinkerprog (ELF)Loaderprocesslibc.so, other.o

Preprocessor (gcc -E): pulls in headers with #include, expands macros, resolves conditional compilation with #ifdef → code.i.

code.cPreprocessorcode.iCompilercode.sAssemblercode.oLinkerprog (ELF)Loaderprocesslibc.so, other.o

Compiler (gcc -S): translates C into the target processor's assembly language and optimizes → code.s.

code.cPreprocessorcode.iCompilercode.sAssemblercode.oLinkerprog (ELF)Loaderprocesslibc.so, other.o

Assembler (gcc -c): turns assembly into machine code → the object file code.o. Addresses of external functions (printf) are not yet known.

code.cPreprocessorcode.iCompilercode.sAssemblercode.oLinkerprog (ELF)Loaderprocesslibc.so, other.o

Linker (gcc code.o other.o -o prog): combines object files and libraries and resolves addresses → an executable (Linux: ELF, Windows: PE/.exe).

code.cPreprocessorcode.iCompilercode.sAssemblercode.oLinkerprog (ELF)Loaderprocesslibc.so, other.o

./prog: the shell does fork + exec; the kernel's loader maps the ELF into memory, the dynamic linker binds libc.so, and the program runs as a process.

Basic Concepts

Metric Units

Exp.NameSymbolExp.NameSymbol
10−3millim103kilok
10−6microµ106megaM
10−9nanon109gigaG
10−12picop1012teraT
10−15femtof1015petaP
10−18attoa1018exaE
Binary (IEC)Value
1 KiB (kibibyte)210 = 1024 B
1 MiB220 B
1 GiB230 B
1 TiB240 B
Memory is usually measured in binary units (16 GiB RAM), disks and networks in decimal: 1 TB = 1012 B ≈ 0.91 TiB; 1 Gb/s = 109 bit/s. Byte B, bit b.
1 ns1 µs1 ms1 sL1 hitDRAMcontext switchNVMe readHDD accesseye blinkeach tick 1000×

Summary

Review Questions

  1. What happens if a user program executes a privileged instruction?
  2. Why does multiprogramming raise CPU utilization? How does it differ from true parallelism?
  3. Which of polling, interrupts and DMA suits bringing a 4 KB disk block into memory? Why?
  4. Why are the upper levels of the memory hierarchy small and the lower ones large? Why does the hierarchy work?
  5. Why must the OS save all registers on a context switch?
  6. Distinguish policy from mechanism with an example.
Show answers
  1. The processor raises an exception (trap); the kernel usually kills the process.
  2. While one job waits for I/O the CPU runs another; on one CPU jobs interleave rather than run simultaneously.
  3. DMA: the whole block moves without keeping the CPU busy, with a single interrupt at the end.
  4. Fast memory is expensive and small; cheap memory is slow. Thanks to locality, most accesses are served by the small, fast upper levels.
  5. There is one set of registers in the CPU and the next process overwrites them; the interrupted process must resume with the same PC, SP and intermediate values.
  6. The timer interrupt is mechanism; the length of the time slice is policy.

Summary

References

  • A. Silberschatz, P. B. Galvin, G. Gagne, Operating System Concepts, 10th ed., Wiley, 2018.
  • A. S. Tanenbaum, H. Bos, Modern Operating Systems, 4th ed., Pearson, 2014; 5th ed., 2023.
  • R. H. Arpaci-Dusseau, A. C. Arpaci-Dusseau, Operating Systems: Three Easy Pieces (OSTEP), version 1.10, pages.cs.wisc.edu/~remzi/OSTEP.
  • Linux man pages: syscall(2), fork(2), execve(2), proc(5).

The End

Chapter 1: Introduction
Next: Chapter 2: Operating-System Structures
1 / 1