I want to writ a java that would read from a file called memory.txt a list of partition sizes of K’s, one partition per line. It will then read from a file called processes.txt a list of processes requesting these partitions. Each line simple contain a list of sizes in K’s too. the code should work with any number of partitions and any number of processes.

The program should determine which partitions are taken for successive segment requests of the processes in processes.txt if the algorithm used is:

i) First Fit
ii) Best Fit
iii) Next Fit (continues from last allocated hole) iv)
iv) Worst Fit.

The program should finally display the total of internal fragmentation and external fragmentation of the algorithms above.


Here is my attempt, am failing to do the worst, best fit and completing next and first algorithms....

import java.io.*;
import java.util.*;
public class MemoryManagement{
public static void main(String[]args)throws FileNotFoundException
{
String  internalFragmentation;
String  externalFragmentation;
int memory,dummycount;
int process;
int[] bestfit,worstfit,nextfit,firstfit;
Scanner iFile= new Scanner(new FileReader("processes.txt"));
Scanner inFile1= new Scanner(new FileReader("memory.txt"));


/* bestfit =inFile.nextInt();
worstfit =inFile.nextInt();
firstfit =inFile.nextInt();
nextfit =inFile.nextInt();


for (int i=0;i<memory;i++)
internalFragmentation = inFile.next();
externalFragmentation = inFile.next();


*/
dummycount=0;
while (inFile1.hasNext())
{
memory = inFile1.nextInt();
dummycount=dummycount+1;
}
firstfit =new int [dummycount];


inFile1.close();


System.out.println("------For FirstFit algorithm---------");


inFile1= new Scanner(new FileReader("memory.txt"));


dummycount=0;
while (iFile.hasNext())
{inFile1= new Scanner(new FileReader("memory.txt"));


process = iFile.nextInt();
while (inFile1.hasNext())
{


memory = inFile1.nextInt();
if ((memory >=process)&&(dummycount< firstfit.length)&&(firstfit[dummycount]==0))
{
firstfit[dummycount]=process;
System.out.println("memory partition" +" "+ memory +" will accomodate process that need"+ " " +process);
}
}



dummycount++;
inFile1.close();
}


iFile.close();


System.out.println("------For BestFit algorithm---------");



nextfit =new int [dummycount];


System.out.println("------For NextFit algorithm---------");
dummycount=0;
iFile= new Scanner(new FileReader("processes.txt"));


while (iFile.hasNext())
{


process = iFile.nextInt();
inFile1= new Scanner(new FileReader("memory.txt"));
while (inFile1.hasNext())
{


memory = inFile1.nextInt();
if ((memory >=process)&&(dummycount< nextfit.length)&&(nextfit[dummycount]==0))
{
nextfit[dummycount]=process;
System.out.println("memory partition" +" "+ memory +" will accomodate process that need"+ " " +process);
}
}



dummycount++;


}
System.out.println("dummycount" +" "+dummycount);


inFile1.close();
iFile.close();
System.out.println("------For WorstFit algorithm---------");


}
}

Edited 3 Years Ago by pyTony: fixed formatting

Help me here is my attempt.........

import java.io.*;
import java.util.*;
public class MemoryManagement{
   public static void main(String[]args)throws FileNotFoundException
  {
     String  internalFragmentation;
     String  externalFragmentation;
     int memory,dummycount;
     int process;
     int[] bestfit,worstfit,nextfit,firstfit;
     Scanner iFile= new Scanner(new FileReader("processes.txt"));
     Scanner inFile1= new Scanner(new FileReader("memory.txt"));

    /* bestfit =inFile.nextInt();
     worstfit =inFile.nextInt();
     firstfit =inFile.nextInt();
     nextfit =inFile.nextInt();

  for (int i=0;i<memory;i++)
   internalFragmentation = inFile.next();
     externalFragmentation = inFile.next();

   */ 
     dummycount=0;
     while (inFile1.hasNext())
     {
        memory = inFile1.nextInt();
        dummycount=dummycount+1;
     } 
     firstfit =new int [dummycount];

     inFile1.close();

     System.out.println("------For FirstFit algorithm---------");

     inFile1= new Scanner(new FileReader("memory.txt"));

     dummycount=0;
     while (iFile.hasNext())
     {inFile1= new Scanner(new FileReader("memory.txt"));

        process = iFile.nextInt();
        while (inFile1.hasNext())
        {     

           memory = inFile1.nextInt();
           if ((memory >=process)&&(dummycount< firstfit.length)&&(firstfit[dummycount]==0))
           {
              firstfit[dummycount]=process;
              System.out.println("memory partition" +" "+ memory +" will accomodate process that need"+ " " +process);  
           }
        }


        dummycount++;
        inFile1.close();
     }

     iFile.close();



     System.out.println("------For BestFit algorithm---------");


     nextfit =new int [dummycount];

     System.out.println("------For NextFit algorithm---------");
     dummycount=0;
     iFile= new Scanner(new FileReader("processes.txt"));

     while (iFile.hasNext())
     {

        process = iFile.nextInt();
        inFile1= new Scanner(new FileReader("memory.txt"));
        while (inFile1.hasNext())
        {     

           memory = inFile1.nextInt();
           if ((memory >=process)&&(dummycount< nextfit.length)&&(nextfit[dummycount]==0))
           {
              nextfit[dummycount]=process;
              System.out.println("memory partition" +" "+ memory +" will accomodate process that need"+ " " +process);  
           }
        }


        dummycount++;

     }
     System.out.println("dummycount" +" "+dummycount);

     inFile1.close();
     iFile.close(); 
     System.out.println("------For WorstFit algorithm---------");

  }
}

Edited 3 Years Ago by pyTony: fixed formatting

This article has been dead for over six months. Start a new discussion instead.