Kyle777w 0 Newbie Poster

Hello, I've got a question about a project in my operating systems class. The task was to create a program that lets the user choose a variety of cpu scheduling algorithms and simulates them with random data or data from a file. I've already done it for first come first serve, shortest job first, and priority. I've got the round robin code to work I think I just have no idea how to calculate the average wait time (AWT) or average turn around time(ATT).

Here is my code for FCFS algorithm:

void FCFS(job jobList[iterationsToRunTest][numJobs], results &output)
{
    queue <job> q;
    for(int n=0; n < iterationsToRunTest; n++)
    {
        stable_sort(jobList[n],jobList[n]+ numJobs, GreaterArrivalTime());
    } //sort all of the iterations by arrival time.
    double arrayOfAWT[iterationsToRunTest];
    double arrayOfATT[iterationsToRunTest];

    //Run FCFS test code.
    for(int n=0; n < 1 ; n++)
    {
        for(int i=0; i < numJobs;i++)
        {
            q.push(jobList[n][i]);
        } //add jobs to queue based on the arival time.

        int accumBurst = 0;
        int sum = 0;
        //Variables to do the calculations part of the algorithm.

        while(q.empty() == false)
        {
            sum = sum + (accumBurst - q.front().at);
            accumBurst+=q.front().cpu;
            q.pop(); //remove that job from queue.
        } //end while loop that covers individual queue

        //do avg awt and att calcs for given iteration and store it.
        arrayOfAWT[n] = double(sum) / double(numJobs);
        arrayOfATT[n] = double((accumBurst + sum)) / double(numJobs);

    } // end iterations for loop

    output.FCFS_run1.AWT = arrayOfAWT[0];
    output.FCFS_run2.AWT = arrayOfAWT[1];
    output.FCFS_run100.AWT = arrayOfAWT[99];
    output.FCFS_run1.ATT = arrayOfATT[0];
    output.FCFS_run2.ATT = arrayOfATT[1];
    output.FCFS_run100.ATT = arrayOfATT[99];

    double temp = 0;
    double tempATT = 0;

    for(int n=0; n < numJobs; n++)
    {
        temp = temp + arrayOfAWT[n];  //Sum the AWTs
        tempATT = tempATT + arrayOfATT[n]; //Sum all AWTs
    }

    temp = temp / numJobs; //Average of all the AWTs.
    tempATT = tempATT / numJobs;

    output.FCFS_total.AWT = temp;
    output.FCFS_total.ATT = tempATT;

    cout << "press enter to continue" << endl;
    cin.get();
}

Each function takes a 2d array of structs of type job where the first number inside the [] symbol is current iteration of the test (example run test 100 times) second number inside [] is specific job. Standard for this project is 100 iterations of 100 jobs.

job struct is as follows:

struct job
{
    int cpu;
    int at;
    int priority;
    int id;
};

Here is the round robin part I've been having problems with:

void roundRobin(job jobList[iterationsToRunTest][numJobs],
                                        results &output, int quantum)
{
    queue <job> q;
    for(int n=0; n < iterationsToRunTest; n++)
    {
        stable_sort(jobList[n],jobList[n]+ numJobs, GreaterArrivalTime());
    } //sort all of the iterations by arrival time.

    for(int n=0; n < iterationsToRunTest ; n++)
    {
        list<int> gantChartList;
        //Were be using a list to store the values found along the bottom
        //of the gant chart if donig this by hand, we need a dynamic data
        //structure though becuase we don't know how many cells will be in
        //the gant chart because incomplete jobs are re added to the queue.


        for(int i=0; i < numJobs;i++)
        {
            q.push(jobList[n][i]);
        } //add jobs to queue based on the arival time.

        //Fun ronud robin.
        while(q.empty() == false)
        {
            int tempCPU = q.front().cpu;
            tempCPU= tempCPU - quantum;

            gantChartList.push_back(tempCPU);

            if(tempCPU <= 0) //pop job if it finished in one quantum
            {
                gantChartList.push_back(q.front().cpu);
                //Add the time the process ran to gant chart list

                q.pop();
            }
            else //else job isn't done add to back of queue.
            {
                job tempJob = q.front();
                tempJob.cpu = tempCPU;

                gantChartList.push_back(tempCPU);

                q.push(tempJob);
                q.pop();
            }
        }
    }

    cout << "round robin finished" << endl;
    cout << "press enter to continue" << endl;
    cin.get();
}

Baically I want to use as similar logic as possible from the FCFS algorithm for round robin, you can see I've tryed to use a list but I don't think thats the best approach anymore. There is alot more code in the project than what I have posted here but this code I posted is the individual algorithms everything I didn't post is mostly file input, random number generation, printing, or menu oriented so isn't really apllicatable.

Any help figureing out how to calculate AWT and ATT would be appreciated, and this project is due Thursday Nov 7 so any help after that isn't really helpfull!

Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.