Memory Allocation scheduling algorithm in c++ with gantt chart .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
#include<bits/stdc++.h> using namespace std; const int N = 100005; int blocks; int processes; bool allocated[N]; int block_size[N]; int allocation[N]; int process_size[N]; void clear() { for(int i=0;i<processes;i++) allocation[i]=-1; for(int i=0;i<blocks;i++) allocated[i]=false; } void print_status() { for(int i=0;i<processes;i++) { if(allocation[i]>=0) cout<<"Process "<<i<<" is allocated to block "<<allocation[i]<<"\n"; else cout<<"Process "<<i<<" is not allocated to any block\n"; } cout<<"\n"; } void first_fit() { clear(); for(int i=0;i<processes;i++) { for(int j=0;j<blocks;j++) { if(process_size[i]<=block_size[j] && !allocated[j]) { allocation[i]=j; allocated[j]=true; break; } } } cout<<"First fit memory allocation:\n"; print_status(); } void best_fit() { clear(); int best,index; for(int i=0;i<processes;i++) { best=INT_MAX; index=-1; for(int j=0;j<blocks;j++) { if(block_size[j]>=process_size[i] && best>block_size[j] && !allocated[j]) { index=j; best=block_size[j]; } } if(index!=-1) { allocation[i]=index; allocated[index]=true; } } cout<<"Best fit memory allocation: \n"; print_status(); } void worst_fit() { clear(); int worst,index; for(int i=0;i<processes;i++) { worst=INT_MIN; index=-1; for(int j=0;j<blocks;j++) { if(block_size[j]>=process_size[i] && worst<block_size[j] && !allocated[j]) { index=j; worst=block_size[j]; } } if(index!=-1) { allocation[i]=index; allocated[index]=true; } } cout<<"Worst fit memory allocation: \n"; print_status(); } int main() { cout<<"Number of Blocks: "; cin>>blocks; cout<<"Block Sizes:\n"; for(int i=0; i<blocks; i++) cin>>block_size[i]; cout<<"Number of Processes: "; cin>>processes; cout<<"Process Sizes:\n"; for(int i=0; i<processes; i++) cin>>process_size[i]; first_fit(); best_fit(); worst_fit(); } /** 6 5 7 4 10 6 3 5 8 5 3 4 2 */ |