fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define NUM_PARTITIONS 5
  5. #define MAX_SIZE 20
  6.  
  7. int partitions[NUM_PARTITIONS];
  8.  
  9. void initPartitions() {
  10. for (int i = 0; i < NUM_PARTITIONS; i++) {
  11. partitions[i] = 0;
  12. }
  13. }
  14.  
  15. void loadProcess(int size) {
  16. for (int i = 0; i < NUM_PARTITIONS; i++) {
  17. if (partitions[i] == 0) {
  18. partitions[i] = size;
  19. printf("Process of size %d loaded into partition %d\n", size, i);
  20. return;
  21. }
  22. }
  23. printf("Failed to load process of size %d (No available partition)\n", size);
  24. }
  25.  
  26. int main() {
  27. initPartitions();
  28.  
  29. loadProcess(5);
  30. loadProcess(8);
  31. loadProcess(10);
  32. loadProcess(2);
  33. loadProcess(15);
  34. loadProcess(7);
  35.  
  36. return 0;
  37. }
Success #stdin #stdout 0s 5300KB
stdin
1
stdout
Process of size 5 loaded into partition 0
Process of size 8 loaded into partition 1
Process of size 10 loaded into partition 2
Process of size 2 loaded into partition 3
Process of size 15 loaded into partition 4
Failed to load process of size 7 (No available partition)