📘 Lecture 1 — Overview of today’s lecture
📖 Overview: This lecture introduces the advanced operating systems course, covering objectives, prerequisites, and the fundamental concepts of operating systems. It explores the dual nature of an OS as both a virtual machine abstraction and a resource manager, and discusses key design issues, types of operating systems, and basic classifications of distributed and parallel systems.
🗂️ Topics Covered
The lecture covers course objectives and prerequisites from a design and research perspective, introduces the definition of an operating system from top-down and bottom-up views, details resource multiplexing techniques, explores major OS issues like structure, sharing, naming, security, and performance, discusses protection and security threats as a case study, and classifies different types of operating systems including mainframe, server, multiprocessor, PC, real-time, and embedded systems. It concludes with an overview of distributed and parallel systems.
📝 Lecture Summary
Course Objectives and Pre-requisites
The course takes an in-depth design perspective, focusing on why things are done in a certain way rather than just how. Students will learn to distinguish between good and bad designs, consider engineering tradeoffs, and understand practical aspects from a computer scientist’s viewpoint. The course includes a case study of the Linux kernel with comparisons to Windows where applicable, along with 4–5 programming assignments for hands-on training in using OS services. From a research perspective, students will read contemporary and classical research literature on OS topics.
🔑 Pre-requisites: Essential requirements are C/C++ programming and an undergraduate course on data structures (lists, stacks, queues, trees). A first course on operating systems is helpful but not strictly required as basics will be covered before deeper topics.
What is an operating system?
From a top-down view, the OS provides an extended or virtual machine abstraction to user programs, making programming easier than dealing directly with hardware. All services are invoked through system calls. From a bottom-up view, the OS acts as a resource manager for processors, memories, timers, disks, network interfaces, and other hardware, managing allocation of these resources to user programs in an orderly and controlled manner.
Resource multiplexing
The OS multiplexes resources in two ways: time multiplexing and space multiplexing. Time multiplexing involves different programs taking turns using a resource, with examples including CPU scheduling and printer sharing. Space multiplexing involves different programs getting a part of the resource, possibly at the same time, with the example of memory being divided among several running programs.
The major OS issues
The key design issues an OS must address include: Structure (how is the OS organized?), Sharing (how are resources shared across users?), Naming (how are resources named?), Security (how is integrity ensured?), Protection (how is one user/program protected from another?), Performance (how do we make it fast?), Reliability (what happens when something goes wrong?), Extensibility (can we add new features?), and Communication (how do programs exchange information, including across a network?).
Additional issues include: Concurrency (how are parallel activities created and controlled?), Scale (what happens as demands or resources increase?), Persistence (how do you make data last longer than program executions?), Distribution (how do multiple computers interact?), and Accounting (how do we track and charge for resource usage?).
💡 Why this matters: These issues represent the fundamental design challenges every OS must solve. Understanding them provides the framework for evaluating any operating system.
Protection and security as an example
Protection exists at multiple levels: from no protection at all, to protecting the OS from user programs, protecting one user's program from another, and protecting a program from itself. Security threats include: access by intruding individuals, access by intruding programs, denial of service (DoS), distributed denial of service (DDoS), spoofing, spam, worms, viruses, Trojan horses (knowingly downloaded bugs), and cookies/spyware (unknowingly downloaded).
Type of Operating Systems
Mainframe operating systems handle huge I/O activity with thousands of disks, providing batch processing (routine non-interactive jobs like claims processing), transaction processing (large numbers of small requests like bank check processing), and time-sharing (multiple remote users running jobs simultaneously, like database queries). Example: OS/390.
Server operating systems run on large PCs, workstations, or mainframes, serving multiple users over a network simultaneously, allowing sharing of hardware and software. Examples include web servers and database transaction servers. OS examples: Win2K, XP, and UNIX flavors.
Multiprocessor operating systems are variations of server operating systems with special provisions for connectivity and communication management between multiple CPUs.
PC operating systems provide a nice interface to a single user, typically used for word processing, spreadsheets, and Internet access.
Real-time operating systems are characterized by time as the key parameter — real-time response to events is more important than any other design goal. They are classified as hard real-time or soft real-time. Example applications: industrial process control, robotics, air traffic control, network routers, multimedia systems.
Embedded operating systems reside in small devices like PDAs, TV sets, microwave ovens, and mobile phones. They have characteristics of real-time systems (mainly soft real-time) with restraints on power consumption and memory usage. Examples: PalmOS, Windows CE. The pinnacle is smart-card systems.
Distributed Systems
Distributed systems distribute computation among several physical processors in a loosely coupled system, where each processor has its own local memory and processors communicate through communications lines (high-speed buses or telephone lines). Advantages include: resource sharing, computation speed-up through load sharing, reliability, and communications.
Parallel Systems
Parallel systems are multiprocessor systems with more than one CPU in close communication, forming a tightly coupled system where processors share memory and a clock, with communication through shared memory. Advantages include: increased throughput, economical benefits, increased reliability, graceful degradation, and fail-soft systems.
⭐ Key Takeaways
The most critical concepts from this lecture are: (1) The OS serves dual roles as both a virtual machine abstraction making hardware easier to program and a resource manager multiplexing resources in time and space. (2) The major design issues span structure, sharing, naming, security, protection, performance, reliability, extensibility, communication, concurrency, scale, persistence, distribution, and accounting. (3) Operating systems are classified into mainframe, server, multiprocessor, PC, real-time, embedded, distributed, and parallel types, each optimized for different workloads and constraints. (4) Protection and security exist at multiple levels, from protecting the OS from programs to defending against viruses, worms, and denial of service attacks. (5) Distributed systems are loosely coupled with independent memory, while parallel systems are tightly coupled with shared memory — each offering different advantages for computation and reliability.
🧠 Quick Revision Questions
- What are the two views of an operating system, and what does each view emphasize?
- Explain the difference between time multiplexing and space multiplexing with examples.
- List at least six of the major OS design issues discussed in the lecture.
- What is the key characteristic that distinguishes real-time operating systems from other types, and what are its two sub-categories?
- What is the primary difference between a distributed system and a parallel system in terms of memory and communication?
📘 Lecture 2 — Overview of today’s lecture
📖 Overview: This lecture provides a comprehensive overview of the major components of an operating system, including process management, memory management, I/O, and file systems. It also explores different operating system architectures—monolithic, layered, microkernel, and virtual machine monitors—highlighting their trade-offs in performance, reliability, and complexity.
🗂️ Topics Covered
This lecture covers major OS components such as process management, memory management, I/O, secondary storage, file system, protection, accounting, shell, GUI, and networking. It then examines OS structure and internal architecture, comparing monolithic design, layering, microkernels, and virtual machine monitors. The lecture concludes with a re-cap of key structural design considerations.
📝 Lecture Summary
Major OS Components
The operating system consists of several major components that manage different aspects of the computer system. These include process management, memory management, I/O (input/output), secondary storage, file system, protection, accounting, shell (OS user interface), GUI (graphical user interface), and networking. Each component performs a specialized function that together enables the OS to manage hardware and provide services to user programs.
Process Operation
The OS provides a process abstraction interface that defines standard operations on processes. These operations include: create a process, delete a process, suspend a process, resume a process, clone a process, inter-process communication, inter-process synchronization, and create/delete a child process. These operations form the basic API for process management.
I/O
A large portion of the OS kernel deals with I/O—Windows XP contains millions of lines of code, including drivers. The OS provides a standard interface between programs and devices. Device drivers are routines that interact with specific device types; they encapsulate device-specific knowledge such as how to initialize a device, how to request I/O, and how to handle interrupts and errors. Examples include SCSI device drivers, Ethernet card drivers, video card drivers, and sound card drivers. Windows has approximately 35,000 device drivers.
Secondary Storage
Secondary storage (disk, tape) is persistent memory, often using magnetic media that survives power failures. Routines that interact with disks operate at a very low level in the OS and are used by many components. They handle scheduling of disk operations, head movement, error handling, and management of space on disk. These routines are usually independent of the file system, although cooperation can exist—file system knowledge of device details can help optimize performance, such as placing related files close together on disk.
File System
Secondary storage devices are crude and awkward to use directly (e.g., writing a 4096-byte block to a sector). File systems provide a convenient abstraction. A file is the basic long-term storage unit, and a directory is just a special kind of file.
🔑 Definition — File: The basic long-term storage unit managed by the file system.
Command interpreter (shell)
The command interpreter (shell) is a particular program that handles the interpretation of user commands and helps manage processes. On some systems, the command interpreter may be a standard part of the OS; on others, it is just non-privileged code that provides an interface to the user; on others, there may be no command language at all.
File system operations
The file system interface defines standard operations including file (or directory) creation and deletion, manipulation of files and directories, copy, and lock operations. File systems also provide higher-level services such as accounting and quotas, backup, indexing or search, and file versioning.
Accounting
The accounting component keeps track of resource usage, both to enforce quotas (e.g., "you're over the disk limit") or to produce bills. This is important for time-shared computers like mainframes.
Networking
An OS typically has a built-in communication infrastructure that implements: a network protocol software stack, a route lookup module to map a given destination address to a next hop, and a name lookup service to map a given name to a destination machine.
OS structure
It is not always clear how to stitch OS modules together. An OS consists of all these components plus many other components and system programs (e.g., bootstrap code, the init program). Major issues include: How do we organize all this? What are all the code modules, and where do they exist? How do they cooperate? This is a massive software engineering and design problem—designing a large complex program that performs well, is reliable, is extensible, and is backwards compatible.
Early structure: Monolithic
Traditionally, operating systems like UNIX and DOS were built as a monolithic entity where all OS components run in kernel mode.
Monolithic Design advantages:
- Cost of module interaction is low
Disadvantages:
- Hard to understand
- Hard to modify
- Unreliable
- Hard to maintain
The alternative is to find ways to organize the OS to simplify its design and implementation.
Layering
The traditional alternative approach is layering, which implements the OS as a set of layers, where each layer presents an enhanced virtual machine to the layer above. The first described layered system was Dijkstra's THE system, with layers: Layer 5 (job managers), Layer 4 (device managers), Layer 3 (console manager), Layer 2 (pager manager), Layer 1 (Kernel), Layer 0 (Hardware).
🔑 Definition — Layering: An OS design where the system is implemented as a set of layers, each presenting an enhanced virtual machine to the layer above.
Problems with layering: Layering imposes a hierarchical structure, but real systems are more complex. For example, the file system requires VM services (buffers), while VM would like to use files for its backing store. Strict layering is not flexible enough and causes poor performance because each layer crossing has overhead. There is a disjunction between model and reality—systems are modeled as layers but not really built that way.
Microkernel's
Microkernels became popular in the late 80's and early 90's, with a recent resurgence for small devices. The goal is to have minimum functionality in the kernel, with most OS functionality in user-level servers (e.g., file servers, terminal servers, memory servers). Each part becomes more manageable, and crashing of one service doesn't bring the system down.
- This results in better reliability (isolation between components)
- Ease of extension and customization
- Poor performance (due to user/kernel boundary crossing)
The kernel provides basic primitives such as transport of messages, loading programs into memory, and device handling. Policy decisions are made in user space while mechanisms are implemented in the microkernel. The microkernel lends itself well to object-oriented design principles and component-based design.
Disadvantage: Performance. Solutions: Reduce microkernel size or increase microkernel size.
💡 Why this matters: The microkernel architecture fundamentally trades performance for reliability and modularity—a key design decision in modern embedded and secure systems.
Virtual Machine Monitors
Virtual Machine Monitors (VMMs) export a virtual machine to user programs that resembles hardware. A virtual machine consists of all hardware features including user/kernel modes, I/O, interrupts, and everything a real machine has. A virtual machine may run any OS. Examples include Java Virtual Machine (JVM), VMware, and User-Mode Linux (UML).
- Advantage: Portability
- Disadvantage: Slow speed
💡 Why this matters: VMMs enable running multiple operating systems on a single physical machine, which is foundational to cloud computing and server virtualization.
⭐ Key Takeaways
The most critical concepts from this lecture are: (1) An operating system consists of many major components including process management, memory management, I/O, secondary storage, file system, protection, accounting, shell, GUI, and networking—each with specific responsibilities. (2) OS structure designs involve critical trade-offs: monolithic designs offer low module interaction cost but are hard to maintain, while layered designs provide structure but suffer from performance overhead and inflexibility. (3) Microkernels improve reliability and extensibility by putting most OS functionality in user-level servers, but the user/kernel boundary crossing significantly degrades performance. (4) Virtual Machine Monitors allow running any OS on a virtualized hardware platform, offering portability at the cost of speed. (5) The core software engineering challenge is designing a large complex OS program that simultaneously achieves good performance, reliability, extensibility, and backwards compatibility.
🧠 Quick Revision Questions
- List at least five major components of an operating system and briefly describe the function of each.
- What are the advantages and disadvantages of a monolithic OS design compared to a microkernel design?
- What specific problem with strict layering does the example of "file system requires VM services, but VM needs files for backing store" illustrate?
- In a microkernel architecture, where are policy decisions made versus where are mechanisms implemented?
- What is the primary advantage and the primary disadvantage of using a Virtual Machine Monitor (VMM)?
📘 Lecture 3 — Overview of today’s lecture
📖 Overview: This lecture examines the ELF object file format in detail, explaining how object files are structured into sections and headers. It then covers the crucial processes of symbol relocation, external reference resolution, and static and shared library mechanisms in linking. Understanding these concepts is essential for grasping how programs are built from multiple source files into executable binaries.
🗂️ Topics Covered
The lecture begins with the ELF object file format, detailing the ELF header, program header table, and all key sections (.text, .data, .bss, .symtab, .rel.text, .rel.data, .debug). It uses an example C program to illustrate relocating symbols and resolving external references, showing relocation info for both .text and .data sections, and the merged executable output. The discussion covers merging relocatable object files into executables, strong and weak symbols with linker rules, linker puzzles, packaging commonly used functions via static libraries (.a archives), creating and using static libararies, linker algorithms for resolving references, disadvantages of static libraries, and finally shared libraries (DLLs) with dynamic linking mechanisms.
📝 Lecture Summary
[ELF Object File Format]
The ELF (Executable and Linkable Format) object file is structured into several key components. The Elf header contains metadata including the magic number (identifying the file as ELF), type (relocatable .o, executable, or shared .so), machine architecture, and byte ordering (endianness). The program header table specifies memory segments: page size, virtual addresses, and segment sizes for loading. The .text section holds the program code. The .data section contains initialized static data (global variables with initial values). The .bss section holds uninitialized static data — interpreted as "Block Started by Symbol" or humorously "Better Save Space" — it has a section header but occupies no space in the file. The .symtab section is the symbol table, listing procedure and static variable names along with section names and locations. The .rel.text section contains relocation info for .text: addresses of instructions that will need modification in the executable and instructions for how to modify them. The .rel.data section holds relocation info for .data: addresses of pointer data that need modification in the merged executable. The .debug section provides info for symbolic debugging (generated with gcc -g).
[Example C Program]
The lecture presents an example C program but does not show the full source code; instead, it proceeds directly to the relocation info for the object files.
[Relocating Symbols and Resolving External References]
Symbols are lexical entities that name functions and variables. Each symbol has a value (typically a memory address). Code consists of symbol definitions and symbol references. References can be either local (within the same file) or external (referring to symbols defined in other files).
🔑 Definition — Symbol: A lexical entity that names a function or variable, having a value (typically a memory address).
[m.o Relocation Info]
The lecture shows relocation info for m.o in a table format. The table includes columns for Offset (hexadecimal addresses like 0x0, 0x4, 0x5, 0x7, 0x8, 0x9, 0xa, 0xc), Type (R_386_32 or R_386_PC32), and Symbol (such as array, swap, .text, buf, bufp0, count). For example, at offset 0x0: Type R_386_32, Symbol array; at offset 0x4: Type R_386_PC32, Symbol swap; at offset 0x7: Type R_386_PC32, Symbol .text; at offset 0x8: Type R_386_32, Symbol buf; etc. The relocation information tells the linker which addresses need modification when combining object files.
[a.o Relocation Info (.text)]
The lecture presents relocation info for the .text section of a.o. The table shows Offset values (0x0, 0x5, 0x7, 0x8, 0xb, 0xc, 0xd, 0xe, 0x12, 0x1c), Type (R_386_PC32 or R_386_32), and Symbol (including _init, libc_init_first, atexit, main, _exit, bufp0, count, buf). For example: at offset 0x0: Type R_386_PC32, Symbol _init; at offset 0x5: Type R_386_PC32, Symbol libc_init_first; at offset 0x7: Type R_386_PC32, Symbol atexit; at offset 0x8: Type R_386_PC32, Symbol main; at offset 0xb: Type R_386_PC32, Symbol _exit; at offset 0xc: Type R_386_32, Symbol bufp0; etc.
[a.o Relocation Info (.data)]
The lecture shows relocation info for the .data section of a.o. The table has Offset (0x0, 0x4), Type (R_386_32 for both), and Symbol (buf and bufp0). This tells the linker to resolve references to buf and bufp0 in the data section.
[Executable after Relocation and External Reference Resolution (.text)]
The lecture shows the merged executable's .text section after relocation. It contains a startup sequence: at address 0x080480c0 <start>:
call libc_init_firstcall _initcall atexitcall maincall _exit
Note: The code that pushes arguments for each function is not shown.
[Executable After Relocation and External Reference Resolution (.data)]
The lecture shows a table for the merged executable's .data section with Address (e.g., memory addresses) and Initial Value for symbols like buf, bufp0, and count. For example, buf (16-byte array at address 0x08049010 with hex initial values) and bufp0 (initial pointer value pointing somewhere, e.g., 0x08049010).
[Merging Relocatable Object Files into an Executable Object File]
The lecture includes a diagram but no additional explanatory text. The concept is that the linker merges the sections from all input relocatable object files into a single executable, resolving all symbol references and performing relocation.
[Strong and Weak Symbols]
Program symbols are classified as either strong or weak. Strong symbols are procedures (functions) and initialized global variables. Weak symbols are uninitialized global variables. The Linker's Symbol Rules are:
- Rule 1: A strong symbol can only appear once.
- Rule 2: A weak symbol can be overridden by a strong symbol of the same name. References to the weak symbol resolve to the strong symbol.
- Rule 3: If there are multiple weak symbols, the linker can pick an arbitrary one.
🔑 Definition — Strong symbol: Procedures and initialized globals. 🔑 Definition — Weak symbol: Uninitialized globals.
📌 Example (implicit from rule 2): If file1.c defines int x = 5; (strong) and file2.c defines int x; (weak), the linker resolves all references to x to the strong definition with value 5.
[Linker Puzzles]
The lecture presents a slide titled "Linker Puzzles" but does not provide the specific puzzle content in the text. This section highlights common pitfalls that arise from the strong/weak symbol rules, such as accidental name collisions and unexpected behavior when global variables are declared multiple times.
[Packaging Commonly Used Functions]
How to package functions commonly used by programmers (e.g., Math, I/O, memory management, string manipulation)? The linker framework has awkward trade-offs:
- Option 1: Put all functions in a single source file. Programmers link a big object file into their programs, which is space and time inefficient.
- Option 2: Put each function in a separate source file. Programmers explicitly link appropriate binaries into their programs. This is more efficient but burdensome on the programmer.
The solution is static libraries (.a archive files): Concatenate related relocatable object files into a single file with an index (called an archive). Enhance the linker so it tries to resolve unresolved external references by looking for symbols in one or more archives. If an archive member file resolves a reference, link it into the executable.
[Static Libraries (archives)]
Static libraries further improve modularity and efficiency by packaging commonly used functions [e.g., C standard library (libc), math library (libm)]. The linker selects only the .o files in the archive that are actually needed by the program. Creating static libraries uses an archiver (e.g., ar) which allows incremental updates: recompile a function that changes and replace its .o file in the archive. Commonly used libraries include:
- libc.a (the C standard library) — 8 MB archive of 900 object files; provides I/O, memory allocation, signal handling, string handling, data and time, random numbers, integer math.
- libm.a (the C math library) — 1 MB archive of 226 object files; provides floating point math (sin, cos, tan, log, exp, sqrt, ...).
[Using Static Libraries]
The linker's algorithm for resolving external references is:
- Scan
.ofiles and.afiles in command line order. - During the scan, keep a list of the current unresolved references.
- As each new
.oor.afile is encountered, try to resolve each unresolved reference against the symbols in that file. - If any entries remain in the unresolved list at end of scan, then error.
The problem with this algorithm is that command line order matters! The moral: put libraries at the end of the command line.
[Shared Libraries]
Static libraries have several disadvantages:
- Potential for duplicating lots of common code in the executable files on a filesystem (e.g., every C program needs the standard C library).
- Potential for duplicating lots of code in the virtual memory space of many processes.
- Minor bug fixes of system libraries require each application to explicitly relink.
The solution is shared libraries (also called dynamic link libraries or DLLs), whose members are dynamically loaded into memory and linked into an application at run-time. Dynamic linking can occur:
- When the executable is first loaded and run — common case for Linux, handled automatically by
ld-linux.so. - After the program has begun — in Linux, done explicitly by the user with
dlopen(). This is the basis for High-Performance Web Servers.
Shared library routines can be shared by multiple processes.
📐 Formula / Mechanism: Dynamic linking → Library code loaded at run-time, not linked at compile-time. 💡 Why this matters: Shared libraries reduce disk and memory usage, allow independent updates to libraries without relinking applications, and enable runtime extensibility.
[Dynamically Linked Shared Libraries]
The lecture includes a diagram showing how dynamically linked shared libraries appear in the linking and loading process. The key concept is that the executable contains only stubs or pointers to shared library functions, which are resolved by the dynamic linker at load time.
[The Complete Picture]
The lecture shows the complete startup sequence for a C program:
- Start-up code in init segment (same for all C programs)
- At address
0x080480c0 <start>:call libc_init_first(startup code in .text)call _init(startup code in .init)call atexit(startup code in .text)call main(application's entry point)call _exit(return control to OS)
Note: The code that pushes the arguments for each function is not shown.
⭐ Key Takeaways
The ELF object file format is a fundamental concept — students must know the purpose of each section (especially .text, .data, .bss, .symtab, .rel.text, .rel.data) and the ELF header fields. Symbol relocation and external reference resolution are critical processes that the linker performs to combine object files into executables using relocation information (offsets, types, and symbol names). The distinction between strong symbols (procedures and initialized globals) and weak symbols (uninitialized globals), along with the three linker rules, is essential because they determine how the linker handles multiple definitions and are a common source of linker errors. Static libraries (.a archives) improve modularity by allowing the linker to select only needed object files, but command-line order matters for resolution. Finally, shared libraries (DLLs) overcome static library disadvantages by enabling dynamic linking at load time or runtime (via ld-linux.so and dlopen()), reducing duplication and allowing independent updates.
🧠 Quick Revision Questions
- What information is stored in the ELF header, and what is the purpose of the .bss section?
- What is the difference between a strong symbol and a weak symbol in the context of linking?
- What are the three linker rules for handling strong and weak symbols?
- How does the linker resolve external references when processing static libraries (.a archives), and why does command-line order matter?
- What are two mechanisms by which dynamic linking of shared libraries can occur, and what are the key advantages of shared libraries over static libraries?
📘 Lecture 4 — Overview of today’s lecture
📖 Overview: This lecture introduces the fundamental concept of a process, its address space, and how the operating system manages multiple processes. It explains how the CPU's control flow is altered through exceptions, interrupts, traps, and faults, and describes the mechanisms for context switching and process state management. Understanding these concepts is critical for grasping how OSes achieve multitasking and resource management.
🗂️ Topics Covered
The lecture covers the definition of a process, private address spaces and their components, methods of executing the operating system (non-process kernel, execution within user processes, process-based OS), mechanisms for altering CPU control flow including interrupts, traps, faults, and aborts with examples. It also details context switching, process creation, and process state models including the two-state and five-state models with their transitions.
📝 Lecture Summary
Process
A process is defined as a program in execution — an instance of a program running on a computer. It is the entity that can be assigned to and executed on a processor. More formally, a process is a unit of activity characterized by the execution of a sequence of instructions, a current state, and an associated set of system instructions.
🔑 Definition — Process: A program in execution; an instance of a program running on a computer; the entity that can be assigned to and executed on a processor.
Private Address Spaces
Each process has its own private address space. This means that processes do not share memory with each other unless explicitly arranged. The address space contains all the memory locations a process can reference, including its code, data, heap, and stack.
💡 Why this matters: Private address spaces provide isolation between processes — if one process crashes, it does not affect others. This is a foundation of modern OS reliability and security.
Implementing the Process Abstraction
The OS must maintain data structures to manage processes, including:
- Process Control Block (PCB) – contains all information about a process
- Address space – the memory layout for the process
- Registers – saved when a process is not running
The Address Space
The address space of a process typically contains:
- Program code (instructions)
- Global variables (data segment)
- Heap (dynamically allocated memory)
- Stack (function call frames, local variables)
Execution of the Operating System
Three approaches exist for executing OS code:
- Non-process Kernel: Execute kernel outside of any process. OS code is executed as a separate entity that operates in privileged mode.
- Execution Within User Processes: OS software runs within the context of a user process. The process executes in privileged mode when executing OS code.
- Process-Based Operating System: Implement the OS as a collection of system processes. Useful in multi-processor or multi-computer environments.
Control Flow
Computers do only one thing: from startup to shutdown, a CPU simply reads and executes a sequence of instructions, one at a time. This sequence is the system's physical control flow (or flow of control).
Altering the Control Flow
Two basic mechanisms available to the programmer for changing control flow:
- Jumps and branches
- Function call and return using the stack discipline
Both react to changes in program state only — they are insufficient for reacting to changes in system state such as:
- Data arriving from a disk or a network adapter
- Instruction dividing by zero
- User hitting Ctrl-C at the keyboard
- System timer expiring
The system needs mechanisms for exceptional control flow.
Exceptional Control Flow
Mechanisms for exceptional control flow exist at all levels of a computer system.
- Low level Mechanism: Exceptions — change in control flow in response to a system event (i.e., change in system state). This is a combination of hardware and OS software.
- Higher Level Mechanisms: Process context switch, Signals, Nonlocal jumps (setjmp/longjmp). Implemented by either OS software (context switch and signals) or C language runtime library (nonlocal jumps).
System context for exceptions
Exceptions
An exception is a transfer of control to the OS in response to some event (i.e., change in processor state).
Asynchronous Exceptions (Interrupts)
Caused by events external to the processor:
- Indicated by setting the processor's interrupt pin
- Handler returns to "next" instruction
Examples:
- I/O interrupts: hitting Ctrl-C at the keyboard, arrival of a packet from a network, arrival of a data sector from a disk
- Hard reset interrupt: hitting the reset button
- Soft reset interrupt: hitting Ctrl-Alt-Delete on a PC
Interrupt Vectors
- Each type of event has a unique exception number k
- Index into a jump table (a.k.a., interrupt vector)
- Jump table entry k points to a function (exception handler)
- Handler for k is called each time exception k occurs
Synchronous Exceptions
Caused by events that occur as a result of executing an instruction:
-
Traps: Intentional. Examples: system calls, breakpoint traps, special instructions. Returns control to "next" instruction.
-
Faults: Unintentional but possibly recoverable. Examples: page faults (recoverable), protection faults (unrecoverable). Either re-executes faulting ( "current" instruction) or aborts.
-
Aborts: Unintentional and unrecoverable. Examples: parity error, machine check. Aborts current program.
🔑 Definition — Trap: An intentional synchronous exception, such as a system call, that returns control to the next instruction. 🔑 Definition — Fault: An unintentional but possibly recoverable synchronous exception, such as a page fault. 🔑 Definition — Abort: An unintentional and unrecoverable synchronous exception that terminates the current program.
Trap Example — Opening a File
- User calls
open(filename, options) - Function
openexecutes system call instructionint - OS must find or create file, get it ready for reading or writing
- Returns integer file descriptor
📌 Example: When a user program calls open("data.txt", O_RDONLY), this triggers a trap. The CPU switches to kernel mode, the OS finds the file "data.txt" on disk, checks permissions, and returns a file descriptor (e.g., 3) that the program uses for subsequent read/write operations.
Fault Example #1 — Memory Reference (Recoverable)
- User writes to a memory location
- That portion (page) of user's memory is currently on disk
- Page handler must load page into physical memory
- Returns to faulting instruction
- Successful on second try
📌 Example: A program tries to access address 0x8049600, but that page is currently swapped to disk. The MMU triggers a page fault. The OS's page fault handler loads the page from disk into physical RAM (e.g., at frame 42), updates the page table, and returns to re-execute the same instruction — this time it succeeds.
Fault Example #2 — Memory Reference (Unrecoverable)
- User writes to a memory location
- Address is not valid
- Page handler detects invalid address
- Sends SIGSEGV signal to user process
- User process exits with "segmentation fault"
📌 Example: A program dereferences a null pointer: *ptr = 5; where ptr = NULL. The address 0x00000000 is not mapped in the process's address space. The OS's fault handler detects an invalid address, sends signal SIGSEGV (segmentation violation), and the process terminates with an error message.
The Abstract Machine Interface
This interface defines how user programs interact with the OS through exceptions and system calls.
Context Switching
Context switching is the mechanism by which the OS switches the CPU from executing one process to another. This involves saving the state (registers, program counter) of the current process and restoring the state of the next process to run.
🔑 Definition — Context Switching: The process of saving the state of the currently running process and restoring the state of another process to resume its execution.
When to Switch a Process
- Clock interrupt: Process has executed for the maximum allowable time slice
- I/O interrupt
- Memory fault: Memory address is in virtual memory so it must be brought into main memory
- Trap: Error or exception occurred — may cause process to be moved to Exit state
- Supervisor call: Such as file open
Process Creation
Steps for creating a new process:
- Assign a unique process identifier
- Allocate space for the process
- Initialize process control block
- Set up appropriate linkages (e.g., add new process to linked list used for scheduling queue)
- Create or expand other data structures (e.g., maintain an accounting file)
Change of Process State
Steps when a process changes state:
- Save context of processor including program counter and other registers
- Update the process control block of the process that is currently in the Running state
- Move process control block to appropriate queue – ready; blocked; ready/suspend
- Select another process for execution
- Update the process control block of the process selected
- Update memory-management data structures
- Restore context of the selected process
Two-State Process Model
A process may be in one of two states:
- Running: Currently being executed by the CPU
- Not-running: Not currently being executed — waiting to be scheduled
Not-Running Process in a Queue
Processes in the Not-running state are kept in a queue. When the scheduler decides to run a new process, it selects one from this queue, moves it to Running, and performs a context switch.
Five-state Model
Processes may be waiting for I/O. The model uses additional states:
- Running: Currently being run
- Ready: Ready to run
- Blocked: Waiting for an event (e.g., I/O)
- New: Just created, not yet admitted to set of runnable processes
- Exit: Completed/error exit
May have separate waiting queues for each event.
Five-State Model Transitions:
- Null → New: Process is created
- New → Ready: OS is ready to handle another process (Memory, CPU)
- Ready → Running: Select another process to run
- Running → Exit: Process has terminated
- Running → Ready: End of time slice or higher-priority process is ready
- Running → Blocked: Process is waiting for an event (I/O, Synchronization)
- Blocked → Ready: The event a process is waiting for has occurred — can continue
- Ready → Exit: Process terminated by OS or parent
- Blocked → Exit: Same reasons as above
📌 Example: A process is in the Running state reading data from a disk file. The read() system call triggers an I/O operation. The process cannot continue until data arrives, so the OS moves it from Running → Blocked. When the disk controller signals completion via an interrupt, the OS moves it from Blocked → Ready. When the scheduler picks it again, it goes from Ready → Running.
⭐ Key Takeaways
The lecture establishes that a process is a program in execution with its own private address space, and the OS must manage multiple processes through context switching and process state models. The CPU's normal sequential control flow is altered by exceptions — asynchronous exceptions (interrupts) from external devices and synchronous exceptions (traps, faults, aborts) caused by executing instructions. The five-state model (New, Ready, Running, Blocked, Exit) with its defined transitions is the standard framework for understanding process lifecycles, and context switching (saving/restoring process state) is the fundamental mechanism enabling multitasking.
🧠 Quick Revision Questions
- What are the three approaches for executing the operating system, and when is each approach most appropriate?
- Explain the difference between a trap, a fault, and an abort — provide one example of each.
- List the seven steps involved in a process state change (context switch).
- Draw and describe the five-state process model — what are all five states and all nine possible transitions?
- What is the difference between an asynchronous exception (interrupt) and a synchronous exception? Give at least two examples of each type.