Program for Next Fit algorithm in Memory Management | Function to allocate memory to blocks as per Next fit algorithm ; Stores block id of the block allocated to a 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 Code
function NextFit ( blockSize , m , processSize , n ) { let allocation = Array . from ( { length : n } , ( _ , i ) => - 1 ) , j = 0 ; for ( let 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 ; } } document . write ( " " + " " ) ; for ( let i = 0 ; i < n ; i ++ ) { document . write ( i + 1 + Array ( 20 ) . fill ( ' \xa0 ' ) . join ( ' ' ) + processSize [ i ] + Array ( 20 ) . fill ( ' \xa0 ' ) . join ( ' ' ) ) ; if ( allocation [ i ] != - 1 ) { document . write ( allocation [ i ] + 1 ) ; } else { document . write ( " " ) ; } document . write ( " " ) ; } } let blockSize = [ 5 , 10 , 20 ] ; let processSize = [ 10 , 20 , 5 ] ; let m = blockSize . length ; let n = processSize . length ; NextFit ( blockSize , m , processSize , n ) ;