Hello,

I am trying to do a first fit allocation in java my problem is how do i keep track of which memory block i have cheked and if there are no suitable blocks put the job in a waiting queue.

Thanks for all help

import java.util.*;
import java.util.Scanner;

public class firstFit
{
    public static void main(String[] args) {
        
          Scanner keyboard = new Scanner(System.in);
          int[] blocks;             
          blocks = new int[4];      
          blocks[0] = 800; 
          blocks[1] = 900; 
          blocks[2] = 900; 
          blocks[3] = 300;
          //Arrays.sort(blocks);
         
          int[] jobs;             
          jobs = new int[3];      
          
          int index;
          
          System.out.println("Enter 3 jobs:");
          jobs[0] = keyboard.nextInt();
          Arrays.sort(jobs);
          
          for(index = 1; index < 3; index++){
              jobs[index] = keyboard.nextInt();
            }

          
          for(int j=0; j<3;j++){
              for(int i=0; i<4; i++){
                  if(jobs[j] <= blocks[i]){
                      blocks[i]=j;
                      System.out.println("Job" + jobs[j] + " in memory" + blocks[i]);
                      break;
                      }
                       else if(jobs[j] > blocks[i]){
                      System.out.println("Jobs in waiting queue " + jobs[j]);
                                            
                    }
                    
                }
            }       
          
        }    
}

Recommended Answers

All 4 Replies

You can declare and initialize your arrays all at once like this:

int[] jobs = new int[4];

When you read input using Scanner, you should ensure that your program does not crash by using:

if (keyboard.hasNextInt()){
myInt = keyboard.nextInt();
}

Calling Arrays.sort() on line 24 in your code doesn't do anything. Sorting an array with only one element, which is already in the first position of the array, does nothing. Combine the loop (notice that you can declare a variable inside of the for statement like so:

System.out.println("Enter 3 jobs");
for(int index = 0; index < 3; index++){
if (keyboard.hasNextInt()) jobs[index] = keyboard.nextInt();
}

Anyway, I was actually just giving you that advice as I went through, but I got to the end of your code and I don't understand what you are trying to do. Is your jobs array supposed to be some sort of queue? Saying it is a queue implies there is some order to the elements, i.e., that they are in some sort of a line and there is an order in which they come out .. but I don't see you using it like that anywhere. Can you copy/paste me your assignment statement (if this is a school assignment) or explain further? I don't understand what the relationship between blocks and jobs is supposed to be.

Hi,
Thank you very much for the reply and advice.
All i want to do is for example a user enters 3 jobs:
740k
500k
700k

And there are 3 blocks of memory:
700k
850k
310k

the program should get the jobs the user enters and allocate it to the first memory block which is equal to or less than the job. If there are no memory blocks that fit the criteira or they are already allocated to another job, then the job should be put in a waiting queue.

the pesudo code given is:

First-fit
1 Set counter to 1
2 Do while counter <= number of blocks in memory
If job_size > memory_size(counter)
counter = counter + 1
Else
load job into memory(counter)
adjust free/busy memory lists
go to step 4
End do
3 Put job in waiting queue
4 Fetch next job

I hope i have explained this well. Thank you

Ok, then you are going to need another array. You have your jobs array and your blocks array, but currently, there is no way for you to tell if a block is already allocated to a job. I suggest using an array of booleans, true to represent "block already taken" and false to represent "block not already taken". In the code below I used BLOCKS_SIZE as a constant - that way, if you need to change the size of your block array, everything else changes with it. I hope this example makes sense. You can use a method to assign one job. Then, by calling that method on every job, you can successfully assign every job. Of course, not all jobs can be assigned, which is why the method returns true (successfully assigned the job) or false (there was no spot for the job, it has to stay in waiting queue).

// .. code up here for class
private static int BLOCKS_SIZE = 4;
public static void main(String[] args){
int[] blocks = new int[BLOCKS_SIZE];
boolean[] blocksUsed = new boolean[BLOCKS_SIZE];

// .. code for getting user input 

for (int i = 0; i < jobs.length; i++){
boolean wasAssigned = assignJob(jobs[i]);
//  Check wasAssigned variable. If wasAssigned is true then
// the job can be taken out of the waiting queue, otherwise,
// no spot was found for the job in blocks, so it should stay.
}

public static boolean assignJob(int job, int[] block, boolean[] blocksUsed){
//I'll leave writing this code to you. This method should return
//true if the job was successfully put into a block, and false otherwise.
//The method should try to put the job into the block array. If it was
 //successful the blocksUsed array should be edited. For example, if 
//you put the job into block[0] then say blocksUsed[0] = true. 
//Before putting anything into a block, make sure it is free (i.e. make
//sure blocksUsed[index] is false.

for (int i = 0; i < block.length; i++){
//try to find a free spot in the block array that is OK for the job.
}

}

}

If you managed to do this, any chance of posting the code?

Edit-Thanks in advance.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.