我正在尝试在 C++ 中实现 FCFS CPU 调度。我有以下代码,它运行时没有错误,但给了我一些数字的“垃圾数据”,例如除第一个计算的所有进程的等待时间、响应时间和周转时间的值
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
class ProcessInfo
{
public:
string ProcessID;
int BurstTime;
int Arrival;
int WaitingTime;
int ResponseTime;
int TurnaroundTime;
};
int main()
{
ProcessInfo P1, P2, P3 = ProcessInfo();
ProcessInfo Array[3] = {P1, P2, P3};
for(int i = 0; i < 3; i++)
{
cout<<"Please enter Process ID for process "<<i<<endl;
cin>>Array[i].ProcessID;
cout<<"Please enter Burst Time for process "<<i<<endl;
cin>>Array[i].BurstTime;
cout<<"Please enter Arrival Time for process "<<i<<endl;
cin>>Array[i].Arrival;
}
if (Array[0].Arrival < Array[1].Arrival && Array[0].Arrival < Array[2].Arrival)
{
if (Array[1].Arrival < Array[2].Arrival)
{
P1.WaitingTime = 0;
P1.ResponseTime = 0;
P1.TurnaroundTime = P1.BurstTime;
P2.WaitingTime = P1.TurnaroundTime - (P2.Arrival - P1.Arrival);
P2.ResponseTime = 0;
P2.TurnaroundTime = P2.WaitingTime + P2.BurstTime;
P3.WaitingTime = P2.TurnaroundTime - (P3.Arrival - P2.Arrival);
P3.ResponseTime = P2.TurnaroundTime - (P3.Arrival - P2.Arrival) ;
P3.TurnaroundTime = P3.WaitingTime + P3.BurstTime;
cout<<"Order of processes is: P1, P2, P3"<<endl;
cout<<"The waiting time for P1: "<< P1.WaitingTime<<endl;
cout<<"The response time for P1: "<<P1.ResponseTime<<endl;
cout<<"The Turnaround time for P1: "<<P1.TurnaroundTime<<endl;
cout<<"The waiting time for P2: "<< P2.WaitingTime<<endl;
cout<<"The response time for P2: "<<P2.ResponseTime<<endl;
cout<<"The Turnaround time for P2: "<<P2.TurnaroundTime<<endl;
cout<<"The waiting time for P3: "<< P3.WaitingTime<<endl;
cout<<"The response time for P3: "<<P3.ResponseTime<<endl;
cout<<"The Turnaround time for P3: "<<P3.TurnaroundTime<<endl;
}
else
if (Array[2].Arrival < Array[1].Arrival)
{
P1.WaitingTime = 0;
P1.ResponseTime = 0;
P1.TurnaroundTime = P1.BurstTime;
P3.WaitingTime = P1.TurnaroundTime - (P3.Arrival - P1.Arrival);
P3.ResponseTime = P3.WaitingTime;
P3.TurnaroundTime = P3.WaitingTime + P3.BurstTime;
P2.WaitingTime = P3.TurnaroundTime - (P2.Arrival - P3.Arrival);
P2.ResponseTime = P2.WaitingTime;
P2.TurnaroundTime = P2.WaitingTime + P2.BurstTime;
cout<<"Order of processes is: P1, P3, P2"<<endl;
cout<<"The waiting time for P1: "<< P1.WaitingTime<<endl;
cout<<"The response time for P1: "<<P1.ResponseTime<<endl;
cout<<"The Turnaround time for P1: "<<P1.TurnaroundTime<<endl;
cout<<"The waiting time for P3: "<< P3.WaitingTime<<endl;
cout<<"The response time for P3: "<<P3.ResponseTime<<endl;
cout<<"The Turnaround time for P3: "<<P3.TurnaroundTime<<endl;
cout<<"The waiting time for P2: "<< P2.WaitingTime<<endl;
cout<<"The response time for P2: "<<P2.ResponseTime<<endl;
cout<<"The Turnaround time for P2: "<<P2.TurnaroundTime<<endl;
}
}
else
if(Array[1].Arrival < Array[0].Arrival && Array[1].Arrival < Array[2].Arrival)
{
if (Array[2].Arrival < Array[0].Arrival)
{
P2.WaitingTime = 0;
P2.ResponseTime = 0;
P2.TurnaroundTime = P2.BurstTime;
P3.WaitingTime = P2.TurnaroundTime - (P3.Arrival - P2.Arrival);
P3.ResponseTime = P3.WaitingTime;
P3.TurnaroundTime = P3.WaitingTime + P3.BurstTime;
P1.WaitingTime = P3.TurnaroundTime - (P1.Arrival - P3.Arrival);
P1.ResponseTime = P1.WaitingTime;
P1.TurnaroundTime = P1.WaitingTime + P1.BurstTime;
cout<<"Order of processes is: P2, P3, P1"<<endl;
cout<<"The waiting time for P2: "<< P2.WaitingTime<<endl;
cout<<"The response time for P2: "<<P2.ResponseTime<<endl;
cout<<"The Turnaround time for P2: "<<P2.TurnaroundTime<<endl;
cout<<"The waiting time for P3: "<< P3.WaitingTime<<endl;
cout<<"The response time for P3: "<<P3.ResponseTime<<endl;
cout<<"The Turnaround time for P3: "<<P3.TurnaroundTime<<endl;
cout<<"The waiting time for P1: "<< P1.WaitingTime<<endl;
cout<<"The response time for P1: "<<P1.ResponseTime<<endl;
cout<<"The Turnaround time for P1: "<<P1.TurnaroundTime<<endl;
}
else
if (Array[0].Arrival < Array[2].Arrival)
{
P2.WaitingTime = 0;
P2.ResponseTime = 0;
P2.TurnaroundTime = P2.BurstTime;
P1.WaitingTime = P2.TurnaroundTime - (P1.Arrival - P2.Arrival);
P1.ResponseTime = P1.WaitingTime;
P1.TurnaroundTime = P1.WaitingTime + P1.BurstTime;
P3.WaitingTime = P1.TurnaroundTime - (P3.Arrival - P1.Arrival);
P3.ResponseTime = P3.WaitingTime;
P3.TurnaroundTime = P3.WaitingTime + P3.BurstTime;
cout<<"Order of processes is: P2, P1, P3"<<endl;
cout<<"The waiting time for P2: "<< P2.WaitingTime<<endl;
cout<<"The response time for P2: "<<P2.ResponseTime<<endl;
cout<<"The Turnaround time for P2: "<<P2.TurnaroundTime<<endl;
cout<<"The waiting time for P1: "<< P1.WaitingTime<<endl;
cout<<"The response time for P1: "<<P1.ResponseTime<<endl;
cout<<"The Turnaround time for P1: "<<P1.TurnaroundTime<<endl;
cout<<"The waiting time for P3: "<< P3.WaitingTime<<endl;
cout<<"The response time for P3: "<<P3.ResponseTime<<endl;
cout<<"The Turnaround time for P3: "<<P3.TurnaroundTime<<endl;
}
}
else
if(Array[2].Arrival < Array[1].Arrival && Array[2].Arrival < Array[0].Arrival)
{
if (Array[1].Arrival < Array[0].Arrival)
{
P3.WaitingTime = 0;
P3.ResponseTime = 0;
P3.TurnaroundTime = P1.BurstTime;
P2.WaitingTime = P3.TurnaroundTime - (P2.Arrival - P3.Arrival);
P2.ResponseTime = P2.WaitingTime;
P2.TurnaroundTime = P2.WaitingTime + P2.BurstTime;
P1.WaitingTime = P2.TurnaroundTime - (P1.Arrival - P2.Arrival);
P1.ResponseTime = P1.WaitingTime;
P1.TurnaroundTime = P1.WaitingTime + P1.BurstTime;
cout<<"Order of processes is: P3, P2, P1"<<endl;
cout<<"The waiting time for P3: "<< P3.WaitingTime<<endl;
cout<<"The response time for P3: "<<P3.ResponseTime<<endl;
cout<<"The Turnaround time for P3: "<<P3.TurnaroundTime<<endl;
cout<<"The waiting time for P2: "<< P2.WaitingTime<<endl;
cout<<"The response time for P2: "<<P2.ResponseTime<<endl;
cout<<"The Turnaround time for P2: "<<P2.TurnaroundTime<<endl;
cout<<"The waiting time for P1: "<< P1.WaitingTime<<endl;
cout<<"The response time for P1: "<<P1.ResponseTime<<endl;
cout<<"The Turnaround time for P1: "<<P1.TurnaroundTime<<endl;
}
else
if (Array[0].Arrival < Array[1].Arrival)
{
P3.WaitingTime = 0;
P3.ResponseTime = 0;
P3.TurnaroundTime = P1.BurstTime;
P1.WaitingTime = P3.TurnaroundTime - (P1.Arrival - P3.Arrival);
P1.ResponseTime = P1.WaitingTime;
P1.TurnaroundTime = P1.WaitingTime + P1.BurstTime;
P2.WaitingTime = P1.TurnaroundTime - (P2.Arrival - P1.Arrival);
P2.ResponseTime = P2.WaitingTime;
P2.TurnaroundTime = P2.WaitingTime + P2.BurstTime;
cout<<"Order of processes is: P3, P1, P2"<<endl;
cout<<"The waiting time for P3: "<< P3.WaitingTime<<endl;
cout<<"The response time for P3: "<<P3.ResponseTime<<endl;
cout<<"The Turnaround time for P3: "<<P3.TurnaroundTime<<endl;
cout<<"The waiting time for P1: "<< P1.WaitingTime<<endl;
cout<<"The response time for P1: "<<P1.ResponseTime<<endl;
cout<<"The Turnaround time for P1: "<<P1.TurnaroundTime<<endl;
cout<<"The waiting time for P2: "<< P2.WaitingTime<<endl;
cout<<"The response time for P2: "<<P2.ResponseTime<<endl;
cout<<"The Turnaround time for P2: "<<P2.TurnaroundTime<<endl;
}
}
}