best fit program in java


import java.io. * ;
class best {
	public static void main(String args[]) throws IOException {
		int flag[] = new int[10];
		int sm[] = new int[10];
		int i,
		sr,
		nm;
		int sp,
		loc = 0;
		int cntrl = 1000;
		DataInputStream in =new DataInputStream(System. in );
		System.out.println("Enter the no of memory segments\n");
		nm = Integer.parseInt( in .readLine());
		System.out.println("Enter the size of memory segments\n");

		for (i = 0; i & lt; nm; i++)
		sm[i] = Integer.parseInt( in .readLine());
		for (i = 0; i & lt; nm; i++)
		flag[i] = 0;
		System.out.println("Before best fit allocation\n");
		System.out.println("\nIndex\t\tMemory Segments\n");
		for (i = 0; i & lt; nm; i++)
		System.out.println((i + 1) + "\t\t" + sm[i]);
		System.out.println("\nEnter the space requirement for new process\n");
		sr = Integer.parseInt( in .readLine());
		for (i = 0; i & lt; nm; i++) {
			if (flag[i] == 0) {
				sp = sm[i];
				if (sr & lt; = sp) {
					if (cntrl & gt; sp) {
						cntrl = sp;
						loc = i;
					}
				}
			}
		}
		if (cntrl == 0) System.out.println("\n Space not available");
		else {
			sm[loc] = sr;
			flag[loc] = 1;
		}
		System.out.println("\nAfter Bestfit Allocation\n");
		System.out.println("Index \t\t Memory Segment\n");
		for (i = 0; i & lt; nm; i++) {
			System.out.println((i + 1) + "\t\t" + sm[i]);
		}
		System.out.println("\n The process allocated to the memory segments " + (loc + 1));
	}
}

/*best fit program in java OUTPUT:
Enter the no of memory segments
3
Enter the size of memory segments
30
15
25
Before best fit allocation

Index Memory Segments

1 30
2 15
3 25

Enter the space requirement for new process
16
After Bestfit Allocation

Index Memory Segment

1 30
2 15
3 16

The process allocated to the memory segments 3
*/

Facing difficulties in understanding the program ?

Ask Your Queries in Comment Box

You may also like...

5 Responses

  1. rajita says:

    can u plz tell the use of variable cntrl =1000 in the bestfit algorithm.

  2. rajita says:

    plz tell the meaning of cntrl =1000 in bestfit program

  3. alisha says:

    jaldi bhai... teacher ko dikhana hai

Leave a Reply

Your email address will not be published. Required fields are marked *