模拟进程调度算法-时间片轮转算法
#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<iomanip>
#include<algorithm>
#define N 25
#define NN 110
using namespace std;
int num;
struct Node{
string str_name;
int time_arrive;
int time_serve;
int time_surplus;
int time_begin;
int time_finish;
int time_revolve;
double weight;
int flag_finish;
}p[N], res[NN];
void input(int u){
for(int i=0;i<u;i++){
cout << "请输入进程名, 到达时间, 服务时间" << endl;
cin >> p[i].str_name;
cin >> p[i].time_arrive;
cin >> p[i].time_serve;
res[i].str_name = p[i].str_name;
res[i].time_arrive = p[i].time_arrive;
res[i].time_serve = p[i].time_serve;
}
}
void output(int u){
cout << endl;
cout << endl;
cout << "结果数据: " << endl;
cout << " ";
cout << "进程名" << setw(10)
<< "到达时间" << setw(10)
<< "服务时间" << setw(10)
<< "开始时间" << setw(10)
<< "完成时间" << setw(10)
<< "周转时间" << setw(10)
<< "带权时间" << endl;
for(int i=0;i<u;i++){
cout << " ";
cout << setw(3) << res[i].str_name
<< setw(10) << res[i].time_arrive
<< setw(10) << res[i].time_serve
<< setw(10) << res[i].time_begin
<< setw(10) << res[i].time_finish
<< setw(10) << res[i].time_revolve
<< setw(13) << res[i].weight << endl;
}
}
bool cmp(Node a, Node b){
return a.time_arrive < b.time_arrive;
}
void convert_to_res(int m, int n){
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(res[i].str_name == p[j].str_name){
res[i].time_begin = j;
break;
}
}
}
for(int i=0;i<m;i++){
for(int j=n-1;j>=0;j--){
if(res[i].str_name == p[j].str_name){
res[i].time_finish = j + 1;
break;
}
}
}
for(int i=0;i<m;i++){
if(i <= 1){
res[i].time_revolve = res[i].time_finish - res[i].time_begin;
}else{
res[i].time_revolve = (res[i].time_finish - res[i].time_begin) + (i - 1);
}
}
for(int i=0;i<m;i++){
res[i].weight = double(res[i].time_revolve) / double(res[i].time_serve);
}
}
void Main(int u){
cout << "请输入时间片长度:" << endl;
int m;
cin >> m;
int flag = 1;
int Count_finish = 0;
cout << "过程数据: " << endl;
cout << " ";
cout << "进程名称" << setw(15)
<< "开始时间" << setw(15)
<< "运行时间" << setw(15)
<< "剩余服务时间" << setw(15)
<< "结束时间" << endl;
int time;
int c = 0;
while(Count_finish < u){
flag = 0;
for(int i=c;i<u;i++){
sort(p, p+u, cmp);
p[i].time_surplus = p[i].time_serve;
p[i].time_begin = p[i-1].time_finish;
if(p[i].time_begin < p[i].time_arrive){
p[i].time_begin = p[i].time_arrive;
}else{
p[i].time_begin = p[i-1].time_finish;
}
time = p[i].time_begin;
if (p[i].flag_finish == 1){
continue;
}else{
if(p[i].time_surplus <= m && time >= p[i].time_arrive){
flag = 1;
time = time + p[i].time_surplus;
p[i].flag_finish = 1;
p[i].time_finish = time;
cout << " ";
cout << setw(8) << p[i].str_name
<< setw(13) << (p[i].time_finish - p[i].time_surplus)
<< setw(15) << p[i].time_surplus
<< setw(16) << 0
<< setw(18) << p[i].time_finish << endl;
p[i].time_surplus = 0;
}else if(p[i].time_surplus > m && time >= p[i].time_arrive){
flag = 1;
time += m;
p[i].time_surplus -= m;
p[i].time_finish = time;
cout << " ";
cout << setw(8) << p[i].str_name
<< setw(13) << (time - m)
<< setw(15) << m
<< setw(16) << p[i].time_surplus
<< setw(18) << p[i].time_finish << endl;
p[u].str_name = p[i].str_name;
p[u].time_arrive = time;
p[u].time_serve = p[i].time_surplus;
u++;
}
if(p[i].flag_finish == 1){
Count_finish ++;
}
}
c++;
}
break;
if(flag == 0 && Count_finish < u){
for(int i=0;i<u;i++){
if(p[i].flag_finish == 0){
time = p[i].time_arrive;
break;
}
}
}
}
int ans = num;
convert_to_res(ans, u);
}
int main () {
int n;
cout << "请输入进程的数量:";
cin >> n;
num = n;
input(n);
Main(n);
output(n);
return 0;
}
运行结果