firstfit
package FF;
import java.util.*;
public class ff_pr {
public static void firstfit(int blockSize[],int m,int processSize[], int n) {
int allocation [] = new int [n];
for(int i=0;i<n;i++) {
allocation[i]=-1;
}
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
if(blockSize[j]>=processSize[i]) {
allocation[i]=j+1;
blockSize[j]-=processSize[i];
break;
}
}
}
System.out.println("\nProcess No.\tProcess Size\tBlock No.");
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]);
}else {
System.out.print("Not Allocated");
}
System.out.println();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner (System.in);
System.out.println("Enter no. of processes : ");
int n = sc.nextInt();
System.out.println("Enter no. of Blocks : ");
int m = sc.nextInt();
int blockSize[] = new int [m];
int processSize[] = new int [n];
for(int i=0;i<n;i++) {
System.out.println("Enter Process size for process : "+(i+1));
processSize[i]=sc.nextInt();
// System.out.println("Enter Block size for Block : "+(i+1));
// blockSize[i]=sc.nextInt();
}
for(int i=0;i<m;i++) {
// System.out.println("Enter Process size for process : "+(i+1));
// processSize[i]=sc.nextInt();
System.out.println("Enter Block size for Block : "+(i+1));
blockSize[i]=sc.nextInt();
}
firstfit(blockSize,m,processSize,n);
}
}
Comments
Post a Comment