Question

In: Computer Science

Objective: Write a C++ -program that will implement 4 Memory Management algorithms Algorithms: A) Best-Fit B)...

Objective: Write a C++ -program that will implement 4 Memory Management algorithms

Algorithms:
A) Best-Fit
B) First-Fit
C) Next-Fit
D) Worst-Fit
Your program must do the following:
1. Program Input:
            User will input to the program
a. Main Memory information, including
i. The Number of Memory partitions.
ii. The Size of each memory partition.
b. Process information (assign a unique identifier to each job)
i. User will input the number of processes
ii. Memory requirements for each process/job
2. For each algorithm, your program should have a data structure(class or struct) that will include the following:
    Job/Process Class
• Name of the process/ job(number or word)
• Process/job status (Run/Wait),
• partition number the process/job was assigned to
Memory Partition Class
• Name of the partition (Alphanumeric ID, ie string or char string)
• Total size of the partition(int)
• Is a job assigned? (bool)
• Job id assigned to it
• Waste or excess produced by the job assignment (int)
You can create an array or list of the object to represent the job queue.
3. Program output:
a) Initial memory allocation: Calculate and display a list of initial memory allocation, i.e which partitions contain which process after the first round of allocation
b) Memory waste: Program will calculate and display the memory waste for each partition
c. total waste for each algorithm.
e. A list of Processes in the waiting State(was not assigned to a partition).

Solutions

Expert Solution

Hi,

Please find the code below.

(A) Best-Fit Algorithm Program
#include<iostream>
using namespace std;

int main()
{
int frag[20],b[20],p[20],i,j,nb,np,temp,lowest=9999;
static int barray[20],parray[20];
cout<<"\n\t\t\tMemory Management Scheme - Best Fit";
cout<<"Enter no. of memory partitions: ";
cin>>nb;
cout<<"\nEnter the number of processes:";
cin>>np;
  
cout<<"\nEnter size of each mermory partition: \n";
for(i=1;i<=nb;i++)
{
cout<<"Partition no."<<i<<":";
cin>>b[i];
}
  
cout<<"\nEnter the size of each process :-\n";
for(i=1;i<=np;i++)
{
cout<<"Process no. "<<i<<":";
cin>>p[i];
}
  
for(i=1;i<=np;i++)
{
for(j=1;j<=nb;j++)
{
if(barray[j]!=1)
{
temp=b[j]-p[i];
if(temp>=0)
if(lowest>temp)
{
parray[i]=j;
lowest=temp;
}
}
}
  
frag[i]=lowest;
barray[parray[i]]=1;
lowest=10000;
}
  
cout<<"\nProcess_no\tProcess_size\tBlock_no\tBlock_size\tFragment";
for(i=1;i<=np && parray[i]!=0;i++)
cout<<"\n"<<i<<"\t\t"<<p[i]<<"\t\t"<<parray[i]<<"\t\t"<<b[parray[i]]<<"\t\t"<<frag[i];
  
return 0;
}


(B) First-Fit Algorithm Program
#include<iostream>
using namespace std;

int main()
{
int bsize[10], psize[10], bno, pno, flags[10], alloc[10], i, j;

for(i = 0; i < 10; i++)
{
flags[i] = 0;
alloc[i] = -1;
}
  
cout<<"Enter no. of memory partitions: ";
cin>>bno;
  
cout<<"\nEnter size of each mermory partition: ";
for(i = 0; i < bno; i++)
cin>>bsize[i];

cout<<"\nEnter no. of processes: ";
cin>>pno;
  
cout<<"\nEnter size of each process: ";
for(i = 0; i < pno; i++)
cin>>psize[i];
for(i = 0; i < pno; i++) //allocation as per first fit
for(j = 0; j < bno; j++)
if(flags[j] == 0 && bsize[j] >= psize[i])
{
alloc[j] = i;
flags[j] = 1;
break;
}
  
//display allocation details
cout<<"\nBlock no.\tsize\t\tprocess no.\t\tsize";
for(i = 0; i < bno; i++)
{
cout<<"\n"<< i+1<<"\t\t"<<bsize[i]<<"\t\t";
if(flags[i] == 1)
cout<<alloc[i]+1<<"\t\t\t"<<psize[alloc[i]];
else
cout<<"Not allocated";
}
  
return 0;
}

(C) Next-Fit Algorithm Program
#include <iostream>
using namespace std;

int main()
{
int memory_size[10][2], process_size[10][3];
int i, j, total_processes = 0, total_memory = 0;
cout<<"\nEnter the Total Number of Processes:\t";
cin>>total_processes;
cout<<"\nEnter the Size of Each Process\n";
for(int i = 0; i < total_processes; i++)
{
cout<<"Enter Size of Process "<<"\t"<<i + 1;
cin>>process_size[i][0];
process_size[i][1] = 0;
process_size[i][2] = i;
}
cout<<"\nEnter no. of memory partitions:\t";
cin>>total_memory;
cout<<"\nEnter the size of each partition:\n";
for(i = 0; i < total_processes; i++)
{
cout<<"Enter size of partition "<<"\t"<<i + 1;;
cin>>memory_size[i][0];
memory_size[i][1] = 0;
}
for(i = 0; i < total_processes; i++)
{
while(j < total_memory)
{
if(memory_size[j][1] == 0 && process_size[i][0] <= memory_size[j][0])
{
process_size[i][1] = 1;
memory_size[j][1] = 1;
cout<<"Process "<<"["<<i+1<<"]"<<"Allocated to Memory Block:\t"<<j+1;
break;
}
j++;
}
}
for(i = 0; i < total_memory; i++)
{
if(process_size[i][1] == 0)
{
cout<<"Process "<<"["<<i+1<<"]"<<"Unallocated";
}
}
cout<<"\n";
return 0;
}

(D) Worst-Fit Algorithm Program
#include<bits/stdc++.h>
using namespace std;
  
// Function to allocate memory to blocks as per worst fit
// algorithm
void worstFit(int blockSize[], int m, int processSize[],
int n)
{
// Stores block id of the block allocated to a
// process
int alloc[n];
  
// Initially no block is assigned to any process
memset(alloc -1, sizeof(alloc));
  
// pick each process and find suitable blocks
// according to its size ad assign to it
for (int i=0; i<n; i++)
{
// Find the best fit block for current process
int ws = -1;
for (int j=0; j<m; j++)
{
if (blockSize[j] >= processSize[i])
{
if (ws == -1)
ws = j;
else if (blockSize[ws] < blockSize[j])
ws = j;
}
}
  
// If we could find a block for current process
if (ws != -1)
{
// allocate block j to p[i] process
alloc[i] = ws;
  
// Reduce available memory in this block.
blockSize[ws] -= processSize[i];
}
}
  
cout << "\nProcess No.\tProcess Size\tBlock no.\n";
for (int i = 0; i < n; i++)
{
cout << " " << i+1 << "\t\t" << processSize[i] << "\t\t";
if (alloc[i] != -1)
cout << alloc[i] + 1;
else
cout << "Not Allocated";
cout << endl;
}
}
  
// Driver code
int main()
{
int m, n;
cout<<"Enter no. of memory partitions: ";
cin>>m;
cout<<"\nEnter the number of processes:";
cin>>n;
  
int blockSize[m];
int processSize[n];
cout<<"\nEnter size of each mermory partition: \n";
for(int i=1;i<=m;i++) {
cout<<"Partition no."<<i<<":";
cin>>blockSize[i];
}
  
cout<<"\nEnter the size of each process :-\n";
for(int i=1;i<=n;i++) {
cout<<"Process no. "<<i<<":";
cin>>processSize[i];
}
  
worstFit(blockSize, m, processSize, n);
  
return 0 ;
}

Please contact me if you need any clarification.

Thank you :)


Related Solutions

