First Fit Algorithm

First Fit Algorithm

First Fit algorithm is used to allocate jobs in the memory in arbitrary order. I will only explain it briefly assuming you wouldn’t be interested in the code unless you know what this algorithm is all about. It works by placing each job in the first memory block that can accommodate it (memory block is equal to or larger than job size).

The job is sent to a waiting queue if it is larger than all available blocks!
The following code is a Java implementation of the algorithm which I developed supported by few comments.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/**
 * Implementation of a First Fit Alogrithm. A number of jobs are processed
 * if they are smaller or equal to the memory blocks. If the job is biger 
 * then it is sent to a waiting queue!
 * 
 * Author: Haider M. al-Khateeb 
 * Last Changed: 26/02/2011
 */
 
public class FirstFitAlgorithm
{
 
    double [] jobSize;
    double [] memorySize;
 
    // the number of memory blocks
    int blocks;
 
    // the number of jobs
    int jobs;
 
    int theCounter;
    int jobIndex;
    int jobInQueue;
 
public void setJobs(double job1, double job2, double job3, double job4, double job5)
{
    jobSize = new double[5];
     // jobs
    jobSize[0] = job1;  // job 1
    jobSize[1] = job2;  // job 2
    jobSize[2] = job3;  // job 3
    jobSize[3] = job4;  // job 4
    jobSize[4] = job5;  // job 5
    jobs = jobSize.length;
}
 
public void setMemory(double memory1, double memory2, double memory3, double memory4, double memory5)
{
     memorySize = new double[5];
    //available memory blocks
    memorySize[0] = memory1;  // memory block 1
    memorySize[1] = memory2;  // memory block 2
    memorySize[2] = memory3;  // memory block 3
    memorySize[3] = memory4;  // memory block 4
    memorySize[4] = memory5;  // memory block 5
    blocks = memorySize.length;
}
 
public void firstFit(int countr, int jobIndex)
{
 
   theCounter = countr;
   jobIndex = jobIndex;
 
do {
            if (jobSize[jobIndex] > memorySize[theCounter-1])
        {  
            theCounter += 1;
        }
        else
        { 
        System.out.println("----------------------------------");
        System.out.println("Job " + (jobIndex+1) + " of size "  + jobSize[jobIndex] + " has been loaded into memory block:" + theCounter);
        memorySize[theCounter-1] = (memorySize[theCounter-1]-jobSize[jobIndex]);
        System.out.println("The size of memory block " + theCounter + " is now " + memorySize[theCounter-1]);
        theCounter = 1;
        jobIndex += 1;
        }
 
  }   while (theCounter <= blocks && jobIndex < jobs);
 
  System.out.println("----------------------------------");
  jobInQueue = jobIndex;
  if (jobInQueue < jobs) {
  System.out.println("Job " + (jobInQueue+1) + " of size " + jobSize[jobInQueue] + " is sent to waiting queue!");
 
}
 
}
 
public void showData() {
System.out.println("==========================================================================");
System.out.println("Available memory blocks are: (" + memorySize[0] + "), (" + memorySize[1] + "),(" + memorySize[2] + "), (" + memorySize[3] + "), (" + memorySize[4] + ")");
System.out.println("And jobs to allocate are: (" + jobSize[0] + "), (" + jobSize[1] + "),(" + jobSize[2] + "), (" + jobSize[3] + "), (" + jobSize[4] + ")");
System.out.println("==========================================================================");
System.out.println("START");
System.out.println("===========================");
}
 
public static void main(String [] args)
 
{
 
  FirstFitAlgorithm ob = new FirstFitAlgorithm();
  ob.setJobs(64,80,10,5,18); // set new jobs, change these values if you want
  ob.setMemory(10,70,20,20,35); // ser memory blocks, change these values if you want
  ob.showData(); // display detail of available memory blocks and jobs to allocate 
  ob.firstFit(1,0);
 
  if (ob.jobInQueue < ob.jobs) {
      ob.firstFit(1,(ob.jobInQueue+1));
    }
 
  }
 
 
}

4 Responses to “First Fit Algorithm in java”

  • Tom Smith:

    Hey,

    thanks for publishing this topic it has helped me understand my homework topic greately, could you be persuaded to write one about the best fit algorithm as I also need to understand that for my homework, with the same java program, 5 jobs, 5 memory blocks etc

    it would be hugely appreciated!

    thanks again and keep up the great examples!

  • ngngar:

    Thanks for this!!!!

  • Anto Oswin:

    this is a c++ code to implement first fit algo…

    #include
    #include
    #include
    void Exception()
    {cout<<"\nException Encountered!";
    getch();
    exit(1);
    }
    void main()
    {int a[20],b[20],i,n,p,j,x=0,s=0,l=0;
    clrscr();
    cout< “;
    cin>>n;
    cout< “;
    cin>>a[0];
    if(a[0]>n){Exception();}
    n=n-a[0];
    cout< \n”;
    for(i=1;s<n;i++)
    {cout<<"Block "<<i< “;
    cin>>a[i];
    s=s+a[i];
    if(s>n){Exception();}
    x=i+1;
    }
    cout< “;
    cin>>p;
    cout< \n”;
    for(i=0;i<p;i++)
    {cout<<"Process "<<i+1< “;
    cin>>b[i];
    l=0;
    j=1;
    while(l==0)
    {
    if(a[j]>=b[i]){a[j]=a[j]-b[i];
    l=1;
    cout<<"\nProcess "<<i+1<<" Stored in Block "<<j<<" .\n\n";
    }
    else{
    l=0;
    j++;
    if(j==x){Exception();}
    }}}
    l=0;
    for(i=0;i<x;i++)
    {if(i==0){cout< “;}
    else{cout<<"Process(Block "<<i< “;
    l=l+a[i];
    }
    cout<<a[i]<<"\n";

    }
    cout<<"\nFragmentation Percentage = "<<(l*100)/s<<"%";
    getch();
    }

  • Anto Oswin:

    the header files appear…
    here are theheader files -
    stdlib.h
    conio.h
    ostream.h

Leave a Reply

*

Haider’s WebSpace
Welcome to my technical blog. This is where I write, archive and share computer related articles. Subjects vary from posting technical solutions to researching particular topics. Feel free to comment and talk IT!
Sponsored Links
My Tweets