Fundamentals of Operating Systems: Architecture and Concurrency
Chapter 1: Introduction to Operating Systems
Components of a Computer System
A computer system consists of four essential components:
- Computer Hardware: CPU, memory, I/O devices.
- Operating Systems (OS)
- Application Programs: Word processors, compilers, web browsers.
- Users: People utilizing the machines.
Operating System Functions and Definition
- User Goals: Users prioritize ease-of-use and performance.
- Server Goals: Servers focus on balancing resource allocation.
- Smartphone Goals: Optimization for usability.
Defining the OS
The OS acts as two primary entities:
- Resource Allocator: Manages system resources and ensures fair allocation.
- Control Program: Prevents errors and improper use of the computer system.
The Kernel is the core component, the only program that is always running.
Computer System Architecture
While most systems use a single processor, multiprocessor (parallel) systems are used to increase throughput and reliability.
Types of Multiprocessing
- Asymmetric Multiprocessing: CPUs are assigned specific, specialized tasks.
- Symmetric Multiprocessing (SMP): All CPUs perform all tasks.
Core and Memory Structures
- Dual-Core Processors: Each core has its own registers and L1 cache, but cores share the L2 cache, which can cause memory bottlenecks.
- NUMA (Non-Uniform Memory Access): Reduces bottlenecks by providing each CPU with its own local memory access.
Multiprogramming and Multitasking
- Multiprogramming: Loads multiple jobs into memory to ensure the CPU is always busy.
- Multitasking (Time-Sharing): Switches jobs quickly to provide a fast response time (typically less than 1 second).
Dual-Mode Operation
The OS uses two modes, enforced by a hardware mode bit, to protect system integrity:
- Kernel Mode (Supervisor Mode): Allows privileged operations.
- User Mode: Prevents direct hardware access.
A System Call is the mechanism that triggers a switch from User Mode to Kernel Mode.
Core Resource Management Areas
The Operating System is responsible for managing:
- Processes
- Memory
- File Systems (e.g., NTFS, FATx)
- I/O Devices
- CPU Scheduling
Process Management Responsibilities
Processes require resources such as CPU time, memory, I/O, and files. The OS handles:
- Process creation and suspension.
- Inter-Process Communication (IPC).
- Handling Deadlocks (a situation where Process A waits for Process B, and Process B waits for Process A, causing both to freeze).
Memory Management
The OS tracks memory usage, moves processes between memory and storage, and handles memory allocation and deallocation.
File System Management
The OS provides file abstraction, organizes directories, and supports access control mechanisms.
Mass-Storage Management
This involves disk scheduling, performance optimization, and managing system backups.
Cache Management
The cache stores frequently accessed data. A cache hit occurs when data is found in the cache; a cache miss occurs when it is not.
Security and Protection
The OS enforces user permissions using mechanisms like User ID and Group ID. Common threats include Denial of Service (DoS) attacks, worms, viruses, and identity theft.
Virtualization
Virtualization allows an operating system to run as an application within another operating system (Host OS).
Kernel Data Structures
The kernel utilizes efficient data structures such as linked lists, trees, and hash tables for its operations.
Computing Environments
- Client-Server: Clients send requests, and servers provide responses.
- Peer-to-Peer (P2P): All nodes are considered equal, operating without a central server (e.g., torrent networks).
Cloud Computing Models
Cloud Deployment Types
- Public Cloud: Services offered externally over the internet.
- Private Cloud: Services maintained on an internal network.
- Hybrid Cloud: A combination of public and private cloud resources.
Cloud Service Types
- SaaS (Software as a Service): Applications delivered over the internet.
- PaaS (Platform as a Service): Online development environments.
- IaaS (Infrastructure as a Service): Cloud storage and fundamental computing services.
Specialized Systems
- Real-Time Embedded Systems: Systems with fixed time constraints, commonly found in cars, microwaves, and fitness trackers.
- Free/Open-Source OS: Operating systems where the source code is publicly available (e.g., Linux, BSD UNIX).
Chapter 2: Operating System Structures
Essential OS Services
The OS provides an execution environment and various services:
User-Facing Services
- User Interface (UI): Command Line Interface (CLI), Graphical User Interface (GUI), and Batch interfaces.
- Program Execution
- I/O Operations
- File System Management: Creation, deletion, searching, listing, and setting permissions.
- Communication: Shared memory and message passing.
- Error Detection: Debugging and system monitoring.
System-Level Services
- Resource Allocation: Managing CPU, memory, storage, and I/O devices.
- Accounting: Tracking resource usage per user or process.
- Protection and Security: User authentication, access control, and firewall defense.
User-OS Interface Types
- CLI (Command Line Interface): Fetches and executes user commands (e.g., Windows
cmd, Linuxbash). - GUI (Graphical User Interface): Intuitive interface using icons, windows, and point-and-click interaction (e.g., Windows, macOS).
- Alternate Interfaces: Touchscreen (swipe, tap) and Voice Interfaces (e.g., Siri, Google Assistant).
System Calls and APIs
System calls provide the interface for programs to access OS services, typically written in C, C++, or assembly language.
Most applications utilize Application Programming Interfaces (APIs) rather than raw system calls:
- Win32 API: Used for Windows systems.
- POSIX API: Standardized for Linux, macOS, and Unix-like systems.
- Java API: Managed by the Java Virtual Machine (JVM).
Example: File Copy Operation
A simple file copy command (e.g., cp in.txt out.txt in Linux) involves a sequence of system calls:
- Open source file.
- Read data from source file.
- Write data to destination file.
- Close both files.
Categories of System Calls
- Process Control: Create, terminate, execute, wait, signal.
- File Management: Open, close, read, write, delete.
- Device Management: Request/release devices, attach/detach.
- Information Maintenance: Get/set time, date, and system data.
- Communications: Create/delete connections, send/receive messages.
- Protection: Get/set file permissions.
System Utilities and Tools
The OS provides various system services (utilities) for management tasks:
- File Management:
ls,cp,rm. - Process Management: Task Manager,
killcommand. - Storage Management:
fdisk,df. - Network Management:
ping,netstat. - Performance Monitoring:
top,htop. - Security:
passwd,chmod. - Backup Utilities:
tar,rsync.
Linkers and Loaders
- Linkers: Combine compiled object files and necessary libraries into a single executable program.
- Loaders: Create a process, load the executable into memory, and adjust memory addresses for execution.
- Dynamically Linked Libraries (DLLs): Shared libraries loaded at runtime, which reduces memory usage and allows for post-compilation updates.
OS Specificity and Portability
Applications are often OS-specific because system calls vary significantly between operating systems. Solutions for cross-platform compatibility include:
- Interpreted languages (e.g., Python, Ruby).
- Virtual machine languages (e.g., Java running on the JVM).
- Standardized APIs (e.g., POSIX).
OS Design Principles
Design Goals
- User Goals: Easy to use, reliable, fast, and secure.
- System Goals: Efficient, flexible, and maintainable.
Policy vs. Mechanism
This separation is crucial in OS design:
- Mechanism: Defines how an operation is performed (e.g., using a CPU timer).
- Policy: Decides what should be done (e.g., determining how long each user process receives CPU time).
Implementation Languages
Early operating systems were written exclusively in assembly language. Modern OSs primarily use C and C++, with some assembly code for low-level tasks (e.g., the Android kernel uses C, while applications use Java).
Operating System Structures
The organization of an OS impacts its efficiency, maintainability, and scalability.
Monolithic Structure
- The entire kernel is a single, large binary containing all OS services (process management, memory, I/O).
- Pros: Fast due to direct function calls, conceptually simple.
- Cons: Difficult to modify or extend; a single bug can crash the entire system.
- Examples: Linux, UNIX.
Layered Structure
- The OS is divided into layers, where lower layers provide services to higher layers.
- Pros: Modular, easier to debug.
- Cons: Performance overhead from inter-layer communication; complex to define layer boundaries.
- Examples: Windows NT, THE OS (historical).
Microkernel Structure
- Minimizes kernel size by moving most services (drivers, file systems, networking) into user space.
- The kernel primarily handles process communication and low-level hardware management.
- Pros: Highly modular, secure, easy to extend.
- Cons: Slower due to message passing between user-mode services and the kernel.
- Examples: macOS (Darwin Kernel), QNX (real-time OS).
Loadable Kernel Modules (LKM)
- A hybrid approach where the OS has a core kernel but allows features to be dynamically loaded as modules.
- Combines monolithic speed with microkernel flexibility, allowing features to be added without rebooting.
- Examples: Modern Linux, Windows, macOS, Solaris.
Hybrid Structure
- Mixes elements from different architectures to balance speed, security, and flexibility.
- Examples: Windows (monolithic kernel with microkernel elements), Android (Linux kernel with a Java framework).
OS Booting Process
- The BIOS/UEFI initializes hardware and performs the POST (Power-On Self-Test).
- The BIOS/UEFI loads the bootloader (e.g., GRUB, Windows Boot Manager).
- The bootloader loads the OS kernel into memory.
Related Concepts
- Dual-Booting: Allows the user to select between multiple operating systems at startup.
- Virtual Machines (VMs): Enable running multiple operating systems concurrently on a single physical machine.
Chapter 3: Process Management
Defining a Process
A process is a program in execution. It includes:
- Program Counter (PC)
- Stack
- Heap
- Data Section
The Process Control Block (PCB)
The OS tracks each process using the Process Control Block (PCB), which stores critical information:
- Process State (New, Ready, Running, Waiting, Terminated)
- CPU Registers
- Memory Limits
- Scheduling Information
- I/O Status
Process Scheduling and Schedulers
The OS uses three types of schedulers to manage process execution:
- Long-Term Scheduler (Job Scheduler): Selects processes from storage and loads them into memory, controlling the degree of multiprogramming.
- Short-Term Scheduler (CPU Scheduler): Chooses which process in the ready queue will execute next on the CPU.
- Medium-Term Scheduler: Swaps processes between disk and main memory to optimize RAM utilization (swapping).
Context Switching
Context switching is the mechanism of saving the state (PCB) of the current process and restoring the state of the next process when switching the CPU. This operation introduces performance overhead.
Common CPU Scheduling Algorithms
- First-Come, First-Served (FCFS): Executes processes in the order they arrive. Simple, but can lead to long average wait times.
- Shortest Job Next (SJN): Minimizes average wait time but requires prior knowledge of the process duration.
- Round Robin (RR): Assigns fixed time slices to each process, preventing starvation but increasing context switch overhead.
- Priority Scheduling: Executes the highest-priority processes first. Can lead to starvation (low-priority jobs never run), often solved using aging.
- Multilevel Queue Scheduling: Categorizes processes into different queues (e.g., foreground, background) and applies distinct scheduling policies to each queue.
Process Operations
- Creation: Processes are created by a parent process using system calls like
fork()(Unix/Linux) orCreateProcess()(Windows). - Termination: Processes terminate using
exit(),abort(), or via OS intervention. - Synchronization: Parent-child relationships often use the
wait()call to synchronize execution flow.
Interprocess Communication (IPC)
Processes can be independent (executing separately) or cooperating (sharing data or tasks). IPC enables cooperating processes to communicate using two main models:
- Shared Memory: Processes read and write data directly into a common memory region.
- Message Passing: Processes exchange messages indirectly via the operating system.
The Producer-Consumer Problem
This classic problem illustrates IPC, involving a producer that generates data and places it into a buffer (which can be bounded or unbounded), and a consumer that retrieves the data from the buffer.
Message-Passing Systems
Communication Methods
- Direct Communication: Processes explicitly name the sender or receiver in their send/receive operations.
- Indirect Communication: Processes use shared mailboxes or ports to relay messages.
Synchronization Modes
- Blocking (Synchronous): The sender waits until the message is received, or the receiver waits until a message is available.
- Non-Blocking (Asynchronous): The sender and receiver proceed independently without waiting for the communication to complete.
Buffering Capacity
Buffering determines how messages are stored:
- Zero Capacity: The sender must wait until the receiver reads the message.
- Bounded Capacity: Uses a fixed-size queue; the sender may block if the queue is full.
- Unbounded Capacity: The queue size grows indefinitely; the sender never blocks.
Practical IPC Mechanisms
- POSIX Shared Memory: Uses functions like
shm_open()andmmap()to allow processes to share memory regions as if they were files. - Pipes: Enable unidirectional communication. Ordinary pipes require a parent-child relationship, while named pipes allow communication between unrelated processes.
- Sockets: Facilitate network-based IPC, enabling data exchange between processes potentially running on different systems, commonly used in client-server models.
Chapter 4: Threads and Concurrency
Introduction to Threads
A thread is the smallest unit of execution within a process.
- A single-threaded process has one sequence of execution.
- A multithreaded process allows multiple threads to share process resources (code, data, files) while maintaining their own private components: registers, stack, and program counter.
Benefits of Multithreading
- Responsiveness: The User Interface (UI) remains active while background tasks execute.
- Resource Sharing: Threads within the same process share memory and resources efficiently.
- Economy: Creating and managing threads is significantly cheaper than creating and managing processes.
- Scalability: Multithreading effectively utilizes the power of multicore processors.
Multicore Programming Concepts
- Concurrency: Multiple tasks make progress, but they may not be executing simultaneously (e.g., time-sharing on a single core).
- Parallelism: Tasks execute simultaneously on multiple processing cores.
Challenges in Multicore Programming
- Identifying tasks (finding independent units of work).
- Balancing workload (preventing bottlenecks on specific cores).
- Data splitting (properly dividing input data).
- Handling data dependency (ensuring correct execution order).
- Debugging and testing (more complex in parallel environments).
Amdahl’s Law
Amdahl’s Law dictates that increasing the number of processing cores only improves speed for the portion of the program that can be executed in parallel.
Multithreading Models
The relationship between user-level threads and kernel-level threads defines the threading model:
- Many-to-One Model: Maps multiple user threads to a single kernel thread. Efficient, but if one user thread blocks, all others block, preventing true parallelism. (Used in early Java implementations).
- One-to-One Model: Maps each user thread to a dedicated kernel thread. Allows better concurrency and parallelism but increases system overhead. (Used in Windows and Linux).
- Many-to-Many Model: Multiplexes many user threads onto an equal or smaller number of kernel threads, balancing efficiency and concurrency.
Thread Libraries and APIs
Thread libraries provide APIs for programmers to create and manage threads:
- POSIX Pthreads: A standard API used across UNIX and Linux systems, allowing explicit thread creation using
pthread_create(). - Windows Threads: A kernel-level library specific to Windows.
- Java Threads: Managed by the Java Virtual Machine (JVM), created by extending the
Threadclass or implementing theRunnableinterface.
Implicit Threading Techniques
Implicit threading allows the OS or runtime environment to manage thread creation and synchronization, reducing the burden on the programmer:
- Thread Pools: A set of pre-created threads waiting for tasks, improving efficiency by avoiding repeated thread creation overhead.
- Fork-Join Model: A pattern where a task splits (forks) into smaller subtasks that run in parallel, and then results are combined (joined).
- OpenMP: An API supporting parallel execution specifically for shared-memory systems.
Advanced Threading Issues
fork()andexec()Behavior: The outcome of these system calls varies by OS. Some duplicate all threads in the new process, while others only copy the calling thread.- Signal Handling: In multithreaded environments, the OS must decide if a signal is delivered to all threads, a specific thread, or a designated thread handler.
- Thread Cancellation: Terminating a thread before it completes. This can be asynchronous (immediate termination) or deferred (termination checked only at safe points).
- Thread-Local Storage (TLS): A mechanism that provides each thread with its own private copy of data that would otherwise be global.
- Scheduler Activations: A technique used to efficiently manage the dynamic mapping between user-level threads and kernel-level threads.
OS-Specific Thread Implementations
- Windows Threads: Utilizes the 1:1 model. Each thread has its own ID, registers, stacks, and private storage, but shares process-wide resources.
- Linux Threads: Implemented using the
clone()system call, which creates lightweight processes that can share resources while executing independently.
