First Fit program in Python
First Fit Program in Python
Memory management is one of the most important tasks of the operating system. Processes are allocated empty memory blocks as per the process demands. This allocation takes place dynamically at the run time.
How first fit works?
Whenever a process (p1) comes with memory allocation request the following happens –
- OS sequentially searches available memory blocks from the first index
- Assigns the first memory block large enough to accommodate process
Whenever a new process P2 comes, it does the same thing. Search from the first index again.
Methods discussed
We will look at two different methods –
- Method 1 – Blocks allowed to keep just one single process
- Method 2 – Blocks allowed to keep multiple processes, if partitioned fragmentation big enough for new processes
Method 1
Run
def FirstFit(block_Size, blocks, process_Size, proccesses): # code to store the block id of the block that needs to be allocated to a process allocate = [-1] * proccesses occupied = [False] * blocks # Any process is assigned with the memory at the initial stage # find a suitable block for each process # the blocks are allocated as per their size for i in range(proccesses): for j in range(blocks): if not occupied[j] and (block_Size[j] >= process_Size[i]): # assign the block j to p[i] process allocate[i] = j occupied[j] = True break print("Process No. Process Size Block No.") for i in range(proccesses): print(str(i + 1) + "\t\t\t" + str(process_Size[i]) + "\t\t\t", end=" ") if allocate[i] != -1: print(allocate[i] + 1) else: print("Not Allocated") # Driver code block_Size = [100, 50, 30, 120, 35] process_Size = [20, 60, 70, 40] m = len(block_Size) n = len(process_Size) FirstFit(block_Size, m, process_Size, n)
Output:
Process No. Process Size Block No. 1 20 1 2 60 4 3 70 Not Allocated 4 40 2
Method 2
This allows multiple processes to share the same block space if the size is enough to keep another process.
Run
def FirstFit(block_Size, m, process_Size, n): # code to store the block id of the block that needs to be allocated to a process allocate = [-1] * n # Any process is assigned with the memory at the initial stage # find a suitable block for each process # the blocks are allocated as per their size for i in range(n): for j in range(m): if block_Size[j] >= process_Size[i]: # assign the block j to p[i] process allocate[i] = j # available block memory is reduced block_Size[j] -= process_Size[i] break print("Process No. Process Size Block No.") for i in range(n): print(str(i + 1) + "\t\t\t" + str(process_Size[i]) + "\t\t\t", end=" ") if allocate[i] != -1: print(allocate[i] + 1) else: print("Not Allocated") # Driver code block_Size = [100, 50, 30, 120, 35] process_Size = [20, 60, 70, 40] m = len(block_Size) n = len(process_Size) FirstFit(block_Size, m, process_Size, n)
Output:
Process No. Process Size Block No. 1 20 1 2 60 1 3 70 4 4 40 2
Read More
- Memory Management Introduction
- Partition Allocation Method
- Buddy- System Allocator
- Paging
- Types of Paging
- Fragmentation
- Mapping Virtual address to Physical Address.
- Virtual Memory
- Demand Paging
- Implementation of Demand paging and page fault
- Segmentation
- Page Replacement Algorithms
- Thrashing
- Belady’s Anomaly
- Static vs Dynamic Loading
- Static vs Dynamic Linking
- Swapping
- Translational Look Aside Buffer
- Process Address Space
- Difference between Segmentation and Paging