操作系统 进程调度算法
#include
# include
#include
# include
struct Process {
std::string name;
int arrive;
int service;
int start;
int finish;
float turnaround;
float weighted_ta;
};
bool compareArrival(const Process& a, const Process& b) {
return a.arrive < b.arrive;
}
int main() {
std::cout << "先来先服务调度算法\n";
std::cout << "输入进程数目:";
int n;
std::cin >> n;
std::vector processes(n);
for (int i = 0; i < n; ++i) {
std::cout << "请输入进程" << (i+1) << "的信息(名称 到达时间 服务时间):";
std::cin >> processes[i].name >> processes[i].arrive >> processes[i].service;
}
std::sort(processes.begin(), processes.end(), compareArrival);
int current_time = 0;
for (std::vector::iterator it = processes.begin(); it != processes.end(); ++it) {
it->start = std::max(current_time, it->arrive);
it->finish = it->start + it->service;
it->turnaround = it->finish - it->arrive;
it->weighted_ta = it->turnaround / static_cast(it->service);
current_time = it->finish;
}
// 输出运行顺序
std::cout << "\n运行顺序:";
for (std::vector::const_iterator it = processes.begin(); it != processes.end(); ++it) {
std::cout << it->name;
if (it + 1 != processes.end()) {
std::cout << " → ";
}
}
// 输出表格
std::cout << std::fixed << std::setprecision(3);
std::cout << "\n\n"
<< std::left << std::setw(8) << "进程"
<< std::right << std::setw(12) << "到达时间"
<< std::setw(12) << "服务时间"
<< std::setw(12) << "开始时间"
<< std::setw(12) << "结束时间"
<< std::setw(12) << "周转时间"
<< "带权周转时间\n";
float total_ta = 0, total_wta = 0;
for (std::vector::const_iterator it = processes.begin(); it != processes.end(); ++it) {
std::cout << std::left << std::setw(8) << it->name
<< std::right << std::setw(12) << it->arrive
<< std::setw(12) << it->service
<< std::setw(12) << it->start
<< std::setw(12) << it->finish
<< std::setw(12) << it->turnaround
<< std::setw(12) << it->weighted_ta
<< "\n";
total_ta += it->turnaround;
total_wta += it->weighted_ta;
}
std::cout << "\n平均周转时间: " << total_ta / n
<< "\n平均带权周转时间: " << total_wta / n << std::endl;
return 0;
}
# include
#include
# include
struct Process {
std::string name;
int arrive;
int service;
int start;
int finish;
float turnaround;
float weighted_ta;
};
bool compareArrival(const Process& a, const Process& b) {
return a.arrive < b.arrive;
}
int main() {
std::cout << "先来先服务调度算法\n";
std::cout << "输入进程数目:";
int n;
std::cin >> n;
std::vector processes(n);
for (int i = 0; i < n; ++i) {
std::cout << "请输入进程" << (i+1) << "的信息(名称 到达时间 服务时间):";
std::cin >> processes[i].name >> processes[i].arrive >> processes[i].service;
}
std::sort(processes.begin(), processes.end(), compareArrival);
int current_time = 0;
for (std::vector::iterator it = processes.begin(); it != processes.end(); ++it) {
it->start = std::max(current_time, it->arrive);
it->finish = it->start + it->service;
it->turnaround = it->finish - it->arrive;
it->weighted_ta = it->turnaround / static_cast(it->service);
current_time = it->finish;
}
// 输出运行顺序
std::cout << "\n运行顺序:";
for (std::vector::const_iterator it = processes.begin(); it != processes.end(); ++it) {
std::cout << it->name;
if (it + 1 != processes.end()) {
std::cout << " → ";
}
}
// 输出表格
std::cout << std::fixed << std::setprecision(3);
std::cout << "\n\n"
<< std::left << std::setw(8) << "进程"
<< std::right << std::setw(12) << "到达时间"
<< std::setw(12) << "服务时间"
<< std::setw(12) << "开始时间"
<< std::setw(12) << "结束时间"
<< std::setw(12) << "周转时间"
<< "带权周转时间\n";
float total_ta = 0, total_wta = 0;
for (std::vector::const_iterator it = processes.begin(); it != processes.end(); ++it) {
std::cout << std::left << std::setw(8) << it->name
<< std::right << std::setw(12) << it->arrive
<< std::setw(12) << it->service
<< std::setw(12) << it->start
<< std::setw(12) << it->finish
<< std::setw(12) << it->turnaround
<< std::setw(12) << it->weighted_ta
<< "\n";
total_ta += it->turnaround;
total_wta += it->weighted_ta;
}
std::cout << "\n平均周转时间: " << total_ta / n
<< "\n平均带权周转时间: " << total_wta / n << std::endl;
return 0;
}
全部评论
相关推荐
04-28 10:29
台州学院 Java 点赞 评论 收藏
分享
点赞 评论 收藏
分享
点赞 评论 收藏
分享