a) Briefly describe each of the following memory allocation algorithms: i. Best fit ii. Next fit...
a) Briefly describe each of the following memory allocation algorithms: i. Best fit ii. Next fit b) Given fixed memory partitions of 200 KB, 100 KB, 400 KB, 300 KB, and 400 KB (in order), how would each of the algorithms from (a) place processes in memory if they required 187 KB, 228 KB, 126 KB, 306 KB (in order)? c) A system has five page frames. The time of loading, time of last access, and the Referenced and Modified...
Write a C++ program (using pointers and dynamic memory allocation only) to implement the following functions...
Write a C++ program (using pointers and dynamic memory allocation only) to implement the following functions and call it from the main function. (1)Write a function whose signature looks like (char*, char) which returns true if the 1st parameter cstring contains the 2nd parameter char, or false otherwise. (2)Create an array of Planets. Populate the array and print the contents of the array using the pointer notation instead of the subscripts.
The objective of this assignment is to implement the tic-tac-toe game with a C program. The...
The objective of this assignment is to implement the tic-tac-toe game with a C program. The game is played by two players on a board defined as a 5x5 grid (array). Each board position can contain one of two possible markers, either ‘X’ or ‘O’. The first player plays with ‘X’ while the second player plays with ‘O’. Players place their markers in an empty position of the board in turns. The objective is to place 5 consecutive markers of...
Write a C++ program to compare those two searching algorithms and also compare two sorting algorithms....
Write a C++ program to compare those two searching algorithms and also compare two sorting algorithms. You need to modify those codes in the book/slides to have some counters to count the number of comparisons and number of swaps. In the main function, you should have an ordered array of 120integers in order to test searching algorithms, and the other two identical arrays of 120integers not in order to test sorting algorithms. Display all counters in the main functions.Counter for...
C++ Memory Allocation: 1) Write a C++ program that allocates static, stack, & heap memory. Your...
C++ Memory Allocation: 1) Write a C++ program that allocates static, stack, & heap memory. Your program does not need to do anything else.  Indicate via comments where memory for at least one variable in each memory area is allocated. a) Write code that allocates static memory (only include one declaration): b) Write code that allocates stack memory (only include one declaration): c) Write code that allocates heap memory (only include one declaration): 2) Edit the C++ program below to include...
Write and test a C program to implement Bubble Sort. . In your C program, you...
Write and test a C program to implement Bubble Sort. . In your C program, you should do: Implement the array use an integer pointer, get the size of the array from standard input and use the malloc function to allocate the required memory for it. Read the array elements from standard input. Print out the sorted array, and don’t forget to free the memory. Debug your program using Eclipse C/C++ CDT.
Write a program to implement and analyzing the Bubble Sort. a. Write a C++ function for...
Write a program to implement and analyzing the Bubble Sort. a. Write a C++ function for Bubble Sort b. Use a dynamic array of integers in a variable size of n. c. Display the following information: 1) Total counts of comparisons 2) Total counts of shifts / moves / swaps, whichever applies d. Write a main() function to test a best, and an average cases in terms of time efficiency i. Fill out the array with random numbers for an...
Write a C++ program that implements both the recursive binary and mergesort algorithms as described in...
Write a C++ program that implements both the recursive binary and mergesort algorithms as described in zyBooks sections 9.4 and 9.5. Prompt the user for the location of a sequence of numbers, via an external file or data entry by the user. If you choose data entry, prompt the user for the number of values and read them into a data structure of your choice. Then use the mergesort algorithm to sort them in ascending order. Finally, prompt for a...
write C program to implement the priority queue with the operation insert
write C program to implement the priority queue with the operation insert
For this computer assignment, you are to write a C++ program to implement a class for...
For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of data types, implement this class as a template. Most of the public member functions of the BinaryTree class call private member functions of the class (with the same name). These private member functions can be implemented as either recursive or non-recursive, but clearly, recursive versions of these functions are preferable because of their short and simple implementations...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT