-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom.cpp
324 lines (266 loc) · 8.41 KB
/
random.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#include <iostream>
#include <map>
#include <list>
#include <fstream>
#include <stdio.h>
#include <unistd.h>
#include <iomanip>
#include <sys/time.h>
#include <pthread.h>
using namespace std;
ofstream ofile;
typedef struct Page {
int pageNumber;
string bindProcessName;
} page;
typedef struct Prcoess {
string name;
int size;
int arrivalTime;
int serviceTime;
map<int, page> pageList;
int hitCounts;
int missingCount;
} process;
int TotalTime = 60;
int CurrentTime = 0;
list <page> memoryFreePage;
list <process> JobList;
list <process> DoneJob;
int arrSize[4] = {5, 11, 17, 31};
pthread_mutex_t mut;
int64_t getCurrentTime() {
struct timeval time;
gettimeofday(&time, NULL);
return time.tv_sec;
}
void Reset(page *page) {
ofile.open("run.txt", ios::app);
ofile << "Process " << page->bindProcessName << ", page " <<
page->pageNumber << " evicted." << endl;
ofile.close();
page->bindProcessName = "";
page->pageNumber = 0;
}
bool IsUsed(const string name) {
string str = "";
return str.compare(name) == 0 ? false : true;
}
Page *GetPage(page pa) {
//pa = memoryFreePage.front();
//memoryFreePage.pop_front();
//return &pa;
Page *pg = new Page;
pg = &memoryFreePage.front();
memoryFreePage.pop_front();
return pg;
}
bool sortArrTime(const process &m1, const process &m2) {
return m1.arrivalTime < m2.arrivalTime;
}
void initVariable() {
TotalTime = 60;
CurrentTime = 0;
JobList.clear();
DoneJob.clear();
for (int i = 0; i < 150; ++i) {
process proInit;
int size = (rand() % 4);
proInit.size = arrSize[size];
proInit.arrivalTime = (rand() % 6000) + 1;
proInit.serviceTime = (rand() % 5) + 1;
char s[40];
sprintf(s, "%s%d", "P", proInit.arrivalTime);
proInit.name = s;
JobList.push_back(proInit);
}
JobList.sort(sortArrTime);
int j = 1;
page pagInit;
int k = 0;
list<process>::iterator job;
job = JobList.begin();
int y = job->size;
while (k < 5) {
while (y > 0) {
pagInit.pageNumber = j;
pagInit.bindProcessName = job->name;
memoryFreePage.push_back(pagInit);
j++;
job++;
y--;
}
k++;
}
while (j < 100) {
pagInit.pageNumber = j;
pagInit.bindProcessName = "";
memoryFreePage.push_back(pagInit);
j++;
}
}
void AssignPage(process *prc, int number) {
auto pag = GetPage(prc->pageList[number]);
if (IsUsed(pag->bindProcessName)) {
prc->hitCounts++;
Reset(pag);
}else{
prc->missingCount++;
}
pag->bindProcessName = prc->name;
pag->pageNumber = number;
prc->pageList[number] = *pag;
ofile.open("run.txt", ios::app);
ofile << setfill('0') << setw(2) << CurrentTime
<< "s: Process " << prc->name << " referenced page " << number
<< ". Page not in memory.\n";
ofile << "--------------------------------------------\n";
ofile.close();
// prc->missingCount++;
}
int locality(int i, int totalPages) {
int delta, j = 0;
int r = rand() % 10;
if (r < 7) {
int r2=rand()%2;
if (r2==0){
return totalPages-1;
}else{
return totalPages+1;
}
} else {
int r3=rand()%9;
return r3+totalPages;
}
j = (((i + delta) % totalPages) + totalPages) % totalPages;
return j;
}
void RandomRepalce(process *prc, int pageNumber) {
int oldKey = rand() % 100 + 1;
string value;
page *pag;
//list<page>::iterator findRan;
//list<page>::iterator deleteRan;
for (auto findRan = memoryFreePage.begin(); findRan != memoryFreePage.end(); findRan++) {
if (findRan->pageNumber == oldKey) {
value = findRan->bindProcessName;
break;
}
}
// delete
for (auto deleteRan = memoryFreePage.begin(); deleteRan != memoryFreePage.end(); deleteRan++) {
if (deleteRan->bindProcessName == value) {
memoryFreePage.erase(deleteRan);
}
}
pag->pageNumber = pageNumber;
prc->pageList[pageNumber] = *pag;
ofile.open("run.txt", ios::app);
ofile << setfill('0') << setw(2) << CurrentTime
<< "s: Process " << prc->name << " referenced page " << pageNumber
<< ". Page not in memory. Process " << prc->pageList[pageNumber].pageNumber << ", page "
<< oldKey << " evicted.\n";
ofile << "--------------------------------------------\n";
ofile.close();
}
static void *Run(void *arg) {
pthread_mutex_lock(&mut);
CurrentTime = 0;
int test = 0;
while (JobList.size() != 0) {
int currentReferenceNumber = 0;
process job = JobList.front();
ofile.open("run.txt", ios::app);
ofile << setfill('0') << setw(2) << CurrentTime
<< "s: Process " << job.name << " referenced page "
<< currentReferenceNumber << ". Page not in memory.\n " << endl;
ofile << "--------------------------------------------\n";
JobList.pop_front();
ofile.close();
if (memoryFreePage.size() > 4) {
AssignPage(&job, currentReferenceNumber);
auto CurrentTime = 1;
sleep(1);
}
auto localTime = CurrentTime;
while (CurrentTime < localTime + job.serviceTime && CurrentTime < TotalTime) {
auto nextReferenceNumber = locality(currentReferenceNumber, job.size);
if (job.pageList[nextReferenceNumber].bindProcessName != "") {
ofile.open("run.txt", ios::app);
ofile << setfill('0') << setw(2) << CurrentTime
<< "s: Process " << job.name << " referenced page "
<< nextReferenceNumber << ". Page in memory. No page evicted.\n"
<< endl;
ofile << "--------------------------------------------\n";
ofile.close();
} else {
if (memoryFreePage.size() != 0) {
AssignPage(&job, nextReferenceNumber);
} else {
RandomRepalce(&job, nextReferenceNumber);
}
}
CurrentTime += 1;
sleep(1);
}
map<int, page>::iterator it;
map<int, page>::iterator eraseIt;
it = job.pageList.begin();
while (it != job.pageList.end()) {
if (it->second.pageNumber != 0) {
job.pageList.erase(it++);
} else {
it++;
}
//memoryFreePage.push_back(job.pageList[it]);
}
ofile.open("run.txt", ios::app);
ofile << setfill('0') << setw(2) << CurrentTime
<< "s: Process " << job.name << " completed. Total size "
<< job.size << "MB. Duration " << job.serviceTime << "s.\n";
ofile << "--------------------------------------------\n";
ofile.close();
DoneJob.push_back(job);
}
pthread_mutex_unlock(&mut);
}
int main() {
int totalHits=0;
int totalMiss = 0;
int totalComplete=0;
for(int i=0;i<5;i++){
pthread_t t[25];
initVariable();
cout << "Init finish!" << endl;
pthread_mutex_init(&mut, NULL);
for (int i = 0; i < 25; ++i) {
pthread_create(&t[i], NULL, Run, NULL);
}
for (int i = 0; i < 25; ++i) {
pthread_join(t[i], NULL);
}
int hits = 0, misses = 0, complete = 0;
for (auto it = DoneJob.begin(); it != DoneJob.end(); ++it) {
process p = *it;
hits += p.hitCounts;
misses += p.missingCount;
complete++;
}
ofile.open("run.txt", ios::app);
ofile << " total Rum: " << complete << " Hits: "<< hits
<< " miss " << misses << " miss%: "<< float(misses)/float(complete) <<" Hit%: "<< float(hits)/float(complete)<<"\n";
ofile << "--------------------------------------------\n";
ofile.close();
totalHits += hits;
totalMiss += misses;
totalComplete += complete;
//pthread_exit(NULL);
cout << "end!" << endl;
}
ofile.open("run.txt", ios::app);
ofile << " total Rum: " << totalComplete << " Hits: "<< totalHits
<< " miss " << totalMiss << " miss%: "<< float(totalMiss)/float(totalComplete) <<" Hit%: "<< float(totalHits)/float(totalComplete)<<"\n";
ofile << "--------------------------------------------\n";
ofile.close();
return 0;
}