Example #1
0
void Simulator::executeScheduling()
{
    int counter = 0;

    // Create a ready queue
    display( "OS: preparing all processes" );
    Queue* readyQueue = new Queue();
    for( Program program : programs_ )
    {
        program.prepare();
        readyQueue->push( program );
    }

    // Execute the programs according to RR scheduling
    while( !readyQueue->empty() || !suspendedPrograms_.empty() )
    {
        // Process interrupts
        while( !interrupts_.empty() )
        {
            int interrupt = interrupts_.front();
            interrupts_.pop();

            if( interrupt != 0 )
            {
                Program suspendedProgram = suspendedPrograms_.at( interrupt );
                suspendedPrograms_.erase( interrupt );

                suspendedProgram.prepare();
                readyQueue->push( suspendedProgram );
            }
        }

        // Execute programs in ready queue
        if( !readyQueue->empty() )
        {
            display( "OS: selecting next process" );
            Program program = next(readyQueue);

            if( program.process_control_block().processID == 0)
            {
                program.assign_pid(++counter);
            }

            // Execute the program until it is interupted
            executeProgram(&program);

            if( program.process_control_block().state == RUNNING )
            {
                program.prepare();
                readyQueue->push(program);
            }
        }
        else
        {
            display("OS Idle: Waiting for I/O to complete");
            wait(30); // 30 millisecond idle before checking again
        }
    }
}