Operating System
Li Wei
Operating System
Preface
Wang Dao PPT:
Introduction
Basic Overview
Overview
An operating system (Operating System, OS) controls and manages the hardware and software resources of an entire computer system, organizing and scheduling the computer’s work and resource allocation to provide convenient interfaces and environments for users and other software. It is the most fundamental system software in a computer system.
Different operating systems may have different kernel structures. For example, Unix‑like systems usually use a monolithic kernel, while Windows and similar systems may use a microkernel or hybrid kernel structure.
Goals
Convenience: easy for users, providing a good, consistent user interface
Effectiveness: easy for system administrators
- Improves resource utilization
- Increases system throughput
Extensibility: related to OS architecture, making it easy to add new functions and modules
- Early unstructured → modular → layered → microkernel and client‑server models
Openness: follows the OSI (Open Systems Interconnection) international standards, achieving application portability and interoperability
Role
Informal definition of an operating system (key points): system software, a collection of program modules, resource management, and user‑interface functions.
- The OS acts as the interface between users and the computer hardware.
- Computers can be used in three ways: command‑line, system‑call, and graphical (icons‑windows) interfaces.
- The OS is the manager of computer system resources.
- It efficiently manages resources such as the processor, memory, I/O devices, and files (data and programs).
- The OS provides abstraction of computer resources (the OS is an extended machine, a virtual machine).
- On bare metal it adds: device management, file management, storage management (for main memory and secondary storage), processor management (for the CPU).
- It organizes workflows: job management, process management.
Historical Development
Computer systems without an OS
Early computers usually lacked an operating system; users had to interact directly with the hardware.- Features: users wrote programs that directly controlled hardware; operations were cumbersome, error‑prone, and resource utilization was low.
- Problems: no resource management or task scheduling; users monopolized resources, making sharing impossible.
Single‑program batch systems
- Allow jobs to be submitted in batches; the system executes them sequentially, running one job to completion before starting the next.
- Features: batch submission reduces manual intervention and improves resource utilization.
- Problems: jobs cannot run in parallel; system utilization and throughput are low; unsuitable for tasks that need fast response times.
Multiprogrammed batch systems
- Permit several jobs to reside in memory simultaneously; a scheduler selects which job to run.
- Features: concurrent execution of multiple programs, sharing of computer resources, higher utilization and throughput.
- Problems: lack of interactivity; jobs may block each other, leading to long wait times; not suitable for real‑time applications.
Time‑sharing systems
- Multiple users access the computer simultaneously via terminals, each feeling as if they have exclusive use.
Features: interactive operation, faster user response, better system utilization.
Problems: resource sharing among many users can degrade performance; requires careful scheduling and resource‑management policies.
- Multiple users access the computer simultaneously via terminals, each feeling as if they have exclusive use.
Real‑time systems
- Must respond to inputs within a specified time window; divided into hard real‑time and soft real‑time systems.
Features: can prioritize urgent tasks; some urgent tasks bypass time‑slice queuing. Provide real‑time behavior and reliability.
Problems: scheduling and resource management must meet strict timing constraints; higher complexity, stricter fault‑tolerance and reliability requirements.
- Must respond to inputs within a specified time window; divided into hard real‑time and soft real‑time systems.
Comparison of real‑time vs. time‑sharing systems
Microcomputer operating systems
- With the rise of microcomputers, OSes such as DOS, Windows, macOS, and Linux appeared.
Features: user‑friendly interfaces and a wide range of applications for personal computers; support multitasking and multi‑user operation.
Problems: limited resources require efficient management to keep performance stable while meeting growing user demands.
- With the rise of microcomputers, OSes such as DOS, Windows, macOS, and Linux appeared.
Core Characteristics
Concurrency
Parallelism: at the same instant, multiple instructions execute on multiple CPUs simultaneously.
Concurrency: within a time interval, multiple instructions execute on a single CPU alternately. It appears simultaneous at a macro level but is interleaved at a micro level.
The concurrency of an OS means that several programs can be running at the same time.
Process: a process is the basic unit that can run independently and be allocated resources. Introducing threads is a hallmark of modern operating systems.
Sharing
Resource sharing (or reuse) in an OS means that resources can be used concurrently by multiple processes residing in memory. Shared resources often require synchronization mechanisms to ensure correct access in a multithreaded environment and avoid race conditions.
Mutual‑exclusive sharing
- A resource may be offered to many processes, but only one process may access it at any given time.
- Once allocated, the resource cannot be used by another process until it is released.
- Such resources are called critical resources (e.g., a printer); they require synchronization to enforce exclusive access.
Simultaneous sharing
- The resource can be accessed by several processes “at the same time” within a time window.
- Examples: re‑entrant code, disk files.
- Concurrency and sharing are the two fundamental traits of multi‑user OSes.
- Achieving optimal resource allocation is difficult.
Virtualization
Virtualization maps a physical entity to one or more logical entities. It is a key technique for OS resource management, improving utilization.
- CPU – each user (process) sees a “virtual processor”.
- Memory – each process has its own address space (instructions + data + stack).
- Display devices – multiple windows or virtual terminals.
Two main virtualization techniques:
- Time‑division multiplexing (TDM)
- Space‑division multiplexing (SDM)
With TDM, multiple processes share a single processor by taking turns, each running for a short time slice before a rapid context switch.
Virtual memory uses SDM: physical memory is abstracted into address spaces. Each process gets its own space; pages are mapped to physical memory on demand. When a needed page is not resident, a page‑replacement algorithm brings it into memory.
Implementation
If a physical device is time‑shared among N logical virtual devices, each virtual device’s average speed is at most 1⁄N of the physical device’s speed. Similarly, with space sharing, each virtual device occupies at most 1⁄N of the physical device’s storage.
Asynchrony
In a multiprogrammed environment, multiple programs may run concurrently, but because resources are limited, a process’s execution is not continuous—it proceeds in unpredictable bursts. This is asynchrony.
Asynchrony (also called nondeterminism) refers to the uncertainty of execution order and timing of processes.
Characteristics
- Process execution speed is unpredictable: in a time‑sharing system, several processes run concurrently, “running and stopping” at indeterminate rates.
- Correctness criterion: regardless of speed, the final result must be the same; mutual exclusion and synchronization ensure this.
- Hard to reproduce a system’s exact state at a given moment, including reproducing runtime bugs.
Sync vs. Async
- Synchronous: you must wait for a result before continuing.
- Asynchronous: you can continue without waiting for a result.
Main Functions
The operating system, as a resource manager, handles the computer’s hardware resources—processor, memory, disk files, I/O devices—and provides interfaces. By allocating resources, scheduling tasks, and controlling access, it ensures efficient utilization.
Processor Management
Key tasks: create and terminate processes, coordinate their execution, enable inter‑process communication, and allocate CPU time according to scheduling algorithms.
- Process control – create processes for jobs, terminate finished processes, and manage state transitions (ready, running, blocked, etc.).
- Process synchronization – coordinate execution order and protect shared resources using mutexes, semaphores, condition variables, etc.
- Inter‑process communication (IPC) – mechanisms such as pipes, message queues, shared memory, and signals.
- Process scheduling – decides which processes receive CPU time; algorithms include FCFS, SJF, Round‑Robin, Multilevel Feedback Queue, etc.
Memory Management
Provides an environment for multiprogramming, improves memory utilization, and offers logical memory expansion.
Memory allocation
- Static allocation: fixed‑size memory assigned when a job is loaded; simple but can waste space.
- Dynamic allocation: jobs request additional memory at runtime (heap, stack).
Memory protection – isolates each process’s address space using hardware (MMU) and OS techniques to prevent illegal access.
Address translation – maps virtual (logical) addresses to physical memory, handled by the OS’s memory‑management unit.
Memory expansion – when physical RAM is insufficient, the OS employs virtual memory, swapping, paging, etc., to extend usable memory.
Device Management
Handles I/O requests from user processes, improving CPU and device utilization.
- Device allocation – decides which devices are free, which are in use, and assigns them to processes, typically tracked in a device‑allocation table.
- Device control – starts, stops, handles interrupts and errors, interacting with device drivers and managing registers and status.
- Buffers – temporary memory areas that smooth speed differences between I/O devices. Types include:
- Single buffering: one shared buffer for input and output.
- Double buffering: separate buffers for input and output, allowing simultaneous I/O.
- Shared buffer pool: a common pool allocated to multiple devices as needed.
- Virtual devices – software‑emulated devices (e.g., virtual disks, virtual printers) that provide functionality without physical hardware, reducing cost and increasing extensibility.
- Device independence – an abstraction layer that lets applications use devices without knowing the underlying hardware; drivers and the abstraction layer expose a uniform interface.
File Management
Manages user and system files, ensuring ease of use and security.
- Directory management – organizes files in a hierarchical structure, storing names, locations, sizes, attributes, etc., to facilitate access and sharing.
- File protection – enforces permissions (ACLs, mode bits, groups) so only authorized users/programs can read, write, or execute files.
- File sharing – allows multiple users/programs to access the same file concurrently, using sharing permissions, locking, and version control to maintain consistency.
- Secondary‑storage allocation – manages disk space using methods such as contiguous allocation, linked allocation, or indexed allocation, each with its own trade‑offs.
Providing Interfaces
User interfaces – ways for humans to interact with the system:
- Online (interactive) UI: command‑line or graphical interfaces that provide immediate feedback.
- Offline (batch) UI: submit jobs or edit files while disconnected, then hand them to the system for processing.
- Graphical UI: windows, buttons, and other visual elements; more intuitive for general users.
Programmatic interfaces – the only way user programs obtain OS services: system calls (e.g.,
open(),read(),write(),fork(),exec()). Programs must go through these calls; they cannot directly manipulate kernel data structures.
New Features in Modern OSes
System security
- Authentication, password mechanisms, access control, anti‑virus technologies.
Network functions and services
- Network communication, resource sharing, application interoperability, multimedia support, admission control, scheduling, multimedia file storage.
Linux System Calls (selected)
Task Commands (process control) fork(); exit(); wait();
Inter‑process communication pipe(); shmget(); mmap();
File operations open(); read(); write();
Device operations ioctl(); read(); write();
Information maintenance getpid(); alarm(); sleep();
Security chmod(); umask(); chown();
System Structure
Architecture Overview
Overall description:
Operating System Kernel – Overview
The kernel is the low‑level software configured on a computer; it is the most fundamental part of an OS. Programs that implement kernel functionality are called kernel programs.
Modules closely tied to hardware:
- Clock management – provides timing functions.
- Interrupt handling – implements the interrupt mechanism.
- Primitives – special low‑level programs at the very bottom of the OS, closest to hardware; they execute atomically (cannot be interrupted) and run quickly, being called frequently.
Functions that manage system resources:
- Process management
- Memory management
- Device management
Note: These management tasks mainly manipulate data structures and do not interact directly with hardware.
Kernel Types
Monolithic kernel (large kernel / macro kernel / single kernel)
All major OS functions are placed in the kernel and run in privileged mode.
- Idea: everything resides in the kernel (large‑kernel OSes often adopt a “modular” design as well).
- Typical examples: Linux, UNIX.
- Advantages: high performance.
- Disadvantages:
- Huge code base, tangled structure, hard to maintain.
- A fault in any kernel module can crash the whole system.
Microkernel
Keeps only the most essential functions in the kernel.
- Advantages:
- Small, clean kernel; easier to maintain; high reliability.
- Failures in user‑mode modules do not bring down the entire system.
- Disadvantages:
- Frequent switches between kernel and user mode → lower performance.
- User‑mode modules cannot call each other directly; they must communicate via the kernel’s message‑passing mechanism.
- Typical example: Windows NT.
Layered architecture
The kernel is divided into multiple layers; each layer may call only the interface of the layer directly below it.
- Advantages:
- Facilitates debugging and verification, bottom‑up testing.
- Clear, fixed interfaces between layers make extension and maintenance easier.
- Disadvantages:
- Can only call the immediate lower layer, making boundary definitions difficult.
- Inefficient; cross‑layer calls are impossible, leading to long system‑call latency.
Modular design
The OS is split into functionally independent modules, each handling a specific management task. Interfaces between modules are well defined, allowing communication via those interfaces. Sub‑modules can further divide functionality; this approach is often called the module‑interface method.
Idea: split the kernel into several cooperating modules.
Kernel = core module + loadable kernel modules
- Core module: handles essential functions such as process scheduling and memory management.
- Loadable kernel modules: can be added at runtime (e.g., device drivers, new file‑system modules) without recompiling the whole kernel.
Advantages:
- Clear logic between modules; once interfaces are fixed, multiple modules can be developed in parallel.
- Supports dynamic loading of new kernel modules, enhancing OS adaptability.
- Any module can call another directly, avoiding heavyweight message passing and improving efficiency.
Disadvantages:
- Interface definitions may be suboptimal.
- Inter‑module dependencies increase debugging complexity.
Exokernel (external kernel)
Idea: the kernel provides only minimal services such as process scheduling and inter‑process communication, leaving most resource management to user‑level libraries. (content truncated)
Originally written by Li Wei (李唯_) and published in Chinese on 后端技术栈全书 (Full-Stack Backend Engineering). Translated and adapted for DriftSeas with permission.