Advertisement
Guest User

Untitled

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