Posts

Indexed allocation

Image
  Indexed allocation is a file allocation method used by operating systems to manage how files are stored on disk. The core idea is simple but powerful: instead of storing a file in contiguous blocks or linking blocks one-by-one, the system uses a special index block that contains a list of all the disk block addresses where the file’s data is stored. This removes many limitations of earlier methods like contiguous and linked allocation. In indexed allocation, each file has its own index block . This index block acts like a table of contents. Every entry inside it points to a data block that belongs to that file. When a file is created, the OS allocates an index block and then fills it with pointers to the actual data blocks as the file grows. These data blocks can be located anywhere on the disk , so the file does not need contiguous space. This makes storage much more flexible and avoids external fragmentation. When the system needs to access a particular part of a file, it fir...

linked list allocation

Image
In linked allocation , a file is divided into blocks (just like other methods), but instead of storing them contiguously, the blocks can be scattered anywhere on the disk . These blocks are connected using a linked list , where each block contains: The data A pointer to the next block The OS only stores the starting block address of the file. From there, it follows the pointers to access the entire file. Suppose a file F needs 4 blocks. Instead of storing them together, the OS may allocate: To read the file:  1. Start from first block 2. Read data 3. Follow pointer to next block 4. Repeat until NULL

File system and storage allocation types.

Image
A file system in an operating system is the component responsible for managing how data is stored, organized, accessed, and protected on a storage device like a disk. What a File System Deals With Types of storage allocation In contiguous allocation , a file is stored in continuous (adjacent) blocks on disk. The disk is already divided into fixed-size blocks, and when a file is created, the operating system allocates a sequence of consecutive blocks to store that file. So instead of scattering data, everything is placed next to each other. The OS only needs to store two things for each file: the starting block (base) and the length (number of blocks). For example, suppose a file F needs 4 blocks. The OS may allocate blocks 10, 11, 12, 13. So the file is stored as:  Start = 10 , Length = 4 . If the CPU wants to access the 3rd block of the file, it can directly calculate:  Physical block = Start + Offset = 10 + 2 = 12.  This makes access very fast, especially for sequenti...

Thrashing

Image
Thrashing occurs in a computer system when the operating system spends a significant amount of time swapping pages in and out of memory, rather than executing actual processes. This typically happens when the system's memory is overcommitted, meaning there are too many processes competing for too little physical memory, leading to frequent page faults. How Memory is Used 1. Initial State: App1 is running, and its two pages are loaded into memory: Frame 1: App1, Page 1 Frame 2: App1, Page 2 Frame 3 and 4 are free. App3's pages are present in swap memory. 2. Switch to App2: Now, you switch to App2, which also requires 2 pages: Frame 3: App2, Page 1 Frame 4: App2, Page 2 Memory is now full with both App1 and App2’s pages. App3's pages are present in swap memory. 3. Switch to App3: You decide to switch to App3, which needs 2 pages as well. Since the memory is full, the system needs to make room: App1’s pages are moved to swap memory: Swap Memory: [App1, Page 1] [App1, Page 2] F...

Page replacement algorithms

Image
In systems that use virtual memory, processes often require more memory than is available in physical RAM. To manage this, the operating system swaps pages between RAM and disk storage (such as a hard drive or SSD). When the system runs out of RAM and needs to load a new page, it has to decide which of the currently loaded pages to swap out to disk. This decision is made by the page replacement algorithm. 1. FIFO (First-In, First-Out) : The oldest page in RAM (the first one loaded) is the first one to be replaced. Example: Page Slots in RAM: 3 slots Page Sequence: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 Load page 1 → [1, -, -] Load page 2 → [1, 2, -] Load page 3 → [1, 2, 3] Load page 4 (remove 1) → [4, 2, 3] Load page 1 (remove 2) → [4, 1, 3] Load page 2 (remove 3) → [4, 1, 2] Load page 5 (remove 4) → [5, 1, 2] Load page 1 (already in RAM) → [5, 1, 2] Load page 2 (already in RAM) → [5, 1, 2] Load page 3 (remove 1) → [5, 3, 2] Load page 4 (remove 2) → [5, 3, 4] Load page 5 (already in RAM) →...

Virtual memory

Image
Virtual memory Virtual memory is a memory management technique that allows a computer to use more memory than what is physically available in RAM. It creates an illusion for the user that there's more RAM than there actually is by using a combination of the computer's RAM and a portion of the hard drive.  Imagine you have a computer with 4 GB of RAM, but you're running several programs that together require 6 GB of memory. The operating system will use 4 GB from the physical RAM and then take 2 GB from the hard drive, which acts as an extension of the RAM. This portion of the hard drive is called the swap space or page file . Here's how it works: Running Programs: Suppose you're using a browser, a word processor, and a game simultaneously. Memory Requirement: These applications together need more memory than your 4 GB of RAM can provide. Swapping: The OS decides which parts of the data or applications are not actively in use and temporarily moves them from RAM to th...

Drawback of paging and how to solve it

Image
Drawbacks of paging When pages are scattered far apart in physical memory, it can lead to additional drawbacks: 1. Reduced Cache Efficiency: Explanation: If the pages of a process are spread out across distant locations in RAM, it can reduce the efficiency of the CPU cache, which relies on the locality of reference. The CPU cache works best when data is close together, as it can quickly load and reuse it. When data is scattered, it increases the chances of cache misses, slowing down memory access. Example: Imagine a book is split into sections and stored on shelves all over a large library (representing RAM). If you have to walk across the library to read each section, it takes much longer than if the sections were on the same or nearby shelves. Similarly, if pages of a process are spread far apart in memory, the CPU might need to fetch data from multiple distant locations, reducing performance 2. Increased Page Table Lookup Time: Explanation: If the pages of a process are not stored c...