File Allocation
> DECRYPTING STORAGE PROTOCOLS...
Every file stored inside a computer occupies physical memory blocks on a disk.
But storing files efficiently is one of the most important responsibilities of an
Operating System.
To manage storage, reduce fragmentation, and improve file access speed, Operating
Systems use different File Allocation Methods.
The three major methods are:
- Contiguous Allocation
- Linked Allocation
- Indexed Allocation
Each method follows a different strategy for storing and accessing data blocks
inside memory.
This interactive platform explores how these allocation methods work through simulations,
animations, visualizations, and interactive experiences.
Visuals & Infographics
Overview
Methods Comparison
External Fragmentation
Disk Block Allocation Visualization
Quick Facts
Real World Usage of File Allocation Methods
Video Library
Formal Explanation
The Warehouse Story
System Dev Lab
Review the actual Python source code of our simulator on the left, and execute the web-compiled
version on the right.
import random
class OSDiskSimulator:
def __init__(self, size=100):
self.size = size
self.disk = ["."] * size
self.file_metadata = {}
def display_disk(self):
for i in range(0, self.size, 10):
row_string = ""
for j in range(i, i + 10):
row_string = row_string + self.disk[j] + " "
print("Block " + str(i) + " | " + row_string)
def allocate_contiguous(self,
file_id, length):
count = 0
start_idx = -1
for i in range(self.size):
if self.disk[i] == ".":
if count == 0: start_idx = i
count += 1
if count == length:
for j in range(start_idx,
start_idx + length):
self.disk[j] = file_id
return True
else:
count = 0
return False
def allocate_linked(self, file_id,
length):
free = [i for i in
range(self.size) if self.disk[i] == "."]
if len(free) < length: return
False
selected = random.sample(free, length)
for idx in selected:
self.disk[idx] = file_id
return True
def allocate_indexed(self,
file_id, length):
free = [i for i in
range(self.size) if self.disk[i] == "."]
if len(free) < length + 1: return
False
selected = random.sample(free, length + 1)
index_block = selected[0]
self.disk[index_block] = "I"
for b in selected[1:]:
self.disk[b] = file_id
return True
def deallocate(self,
file_id):
if file_id not in
self.file_metadata: return False
meta = self.file_metadata[file_id]
for b in meta["blocks"]:
self.disk[b] = "."
if meta["type"] == "Indexed":
self.disk[meta["index"]] = "."
del self.file_metadata[file_id]
return True