C ++中的FCFS CPU调度程序

问题描述 投票:0回答:3

我试图在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;
                    }
            }



    }
c++ cpu scheduling
3个回答
0
投票

不要使用qazxsw poi,qazxsw poi和P1进行计算。而是使用P2P3Array[0]


1
投票

您没有初始化变量Array[1]Array[2],因此这些结构的内容将是未定义的。

这将使操作像

P1

也未定义。

如果您想要良好的起始值,请添加默认构造函数。


-3
投票
P2
© www.soinside.com 2019 - 2024. All rights reserved.