Advertisement
CHU2

Memory Management

Jan 19th, 2025 (edited)
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.24 KB | Source Code | 0 0
  1. #include <iostream>
  2. #include <iomanip>  // header to control printing output
  3.  
  4. using namespace std;
  5.  
  6. struct block {  // memory block
  7.     int size;
  8.     bool is_allocated;
  9. };
  10.  
  11. struct process {    // processess block
  12.     int size;
  13.     int allocated_in;
  14. };
  15.  
  16. void first_fit(block[], int, process[], int);
  17. void best_fit(block[], int, process[], int);
  18. void worst_fit(block[], int, process[], int);
  19. void print(block[], process[], int);
  20.  
  21. int main() {
  22.     const int max_blocks = 10;  // max blocks  and processes
  23.     const int max_processes = 10;
  24.  
  25.     block blocks[max_blocks] = {{100, false}, {500, false}, {200, false}, {300, false}, {600, false}};  // values to be processed
  26.     process processes[max_processes] = {{212, -1}, {417, -1}, {112, -1}, {426, -1}};
  27.     int block_count = 5, process_count = 4; // size of chuchu
  28.    
  29.     cout << "First Fit Allocation:\n";
  30.     first_fit(blocks, block_count, processes, process_count);
  31.     print(blocks, processes, process_count);
  32.    
  33.     for (int i = 0; i < block_count; i++) { // restarts the memory block usage allocation before continuing to another allocation
  34.         blocks[i].is_allocated = false;
  35.     }
  36.    
  37.     cout << "\nBest Fit Allocation:\n";
  38.     best_fit(blocks, block_count, processes, process_count);
  39.     print(blocks, processes, process_count);
  40.    
  41.     for (int i = 0; i < block_count; i++) {
  42.         blocks[i].is_allocated = false;
  43.     }
  44.    
  45.     cout << "\nWorst Fit Allocation:\n";
  46.     worst_fit(blocks, block_count, processes, process_count);
  47.     print(blocks, processes, process_count);
  48.    
  49.     return 0;
  50. }
  51.  
  52. void first_fit(block blocks[], int block_count, process processes[], int process_count) {
  53.     for (int i = 0; i < process_count; i++) {
  54.         processes[i].allocated_in = -1;
  55.         for (int j = 0; j < block_count; j++) {
  56.             if (!blocks[j].is_allocated && blocks[j].size >= processes[i].size) {   // is like linear search
  57.                 processes[i].allocated_in = j;
  58.                 blocks[j].is_allocated = true;
  59.                 break;
  60.             }
  61.         }
  62.     }
  63. }
  64.  
  65. void best_fit(block blocks[], int block_count, process processes[], int process_count) {
  66.     for (int i = 0; i < process_count; i++) {
  67.         int best_index = -1;
  68.         for (int j = 0; j < block_count; j++) {
  69.             if (!blocks[j].is_allocated && blocks[j].size >= processes[i].size) {
  70.                 if (best_index == -1 || blocks[j].size < blocks[best_index].size) { // finds the best index first before allocating value
  71.                     best_index = j;
  72.                 }
  73.             }
  74.         }
  75.         if (best_index != -1) {
  76.             processes[i].allocated_in = best_index;
  77.             blocks[best_index].is_allocated = true;
  78.         } else {
  79.             processes[i].allocated_in = -1;
  80.         }
  81.     }
  82. }
  83.  
  84. void worst_fit(block blocks[], int block_count, process processes[], int process_count) {
  85.     for (int i = 0; i < process_count; i++) {
  86.         int worst_index = -1;
  87.         for (int j = 0; j < block_count; j++) {
  88.             if (!blocks[j].is_allocated && blocks[j].size >= processes[i].size) {
  89.                 if (worst_index == -1 || blocks[j].size > blocks[worst_index].size) {   // same as best_fit but this use > condition in finding the n_fit to ensure it is the highest value available
  90.                     worst_index = j;
  91.                 }
  92.             }
  93.         }
  94.         if (worst_index != -1) {
  95.             processes[i].allocated_in = worst_index;
  96.             blocks[worst_index].is_allocated = true;
  97.         } else {
  98.             processes[i].allocated_in = -1;
  99.         }
  100.     }
  101. }
  102.  
  103. void print(block blocks[], process processes[], int process_count) {
  104.     cout << "Process No." << setw(15) << "Process Size" << setw(15) << "Block No." << setw(15) << "Block Size\n";   // setw() is for setting width size
  105.     for (int i = 0; i < process_count; i++) {                                                                       // I use this instead of \t
  106.         cout << i + 1 << setw(16) << processes[i].size << setw(24);
  107.         if (processes[i].allocated_in != -1) {
  108.             cout << processes[i].allocated_in + 1;
  109.         }
  110.         else {
  111.             cout << "Not Allocated";
  112.         }
  113.         cout << setw(14) << blocks[i].size << "\n";
  114.     }
  115. }
  116.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement