Next Fit In Python

Next Fit in Python

The Next Fit algorithm is inspired by the First Fit Memory Allocation Method, in which a memory is searched for empty spaces and then assigned to a process in need. The algorithm starts to search the memory slots as the first fit algorithm and from the starting. But, when the method is called the second time, it does not search the memory from the beginning. Rather, it starts to search it from the place where it left. The entire concept is based on a moving pointer which traverses the memory blocks to search an empty memory block which can be assigned to a process.

The Next Fit algorithm, can thus be defined as a changed version of the first fit algorithm as it first starts searching the memory following its concept. Also, unlike the concept of first fit method, it does not allocate memory from the starting of the memory block. Rather, the memory allocation is done from the point where the second search is carried out. The overall task is done using a scheduling algorithm conducted with the help of an operating system.    

Algorithm for memory allocation using Next Fit

Step 1. Start

Step 2. Enter the number of memory slots.

Step 3. Enter each memory slot size.

Step 4. Enter the number of processes with their sizes.

Step 5. Start by selecting each process to check if it can be assigned to the current memory block.

Step 6. If the condition in step 4 is true, then allocate the process with the required memory and check for the next process from the memory block where the searching was halted, not from the starting.

Step 7. If the current memory size is smaller, then continue to check the next blocks.

Step 8. Stop

Read Also: Next Fit in Java

Python Program for Next Fit Memory Management 

# Python program for next fit memory management scheme   
  
# Method for memory allocation to empty blocks using the Next fit algorithm  

Next_fit(block_size, m, process_size, n): 
      
# Code to stores block id for the block which can be used for process allocation  
     
# no block is assigned at the initial stage to a process  
    allocate = [-1] * n  
    j = 0
  
# for each process, find an appropriate block which best fits its size for assigning the memory 
     
    for i in range(n): 
  
# Searching is not carried out from the beginning 
        while j < m: 
  
            if block_size[j] >= process_size[i]: 
  
                # block j is allocated to p[i] process  
                allocate[i] = j 
 
   # Available memory in this block is reduced.  
                Block_size[j] -= process_size[i]  
  
                break
  
# The blocks will be traversed by mod m from the starting block when we reach towards the end
            j = (j + 1) % m 
  
    
printf("Process No. Process Size Block no.")  
    for i in range(n): 
        print(i + 1, "         ", process_size[i], 
                                    end = "     ") 
        if allocate[i] != -1: 
            print(allocate[i] + 1)  
        else: 
            print("Not Allocated") 
  
 # Driver Code 
if __name__ == '__main__':  
    block_size = [5, 10, 20] 
    process_size = [10, 20, 5]  
    m = len(block_size) 
    n = len(process_size) 
  
    NextFit(block_size, m, process_size, n)



Output

Process No.	Process Size	Block No.
1		10		2
2		20		3
3		5		1