.. include:: global.rst ************************************************ Chapter 2: Operating System Structures ************************************************ System Components of an Operating System ======================================== When discussing the system components of an operating system (OS), we are referring to the major parts that manage computer hardware and software. Kernel ------ - The **core** of the operating system. - Manages communication between hardware and software. - Responsibilities: - Process management (creating, scheduling, terminating processes) - Memory management (allocating and freeing RAM) - Device management (drivers for I/O devices) - System calls (interface between programs and OS functions) Process Management ------------------ - Handles the **creation, scheduling, and termination** of processes. - Ensures **CPU time** is distributed fairly and efficiently. - Supports multitasking, synchronization, and deadlock handling. Memory Management ----------------- - Controls and coordinates computer memory. - Allocates space to programs and data. - Handles virtual memory, paging, and segmentation. File System Management ---------------------- - Organizes and manages data on storage devices. - Provides ways to create, read, write, and delete files. - Handles file permissions and directories. Device Management ----------------- - Controls input/output devices (keyboard, mouse, disk drives, printers). - Uses **device drivers** to translate OS commands into hardware actions. - Manages device communication and buffering. I/O System ---------- - Provides a **uniform interface** for input/output operations. - Shields programs from low-level hardware complexities. Security and Protection ----------------------- - Ensures only authorized users and programs can access system resources. - Handles authentication, access control, and encryption. User Interface (UI) ------------------- - Provides interaction between the user and the OS. - Types: - **Command-Line Interface (CLI)** – e.g., Linux terminal, Windows PowerShell - **Graphical User Interface (GUI)** – e.g., Windows, macOS, GNOME Summary ------- An operating system’s system components work together to manage hardware (CPU, memory, devices), provide essential services (processes, file system, I/O), and allow users and programs to interact safely and efficiently. Operating System Services ========================= An operating system provides a set of essential services that make using the computer convenient, efficient, and secure. These services act as an interface between the user, application programs, and hardware. Program Execution ----------------- - Loads programs into memory and runs them. - Handles program termination (normal or abnormal). I/O Operations -------------- - Manages input/output with devices such as keyboard, mouse, printer, and storage. - Provides uniform interfaces for device communication. File System Manipulation ------------------------ - Allows programs and users to create, read, write, and delete files. - Provides access control and directory management. Communication ------------- - Enables processes to exchange data and information. - Supports: - **Shared memory** - **Message passing** (between processes, or over a network) Error Detection --------------- - Monitors system activity for errors (hardware or software). - Ensures proper action is taken (log, recovery, or graceful shutdown). Resource Allocation ------------------- - Distributes system resources (CPU, memory, I/O devices) among multiple users and processes. - Uses scheduling and priority policies to optimize performance. Protection and Security ----------------------- - Protects data and resources from unauthorized access. - Provides authentication, permissions, and encryption. Summary ------- Operating system services ensure that applications can execute smoothly, resources are used efficiently, and the system remains stable and secure. System Calls in Operating Systems ================================= A **system call** is the mechanism that allows a program (running in *user mode*) to request a service from the **operating system’s kernel (privileged mode)**. They act as the **interface between user programs and the OS**, enabling controlled access to hardware and low-level resources. Main Categories of System Calls ------------------------------- Process Control ^^^^^^^^^^^^^^^ - Create, execute, and terminate processes. - Examples: ``fork()``, ``exec()``, ``exit()``, ``wait()`` - Functions: load programs into memory, manage execution, and handle errors. File Management ^^^^^^^^^^^^^^^ - Operations on files and directories. - Examples: ``open()``, ``read()``, ``write()``, ``close()``, ``unlink()`` - Functions: create, delete, read/write, reposition file pointer, set/get file attributes. Device Management ^^^^^^^^^^^^^^^^^ - Request and release devices. - Examples: ``ioctl()``, ``read()``, ``write()`` - Functions: get device attributes, perform I/O operations, buffer management. Information Maintenance ^^^^^^^^^^^^^^^^^^^^^^^ - Retrieve or set system data. - Examples: ``getpid()``, ``alarm()``, ``time()`` - Functions: system time/date, process information, system data settings. Communication ^^^^^^^^^^^^^ - Exchange information between processes. - Examples: ``pipe()``, ``shmget()``, ``mmap()``, ``send()``, ``recv()`` - Functions: - Shared memory - Message passing - Socket communication Protection ^^^^^^^^^^ - Control access to resources. - Examples: ``chmod()``, ``umask()``, ``setuid()`` - Functions: provide authentication, permissions, and ownership management. How System Calls Work --------------------- 1. A user program makes a request (e.g., to read a file). 2. The system call instruction switches the CPU from **user mode** to **kernel mode**. 3. The OS kernel executes the requested operation. 4. Control is returned to the user program. Summary ------- System calls are the bridge between user programs and the operating system kernel, allowing programs to safely and efficiently access system resources. Layered and Microkernel Architectures ===================================== This section explains two important operating system architectures: **Layered Architecture** and **Microkernel Architecture**. Layered Architecture -------------------- - The operating system is divided into **layers (levels)**, each built on top of the lower one. - Each layer only interacts with the one directly beneath it. - Higher layers use services provided by lower layers. Characteristics ^^^^^^^^^^^^^^^ - Easy to design and implement. - Provides modularity, making it easier to test and debug. - Each layer hides the details of lower layers. Disadvantages ^^^^^^^^^^^^^ - Rigid structure; modification requires changes across multiple layers. - Performance overhead due to multiple layers of communication. Examples ^^^^^^^^ - THE Operating System (early layered OS). - Modern OSes often have layered influences. Microkernel Architecture ------------------------ - Instead of a large monolithic kernel, the OS keeps only **minimal essential functions** in the kernel. - Other services (file system, device drivers, networking, etc.) run in **user space** as separate processes. Kernel Responsibilities (Minimal) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - Inter-process communication (IPC) - Basic process management - Basic memory management Advantages ^^^^^^^^^^ - High reliability (a failure in one service does not crash the whole system). - Easier to extend and maintain. - Improved security (less code runs in privileged mode). Disadvantages ^^^^^^^^^^^^^ - Performance overhead due to frequent IPC (context switching between kernel and user processes). Examples ^^^^^^^^ - Mach (basis of macOS and iOS kernel, XNU hybrid) - Minix - QNX Comparison ---------- .. list-table:: :header-rows: 1 :widths: 20 40 40 * - Aspect - Layered Architecture - Microkernel Architecture * - Structure - OS divided into layers - Minimal kernel, services in user space * - Modularity - High (layers hide details) - Very high (services isolated) * - Reliability - Moderate - High (failures isolated) * - Performance - Can suffer due to extra layers - Can suffer due to IPC overhead * - Examples - THE OS, layered designs - Mach, Minix, QNX Summary ------- - **Layered architecture** organizes the OS into levels of abstraction. - **Microkernel architecture** minimizes kernel size, moving most services to user space. - The trade-off lies between **modularity and reliability** versus **performance efficiency**.