Question
Need assistance with C language program, please help asap, will upvote !!!!!! Please modify this base code so that it properly outputs the correct block
Need assistance with C language program, please help asap, will upvote !!!!!!
Please modify this base code so that it properly outputs the correct block no.'s for the following memory allocations....
- first fit - needed output for block no: 1,5,1,1,3, respectively in that order
- best fit- needed output for block no: 4,5,2,3,1, respectively in that order
- worst fit- needed output for block no: 1,5,3,3, Not allocated, respectively in that order
- next fit - needed output for block no: 1,5,1,1,3, respectively in that order
***** Logic for the fits******
First Fit Implementation: a. Input memory blocks with size and processes with size. b. Initialize all memory blocks as free. c. Start by picking each process and check if it can be assigned to current block. d. If size-of-process <= size-of-block if yes then assign and check for next process. e. If not then keep checking the further blocks.
2. Best Fit Implementation: a. Input memory blocks with size and processes with size. b. Initialize all memory blocks as free. c. Start by picking each process and find the minimum block size that can be assigned to current process i.e., find min(blockSize[1], blockSize[2],.....blockSize[n]) > processSize[current], if found then assign it to the current process. d. If not, then leave that process and keep checking the further processes.
3. Worst Fit Implementation: a. Input memory blocks with size and processes with size. b. Initialize all memory blocks as free. c. Start by picking each process and find the maximum block size that can be assigned to current process i.e., find max(blockSize[1], blockSize[2],.....blockSize[n]) > processSize[current], if found then assign it to the current process. d. If not, then leave that process and keep checking the further processes.
4. Next Fit Implementation: a. Input memory blocks with size and processes with size. b. Initialize all memory blocks as free. c. Start by picking each process and check if it can be assigned to the current block, if yes, allocate it the required memory and check for next process but from the block where we left not from starting. d. If the current block size is smaller, keep checking the further blocks
Base Code to modify :
#include
#include
int displayMenu();
void clearScreen();
void memoryManagement();
void firstFit(int[], int[], int, int);
void bestFit(int[], int[], int, int);
void worstFit(int[], int[], int, int);
void nextFit(int[], int[], int, int);
int main() {
int choice = -1;
while (choice != 0) {
choice = displayMenu();
switch (choice) {
case 1:
memoryManagement();
break;
case 2:
// fileManagement TBD
break;
case 3:
// multiThreads TBD
break;
case 0:
exit(0);
break;
default:
break;
}
}
return 0;
}
int displayMenu() {
int choice = -1;
while (choice == -1) {
printf("Select the OS program to run, ");
printf("1. Memory Management ");
printf("2. File Management ");
printf("3. Multi-Threading ");
printf("0. Exit ");
printf("Enter the number of your selection: ");
scanf("%d", &choice);
if (choice < 0 || choice > 3) {
choice = -1;
clearScreen();
}
}
return choice;
}
void clearScreen() {
printf("Press enter to continue... ");
char c;
scanf("%c", &c); // Consume newline character from previous input
scanf("%c", &c);
system("CLS");
}
void memoryManagement() {
int n, m, i;
printf("Enter the number of memory blocks: ");
scanf("%d", &n);
int blockSize[n];
printf("Enter the size of each memory block: ");
for (i = 0; i < n; i++) {
scanf("%d", &blockSize[i]);
}
printf("Enter the number of processes: ");
scanf("%d", &m);
int processSize[m];
printf("Enter the size of each process: ");
for (i = 0; i < m; i++) {
scanf("%d", &processSize[i]);
}
printf(" First Fit Implementation: ");
firstFit(blockSize, processSize, n, m);
printf(" Best Fit Implementation: ");
bestFit(blockSize, processSize, n, m);
printf(" Worst Fit Implementation: ");
worstFit(blockSize, processSize, n, m);
printf(" Next Fit Implementation: ");
nextFit(blockSize, processSize, n, m);
}
void firstFit(int blockSize[], int processSize[], int n, int m) {
int allocation[m], i, j;
for (i = 0; i < m; i++) {
allocation[i] = -1;
for (j = 0; j < n; j++) {
if (blockSize[j] >= processSize[i]) {
allocation[i] = j;
blockSize[j] -= processSize[i];
break;
}
}
}
printf("Process No.\tProcess Size\tBlock No. ");
for (i = 0; i < m; i++) {
printf("%d\t\t%d\t\t", i + 1, processSize[i]);
if (allocation[i] != -1) {
printf("%d ", allocation[i] + 1);
} else {
printf("Not allocated ");
}
}
}
void bestFit(int blockSize[], int processSize[], int n, int m) {
int allocation[m], i, j, min, idx;
for (i = 0; i < m; i++) {
allocation[i] = -1;
min = 1000000;
idx = -1;
for (j = 0; j < n; j++) {
if (blockSize[j] >= processSize[i] && blockSize[j] < min) {
min = blockSize[j];
idx = j;
}
}
if (idx != -1) {
allocation[i] = idx;
blockSize[idx] -= processSize[i];
}
}
printf("Process No.\tProcess Size\tBlock No. ");
for (i = 0; i < m; i++) {
printf("%d\t\t%d\t\t", i + 1, processSize[i]);
if (allocation[i] != -1) {
printf("%d ", allocation[i] + 1);
} else {
printf("Not allocated ");
}
}
}
void worstFit(int blockSize[], int processSize[], int n, int m) {
int allocation[m], i, j, max, idx;
for (i = 0; i < m; i++) {
allocation[i] = -1;
max = -1;
idx = -1;
for (j = 0; j < n; j++) {
if (blockSize[j] >= processSize[i] && blockSize[j] > max) {
max = blockSize[j];
idx = j;
}
}
if (idx != -1) {
allocation[i] = idx;
blockSize[idx] -= processSize[i];
}
}
printf("Process No.\tProcess Size\tBlock No. ");
for (i = 0; i < m; i++) {
printf("%d\t\t%d\t\t", i + 1, processSize[i]);
if (allocation[i] != -1) {
printf("%d ", allocation[i] + 1);
} else {
printf("Not allocated ");
}
}
}
void nextFit(int blockSize[], int processSize[], int n, int m) {
int allocation[m], i, j, startIdx = 0;
for (i = 0; i < m; i++) {
allocation[i] = -1;
for (j = startIdx; j < n; j++) {
if (blockSize[j] >= processSize[i]) {
allocation[i] = j;
blockSize[j] -= processSize[i];
startIdx = j;
break;
}
}
if (allocation[i] == -1) {
for (j = 0; j < startIdx; j++) {
if (blockSize[j] >= processSize[i]) {
allocation[i] = j;
blockSize[j] -= processSize[i];
startIdx = j;
break;
}
}
}
}
printf("Process No.\tProcess Size\tBlock No. ");
for (i = 0; i < m; i++) {
printf("%d\t\t%d\t\t", i + 1, processSize[i]);
if (allocation[i] != -1) {
printf("%d ", allocation[i] + 1);
} else {
printf("Not allocated ");
}
}
}
*******Please provide the full, updated, and modified code ********
Thank you greatly :)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started