#include <bits/stdc++.h>
using namespace std;
ifstream infile;
ofstream outfile;
int n, k;//进程个数n 时间片长度k
struct pcb{
int id; // 进程编号
int st; // 到达时间
int len; // 运行时间
int res; // 剩余运行时间
};
struct result{
int id, ariv, len, st, end, t1, t2;// 编号 到达时间 运行时间 开始时间 结束时间 周转时间 等待时间
double t3; // 带权周转时间
};
struct cmp1{
bool operator()(const pcb& a, const pcb& b) {
return a.len > b.len;
}
};
struct cmp2{
bool operator()(const pcb& a, const pcb& b) {
return a.res > b.res;
}
};
vector<pcb> arr;
//先来先服务
void FCFS() {
outfile << "FCFS算法:\n";
queue<pcb> q; //阻塞队列
vector<result> rec;
int pre = arr[0].st;
for(int i = 1; i < n; i++) {
q.push(arr[i]);
}
auto& u = arr[0];
rec.push_back({u.id, u.st, u.len, pre, pre + u.len, pre + u.len - u.st, pre - u.st, (pre + u.len - u.st) /(double)u.len}), pre += u.len;
while(!q.empty()) {
auto u = q.front();
q.pop();
rec.push_back({u.id, u.st, u.len, pre, pre + u.len, pre + u.len - u.st, pre - u.st, (pre + u.len - u.st) /(double)u.len}), pre += u.len;
}
outfile << "进程名 " << ' ' << "到达时间" << ' ' << "运行时间" << ' ' << "开始时间" << ' ' << "结束时间" << ' ' << "周转时间" << ' ' << "等待时间" << ' ' << "带权周转时间" << '\n';
double sum = 0;
for(auto& x : rec) {
outfile << 'P' << x.id << "\t\t" << x.ariv << "\t\t" << x.len << "\t \t" << ' ' << x.st << "\t\t" << " " << x.end << "\t\t" << " " << x.t1 << "\t\t" << " " << x.t2 << "\t\t" << " " << x.t3 << '\n';
sum += x.t3;
}
outfile << "平均带权周转时间:" << sum / n << '\n';
}
//短进程优先(不可抢占)
void spf() {
outfile << "SPF算法:\n";
priority_queue<pcb, vector<pcb>, cmp1> q;
vector<result> rec;
int idx = 0, pre = arr[idx].st;
q.push(arr[idx++]);
while(!q.empty()) {
auto u = q.top();
q.pop();
rec.push_back({u.id, u.st, u.len, pre, pre + u.len, pre + u.len - u.st, pre - u.st, (pre + u.len - u.st) /(double)u.len}), pre += u.len;
while(idx < n && arr[idx].st <= pre) {
q.push(arr[idx++]);
}
}
double sum = 0;
outfile << "进程名 " << ' ' << "到达时间" << ' ' << "运行时间" << ' ' << "开始时间" << ' ' << "结束时间" << ' ' << "周转时间" << ' ' << "等待时间" << ' ' << "带权周转时间" << '\n';
for(auto& x : rec) {
outfile << 'P' << x.id << "\t\t" << x.ariv << "\t\t" << x.len << "\t \t" << ' ' << x.st << "\t\t" << " " << x.end << "\t\t" << " " << x.t1 << "\t\t" << " " << x.t2 << "\t\t" << " " << x.t3 << '\n';
sum += x.t3;
}
outfile << "平均带权周转时间:" << sum / n << '\n';
}
//最短剩余时间算法(可抢占)
void srt() {
outfile << "SRT算法:\n";
priority_queue<pcb, vector<pcb>, cmp2> q;
vector<result> rec;
unordered_map<int, int> mp; //记录每个进程的开始时间
int pre = arr[0].st;
auto u = arr[0]; // u是当前正在运行的进程
mp[u.id] = pre;
for(int i = 1; i < n;) {
int t = u.res - (arr[i].st - pre);
if(t > arr[i].len) {
outfile << 'P' << u.id << ":[" << pre << ", " << arr[i].st << "] ";
u.res -= (arr[i].st - pre);
pre = arr[i].st;
q.push(u);
u = arr[i++];
mp[u.id] = u.st;
} else {
outfile << 'P' << u.id << ":[" << pre << ", " << pre + u.res << "] ";
rec.push_back({u.id, u.st, u.len, mp[u.id], pre + u.res, pre + u.res - u.st, pre + u.res - u.st - u.len, (pre + u.res - u.st) /(double)u.len});
pre += u.res;
if(!q.empty() && arr[i].len > q.top().res) {
u = q.top();
q.pop();
} else u = arr[i++], mp[u.id] = max(u.st, pre), pre = mp[u.id];
}
}
q.push(u);
while(!q.empty()) {
u = q.top();
q.pop();
outfile << 'P' << u.id << ":[" << pre << ", " << pre + u.res << "] ";
rec.push_back({u.id, u.st, u.len, mp[u.id], pre + u.res, pre + u.res - u.st, pre + u.res - u.st - u.len, (pre + u.res - u.st) /(double)u.len});
pre += u.res;
}
double sum = 0;
outfile << '\n' << "进程名 " << ' ' << "到达时间" << ' ' << "运行时间" << ' ' << "开始时间" << ' ' << "结束时间" << ' ' << "周转时间" << ' ' << "等待时间" << ' ' << "带权周转时间" << '\n';
for(auto& x : rec) {
outfile << 'P' << x.id << "\t\t" << x.ariv << "\t\t" << x.len << "\t \t" << ' ' << x.st << "\t\t" << " " << x.end << "\t\t" << " " << x.t1 << "\t\t" << " " << x.t2 << "\t\t" << " " << x.t3 << '\n';
sum += x.t3;
}
outfile << "平均带权周转时间:" << sum / n << '\n';
}
//时间片轮转算法
void rr() {
outfile << "RR算法:\n";
queue<pcb> q;
vector<result> rec;
unordered_map<int, int> mp; //记录每个进程的开始时间
int pre = arr[0].st, idx = 0;
mp[arr[0].id] = arr[0].st;
q.push(arr[idx++]);
while(!q.empty()) {
auto u = q.front();
q.pop();
if(!mp.count(u.id)) mp[u.id] = pre;
while(idx < n && arr[idx].st <= pre + k) {
q.push(arr[idx++]);
}
if(u.res - k > 0) {
u.res -= k;
q.push(u);
outfile << 'P' << u.id << ":[" << pre << ", " << pre + k << "] ";
pre += k;
} else {
outfile << 'P' << u.id << ":[" << pre << ", " << pre + u.res << "] ";
rec.push_back({u.id, u.st, u.len, mp[u.id], pre + u.res, 0,0,0});
pre += u.res;
}
}
outfile << '\n' << "进程名 " << ' ' << "到达时间" << ' ' << "运行时间" << ' ' << "开始时间" << ' ' << "结束时间" << '\n';
for(auto& x : rec) {
outfile << 'P' << x.id << "\t\t" << x.ariv << "\t\t" << x.len << "\t \t" << ' ' << x.st << "\t\t" << " " << x.end << '\n';
}
}
int main() {
infile.open("input.txt", ios::in);
outfile.open("output.txt", ios::out);
infile >> n >> k;
for(int i = 0; i < n ; i++) {
pcb p;
infile >> p.id >> p.st >> p.len;
p.res = p.len;
arr.push_back(p);
}
sort(arr.begin(), arr.end(), [&](auto& a, auto& b) {
if(a.st != b.st) return a.st < b.st;
return a.len < b.len;
});
FCFS();
spf();
srt();
rr();
return 0;
}
06-29
11-24
05-24