-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_main.cpp
executable file
·123 lines (101 loc) · 2.68 KB
/
test_main.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
#include "disk.h"
#include "thread.h"
#include <vector>
#include <fstream>
#include <iostream>
int num_thread = 0;
int max_disk_queue = 0;
int count = 0;
int currentPos = 0;
int started = 0;
char** argv_local;
mutex mutex1;
cv cv1;
std::vector<std::vector<int>> inputData;
std::vector<std::pair<int, int>> diskQueue;
std::vector<int> current;
void readData(int argc, char** argv) {
num_thread = argc - 1;
count = num_thread - 1;
max_disk_queue = std::atoi(argv[1]);
inputData.resize(num_thread - 1);
diskQueue.reserve(std::atoi(argv[1]));
current.resize(num_thread - 1, -1);
argv_local = argv;
}
void issue(void* i) {
mutex1.lock();
std::ifstream file;
file.open(argv_local[*((int*)i) + 2]);
int data = -1;
while (file >> data) {
inputData[*((int*)i)].push_back(data);
}
if (inputData[*((int*)i)].size() == 0) count--;
started++;
cv1.broadcast();
mutex1.unlock();
mutex1.lock();
size_t s = 0;
int* index = (int*)i;
while (s != (inputData[*index].size())) {
while ((int)diskQueue.size() == std::min(max_disk_queue, std::min(count, num_thread - 1)) || current[*index] != ((int)s - 1)) {
cv1.wait(mutex1);
}
int tmp = inputData[*index][s];
diskQueue.push_back({ *index, tmp });
print_request(*index, tmp);
++s;
cv1.broadcast();
cv1.wait(mutex1);
}
mutex1.unlock();
file.close();
}
void service(void* s) {
std::vector<thread*> threads;
for (int i = 0; i < (num_thread - 1); ++i) {
int* tmp = new int(i);
threads.push_back(new thread((thread_startfunc_t)issue, (void*)tmp));
}
mutex1.lock();
while (started != (num_thread - 1)) {
cv1.wait(mutex1);
}
mutex1.unlock();
mutex1.lock();
while (count != 0) {
while (int(diskQueue.size()) < std::min(max_disk_queue, std::min(count, num_thread - 1))) {
cv1.wait(mutex1);
}
int dis = 1000;
std::pair<int, int> best = { -1,-1 };
int bestIndex = -1;
for (int i = 0; i < int(diskQueue.size()); ++i) {
if (abs(diskQueue[i].second - currentPos) < dis) {
best = diskQueue[i];
bestIndex = i;
dis = abs(diskQueue[i].second - currentPos);
}
}
currentPos = best.second;
++current[best.first];
print_service(best.first, best.second);
diskQueue.erase(diskQueue.begin() + bestIndex);
if (current[best.first] == ((int)inputData[best.first].size() - 1)) {
count--;
}
if ((int)diskQueue.size() < count) {
cv1.broadcast();
}
}
mutex1.unlock();
for (size_t i = 0; i < threads.size(); ++i) {
(*(threads[i])).join();
delete threads[i];
}
}
int main(int argc, char** argv){
readData(argc, argv);
cpu::boot((thread_startfunc_t)service, (void*)100, 0);
}