Worst Fit Program In Java

Worst Fit algorithm in OS

Worst Fit Program in Java

 

Processes in an operating system are allocated memory during their processing time in the CPU. The processes demand memory locations to the processor which allocates memory based on the size of the process. The three most commonly used memory allocation schemes are Best fit, worst fit, and first fit.

Processes in an operating system are allocated memory during their processing time in the CPU. The processes demand memory locations to the processor which allocates memory based on the size of the process. The three most commonly used memory allocation schemes are Best fit, worst fit, and first fit.

In the case of worst fit memory allocation scheme, the processor assigns any random free memory block which is greater than the memory demanded by the process. In this case, there is a probability of maximum memory wastage as the processor assigns any free memory block randomly to the demanding process. Therefore, this memory allocation scheme is also known as the worst fit.

Program code for Worst Fit Memory management Scheme

public class PrepInsta
{
//Function to allocate memory to empty blocks based on the worst fit algorithm. 
static void WorstFit(int b_size[], int m, int p_size[], int n)
{
//stores block id of the block which is allocated to a process
Int allocate[] = new int[n];
// no block is assigned to a process initially
for ( int I =0; i< allocate.length++)
allocate[i] =-1;
//select each process and find suitable blocks as per its size 
//block assignment is also done here
for (int i=0; i<n; i++)
{
// determine the first fit block for the current process
Int wstIdx = -1;
for (int j=0; j<m; j++) { if (b_Size[j] >= p_Size[i]) 
{ 
 if (wstIdx == -1) 
wstIdx = j; 
else if (b_Size[wstIdx] < b_Size[j]) 
wstIdx = j;
} 
}


// searching an empty memory block for the current process 
 
if (wstIdx != -1) 
{ 
// assigning memory block j to the process p[i] 
allocate[i] = wstIdx; 

// Reduce available memory in this block. 
b_Size[wstIdx] -= p_Size[i]; 
} 
} 
 
 
System.out.println
 
("\nProcess Number \tProcess Size\tBlock Number "); 
for (int i = 0; i < n; i++) 
{ 
 System.out.print
 
(" " + (i+1) + "\t\t" + processSize[i] + "\t\t"); 
 
if (allocation[i] != -1) 
System.out.print
 
(allocation[i] + 1); 

else
 System.out.print
 
("Not Allocated");


 System.out.println(); 
} 
} 

// Driver code 


public static void main(String[] args) 
{ 
int b_Size[] = {100, 500, 200, 300, 600}; 
int p_Size[] = {212, 417, 112, 426}; 
int m = b_Size.length; 
int n = p_Size.length; 

worstFit(b_Size, m, p_Size, n); 
} 
} 

Output

[table id=678 /]