Advertisement
_Black_Panther_

Untitled

Mar 19th, 2021
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.75 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstring>
  3. #include<string>
  4. #include<ctime>
  5. #include<iomanip>
  6. using namespace std;
  7. int firstFit(int blockSize[], int m, int processSize[], int n)
  8. {
  9. // Stores block id of the block allocated to a process
  10. int allocation[n], cnt = 0;;
  11. int i, j;
  12. // Initially no block is assigned to any process
  13. memset(allocation, -1, sizeof(allocation));
  14.  
  15. // pick each process and find suitable blocks according to its size ad assign to it
  16. for (i=0; i<n; i++)
  17. {
  18. for (j=0; j<m; j++)
  19. {
  20. if (blockSize[j] >= processSize[i])
  21. {
  22. allocation[i] = j;
  23. blockSize[j] -= processSize[i];
  24. break;
  25. }
  26. }
  27. if(j==m)
  28. cnt++; //increment the counter if process if unallocated
  29. }
  30. return cnt;
  31. }
  32.  
  33. int bestFit(int blockSize[], int m, int processSize[], int n)
  34. {
  35. int allocation[n], cnt = 0;
  36. memset(allocation, -1, sizeof(allocation));
  37.  
  38. // pick each process and find suitable blocks according to its size ad assign to it
  39. for (int i=0; i<n; i++)
  40. {
  41. // Find the best fit block for current process
  42. int bestIdx = -1;
  43. for (int j=0; j<m; j++)
  44. {
  45. if (blockSize[j] >= processSize[i])
  46. {
  47. if (bestIdx == -1)
  48. bestIdx = j;
  49. else if (blockSize[bestIdx] > blockSize[j])
  50. bestIdx = j;
  51. }
  52. }
  53. // If we could find a block for current process
  54. if (bestIdx != -1)
  55. {
  56. allocation[i] = bestIdx;
  57. blockSize[bestIdx] -= processSize[i];
  58. }
  59. else
  60. cnt++;
  61. }
  62. return cnt;
  63. }
  64.  
  65. int worstFit(int blockSize[], int m, int processSize[], int n)
  66. {
  67. int allocation[n], cnt = 0;
  68. memset(allocation, -1, sizeof(allocation));
  69.  
  70. // pick each process and find suitable blocks according to its size ad assign to it
  71. for (int i=0; i<n; i++)
  72. {
  73. // Find the best fit block for current process
  74. int wstIdx = -1;
  75. for (int j=0; j<m; j++)
  76. {
  77. if (blockSize[j] >= processSize[i])
  78. {
  79. if (wstIdx == -1)
  80. wstIdx = j;
  81. else if (blockSize[wstIdx] < blockSize[j])
  82. wstIdx = j;
  83. }
  84. }
  85. // If we could find a block for current process
  86. if (wstIdx != -1)
  87. {
  88. allocation[i] = wstIdx;
  89. blockSize[wstIdx] -= processSize[i];
  90. }
  91. else
  92. cnt++;
  93. }
  94. return cnt;
  95. }
  96.  
  97. void call_algo(int freeblocks[], int n_blocks, int process[], int n_process)
  98. {
  99. int i, j, no_of_unalloc;
  100. clock_t timer;
  101.  
  102. int tmp_freeblocks[n_blocks];
  103. for(i=0; i<n_blocks; ++i)
  104. tmp_freeblocks[i] = freeblocks[i];
  105.  
  106. for(i=1; i<4; ++i)
  107. {
  108. if(i==2 || i==3)
  109. for(j=0; j<n_blocks; ++j)
  110. freeblocks[j] = tmp_freeblocks[j];
  111. timer = clock();
  112. switch(i)
  113. {
  114. case 1: no_of_unalloc = firstFit(freeblocks, n_blocks, process, n_process);
  115. break;
  116.  
  117. case 2: no_of_unalloc = bestFit(freeblocks, n_blocks, process, n_process);
  118. break;
  119.  
  120. case 3: no_of_unalloc = worstFit(freeblocks, n_blocks, process, n_process);
  121. break;
  122. }
  123. timer = (double(clock() - timer))/CLOCKS_PER_SEC;
  124. cout << "| " << setw(15) << left << no_of_unalloc << setw(11) << right << timer;
  125. }
  126. cout << endl;
  127. }
  128.  
  129. int main()
  130. {
  131. char i,filename[12];
  132.  
  133. char block_data_type[15]; //sorted,reverse sorted or random
  134. int no_of_freeblocks,j;
  135.  
  136. strcpy(filename,"inputi.txt");
  137.  
  138. int no_of_process;
  139. //read the process data for once and all
  140.  
  141. freopen("process.txt","r",stdin);
  142. cin>>no_of_process;
  143.  
  144. int process[no_of_process];
  145.  
  146. for(j=0;j<no_of_process;++j)
  147. cin>>process[j];
  148.  
  149. fclose(stdin);
  150. //formatted output
  151. cout << left << setw(12) << " " << "| " << setw(18) << " ";
  152. cout << "| " << setw(26) << "First Fit";
  153. cout << "| " << setw(26) << "Best Fit"<< "| " << setw(26) << "Worst Fit" << endl;
  154. cout << left << setw(12) << "Dataset #" << "| " << setw(18) << "Dataset Descrp.";
  155. for(i=0; i<3; ++i)
  156. cout << "| " << left << setw(15) << "#unalloc. proc" << right << setw(11) << "time(sec)";
  157. cout << endl;
  158. for(i=0; i<117; ++i)
  159. cout << '-';
  160. cout << endl;
  161.  
  162. //read free block data
  163. for(i='0';i<'5';++i)
  164. {
  165. filename[5]=i;
  166. // cout<<filename<<endl;
  167.  
  168. freopen(filename,"r",stdin);
  169.  
  170. cin.getline(block_data_type,15);
  171. cin>>no_of_freeblocks;
  172.  
  173. cout << setw(12) << left << i << "| " << setw(18) << block_data_type;
  174.  
  175. int free_blocks[no_of_freeblocks];
  176.  
  177. for(j=0;j<no_of_freeblocks;++j)
  178. cin>>free_blocks[j];
  179.  
  180. call_algo(free_blocks, no_of_freeblocks, process, no_of_process);
  181.  
  182. fclose(stdin);
  183. }
  184.  
  185. return 0;
  186. }
  187.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement