Advertisement
Bewin

Memory Allocation

Jul 12th, 2024 (edited)
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.59 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define m 30
  5.  
  6. typedef struct memBlock{
  7.     int max,alloc[m],avail,remain;
  8.     /* max -> max block size
  9.      * alloc[] -> array to store allocated memory fragments
  10.      * avail -> number of fragments allocated in the block
  11.      * remain -> remaining memory in the block
  12.      */
  13. }MemoryBlocks;
  14.  
  15. void takeInput(MemoryBlocks B[], int n){
  16.   printf("Enter the maximum allocation of each block:\n");
  17.   for (int i = 0; i < n; ++i) {
  18.     printf("Block %d:\n",i+1);
  19.     scanf("%d",&B[i].max);
  20.     for (int j = 0; j < m; ++j) {
  21.       B[i].alloc[j] = 0;
  22.     }
  23.     B[i].avail = 0;
  24.     B[i].remain = B[i].max;
  25.   }
  26. }
  27.  
  28. void printBlocks(MemoryBlocks B[], int n){
  29.   printf("\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
  30.   for (int i = 0; i < n; ++i) {
  31.     printf("| Block %-10d  ",B[i].max);
  32.     for (int j = 0; j < B[i].avail; ++j) {
  33.       printf("| %-10d  ",B[i].alloc[j]);
  34.     }
  35.     if(i<n-1)
  36.       printf("|\n--------------------------------------------------------------------------\n");
  37.   }
  38.   printf("|\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
  39. }
  40.  
  41. void FirstFit(MemoryBlocks B[], int n){
  42.   //Insert input into the first block that has enough remaining memory (B[].remain >= input)
  43.   int input;
  44.   printf("Enter the resource to be inserted:\n");
  45.   scanf("%d",&input);
  46.   for (int i = 0; i < n; ++i) {
  47.     if(B[i].remain >= input){
  48.       B[i].alloc[B[i].avail++] = input;
  49.       B[i].remain -= input;
  50.       printf("\nAllocation Successful!\n");
  51.       return;
  52.     }
  53.   }
  54.   printf("\nNot enough memory available!\n");
  55. }
  56.  
  57. void BestFit(MemoryBlocks B[], int n){
  58.   int input;
  59.   printf("Enter the resource to be inserted:\n");
  60.   scanf("%d",&input);
  61.   int select = -1; // initialize select to -1
  62.   for (int i = 0; i < n; ++i) {
  63.     if (B[i].remain >= input){
  64.       select = i;
  65.       break; //Find the first block that has remaining memory greater than input
  66.     }
  67.   }
  68.   if(select >= 0) {
  69.     for (int i = 1; i < n; ++i) {
  70.       if (input <= B[i].remain && B[select].remain > B[i].remain)
  71.         select = i; // Finding the best memory block for allocation
  72.     }
  73.     if (B[select].remain >= input) {
  74.       B[select].alloc[B[select].avail++] = input;
  75.       B[select].remain -= input;
  76.       printf("\nAllocation Successful!\n");
  77.       return;
  78.     }
  79.   }
  80.   printf("\nNot enough memory available!\n");
  81. }
  82.  
  83. void WorstFit(MemoryBlocks B[], int n){
  84.   int input;
  85.   printf("Enter the resource to be inserted:\n");
  86.   scanf("%d",&input);
  87.   int select = 0; //Initialize select to 0
  88.   for (int i = 1; i < n; ++i) {
  89.     if(input <= B[i].remain && B[select].remain < B[i].remain)
  90.       select = i; //Find the memory block with the highest remaining space
  91.   }
  92.   if(B[select].remain >= input){
  93.     B[select].alloc[B[select].avail++] = input;
  94.     B[select].remain -= input;
  95.     printf("\nAllocation Successful!\n");
  96.     return;
  97.   }
  98.   printf("\nNot enough memory available!\n");
  99. }
  100.  
  101. int main(){
  102.   int n;
  103.   printf("Enter the number of memory Blocks:\n");
  104.   scanf("%d",&n);
  105.   MemoryBlocks B[n];
  106.   takeInput(B, n);
  107.   while(1){
  108.     int ch;
  109.     printf("Enter your choice\n1. First Fit\n2. Best Fit\n3. Worst Fit\n4. Print Blocks\n0. Exit\n");
  110.     scanf("%d",&ch);
  111.     switch(ch) {
  112.       case 1:
  113.         FirstFit(B, n);
  114.         break;
  115.       case 2:
  116.         BestFit(B, n);
  117.         break;
  118.       case 3:
  119.         WorstFit(B, n);
  120.         break;
  121.       case 4:
  122.         printBlocks(B,n);
  123.         break;
  124.       case 0:
  125.         exit(0);
  126.       default:
  127.         printf("Invalid Argument!\n");
  128.     }
  129.   }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement