-
Notifications
You must be signed in to change notification settings - Fork 0
/
FIFO.cpp
173 lines (139 loc) · 5.22 KB
/
FIFO.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include<iostream>
#include <fstream>
#include <stdio.h>
#include <algorithm>
using namespace std;
struct FIFO
{
/*PR -> values of Page Referencing
MorH -> MISS or HIT //Tablica struktur zawieraj¹ca wymienione zmienne potrzebne do obliczen w algorytmie
*/
int PR,MorH;
} tab[1000];
int main()
{
int count_=1; //zlicza, który ci¹g zosta³ wpisany
int frame[20]; //tablica w ktorej element jest ramka w algorytmie( w zależności od wybranej liczby ramek )
int PF=0; //Page Frame //liczba ramek wczytywana z pliku
int NCH=0; //not count hit //do zliczania nie trafionych stron
int CH=0; //count hit //do zliczania trafionych stron
int pf=0; //Number of Misses //licznik błędów strony
int i=0; //zmienna do sterowania petla while
int n=100; //liczba numerow stron w jednym ciagu
int temp; //zmienna pomocnicza
ifstream plik; // deklaracja pokazujaca programowi,
plik.open("dane_STRONY.txt"); // sk¹d bêd¹ wczytywane dane
ofstream zapis;
zapis.open("zapis_dane_test_FIFO.txt",ios_base::app);
while(!plik.eof()) //wykonuje, a¿ nie na trafi na koniec pliku
{
//Page frames
plik>>PF; //wczytuje PF które jest pierwszą liczba w wczytywanym pliku
for(int i=0; i<n; i++) //pêtla wczytuj¹ca PR ktorymi s¹ drugie 100 liczb
{
//values of Page Referencing
plik>>tab[i].PR;
}
for(int i=0; i<PF; i++) //wypelniamy całą tablice -1
{
frame[i]=-1;
}
//wypisywanie na ekran/do pliku
zapis<<endl;
zapis<<"\t\t\t\t\t"<<count_<<"\t\t\t"<<endl;
zapis<<"LP.\tPage Trace\t";
for(int i=0; i<PF; i++)
{
zapis<<"\tPage Frame"<<i+1<<"\t";
}
zapis<<endl;
//ALGORYTM
while(i<n) //pêtla dla 100 procesow jednego ci¹gu steruj¹ca algorytmem
{
if(frame[i]==-1 && i<PF) //warunek sprawdza czy ramka jest pusta (pusta jeśli wartość równa -1)tylko dla pierwszych 3 stron
{
frame[i]=tab[i].PR;
for(int j=0; j<PF; j++)
{
if(frame[j]==tab[i+1].PR) //sprawdzamy czy w ramce jest numer strony ktorej szukamy
{
tab[i+1].MorH=1; //zapisanie do tablicy trafień w odpowiednim indeksie
}
else
{
NCH++; //zlicanie nie trafionych
}
}
if(tab[i].MorH==1) //jeśli mamy trafienie to wczytaywana jest wartość podstawowa w tym przypadku -1
{
frame[i]=-1;
}
}
else
{
NCH=0;
for(int j=0; j<PF; j++)
{
if(frame[j]==tab[i].PR) //sprawdzamy czy w ramce jest numer strony ktorej szukamy
{
tab[i].MorH=1; //zapisanie do tablicy trafień w odpowiednim indeksie
}
else
{
NCH++; //zlicanie nie trafionych
}
}
if(NCH==PF) //warunek sprawdzający czy nie trafione strony są równe licbie ramek
{
for(int j=0; j<PF-1; j++) //przesunięcie stron w ramkach
{
temp=frame[j];
frame[j]=frame[j+1];
frame[j+1]=temp;
}
frame[PF-1]=tab[i].PR; //wstawiamy wartość nowej strony
}
}
//przekierowanie na ekran/do pliku
zapis<<i+1<<"\t"<<tab[i].PR<<"\t";
for(int i=0; i<PF; i++)
{
if(frame[i]==-1)
zapis<<"\t\t"<<0<<"\t";
else
zapis<<"\t\t"<<frame[i]<<"\t";
}
if(tab[i].MorH==1)
{
zapis<<"\t"<<"Hit";
CH++; //zliczanie trafień
}
else if(tab[i].MorH==0)
pf++;
zapis<<endl;
i++;
}
zapis<<"\nNumber of Misses= "<< pf;
zapis<<"\nMiss ratio= "<< float(pf)/n;
zapis<<"\nNumber of Hits="<<CH;
zapis<<"\nHit ratio= "<<float(CH)/n;
//ZEROWANIE
PF=0;//Page frames
NCH=0; //not count hit
CH=0; //count hit
pf=0; //Page fault
temp=0;
i=0;
for(int i=0; i<PF; i++)
{
frame[i]=0;
}
for(int i=0; i<100; i++)
{
tab[i].PR=0;
tab[i].MorH=0;
}
count_++; //zlicza ktory ciąg wykonuje program
}
return 0;
}