I've posted in the java forum but nobody has given any feedback.
Develop a program which represents a simple memory allocation system.
This sytem processes one integrated memory and the allocating technique.
In this system, process ID will be created by the program but adding or removing
any process to/from the memory is based on the user's entry. each process has a unique
ID and size(the space needed for process in memory).
User's entry should include 3 parts:
1. User will choose to add or remove a process
2. If user chooses to add the next entry is task size. If user chooses
delete the next entry is the process ID to remove
3. If user selects add, then he must choose either the First fit or BestFit method.
I've enclosed my code which is how far I've gotten so far I finished everything but the best fit method only i dont know how to code it so that the best position in memory rather than the first available one is chosen.
Code:
public class Main{
int[]memory = new int[100];
int processId = 0;
int taskSize = 0;
public static void main(String []args){
Main themain = new Main();
themain.startMemory();
}
public void startMemory()
{
int choice;
String A = JOptionPane.showInputDialog(null, "Please enter either 1 or 2 : \n 1- Add Process " +
"\n 2- Remove Process ","CHOOSE", JOptionPane.QUESTION_MESSAGE);
choice = Integer.parseInt(A);
switch(choice)
{
case 1:
addProcess();
break;
case 2:
processId=getProcessId();
removeProcess(processId);
printMemory(memory);
switch(choice)
{
case 1:
addProcess();
break;
case 2:
processId=getProcessId();
removeProcess(processId);
printMemory(memory);
break;
}
startMemory();
}
public void addProcess(){
//printMemory(memory);
processId++;
int choice;
taskSize = getTaskSize();//getting taskSize from user after user decides to add process
String B = JOptionPane.showInputDialog(null, "Please enter either 1 or 2 : \n 1- First Fit (FF) \n 2- BestFit (BF)",
"CHOOSE", JOptionPane.QUESTION_MESSAGE);
choice = Integer.parseInt(B);
switch(choice){
case 1:
firstFit(taskSize,processId,memory);
printMemory(memory);
break;
case 2:
bestFit(taskSize,processId,memory);
printMemory(memory);
break;
default: JOptionPane.showMessageDialog(null,"Invalid Choice, Please try again",
"MESSAGE",JOptionPane.INFORMATION_MESSAGE);
}
}
public void firstFit(int taskSize,int processId, int[]memory){
int len = 0;
int check=0;
for (int i = 0; i < 100; i++)
{
if (memory[i] == 0)
{
if(len == 0){
taskSize = taskSize+i;
len++;// len is incrementd to ensure that task size is only added once to i
}
check= 0;
for(int x=i; x<taskSize; x++)
{
if (memory[i] != 0)
{
check = 1;
break;
}
}
if (check == 0)
{
for (int n=i; n<taskSize; n++)
memory[n] = processId;
}
}
}
}
public void bestFit(int taskSize,int processId, int[]memory)
{
int len = 0;
int count = 0;
int tempSize = taskSize;
int smallest = 100;
for (int i = 0; i < 100; i++)
{
if (memory[i] == 0)
{
if(len == 0)
{
taskSize = taskSize +i;
len= len+1;
}
int flip = 0;
for (int j = i; j< taskSize; j++)
{
if (memory[j] != 0)
{
if(smallest> tempSize && count < smallest)
{
smallest = count;
count = 0;
}
}
else count = count+1;
}
if (flip == 0)
{
for (int k=i; k<taskSize; k++)
memory[k] = processId;
}
}
}
}
public int getTaskSize(){
String C = JOptionPane.showInputDialog(null, "Task Size - Integers from 1 to 9",
"CHOOSE", JOptionPane.QUESTION_MESSAGE);
int ts = Integer.parseInt(C);
return ts;
}
public int getProcessId()
{
String C = JOptionPane.showInputDialog(null, "Please enter the process Id - Integers from 1 to 9",
"INPUT", JOptionPane.QUESTION_MESSAGE);
int pId = Integer.parseInt(C);
return pId;
}
public void removeProcess(int processId)
{
for(int i=0;i<100;i++)
if (memory[i]==processId)
{
memory[i]=0;
}
}
public void printMemory(int[]memory){
for(int cell=0; cell<50; cell++)
System.out.print(memory[cell]);
}
}
Help would be very much appreciated
Edited by WingedPanther, 14 October 2008 - 04:31 PM.
add code tags


Sign In
Create Account

Back to top









