You are to simulate a multi-partitioned memory management system where processes
ID: 3853096 • Letter: Y
Question
You are to simulate a multi-partitioned memory management system where
processes are initiated and terminated requiring that memory space be allocated
and de-allocated. Free memory space is to kept in a singly linked list as
discussed in class, using the data structure containing allocation bits and
various pointers also discussed. This is a variable-partition system with
different sized "holes" and not the fixed (same) sized partions also
discussed in the text.
New processes are to be entered from a GUI with a size as a parameter.
(numbering should be automatic). Processes are also to be terminated by a
GUI action. With each process change a free memory list status is to
be output clearly showing the list items and their sizes. First-fit is to
be the allocation algorithm. Memory sizes and numbers of processes can be
small, just big enough to demonstrate the below listed functionality.
Functionality to be provided by you:
1. FF allocation.
2. De-allocation with the four combination possibilities accounted for and
demonstrated.
3. A free space status provided for every instance of #1 and #2 above.
4. A process status on command (p#, size, and where located).
5. Garbage compaction as necessary.
6. Appropriate error msg when there is insufficient memory available AFTER
compaction.
can be done using java in eclipse with window builder
The 4 De-allocation possibilities are that
1) if the free space is used and the top is empty and the bottom is empty
2) if the top is full and the bottom is empty (easier than option 3)
3) if the top is empty and the bottom is full
4) if all spaces are full
Explanation / Answer
import java.io.*;
public class Fitting {
BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
int process[], rprocess[], block[], rblock[], usage[], rusage[];
int p, b, free, used, rfree, rused, c, c1;
public Fitting() throws IOException{
System.out.println("Enter number of blocks");
b=Integer.parseInt(input.readLine());
System.out.println("Enter number of processes");
p=Integer.parseInt(input.readLine());
process=new int[p];
rprocess=new int[p];
block=new int[b];
rblock=new int[b];
usage=new int[b];
rusage=new int[b];
c=0;
// TODO Auto-generated constructor stub
}
void read() throws IOException
{
int i;
System.out.println("Enter block sizes");
for(i=0;i<b;i++)
{
System.out.print("Block "+(i+1)+" : ");
rblock[i]=Integer.parseInt(input.readLine());
}
System.out.println("Enter block usage scenario");
for(i=0;i<b;i++)
{
System.out.println("Is block "+(i+1)+" used (1) or free (0)?");
rusage[i]=Integer.parseInt(input.readLine());
if(rusage[i]==1)
{
rused=rused+rblock[i];
c1++;
}
else
rfree=rfree+rblock[i];
}
System.out.println("Enter process demand");
for(i=0;i<p;i++)
{
System.out.print("Process "+(i+1)+" : ");
rprocess[i]=Integer.parseInt(input.readLine());
}
}
void reset()
{
int i;
for(i=0;i<b;i++)
{
block[i]=rblock[i];
usage[i]=rusage[i];
}
for(i=0;i<p;i++)
process[i]=rprocess[i];
used=rused;
free=rfree;
c=c1;
}
// A free space status
void display()
{
int total;
total=rused+rfree;
System.out.println("Blocks used = "+c);
System.out.println("Total used space = "+used);
System.out.println("Blocks free = "+(b-c));
System.out.println("Total free space = "+(total-used));
}
// FF allocation.
void f_fit()
{
int i,j;
for(i=0;i<p;i++) //Processes.
for(j=0;j<b;j++) //Blocks.
{
if(process[i]<=block[j]&&usage[j]==0)
{
usage[j]=1;
used=used+block[j];
c++;
System.out.println("Process "+(i+1)+" is in block "+(j+1)); A process status on command (p# and where located).
break;
}
}
}
void f_fit()
{
int i,j;
for(i=0;i<p;i++) //Processes.
for(j=0;j<b;j++) //Blocks.
{
if(process[i]<=block[j]&&usage[j]==0)
{
usage[j]=1;
used=used+block[j];
c++;
System.out.println("Process "+(i+1)+" is in block "+(j+1)); A process status on command (p# and where located).
break;
}
}
}
// if the free space is used and the top is empty and the bottom is empty
void f1_deallocation()
{
int i,j;
for(i=0;i<p;i++) //Processes.
for(j=0;j<b;j++) //Blocks.
{
if(process[i]<=block[j]&&usage[j]==0)
{
usage[j]=1;
used=used+block[j];
c++;
System.out.println("Process "+(i+1)+" is in block "+(j+1)); A process status on command (p# and where located).
break;
}
}
}
// if the top is full and the bottom is empty
void f2_deallocation()
{
int i,j,size,b;
for(i=0;i<p;i++)
{
size=32967;
b=-1;
for(j=0;j<b;j++)
{
if(process[i]<=block[j]&&usage[j]==0&&(block[j]-process[i])<size)
{
size=block[j]-process[i];
b=j;
}
}
if(size<32967&&b!=-1)
{
usage[b]=1;
used=used+block[b];
c++;
System.out.println("Process "+(i+1)+" is in block "+(b+1));A process status on command (p# and where located).
}
}
}
// if the top is empty and the bottom is full
void f3_deallocation()
{
int i,j;
for(i=0;i<p;i++) //Processes.
for(j=0;j<b;j++) //Blocks.
{
if(process[i]<=block[j]&&usage[j]==0)
{
usage[j]=1;
used=used+block[j];
c++;
System.out.println("Process "+(i+1)+" is in block "+(j+1)); A process status on command (p# and where located).
break;
}
}
}
//if all spaces are full
void f4_deallocation()
{
int i,j,size,w;
for(i=0;i<p;i++)
{
size=0;
w=-1;
for(j=0;j<b;j++)
{
if(process[i]<=block[j]&&usage[j]==0&&(block[j]-process[i])>size)
{
size=block[j]-process[i];
w=j;
}
}
if(w!=-1)
{
usage[w]=1;
used=used+block[w];
c++;
System.out.println("Process "+(i+1)+" is in block "+(w+1));A process status on command (p# and where located).
}
}
}
/**
* @param args
*/
public static void main(String[] args) throws IOException{
BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
int option;
String choice;
Fitting f=new Fitting();
f.read();
do
{
f.reset();
System.out.println("Menu");
System.out.println("1. First fit");
System.out.println("2. if the free space is used and the top is empty and the bottom is empty");
System.out.println("3. if the top is full and the bottom is empty");
System.out.println("4. if the top is empty and the bottom is full");
System.out.println("5. if all spaces are full");
System.out.println("6. Block information");
System.out.println("Enter option");
option=Integer.parseInt(input.readLine());
switch(option)
{
case 1: f.f_fit();
break;
case 2: f.f1_dealloacation();
break;
case 3: f.f2_deallocation();
break;
case 4: f.f3_deallocation();
break;
case 5: f.f4_deallocation();
break;
case 6: f.display();
break;
default: System.out.println("Invalid input");
}
f.display();
System.out.println("Press Y to continue");
choice=input.readLine();
}
while(choice.compareToIgnoreCase("y")==0);
// TODO Auto-generated method stub
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.