OS Components

The operating system is divided into several layers, each performing specific tasks:

  • Kernel: The core of the OS that manages resources (CPU, memory, I/O devices).
    Types:

    • Monolithic Kernel: All OS services run in kernel space (e.g., Linux).

    • In a monolithic kernel:

      • All OS core functions, such as file systems, process management, device drivers, memory management, and networking, are implemented within the kernel space.

      • The kernel operates in a privileged mode, giving it unrestricted access to the system's hardware.

      • The services are not modularized into separate processes like in a microkernel; instead, they run as part of a single kernel image.

    • Performance:

      • Monolithic kernels, like Linux, avoid the overhead of context switching between the kernel and user space.

      • All components run in the same address space, making inter-process communication (IPC) faster.

      • Although monolithic, Linux allows for modularity through loadable kernel modules (LKMs). This lets users add or remove features (e.g., device drivers) at runtime without rebuilding the entire kernel.

      • monolithic, it supports dynamic modularity through loadable kernel modules (LKMs)

    • Microkernel: moves many services, such as device drivers and file systems, into user space, reducing the kernel's complexity and size (e.g., Minix).

  • User Space: Non-kernel applications run here. OS APIs provide an interface to interact with hardware resources.

  • Communication Between User Space and Kernel Space

    Since direct communication is prohibited, applications in user space must use the following mechanisms to interact with the kernel:

    1. System Calls

    • The primary mechanism for communication between user space and kernel space.

    • Applications invoke system calls to request services like file I/O, process management, or networking.

    • Example: read(), write(), open(), close(), socket().

How System Calls Work:

  1. User-space program invokes a library function (e.g., fopen).

  2. The library function translates this into a system call (e.g., open).

  3. The CPU switches from user mode to kernel mode using a trap instruction.

    A trap instruction is a special CPU instruction that transfers control from user space to kernel space. It triggers a software interrupt, allowing user-space applications to request services from the kernel in a safe and controlled manner. This is commonly used for system calls.

  4. The kernel executes the requested service and returns the result to the user-space program.

2. Device Files

  • Kernel-space drivers expose device functionality via special files in /dev.

  • Applications access hardware by reading/writing these files.

  • Example: Writing to /dev/null or reading from /dev/sda.

3. Shared Memory

  • A region of memory shared between kernel and user-space processes for faster data transfer.

  • Requires system calls to set up the shared memory region.

4. IOCTL (Input/Output Control)

  • A mechanism for sending custom commands to device drivers.

  • Applications invoke the ioctl() system call to pass control commands or data to a kernel-space driver.

  • A communication protocol between user-space applications and the kernel, commonly used in networking.

  • Example: Applications like iproute2 use netlink to configure network interfaces.

6. Procfs and Sysfs

  • Kernel information and configuration are exposed to user space via virtual file systems like /proc and /sys.

  • A Virtual File System (VFS) is an abstraction layer within the kernel that provides a unified interface for accessing different types of file systems. It allows applications to perform file operations (e.g., open, read, write) without needing to know the details of the underlying file system.

    Key Characteristics:

    • Abstraction: Hides the complexities of specific file systems (e.g., ext4, NTFS, FAT32).

    • Unified API: Provides a standard interface (POSIX API) for file operations, regardless of the file system type.

    • Kernel Module: Acts as a bridge between the user space and kernel modules implementing the actual file system drivers.

How VFS Works:

  1. User-space applications issue system calls like open(), read(), or write().

  2. The VFS translates these calls into operations specific to the underlying file system.

  3. The appropriate file system driver is invoked to perform the requested operation on the physical storage device.

Examples of Virtual File Systems:

  1. /proc:

    • A virtual file system for accessing kernel and process information.

    • Contains files like /proc/cpuinfo (CPU details) and /proc/meminfo (memory usage).

  2. /sys:

    • Exposes kernel objects and hardware configurations.

    • Used for tasks like device management.

  3. tmpfs:

    • A virtual file system stored in memory.

    • Used for temporary storage, such as /tmp.

  4. Network File Systems:

    • Protocols like NFS and SMB/CIFS allow accessing remote files as if they were local.

7. Signals

  • The kernel sends signals to user-space processes to notify them of events (e.g., SIGKILL, SIGSEGV).

  • User-space processes can also send signals to each other using system calls like kill().

  • SIGKILL (Signal 9)

    • Purpose: Forces the termination of a process.

    • Source: Sent by the kernel or another process using kill or similar commands.

    • Behavior:

      • Cannot be ignored or handled by the process.

      • Ensures immediate termination without cleanup, such as closing files or releasing resources.

SIGSEGV (Signal 11)

  • Purpose: Indicates a segmentation fault, usually caused by a process attempting to access invalid memory.

  • Source: Generated by the kernel when a process violates memory protection rules, such as:

    • Dereferencing a null or unallocated pointer.

    • Writing to a read-only segment of memory.

    • Accessing memory beyond the bounds of an allocated array.

  • Behavior:

    • By default, it terminates the process and generates a core dump, which can be analyzed to debug the issue.

Examples of User-Kernel Interaction

Example 1: Reading a File

  1. User Space: An application calls the fread() function in a library.

  2. System Call: The library issues a read() system call.

  3. Kernel Space: The kernel locates the file, reads its data, and copies the result to the user-space buffer.

  4. Return to User Space: The result is returned to the application.

Example 2: Network Communication

  1. User Space: A web browser opens a socket using the socket() system call.

  2. Kernel Space: The kernel creates a socket and manages the network protocol stack.

  3. User Space: The browser sends data via the send() system call, which is passed to the kernel.

  4. Kernel Space: The kernel transmits the data using the NIC driver.


Processes and Threads

  • Processes:

    • An independent program running in its own memory space.

    • different processes have separate memory spaces. This isolation is a fundamental principle of modern operating systems to ensure process security and stability.

      Key Points:

      1. Memory Isolation:

        • Each process has its own virtual memory space, meaning:

          • A process cannot directly access another process's memory.

          • Memory corruption or bugs in one process do not affect others.

      2. Memory Layout of a Process: Each process typically has a memory layout divided into the following segments:

        • Code Segment (Text):

          • Stores the executable instructions.
        • Data Segment:

          • Stores global and static variables.
        • Heap:

          • Used for dynamic memory allocation (e.g., malloc in C).
        • Stack:

          • Stores local variables, function parameters, and return addresses.
      3. Role of the MMU (Memory Management Unit):

        • The MMU, along with virtual memory, maps the virtual addresses used by processes to physical memory.

        • Each process's memory map is maintained in the page table, which the kernel sets up during process creation.

      4. Shared Memory:

        • While processes are isolated, some mechanisms (like shared memory or pipes) allow inter-process communication (IPC).

        • These regions are explicitly created and managed to be accessible by multiple processes.

      5. System Calls for Isolation:

        • Any memory access outside a process's allocated memory space generates a segmentation fault (SIGSEGV), ensuring no accidental access to unauthorized memory.
    • Lifecycle: Created → Ready → Running → Blocked → Terminated.

    • Context Switching: Switching the CPU from one process to another involves saving and restoring the process state.

    • Key Steps in Context Switching:

      1. Save Context of the Current Process:

        • The CPU saves the current state of the process being executed (called the context) into a process control block (PCB).

        • Context includes:

          • CPU registers (e.g., program counter, stack pointer, general-purpose registers).

          • Process state (e.g., running, waiting, ready).

          • Memory mappings and kernel state for the process.

      2. Select the Next Process to Execute:

        • The scheduler determines which process to execute next based on a scheduling policy (e.g., round-robin, priority scheduling).
      3. Load Context of the Next Process:

        • The CPU loads the context of the selected process from its PCB into the CPU registers.

        • This process includes:

          • Setting the program counter to the instruction where the process left off.

          • Updating the stack pointer and other registers.

      4. Resume Execution:

        • The CPU resumes execution of the selected process from the point it was paused.
  • Threads:

    • Lightweight processes sharing the same memory space.

    • Multithreading: Enables parallel execution within a single process.(refer multithreading blog)

    • Synchronization mechanisms like mutexes and condition variables are crucial.

Key Concepts:

  • Process Table: A data structure the OS uses to track all processes.

  • Thread States: Running, ready, waiting, terminated.

Memory Management

  • Virtual Memory: Enables processes to use more memory than physically available by swapping unused memory to disk.

    • Paging: Divides memory into fixed-size pages.

    • Segmentation: Divides memory into variable-sized segments based on logical divisions.

  • Page Tables:

    • Translate virtual addresses to physical addresses.

    • TLB (Translation Lookaside Buffer): A cache for page table lookups to speed up translation.

  • Memory Allocation:

    • Dynamic: malloc, free.

    • Static: Fixed-size allocations at compile-time.

Key Concepts:

  • Swapping: Moving inactive memory pages to disk to free up physical RAM.

  • Fragmentation:

    • Internal: Wasted space within allocated memory.

    • External: Free memory space scattered across regions.

Inter-Process Communication (IPC)

Processes need mechanisms to share data or signals.

  • Pipes: Unidirectional communication between processes.

  • Message Queues: Queue-based message exchange.

  • Shared Memory: Fastest IPC mechanism; processes share a memory segment.

  • Sockets: Enable communication between processes over a network.

  • Signals: Notify a process of an event (e.g., SIGKILL, SIGTERM).

Key Concepts:

  • Synchronization: Avoid race conditions using semaphores or mutexes.

  • Deadlocks: Circular wait for resources causing system freeze.

Scheduling

Determines which process/thread gets CPU time.

  • Preemptive Scheduling: OS can interrupt a running process (e.g., Round Robin, Priority Scheduling).

  • Non-Preemptive Scheduling: A process runs to completion once started.

  • Multilevel Queue Scheduling:

    • Different queues for system processes, user processes, etc.
  • Context Switching Overhead: Minimize by using efficient schedulers.

Key Metrics:

  • Turnaround Time: Total time taken to complete a process.

  • Throughput: Number of processes completed per unit time.

I/O Management

  • I/O Devices: Communicate via device drivers.

  • Interrupts:

    • Mechanisms to signal the CPU that an I/O operation is complete.

    • Types: Hardware Interrupts, Software Interrupts.

  • DMA (Direct Memory Access):

    • Bypasses CPU for faster data transfer between memory and devices.

Virtualization

  • Allows multiple OS instances to run on a single physical machine.

  • Hypervisors:

    • Type 1: Runs directly on hardware (e.g., VMware ESXi).

    • Type 2: Runs on a host OS (e.g., VirtualBox).

  • Containers:

    • Lightweight virtualization (e.g., Docker).

Security

  • User Authentication: Validates user identity.

  • Access Control: Restricts file/device access (e.g., chmod, chown in Linux).

  • Firewalls: Monitor and control incoming/outgoing network traffic.

  • System Integrity:

    • Prevent unauthorized kernel modification using tools like SELinux.