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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
| #include <stdio.h> #include <stdlib.h> #include <math.h> #include <stdbool.h> #include <stdarg.h>
int processNum = 5; int resourceNum = 3; int requireNum = 0;
int Max[20][20]; int Allocation[20][20]; int Need[20][20]; int Available[20]; int request[20]; int WorkStatus[20]; bool Finished[20] = {false}; int Safety[20]; int AllocationStatus[20]; int NeedStatus[20]; int AvailableStatus[20];
void color_printf(const char* color_code, const char* format, ...) { va_list args; printf("%s", color_code); va_start(args, format); vprintf(format, args); va_end(args); printf("\033[0m"); } void red_printf(const char* format, ...) { va_list args; printf("\033[31m"); va_start(args, format); vprintf(format, args); va_end(args); printf("\033[0m"); } void blue_printf(const char* format, ...) { va_list args; printf("\033[1;34m"); va_start(args, format); vprintf(format, args); va_end(args); printf("\033[0m"); } void green_printf(const char* format, ...) { va_list args; printf("\033[32m"); va_start(args, format); vprintf(format, args); va_end(args); printf("\033[0m"); } void darkgreen_printf(const char* format, ...) { va_list args; printf("\033[36m"); va_start(args, format); vprintf(format, args); va_end(args); printf("\033[0m"); } void yellow_printf(const char* format, ...) { va_list args; printf("\033[33m"); va_start(args, format); vprintf(format, args); va_end(args); printf("\033[0m"); } void purple_printf(const char* format, ...) { va_list args; printf("\033[35m"); va_start(args, format); vprintf(format, args); va_end(args); printf("\033[0m"); }
void getMax() { yellow_printf("Welcome!\n"); printf("Please enter the number of processes and the number of resource classes:\n"); while (1) { scanf("%d %d", &processNum, &resourceNum); if (processNum < 5) { red_printf("\nIf the number of processes is at least 5, enter them again.\n"); continue; } if (resourceNum < 3) { red_printf("\nThe resource category is at least 3, please re-enter it.\n"); continue; } break; } printf("\n-----------------------------------\n"); yellow_printf("Please enter the Max matrix:\n"); for (int i = 0; i < processNum; i++) { printf("%2d process:\n", i + 1); for (int j = 0; j < resourceNum; j++) { scanf("%d", &Max[i][j]); } } }
void getAllocation() { printf("-----------------------------------\n"); yellow_printf("Please enter the Allocation matrix:\n"); for (int i = 0; i < processNum; i++) { printf("%d process assigned:\n", i + 1); for (int j = 0; j < resourceNum; j++) { scanf("%d", &Allocation[i][j]); } } }
void getNeed() { for (int i = 0; i < processNum; i++) { for (int j = 0; j < resourceNum; j++) { Need[i][j] = Max[i][j] - Allocation[i][j]; } } green_printf("\nThe Need matrix is calculated\n"); }
void getAvailable() { printf("-----------------------------------\n"); yellow_printf("Enter the Available matrix:\n"); for (int i = 0; i < resourceNum; i++) { scanf("%d", &Available[i]); } green_printf("\nGot it!\n"); }
void getRequirement() { printf("-----------------------------------\n"); yellow_printf("Enter the request process number:"); scanf("%d", &requireNum); while(requireNum>processNum) { red_printf("\nOps!This process does not exist!\nPlease re-enter it:"); scanf("%d",&requireNum); } Safety[0] = requireNum; for (int i = 0; i < resourceNum; i++) { printf("The %d class resource requires:\n", i + 1); scanf("%d", &request[i]); if (request[i] > Need[requireNum - 1][i]) { red_printf("\nError\nThe number of resources requested is more than the current need!\nDon't be too greedy!\n"); getRequirement(); } if (request[i] > Available[i]) { red_printf("\nProcess %d is blocked\n", requireNum); getRequirement(); } else { AvailableStatus[i]=Available[i]; AllocationStatus[i]=Allocation[requireNum-1][i]; NeedStatus[i]=Need[requireNum-1][i]; Available[i] -= request[i]; Allocation[requireNum - 1][i] += request[i]; Need[requireNum - 1][i] -= request[i]; Finished[requireNum - 1] = true; } } }
bool is_safe() { int i, j, i1 = 0; bool t = false;
for (i = 0; i < resourceNum; i++) { WorkStatus[i] = Available[i]; }
for (i = 0; i < processNum; i++) { Finished[i] = false; }
while (1) { for (j = 0; j < processNum; j++) { if (Finished[j]) continue; t = true; for (i = 0; i < resourceNum; i++) { if (Need[j][i] <= WorkStatus[i]) continue; else { t = false; break; } } if (t) break; }
if (t && (Finished[j] == false)) { for (i = 0; i < resourceNum; i++) { WorkStatus[i] = WorkStatus[i] + Allocation[j][i]; } Finished[j] = true; Safety[i1] = j+1; i1++; } else { for (i = 0; i < processNum; i++) { if (Finished[i]) continue; else return false; } return true; } } return true; }
void showResource() { printf("-------------------------------------------\n"); red_printf("Current resource situation\n"); yellow_printf("process Max Allocation Need\n "); for(int i=0;i<3;i++) { for(int j=0;j<resourceNum;j++) { yellow_printf("r%d ",j+1); } printf(" "); } printf("\n"); for(int i=0;i<processNum;i++) { yellow_printf("P%d ",i+1); for(int j=0;j<resourceNum;j++) { printf("%2d ",Max[i][j]); } printf(" "); for(int j=0;j<resourceNum;j++) { printf("%2d ",Allocation[i][j]); } printf(" "); for(int j=0;j<resourceNum;j++) { printf("%2d ",Need[i][j]); } printf("\n"); } }
void showAvailable() { yellow_printf("\nAvailable\n"); for(int i=0;i<resourceNum;i++) { yellow_printf("r%d ",i+1); } printf("\n"); for(int i=0;i<resourceNum;i++) { printf("%2d ",Available[i]); } printf("\n"); }
int main() { getMax(); getAllocation(); getNeed(); getAvailable(); if(!is_safe()) { red_printf("The current system is not secure!\nResources could not be allocated.\n"); exit(0); } else { green_printf("The system is secure.\n"); while (1) { getRequirement(); if(!is_safe()) { red_printf("After allocation, the system is not secure.\nIt has been restored to its original state!\n"); for (int i = 0; i < resourceNum; i++) { Allocation[requireNum-1][i]=AllocationStatus[i]; Need[requireNum-1][i]=NeedStatus[i]; Available[i]=AvailableStatus[i]; } continue; } else { green_printf("\nThe request was successful."); blue_printf("\nA possible safety sequence would be:\n"); for (int i = 0; i < processNum-1; i++) { blue_printf("%d->", Safety[i]); } blue_printf("%d\n",Safety[processNum-1]); showResource(); showAvailable(); } } } return 0; }
|