Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define m 30
- typedef struct memBlock{
- int max,alloc[m],avail,remain;
- /* max -> max block size
- * alloc[] -> array to store allocated memory fragments
- * avail -> number of fragments allocated in the block
- * remain -> remaining memory in the block
- */
- }MemoryBlocks;
- void takeInput(MemoryBlocks B[], int n){
- printf("Enter the maximum allocation of each block:\n");
- for (int i = 0; i < n; ++i) {
- printf("Block %d:\n",i+1);
- scanf("%d",&B[i].max);
- for (int j = 0; j < m; ++j) {
- B[i].alloc[j] = 0;
- }
- B[i].avail = 0;
- B[i].remain = B[i].max;
- }
- }
- void printBlocks(MemoryBlocks B[], int n){
- printf("\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
- for (int i = 0; i < n; ++i) {
- printf("| Block %-10d ",B[i].max);
- for (int j = 0; j < B[i].avail; ++j) {
- printf("| %-10d ",B[i].alloc[j]);
- }
- if(i<n-1)
- printf("|\n--------------------------------------------------------------------------\n");
- }
- printf("|\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
- }
- void FirstFit(MemoryBlocks B[], int n){
- //Insert input into the first block that has enough remaining memory (B[].remain >= input)
- int input;
- printf("Enter the resource to be inserted:\n");
- scanf("%d",&input);
- for (int i = 0; i < n; ++i) {
- if(B[i].remain >= input){
- B[i].alloc[B[i].avail++] = input;
- B[i].remain -= input;
- printf("\nAllocation Successful!\n");
- return;
- }
- }
- printf("\nNot enough memory available!\n");
- }
- void BestFit(MemoryBlocks B[], int n){
- int input;
- printf("Enter the resource to be inserted:\n");
- scanf("%d",&input);
- int select = -1; // initialize select to -1
- for (int i = 0; i < n; ++i) {
- if (B[i].remain >= input){
- select = i;
- break; //Find the first block that has remaining memory greater than input
- }
- }
- if(select >= 0) {
- for (int i = 1; i < n; ++i) {
- if (input <= B[i].remain && B[select].remain > B[i].remain)
- select = i; // Finding the best memory block for allocation
- }
- if (B[select].remain >= input) {
- B[select].alloc[B[select].avail++] = input;
- B[select].remain -= input;
- printf("\nAllocation Successful!\n");
- return;
- }
- }
- printf("\nNot enough memory available!\n");
- }
- void WorstFit(MemoryBlocks B[], int n){
- int input;
- printf("Enter the resource to be inserted:\n");
- scanf("%d",&input);
- int select = 0; //Initialize select to 0
- for (int i = 1; i < n; ++i) {
- if(input <= B[i].remain && B[select].remain < B[i].remain)
- select = i; //Find the memory block with the highest remaining space
- }
- if(B[select].remain >= input){
- B[select].alloc[B[select].avail++] = input;
- B[select].remain -= input;
- printf("\nAllocation Successful!\n");
- return;
- }
- printf("\nNot enough memory available!\n");
- }
- int main(){
- int n;
- printf("Enter the number of memory Blocks:\n");
- scanf("%d",&n);
- MemoryBlocks B[n];
- takeInput(B, n);
- while(1){
- int ch;
- printf("Enter your choice\n1. First Fit\n2. Best Fit\n3. Worst Fit\n4. Print Blocks\n0. Exit\n");
- scanf("%d",&ch);
- switch(ch) {
- case 1:
- FirstFit(B, n);
- break;
- case 2:
- BestFit(B, n);
- break;
- case 3:
- WorstFit(B, n);
- break;
- case 4:
- printBlocks(B,n);
- break;
- case 0:
- exit(0);
- default:
- printf("Invalid Argument!\n");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement