Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include<math.h>
  4. using namespace std;
  5. double genExp(double lambda)
  6. {
  7. double u, x;
  8. u = (double)rand() / (RAND_MAX + 1);
  9. x = -1 / lambda*log(1 - u);
  10. return x;
  11. }
  12.  
  13.  
  14. double genJmen(double c)
  15. {
  16. double u, x;
  17. u = (double)rand() / (RAND_MAX + 1);
  18. x = u* c;
  19. return x;
  20. }
  21.  
  22. double genGauss(double medie, double sigma)
  23. {
  24. double s = 0; int i;
  25. for (i = 1; i <= 12; i++)
  26. s += (double)rand() / (RAND_MAX + 1);
  27. return medie + sigma*(s - 6);
  28. }
  29.  
  30.  
  31. double calcTp(int N)
  32. {
  33. double tpmin = 1.2;
  34. double tunif = 0.0012;
  35. return tpmin + (N - 1)*tunif;
  36. }
  37. int main1()
  38. {
  39. int NS = 1000000;
  40. int pc = 0;
  41.  
  42. int ns = 255;
  43. double STa = 0;
  44. double TMA;
  45. double lambda = 0.00032188758;
  46. double T = 11.1111111111;
  47. int b;//dimensiunea blocului pentru o cerere
  48. int c;//adresa unei cereri
  49. int i = 1;
  50. double cap = 10000;
  51.  
  52. while (i <= NS)
  53. {
  54. //generare cerere
  55. b = genGauss(20, 5.42857);
  56. c = genJmen(cap);
  57.  
  58. STa += calcTp(abs(pc - c));
  59. STa += T / 2.0;
  60. STa += (b*T / ns);
  61. pc = c;
  62. ++i;
  63. }
  64.  
  65. TMA = STa / NS;
  66. cout << "Timp acces este:" << TMA << endl;
  67. return 0;
  68.  
  69. }
  70.  
  71.  
  72.  
  73. int main()
  74. {
  75. main1();
  76. return 0;
  77. }
  78.  
  79.  
  80.  
  81.  
  82. int main2(double read_per)
  83. {
  84. int NS = 1000000;
  85. int pc = 0;
  86.  
  87. int ns = 255;
  88. double STa = 0;
  89. double Tma;
  90. double lambda = 0.001609;
  91. double T = 8;
  92. double rnd;
  93. int b;//dimensiunea blocului pentru o cerere
  94. int c;//adresa unei cereri
  95. int t;//tipul cererii
  96. int C = 2000;
  97. int i = 1;
  98. //double read_per = 0.90;
  99. double write_per = 1 - read_per;
  100.  
  101. //0-citire
  102. //1-scriere
  103.  
  104. while (i <= NS)
  105. {
  106. //generare cerere
  107. b = genGauss(20, 5.42857);
  108. c = genExp(lambda);
  109. rnd = (double)rand() / (RAND_MAX + 1);//[0,1]
  110. if (rnd <= read_per)//citire
  111. t = 1;
  112. else //scriere
  113. t = 0;
  114.  
  115.  
  116. if (t == 1)//citire
  117. {
  118. if (abs(pc - c) < abs(pc - (c + C / 2)))
  119. {
  120. //simulare citire prima jumatate
  121. STa += calcTp(abs(pc - c));
  122. STa += T / 2;
  123. STa += b*T / ns;
  124. pc = c;
  125. }
  126. else
  127. {
  128. //simulare cautare in a doua jumatate
  129. //simulare citire prima jumatate
  130. STa += calcTp(abs(pc - (c + C / 2)));
  131. STa += T / 2;
  132. STa += b*T / ns;
  133. pc = c;
  134. }
  135. }
  136. else//scriere
  137. {
  138. if (abs(pc - c) < abs(pc - (c + C / 2)))
  139. {
  140. STa += calcTp(abs(pc - c));
  141. STa += T / 2;
  142. STa += b*T / ns;
  143.  
  144. STa += calcTp(C / 2);
  145. STa += T / 2;
  146. STa += b*T / ns;
  147. pc = c + C / 2;
  148. }
  149. else
  150. {
  151. STa += calcTp(abs(pc - (c + C / 2)));
  152. STa += T / 2;
  153. STa += b*T / ns;
  154.  
  155. STa += calcTp(C / 2);
  156. STa += T / 2;
  157. STa += b*T / ns;
  158. pc = c;
  159.  
  160. }
  161. }
  162.  
  163. ++i;
  164. }
  165.  
  166. Tma = STa / NS;
  167. cout << "Timpul mediu de acces: " << Tma << endl;
  168. return 0;
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement