Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // lab2_str.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <iostream>
- using namespace std;
- double genExp(double lambda)
- {
- double u, x;
- u = (double)rand() / (RAND_MAX + 1);
- x = -1 / lambda*log(1 - u);
- return x;
- }
- double genGauss(double medie, double sigma)
- {
- double s = 0; int i;
- for (i = 1; i <= 12; i++)
- s += (double)rand() / (RAND_MAX + 1);
- return medie + sigma*(s - 6);
- }
- double calcTp(int N)
- {
- double tpmin = 1.2;
- double tunif = 0.0012;
- return tpmin + (N - 1)*tunif;
- }
- int main1()
- {
- int NS = 1000000;
- int pc = 0;
- int ns = 255;
- double STa = 0;
- double Tma;
- double lambda = 0.001609;
- double T = 8;
- int b;//dimensiunea blocului pentru o cerere
- int c;//adresa unei cereri
- int i = 1;
- while (i <= NS)
- {
- //generare cerere
- b = genGauss(20, 5.42857);
- c = genExp(lambda);
- STa += calcTp(abs(pc - c));
- STa += T / 2.0;
- STa += (b*T / ns);
- pc = c;
- ++i;
- }
- Tma = STa / NS;
- cout << "1.Timpul mediu de acces : " << Tma << endl;
- return 0;
- }
- int main2(double read_per)
- {
- int NS = 1000000;
- int pc = 0;
- int ns = 255;
- double STa = 0;
- double Tma;
- double lambda = 0.001609;
- double T = 8;
- double rnd;
- int b;//dimensiunea blocului pentru o cerere
- int c;//adresa unei cereri
- int t;//tipul cererii
- int C = 2000;
- int i = 1;
- //double read_per = 0.90;
- double write_per = 1 - read_per;
- //0-citire
- //1-scriere
- while (i <= NS)
- {
- //generare cerere
- b = genGauss(20, 5.42857);
- c = genExp(lambda);
- rnd = (double)rand() / (RAND_MAX + 1);//[0,1]
- if (rnd <= read_per)//citire
- t = 1;
- else //scriere
- t = 0;
- if (t == 1)//citire
- {
- if (abs(pc - c) < abs(pc - (c + C / 2)))
- {
- //simulare citire prima jumatate
- STa += calcTp(abs(pc - c));
- STa += T / 2;
- STa += b*T / ns;
- pc = c;
- }
- else
- {
- //simulare cautare in a doua jumatate
- //simulare citire prima jumatate
- STa += calcTp(abs(pc - (c + C / 2)));
- STa += T / 2;
- STa += b*T / ns;
- pc = c;
- }
- }
- else//scriere
- {
- if (abs(pc - c) < abs(pc - (c + C / 2)))
- {
- STa += calcTp(abs(pc - c));
- STa += T / 2;
- STa += b*T / ns;
- STa += calcTp(C / 2);
- STa += T / 2;
- STa += b*T / ns;
- pc = c + C / 2;
- }
- else
- {
- STa += calcTp(abs(pc - (c + C / 2)));
- STa += T / 2;
- STa += b*T / ns;
- STa += calcTp(C / 2);
- STa += T / 2;
- STa += b*T / ns;
- pc = c;
- }
- }
- ++i;
- }
- Tma = STa / NS;
- cout << "Timpul mediu de acces: " << Tma << endl;
- return 0;
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- main1();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement