Posts

Linux Kernel Synchronization-Atomic Operations

  Linux Kernel Synchronization — Atomic Operations 1. What Are Atomic Operations? Atomic operations are instructions that execute: Indivisibly Without interruption Without race conditions This means: ✔ Operation completes fully or not at all ✔ No other CPU/thread can see partial results ✔ Cannot interleave with another operation Example: Instead of: read i increment i write i Atomic instruction performs: read + increment + write (single step) So two threads incrementing a value cannot overwrite each other. 2. Why Atomic Operations Are Important They form the foundation of kernel synchronization because: Faster than locks Lower overhead Prevent race conditions on simple variables Avoid complex locking for small updates Common uses: Counters Reference counts Flags State tracking 3. Atomic Integer Operations Linux provides atomic operations using a special datatype: atomic_t Defined as: typedef struct { volati...

Introduction to Address Spaces and Virtual Memory

Image
 Virtual Memory and Address Space What early systems were like Early computers had simple memory layouts : OS stored at low physical addresses (e.g., 0 KB) Only one program loaded after it No abstraction or isolation Program directly used physical memory ➡️ Easy for OS developers, but very limited.  Why virtual memory became necessary As computers evolved: ✔ Multiprogramming Multiple processes kept in memory simultaneously OS switches CPU between them Improves CPU utilization ✔ Time sharing Many users interact with system concurrently Need fast switching Cannot save entire memory to disk each time (too slow) ➡️ Therefore, processes must stay in memory together . This created two big problems: Protection → processes must not access each other’s memory Convenience → programs should still see simple memory layout 1️⃣ What is an Address Space ? An address space is the program’s view of memory — an abstraction created by th...

Memory API in UNIX/C

  Memory API in UNIX/C  The memory API in UNIX/C provides functions that allow programs to allocate, use, resize, and free memory during execution. Proper memory management is essential for building reliable and bug-free software . 1. Types of Memory in C Programs A. Stack Memory (Automatic Memory) Managed automatically by the compiler . Used for: Local variables Function parameters Return values Allocated when a function is called and freed when it returns . Short-lived: data does not persist after the function exits . Example void func () { int x; // stored on stack } B. Heap Memory (Dynamic Memory) Managed explicitly by the programmer . Used when memory must persist beyond function calls . Requires manual allocation and deallocation. Example int *x = ( int *) malloc ( sizeof ( int )); 2. Core Memory Allocation Functions malloc(size_t size) Allocates size bytes on the heap. Returns: Pointer to allocated...

Virtual memory and Address Translation

Image
  Virtual memory and Address Translation  Address translation is the hardware-supported mechanism that enables the OS to virtualize memory efficiently while maintaining protection and control . It extends the idea of limited direct execution (LDE) from CPU virtualization into memory virtualization. 1. The Core Problem A program thinks: Its address space starts at 0 It owns all memory up to some limit (e.g., 16 KB) It runs alone in memory Reality: Multiple programs share physical memory. The OS occupies part of memory. Each process may be loaded at a different physical location . 👉 The OS must create the illusion of private memory while actually sharing physical memory. The Crux How can the OS: Virtualize memory efficiently? Provide flexibility to applications? Maintain strict control over memory access? Do all this without slowing the system down? 2. Hardware-Based Address Translation The solution is: Hardware translates every...

Segmentation

Image
  Segmentation  Segmentation is a memory-virtualization technique that improves upon the simple base-and-bounds approach by dividing a process’s address space into logical pieces called segments . Instead of treating the entire address space as one large block, segmentation treats: Code Heap Stack as separate regions , each with its own base and bounds. Why Segmentation Is Needed With simple base and bounds: The entire virtual address space must be placed contiguously in physical memory. Even unused space (e.g., gap between heap and stack) occupies physical memory. Large address spaces (e.g., 4GB) waste huge amounts of memory. Hard to support sparse address spaces. Problem: 👉 Too much wasted physical memory 👉 Poor flexibility Core Idea of Segmentation Instead of one base/bounds pair: ➡ Have one base and bounds pair per segment Each segment: Is a contiguous region Has its own: Base register Bounds register Growth directio...