-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest4.cpp
More file actions
executable file
·130 lines (110 loc) · 2.79 KB
/
test4.cpp
File metadata and controls
executable file
·130 lines (110 loc) · 2.79 KB
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
//
// main.cpp
// p1
//
// Created by Skylar Chen on 1/30/19.
// Copyright © 2019 Skylar Chen. All rights reserved.
//
#include <iostream>
#include <fstream>
#include <vector>
#include "thread.h"
using std::pair;
using std::cout;
using std::cin;
using std::vector;
using std::ifstream;
using std::string;
using std::min;
using std::endl;
int MAX_DISK_QUEUE = 0;
int ACT_REQ = 0;
int num = 0;
vector< pair<int, int> > Q;
vector<bool> *F;
int CUR_ID = -1;
std::vector<std::vector<int>> INPUT;
mutex reqid;
mutex queue;
mutex print;
cv request;
cv service;
void scheduler(void *a);
void requester(void *a);
void servicer(void *a);
void scheduler(void *a){
for(int i = 0; i < num; i++){
thread req ((thread_startfunc_t) requester, (void *) 0);
}
thread ser ((thread_startfunc_t) servicer, (void *) 0);
}
void requester(void *a){
reqid.lock();
CUR_ID+=1;
int id = CUR_ID;
reqid.unlock();
std::vector<int> inp = INPUT[id];
for(int i = 0; i < (int)inp.size(); i++){
int track = inp[i];
queue.lock();
while(((int)Q.size() >= min(MAX_DISK_QUEUE, ACT_REQ))
|| (*F)[id] == false){
request.wait(queue);
}
pair<int, int> p(id, track);
Q.push_back(p);
print.lock();
cout << id << " " << track << endl; //*******optimize?
print.unlock();
(*F)[id] = false;
service.signal();
queue.unlock();
}
queue.lock();
while(!(*F)[id]){
request.wait(queue);
}
ACT_REQ--;
service.signal();
queue.unlock();
}
void servicer(void *a){
int head = 0;
while(true){
queue.lock();
while((int)Q.size() < min(MAX_DISK_QUEUE, ACT_REQ)
|| (int)Q.size()==0){
service.wait(queue);
}
if(!Q.empty()){
int short_index = 0;
for(int i = 0; i < (int)Q.size(); i++){
if(abs(Q[i].second-head) < abs(Q[short_index].second - head)){
short_index = i;
}
}
head = Q[short_index].second;
print.lock();
cout << Q[short_index].first << " " << head << endl;
print.unlock();
(*F)[Q[short_index].first] = true;
Q.erase(Q.begin()+short_index);
request.broadcast();
}
queue.unlock();
}
}
int main(int argc, char **args){
INPUT.resize(5);
INPUT[0] = {785};
INPUT[1] = {350,914,589};
INPUT[2] = {827,567,21};
INPUT[3] = {302,230};
INPUT[4] = {631,11};
MAX_DISK_QUEUE = 3;
ACT_REQ = 5;
num = ACT_REQ;
F = new vector<bool>(ACT_REQ, true);
cpu::boot(1, (thread_startfunc_t) scheduler, (void *)5, true, true, 2);
return 0;
}