Program for Next Fit algorithm in Memory Management | C # program for next fit memory management algorithm ; Function to allocate memory to blocks as per Next fit algorithm ; Stores block id of the block allocated to a process ; Initially no block is assigned to any process ; pick each process and find suitable blocks according to its size ad assign to it ; Do not start from beginning ; allocate block j to p [ i ] process ; Reduce available memory in this block . ; mod m will help in traversing the blocks from starting block after we reach the end . ; Driver program
using System ; using System . Linq ; public class GFG { static void NextFit ( int [ ] blockSize , int m , int [ ] processSize , int n ) { int [ ] allocation = new int [ n ] ; int j = 0 ; Enumerable . Repeat ( - 1 , n ) . ToArray ( ) ; for ( int i = 0 ; i < n ; i ++ ) { while ( j < m ) { if ( blockSize [ j ] >= processSize [ i ] ) { allocation [ i ] = j ; blockSize [ j ] -= processSize [ i ] ; break ; } j = ( j + 1 ) % m ; } } Console . Write ( " STRNEWLINE Process ▁ No . TABSYMBOL Process ▁ Size TABSYMBOL Block ▁ no . STRNEWLINE " ) ; for ( int i = 0 ; i < n ; i ++ ) { Console . Write ( i + 1 + " TABSYMBOL TABSYMBOL " + processSize [ i ] + " TABSYMBOL TABSYMBOL " ) ; if ( allocation [ i ] != - 1 ) { Console . Write ( allocation [ i ] + 1 ) ; } else { Console . Write ( " Not ▁ Allocated " ) ; } Console . WriteLine ( " " ) ; } } static public void Main ( ) { int [ ] blockSize = { 5 , 10 , 20 } ; int [ ] processSize = { 10 , 20 , 5 } ; int m = blockSize . Length ; int n = processSize . Length ; NextFit ( blockSize , m , processSize , n ) ; } }