我正在创建一个CPU调度程序,我已经使用在线编译器编译并测试了我的代码,一切工作都很好,但是当我将其传输到Microsoft Visual Studio以便制作一个exe时。文件响应时间(RT)的输出未显示任何值。
responseTime和counterTime以前是一个数组,在编译器中工作正常,但是MVS不允许表达式中使用非恒定值,因此我将其设为动态。但是输出仍然相同。
using namespace std;
void waiting_time(struct process a[], int n);
struct process { int process_id, burst_time, waiting_time, arrival_time, remain_time; }arr[100];
int process_finish[100];
int main() {
arr[99].remain_time = 9999;
//No of process in variable n
int n;
cout << "\nPlease enter the Number of Processor: ";
cin >> n;
cout << endl;
system("CLS");
//Take the Burst time for each process by using loop
for (int i = 0; i < n; i++) {
cout << endl;
cout << "Enter the Arrival Time of Process " << i + 1 << " : ";
cin >> arr[i].arrival_time;
//increment the process_id by 1 after each burst_time
arr[i].process_id = i + 1;
cout << "Enter the Burst Time : ";
cin >> arr[i].burst_time;
//copy each process burst_time to another array remain_time[]
arr[i].remain_time = arr[i].burst_time;
}
system("CLS");
waiting_time(arr, n);
system("PAUSE");
return 0;
}
void waiting_time(struct process a[], int n) {
int remain = 0, sum_wait = 0, sum_resp = 0, sum_turn = 0, sum_turnaround = 0, endTime, smallest;
cout << "\n\nProcess Finish\t| Turnaround Time | Waiting Time | Response Time\n\n";
// handle the INDEX of array process_finish.
int process_f = 0;
int* responseTime = new int[n];
int* counterTime = new int[n];
for (int i = 0; i < n; i++) {
responseTime[i] = 0;
counterTime[n] = 0;
}
for (int time = 0; remain != n; time++) {
smallest = 99;
for (int i = 0; i < n; i++) {
if ((a[i].arrival_time <= time) && (a[i].remain_time < a[smallest].remain_time) && (a[i].remain_time > 0)) {
smallest = i;
}
}
if (counterTime[smallest] == 0) {
responseTime[smallest] = time;
counterTime[smallest] = 1;
}
a[smallest].remain_time--;
if (a[smallest].remain_time == 0) {
//to assign a process # which finish the total job
process_finish[process_f] = smallest + 1;
process_f++;
//to assign a process_id
a[smallest].process_id = smallest + 1;
int tt, rt, wt;
//One process complete the total job
remain++;
//Total completion time of process
endTime = time + 1;
//Calculate the TURNaround TIME (completionTime - TT )
tt = endTime - a[smallest].arrival_time;
//Calculate the Waiting Time
wt = a[smallest].waiting_time = tt - a[smallest].burst_time;
//Calculate the Response Time (completionTime - TT ) +++++++++++++
rt = responseTime[smallest] - a[smallest].arrival_time;
if (rt < 0)
rt += abs(rt);
cout << "\n\tP[" << smallest + 1 << "]\t|\t" << tt << "\t|\t" << wt << "\t|\t" << rt;
//For Average Waiting Time
sum_wait += wt;
//For Average Turnaround Time
sum_turn += tt;
//For Average Response Time
sum_resp += rt;
}
}
cout << "\n\nAverage waiting time =" << sum_wait * 1.0 / n;
cout << "\n\nAverage turnaround time =" << sum_turn * 1.0 / n;
cout << "\n\nAverage response time =" << sum_resp * 1.0 / n;
}
如果我们使用输入
number of process= 6 Arrival Burst p1 - 0 7 p2 - 1 5 p3 - 2 3 p4 - 3 1 p5 - 4 2 p6 - 5 1
Response Time
的输出应为-0,0,1,3,0,0
和Average response time = 0.666667
。
但是MVS输出响应时间-0,0,0,0,0,0
和Average response time = 0
。
答案 0 :(得分:0)
发现我的错误:
for (int i = 0; i < n; i++) {
responseTime[i] = 0;
counterTime[n] = 0;
}
一个简单的答案:
for (int i = 0; i < n; i++) {
responseTime[i] = 0;
counterTime[i] = 0;
}