#include<iostream> #include<string> using namespace std; const int MAX = 100; #define W "waitting" //等待状态 #define R "running" //运行状态 #define F "finish" //完成状态 #define N "no" //未进入状态 class PROCESS { public: string name; //进程名 int prior=0; //优先数 string state=N; //运行状态 float arrivetime=0; //到达时间 float runningtime=0; //运行时间 float remaintime = 0; //剩余运行时间 float waittingtime=0; //等待时间 float finishtime=0; //完成时间 float roundtime=0; //周转时间 float weighttime=0; //带权周转时间 PROCESS* next=NULL; //用于时间片转轮算法的指针,指向下一个进程。 PROCESS& operator=(PROCESS& p); //重载运算符,方便后续对进程排序。 }; PROCESS process[MAX]; int processnumber; int timeslice; int judge=1; float Time=0;续写这段代码以实现时间片轮转调度算法
时间: 2024-02-10 09:14:07 浏览: 66
#include_iostream.h_
4星 · 用户满意度95%
// 头文件和全局变量省略
// 重载运算符,用于后续对进程排序
PROCESS& PROCESS::operator=(PROCESS& p) {
name = p.name;
prior = p.prior;
state = p.state;
arrivetime = p.arrivetime;
runningtime = p.runningtime;
remaintime = p.remaintime;
waittingtime = p.waittingtime;
finishtime = p.finishtime;
roundtime = p.roundtime;
weighttime = p.weighttime;
next = p.next;
return *this;
}
// 时间片轮转调度算法
void RR() {
// 按到达时间排序
for (int i = 0; i < processnumber - 1; i++) {
for (int j = i + 1; j < processnumber; j++) {
if (process[j].arrivetime < process[i].arrivetime) {
swap(process[j], process[i]);
}
}
}
// 初始化
int current = 0; // 当前执行的进程
int count = 0; // 时间片计数器
PROCESS* head = &process[current]; // 时间片转轮算法的头指针
// 时间片轮转调度
while (judge) {
// 判断是否所有进程都执行完毕
int flag = 1;
for (int i = 0; i < processnumber; i++) {
if (process[i].state != F) {
flag = 0;
break;
}
}
if (flag == 1) {
break;
}
// 执行进程
if (process[current].remaintime == 0) { // 进程执行完毕
process[current].state = F;
process[current].finishtime = Time;
process[current].roundtime = process[current].finishtime - process[current].arrivetime;
process[current].weighttime = process[current].roundtime / process[current].runningtime;
head = head->next; // 头指针指向下一个进程
}
else if (count == timeslice) { // 时间片用完,切换进程
process[current].state = W;
process[current].waittingtime += timeslice;
count = 0;
head = head->next; // 头指针指向下一个进程
}
// 找到下一个可执行的进程
int next = -1;
for (int i = 0; i < processnumber; i++) {
if (process[i].state != F && process[i].arrivetime <= Time && process[i].remaintime > 0) {
next = i;
break;
}
}
// 执行下一个进程
if (next != -1 && next != current) { // 如果有下一个进程可执行
if (process[current].state == R) { // 如果当前进程正在执行
process[current].state = W;
process[current].waittingtime += count;
}
current = next;
process[current].state = R;
if (process[current].remaintime == process[current].runningtime) { // 第一次执行
process[current].waittingtime = Time - process[current].arrivetime;
}
process[current].remaintime--;
count = 1;
head = &process[current]; // 头指针指向当前进程
}
else { // 如果没有下一个进程可执行
if (process[current].state == R) {
process[current].remaintime--;
count++;
}
else if (process[current].state == W) {
process[current].waittingtime++;
}
}
Time += 1;
}
// 按进程名排序
for (int i = 0; i < processnumber - 1; i++) {
for (int j = i + 1; j < processnumber; j++) {
if (process[j].name < process[i].name) {
swap(process[j], process[i]);
}
}
}
// 输出结果
cout << "Process\tPriority\tState\tArrive Time\tRunning Time\tFinish Time\tTurnaround Time\tWeighted Turnaround Time\tWaiting Time" << endl;
for (int i = 0; i < processnumber; i++) {
cout << process[i].name << "\t" << process[i].prior << "\t\t" << process[i].state << "\t" << process[i].arrivetime << "\t\t" << process[i].runningtime << "\t\t" << process[i].finishtime << "\t\t" << process[i].roundtime << "\t\t\t" << process[i].weighttime << "\t\t\t" << process[i].waittingtime << endl;
}
}
int main() {
// 输入数据
cout << "Please input the number of processes: ";
cin >> processnumber;
cout << "Please input the time slice: ";
cin >> timeslice;
for (int i = 0; i < processnumber; i++) {
cout << "Please input the name of process " << i + 1 << ": ";
cin >> process[i].name;
cout << "Please input the priority of process " << i + 1 << ": ";
cin >> process[i].prior;
cout << "Please input the arrival time of process " << i + 1 << ": ";
cin >> process[i].arrivetime;
cout << "Please input the running time of process " << i + 1 << ": ";
cin >> process[i].runningtime;
process[i].remaintime = process[i].runningtime;
}
// 执行时间片轮转调度算法
RR();
return 0;
}
阅读全文