First Fit program in JAVA

First Fit Program in JAVA

The operating system is also refereed as the resource manager which allocates the different resources to the processes. One of such processes is the operating system memory which is allocated based on the demand of the processes.  The operating system uses different schemes for this purpose, the most common among them are, first fit, best fit, and worst fit.

How first fit works?

Whenever a process (p1) comes with memory allocation request the following happens –

  • OS sequentially searches available memory blocks from the first index
  • Assigns the first memory block large enough to accommodate process

Whenever a new process P2 comes, it does the same thing. Search from the first index again.

First Fit Allocation in OS

Methods discussed

We will look at two different methods –

  • Method 1 – Blocks allowed to keep just one single process
  • Method 2 – Blocks allowed to keep multiple processes, if partitioned fragmentation big enough for new processes

Method 1 Code

Run
class Main
{
    public static void main (String[]args)
    {

        int blockSize[] = {100, 50, 30, 120, 35};
        int processSize[] = {20, 60, 70, 40};
        int m = blockSize.length;
        int n = processSize.length;
        implimentFirstFit(blockSize, m, processSize, n);
    }

    static void implimentFirstFit(int blockSize[], int blocks, int processSize[], int processes) {
        // This will store the block id of the allocated block to a process
        int allocate[] = new int[processes];
        int occupied[] = new int [blocks];

        // initially assigning -1 to all allocation indexes
        // means nothing is allocated currently
        for (int i = 0; i < allocate.length; i++)
            allocate[i] = -1;

        for(int i = 0; i < blocks; i++){
            occupied[i] = 0;
        }

        // take each process one by one and find
        // first block that can accomodate it
        for (int i = 0; i < processes; i++)
        {
            for (int j = 0; j < blocks; j++)
            {
                if (!(occupied[j] > 0) && blockSize[j] >= processSize[i])
                {
                    // allocate block j to p[i] process
                    allocate[i] = j;
                    occupied[j] = 1;

                    break;
                }
            }
        }

        System.out.println("\nProcess No.\tProcess Size\tBlock no.\n");
        for (int i = 0; i < processes; i++)
        {
            System.out.print(i + 1 + "\t\t\t" + processSize[i] + "\t\t\t");
            if (allocate[i] != -1)
                System.out.println(allocate[i] + 1);
            else
                System.out.println("Not Allocated");
        }
    }
}

Output:

Process No.	Process Size	Block no.

1		20		1
2		60		4
3		70		Not Allocated
4		40		2

Method 2

This method allows multiple processes to share same block if size is enough

Run
class Main
{
    public static void main (String[]args)
    {

        int blockSize[] = {100, 50, 30, 120, 35};
        int processSize[] = {20, 60, 70, 40};
        int m = blockSize.length;
        int n = processSize.length;
        implimentFirstFit(blockSize, m, processSize, n);
    }

    static void implimentFirstFit(int blockSize[], int blocks, int processSize[], int processes) {
        // This will store the block id of the allocated block to a process
        int allocate[] = new int[processes];
        int occupied[] = new int [blocks];

        // initially assigning -1 to all allocation indexes
        // means nothing is allocated currently
        for (int i = 0; i < allocate.length; i++)
            allocate[i] = -1;

        // take each process one by one and find
        // first block that can accommodate it
        for (int i = 0; i < processes; i++)
        {
            for (int j = 0; j < blocks; j++) {
                if (blockSize[j] >= processSize[i])
                {
                    // allocate block j to p[i] process
                    allocate[i] = j;

                    // Reduce size of block j as it has accommodated p[i]
                    blockSize[j] -= processSize[i];

                    break;
                }
            }
        }

        System.out.println("\nProcess No.\tProcess Size\tBlock no.\n");
        for (int i = 0; i < processes; i++)
        {
            System.out.print(i + 1 + "\t\t\t" + processSize[i] + "\t\t\t");
            if (allocate[i] != -1)
                System.out.println(allocate[i] + 1);
            else
                System.out.println("Not Allocated");
        }
    }
}

Output:

Process No.	Process Size	Block no.

1		20		1
2		60		1
3		70		4
4		40		2