Objective: To simulate memory allocation with hole-fitting algorithms (First-fit, Best-fit) and implement deallocation a
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am
Objective: To simulate memory allocation with hole-fitting algorithms (First-fit, Best-fit) and implement deallocation a
Objective: To simulate memory allocation with hole-fitting algorithms (First-fit, Best-fit) and implement deallocation and defragmentation of memory blocks. Specification: The program simulates memory allocation with a chosen hole-fitting algorithm (First-fit, Best-fit) and implements deallocation and defragmentation. A menu controls the operations, and each choice calls the appropriate procedure, where the choices are: 1) Enter parameters 2) Allocate memory for a block 3) Deallocate memory for a block 4) Defragment memory 5) Quit program and free memory Assignment: • The size of physical memory is represented by an integer pm_size. • The allocated blocks are contained within a linked list, where each allocated block is a structure containing: (1) the id, (2) the starting address of the block, (3) the ending address of the block, and (4) a link to the next allocated block. • Each allocation request prompts for: (1) the id and (2) the size of the new block. If the id is a duplicate and/or the remaining physical memory is not enough to fit the request, the request is rejected, and an appropriate message is displayed. • Each deallocation request prompts for the id. If the id is invalid, the request is rejected. • Defragmentation compacts the blocks to be contiguous, and coalesces the holes into one hole at the far--right end (highest memory addresses) of physical memory.
What NOT to do (any violation will result in an automatic score of 0 on the assignment): • Do NOT modify the choice values (1,2,3,4,5) or input . characters and then try to convert them to integers--the test script used for grading your assignment will not work correctly. • Do NOT turn in an alternate version of the assignment downloaded from the Internet (coursehero, answers, reddit, github, etc.) or submitted from you or another student from a previous semester. • Do NOT turn in your assignment coded in another programming language (C++, C#, Java). What to turn in: • The source code as a C file uploaded to Canvas by the deadline of 11:59pm PST (-20% per consecutive day for late submissions, up to the 4th day-note 1 minute late counts as a day late, 1 day and I minute late counts as 2 days late, etc.) • Make sure your code compiles with the online C compiler . before submitting: https://www.onlinegdb.com/online compiler Sample output - Best Fit Memory allocation. 1) Enter parameters 2) Allocate memory for block 3) Deallocate memory for block 4) Defragment memory 5) Quit program Enter selection: 1 Enter size of physical memory: 1024 Enter hole-fitting algorithm (0=first fit, 1-best_fit): 1 Memory allocation ‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒ 1) Enter parameters 2) Allocate memory for block 3) Deallocate memory for block 4) Defragment memory 5) Quit program Enter selection: 2 Enter block id: 0 Enter block size: 128 ID Start End
#include <stdio.h> #include <stdlib.h> // declare structure to store block information (id, starting address, ending address, link to next block) // declare linked list to connect allocation block // declare global variables void "OPTION #1" () { // declare local variables (if any) // prompt for size of physical memory and choice of hole-fitting algorithm (0=first-fit, 1-best-fit), initialize remaining memory // initilize linked list with "dummy" block of size 0 return; } void "PROCEDURE TO PRINT ALLOCATION TABLE" () { // declare local variables // print table containing block id, starting address, ending address return; } void "OPTION #2" () { // declare local variables // initialize best hole so far to size of physical memory // prompt for block id & block size // check if size of block is larger than remaining unallocated space, if so, print message and return // allocate space for new block and set id // if only "dummy" block exists, insert block at end of linked list, set fields, return // else traverse list until either appropriate hole is found or the end of the list is reached // if id already exists, reject request and return
// set values for start and end of currently found hole // if hole is large enough for block // if first-fit algorithm // set start & end fields of new block & add block into linked list // reduce remaining available memory and return smaller than best so far // set values of best start & best end & best hole size so far // update best block & advance next block // set start & end fields of new block & add block into linked list // reduce remaining available memory and return } ***/ void "OPTION #3" () { return; } block id is found // declare local variables // else--best-fit algorithm // if hole is // prompt for block id // until end of linked list is reached or block id not found } // traverse list // if end of linked list reached, print void "OPTION #4" () { // else remove block and deallocate memory return; // declare local variables // until end of list is reached // calculate current hole size // adjust start & end fields of current block to eliminate hole return;
***/ int main() { /* declare local vars */ /* while user has not chosen to quit * ·/ /* print menu of options */ /* prompt for menu selection */ /* call appropriate procedure based on choice--use switch statement or series of if, else if, else statements */ } /* while loop */ return 1; /* indicates success */ } /* end of procedure */