C ++中的FCFS CPU调度程序

时间:2013-10-21 11:30:33

标签: c++ cpu scheduling

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



    }

3 个答案:

答案 0 :(得分:1)

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

这将进行类似

的操作
P1.TurnaroundTime = P1.BurstTime;

未定义。

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

答案 1 :(得分:0)

请勿使用P1P2P3进行计算。而是使用Array[0]Array[1]Array[2]

答案 2 :(得分:-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;


}