0

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



    }
4

3 回答 3

1

您没有初始化变量P1and P2,因此这些结构的内容将是未定义的。

这将使操作像

P1.TurnaroundTime = P1.BurstTime;

也未定义。

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

于 2013-10-21T11:33:52.127 回答
0

不要使用P1,P2P3进行计算。而是使用Array[0],Array[1]Array[2]为它。

于 2013-11-24T09:13:46.790 回答
-3
#include<iostream>

#include<cstdio>

#include<cstdlib>

int process,*q,i,slice,a=0,at2[10],to[10];

void takeprocess()

{

    printf("Enter the total number of process\n");

    scanf("%d",&process);


}


void takecputime()

{

    q=(int*)calloc(process,(sizeof(int)));

for(i=1;i<=process;i++)

{

printf("Enter the CPU burst time for the process P%d : ",i);

   scanf("%d",q+i);

   to[i]=at2[i]=*(q+i);

}

}

void checkfcfs()

{
 int wt1[process];

    for(i=1;i<process;i++)

    {

        a=wt1[i]=*(q+i)+a;

    }

    a=0;

    printf("the waiting time for process P1 is : 0\n");

    for(i=1;i<process;i++)

    {

   a=wt1[i]+a;

        printf("the waiting time for process P%d is : %d\n",i+1,wt1[i]);
    }

    a=a/process;

    printf("the average waiting time for processes are :%d\n",a);

    printf("the sequence of the processes are as follows\n");

    for(i=1;i<=process;i++)

    {

        printf("P%d\n",i);

    }


}

using namespace std;

int main()

{

             takeprocess();

             takecputime();

             checkfcfs();

             return 0;


}
于 2018-09-02T11:09:38.787 回答