Facebook
From Adiii, 2 Months ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 155
  1. #include<stdio.h>
  2.  
  3. #define max 25
  4.  
  5. void main() {
  6. int frag[max], b[max], f[max], i, j, nb, nf, temp, highest = 0, lowest = 10000;
  7. static int bf[max], ff[max], bf2[max], ff2[max], bf3[max], ff3[max];
  8.  
  9.  
  10. printf("\nEnter the number of blocks:");
  11. scanf("%d", &nb;);
  12. printf("Enter the number of files:");
  13. scanf("%d", &nf;);
  14.  
  15. printf("\nEnter the size of the blocks:-\n");
  16. for (i = 1; i <= nb; i++) {
  17. printf("Block %d:", i);
  18. scanf("%d", &b[i]);
  19. #define max 25
  20. }
  21. printf("Enter the size of the files :-\n");
  22. for (i = 1; i <= nf; i++) {
  23. printf("File %d:", i);
  24. scanf("%d", &f[i]);
  25. }
  26.  
  27. // First Fit
  28. printf("\n\tMemory Management Scheme - First Fit");
  29. for (i = 1; i <= nf; i++) {
  30. for (j = 1; j <= nb; j++) {
  31. if (bf[j] != 1) {
  32. temp = b[j] - f[i];
  33. if (temp >= 0) {
  34. ff[i] = j;
  35. break;
  36. }
  37. }
  38. }
  39. frag[i] = temp;
  40. bf[ff[i]] = 1;
  41. }
  42. printf("\nFile_no:\tFile_size :\tBlock_no:\tBlock_size:\tFragment");
  43. for (i = 1; i <= nf; i++)
  44. printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d", i, f[i], ff[i], b[ff[i]], frag[i]);
  45.  
  46. // Best Fit
  47. printf("\n\n\tMemory Management Scheme - Best Fit");
  48. for (i = 1; i <= nf; i++) {
  49. for (j = 1; j <= nb; j++) {
  50. if (bf2[j] != 1) {
  51. temp = b[j] - f[i];
  52. if (temp >= 0)
  53. if (lowest > temp) {
  54. ff2[i] = j;
  55. lowest = temp;
  56. }
  57. }
  58. }
  59. frag[i] = lowest;
  60. bf2[ff2[i]] = 1;
  61. lowest = 10000;
  62. }
  63. printf("\nFile No\tFile Size \tBlock No\tBlock Size\tFragment");
  64. for (i = 1; i <= nf && ff2[i] != 0; i++)
  65. printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d", i, f[i], ff2[i], b[ff2[i]], frag[i]);
  66.  
  67. // Worst Fit
  68. printf("\n\n\tMemory Management Scheme - Worst Fit");
  69. for (i = 1; i <= nf; i++) {
  70. for (j = 1; j <= nb; j++) {
  71. if (bf3[j] != 1) {
  72. temp = b[j] - f[i];
  73. if (temp >= 0)
  74. if (highest < temp) {
  75. ff3[i] = j;
  76. highest = temp;
  77. }
  78. }
  79. }
  80. frag[i] = highest;
  81. bf3[ff3[i]] = 1;
  82.         highest = 0;
  83. }
  84. printf("\nFile_no:\tFile_size :\tBlock_no:\tBlock_size:\tFragment");
  85. for (i = 1; i <= nf; i++)
  86. printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d", i, f[i], ff3[i], b[ff3[i]], frag[i]);
  87.  }