Question

In: Computer Science

Write a program using c, c++, or java that have four dynamic memory partitions of size...

Write a program using c, c++, or java that have four dynamic memory partitions of size 100 KB, 500 KB, 200 KB, and 450 KB. The program should accept from user the number of processes and their sizes. Then output the assignment of processes using the next fit algorithm (specifying which process, if any, is block).

Solutions

Expert Solution

source code:

import java.util.Arrays;
public class Main
{
   // Function to allocate memory to blocks as per Next fit
// algorithm
static void NextFit(int blockSize[], int m, int processSize[], int n) {
// Stores block id of the block allocated to a
// process
int allocation[] = new int[n], j = 0;
  
// Initially no block is assigned to any process
Arrays.fill(allocation, -1);
  
// pick each process and find suitable blocks
// according to its size ad assign to it
for (int i = 0; i < n; i++) {
  
// Do not start from beginning
while (j < m) {
  
if (blockSize[j] >= processSize[i]) {
  
// allocate block j to p[i] process
allocation[i] = j;
  
// Reduce available memory in this block.
blockSize[j] -= processSize[i];
  
break;
}
  
// mod m will help in traversing the blocks from
// starting block after we reach the end.
j = (j + 1) % m;
}
}
  
System.out.print("\nProcess No.\tProcess Size\tBlock no.\n");
for (int i = 0; i < n; i++) {
System.out.print( i + 1 + "\t\t" + processSize[i]
+ "\t\t");
if (allocation[i] != -1) {
System.out.print(allocation[i] + 1);
} else {
System.out.print("Not Allocated");
}
System.out.println("");
}
}
  
// Driver program
static public void main(String[] args) {
int blockSize[] = {100, 500, 200,300};
int processSize[] = {100, 250, 300};
int m = blockSize.length;
int n = processSize.length;
NextFit(blockSize, m, processSize, n);
}
}


Related Solutions

Write a multithreaded program in C using the pthread library and dynamic memory(malloc) that multiplies two...
Write a multithreaded program in C using the pthread library and dynamic memory(malloc) that multiplies two matrices together. The numbers in the matrices must be read in from a text file. The program should also check if the two matrices are capable of being multiplied together. The amount of threads used has to be dynamic. The user should be able to choose how many threads they wish to use using the command line. Finally, the result must be stored in...
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.
using Java, Implement the following algorithm: - Accept 5 different memory partitions and 4 different processes...
using Java, Implement the following algorithm: - Accept 5 different memory partitions and 4 different processes from user and show how best fit algorithm allocates them.
Write a C++ program using dynamic arrays that allows the user to enter the last names...
Write a C++ program using dynamic arrays that allows the user to enter the last names of the candidates in a local election and the number of votes received by each candidate. The program must ask the user for the number of candidates and then create the appropriate arrays to hold the data. The program should then output each candidate’s name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should...
Using C language: Write a program that asks the user for the size of an array,...
Using C language: Write a program that asks the user for the size of an array, then reads a number of integer values (from user input) into the array. Write a function to print out the array and call it to print the array out after all values are read in. Write a function to implement Insertion Sort and run it on the data array to sort the array. Write another function to implement Selection Sort and run it on...
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...
Hi. I'm trying to write a program that uses dynamic memory management to create two arrays....
Hi. I'm trying to write a program that uses dynamic memory management to create two arrays. splice() must create the third array and return a pointer to main(). Main() must capture the pointer returned from the splice() function. Sol'n so far, I get the results of the first and second array, but it isn't showing the results of the spliced function: #include <iostream> using namespace std; // Function int* splice() inserts the 2nd array into 1st array starting at int...
write a c++ program to read two matrices with any size. Your program should have at...
write a c++ program to read two matrices with any size. Your program should have at least the following functions Main() Read a Matrix Add two matrices Subtract two matrices multiply two matrices display a matrice
Write a code in c++ using dynamic array of structure and dynamic array list. Make a...
Write a code in c++ using dynamic array of structure and dynamic array list. Make a dummy list for a company which stores following information about its customers. Customer ID Customer Name Gender Total items purchased Item category 20% discount in percentage of total purchase amount. Use dynamic array to save at least 20 items by dividing them into 3 different categories. Make a dummy list of items that company sells by dividing them into two categorizes. Items has following...
Write a Java program to 1. read in the size of a square boolean matrix A...
Write a Java program to 1. read in the size of a square boolean matrix A 2. read in the 0-1 matrix elements 3. read in a positive integer n 4. display A^n
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT