SSD5实验1-Printer-含详细注释

//fifo.cpp要写的只有这个
#include 
using namespace std;
#include "fifo.h"
#include "job.h"

fifo::fifo( int seconds_per_page ) : simulator( seconds_per_page )
{
}

void fifo::simulate( string file )
{
    int aggregate_latency   = 0;
    int complete_jobs       = 0;
    /* 读入文件 */
    loadworkload( file );

    /* 判断栈是否为空 */
    if ( workload.empty() )
    {
        cout << "\n\tThe queue is  empty\n";
    } else {
        /* 开始工作了 */
        cout << "\n\nFIFO Simulaton\n\n";
        /*下一次可以工作的时间,是当前时间加上打印的耗时 */
        int next_time = 0;

        /* 工作台和等待队列均不为空时执行for循环*/
        for ( int time = 0; !(workload.empty() && waiting.empty() ); time++ )
        {
            /*
             * Look at the workload, enqueue job, if it is time
             * 如果工作台列队不为空而且当前时间大于等于工作台的第一个event到来时间,就把它放入等待队列
             */
            while ( !workload.empty() && time >= workload.front().arrival_time() )
            {
                /* 工作台队头加入到等待队列 */
                event e = workload.front();
                workload.pop();
                waiting.push( e );
                job j = e.getjob();
                /* 正在等待打印 */
                cout << "\tArriving: " << j << " at " << time << " seconds\n";
            }
            /* 当前时间小于下一个到达的时继续for循环*/
            if ( time < next_time )
            {
                continue;
            }
            /* 等待列队不为空则打印 */
            if ( !waiting.empty() )
            {
                /* 打印waiting队头元素 */
                event   e   = waiting.front();
                job j   = e.getjob();
                /* 打印时间 */
                cout << "\tServicing: " << j << " at " << time << " seconds\n";
                /* waiting队头元素出列表示完成 */
                waiting.pop();
                /*下一次循环的时间*/
                next_time = time + (seconds_per_page * e.getjob().getnumpages() );
                /* 总延迟,是当前时间减去event到达的时间*/
                aggregate_latency += (time - e.arrival_time() );
                /* 工作计数*/
                complete_jobs++;
            }
        }
        /*总工作数*/
        cout << "\n\tTotal jobs: " << complete_jobs;
        /* 总延迟*/
        cout << "\n\tAggregate latency: " << aggregate_latency << " seconds";
        /* 平均延迟*/
        cout << "\n\tMean latency: "<< (float) aggregate_latency /(float) complete_jobs << " seconds\n";
    }
}

你可能感兴趣的:(SSD5实验1-Printer-含详细注释)