Program for Worst Fit algorithm in Memory Management | Method to allocate memory to blocks as per worst 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 ; Find the best fit block for current process ; If we could find a block for current process ; Allocate block j to p [ i ] process ; Reduce available memory in this block . ; Driver code
function worstFit ( blockSize , m , processSize , n ) { let allocation = new Array ( n ) ; for ( let i = 0 ; i < allocation . length ; i ++ ) allocation [ i ] = - 1 ; for ( let i = 0 ; i < n ; i ++ ) { let wstIdx = - 1 ; for ( let j = 0 ; j < m ; j ++ ) { if ( blockSize [ j ] >= processSize [ i ] ) { if ( wstIdx == - 1 ) wstIdx = j ; else if ( blockSize [ wstIdx ] < blockSize [ j ] ) wstIdx = j ; } } if ( wstIdx != - 1 ) { allocation [ i ] = wstIdx ; blockSize [ wstIdx ] -= processSize [ i ] ; } } document . write ( " " + " " + " " ) ; for ( let i = 0 ; i < n ; i ++ ) { document . write ( " " + ( i + 1 ) + " " + " " + processSize [ i ] + " " ) ; if ( allocation [ i ] != - 1 ) document . write ( allocation [ i ] + 1 ) ; else document . write ( " " ) ; document . write ( " " ) ; } } let blockSize = [ 100 , 500 , 200 , 300 , 600 ] ; let processSize = [ 212 , 417 , 112 , 426 ] ; let m = blockSize . length ; let n = processSize . length ; worstFit ( blockSize , m , processSize , n ) ;