answersLogoWhite

0

import java.util.*;

class Partition

{

private int partstart;

private int partend;

private int partsize;

private int prosssize;

private int pid;

//private int intfrag;

private boolean empty;

private Partition next;

Partition Head=null;

public void createDynamicPartitions(int totmem)

{

Partition p=new Partition();

p.partsize=totmem;

//p.prosssize=0;

p.pid=-1;

//p.intfrag=0;

p.empty=true;

p.next=null;

p.partstart=0;

p.partend=totmem-1;

Head=p;

memStatus();

}

private Partition selectFirstHole(int prsize)

{

Partition temp=Head,first=null;

while(temp != null)

{

if (temp.empty && temp.partsize >= prsize)

{

break;

}

temp=temp.next;

}

return first;

}

public void loadProcess(int pid,int prsize)

{

Partition hole=selectFirstHole(prsize);

if(hole null)

System.out.println("There is no Process with ID :"+dpid);

else

memStatus();

}

private void memStatus()

{

Partition temp=Head;

System.out.println("Current Memory Status");

// int totfrag=0;

while(temp != null)

{

if(temp.empty)

System.out.println(temp.partstart+"-"+temp.partend+" : is hole of size "+temp.partsize);

else

System.out.println(temp.partstart+"-"+temp.partend+" :Memory with Process ID "+temp.pid+" of size "+temp.partsize);

//totfrag=totfrag+temp.intfrag;

temp=temp.next;

}

//System.out.println("\n Total Internal Fragmentation : "+totfrag);

}

}

public class FirstFit

{

public static void main(String args[])

{

Scanner sc=new Scanner(System.in);

System.out.println("Enter Total Memory size :");

int totmem=sc.nextInt();

//System.out.println("Enter number of partitions :");

//int numofpartions=sc.nextInt();

//creating partitions

Partition dp=new Partition();

dp.createDynamicPartitions(totmem);

while(true)

{

System.out.println("Menu");

System.out.println("1.Load Process");

System.out.println("2.Remove Process");

System.out.println("3.Exit");

System.out.println("Enter ur Choice : ");

int ch=sc.nextInt();

switch(ch)

{

case 1:

System.out.println("Enetr Process ID : ");

int pid=sc.nextInt();

System.out.println("Enetr Process size : ");

int prsize=sc.nextInt();

dp.loadProcess(pid,prsize);

break;

case 2:

System.out.println("Enetr Process ID : ");

int dpid=sc.nextInt();

dp.removeProcess(dpid);

break;

case 3:

System.exit(0);

//break;

default:

System.out.println("*** Wrong Choice ***\n Choice must be in b/w 1- 3");

}

}

}

}

User Avatar

Wiki User

12y ago

Still curious? Ask our experts.

Chat with our AI personalities

TaigaTaiga
Every great hero faces trials, and you—yes, YOU—are no exception!
Chat with Taiga
ViviVivi
Your ride-or-die bestie who's seen you through every high and low.
Chat with Vivi
RafaRafa
There's no fun in playing it safe. Why not try something a little unhinged?
Chat with Rafa

Add your answer:

Earn +20 pts
Q: Java code to implement the the first fit algorithm?
Write your answer...
Submit
Still have questions?
magnify glass
imp