(Shell) Operating system task scheduling simulation // //-----------------------
ID: 3822386 • Letter: #
Question
(Shell) Operating system task scheduling simulation
//
//--------------------------------------------------------------------
// Simulates an operating system's use of a priority queue to regulate
// access to a system resource (printer, disk, etc.).
#include <iostream>
#include <cstdlib>
#include "PriorityQueue.cpp"
#include <time.h>
using namespace std;
//--------------------------------------------------------------------
//
// Declaration for the task data struct
//
struct TaskData
{
int getPriority () const
{ return priority; } // Returns the priority. Needed by the heap.
int priority, // Task's priority
arrived; // Time when task was enqueued
};
//--------------------------------------------------------------------
int main()
{
PriorityQueue<TaskData, int, Less<int> > taskPQ; // Priority queue of tasks
TaskData task; // Task
int simLength, // Length of simulation (minutes)
minute, // Current minute
numPtyLevels, // Number of priority levels
numArrivals, // Number of new tasks arriving
j; // Loop counter
// Seed the random number generator
srand((unsigned int)time(NULL));
cout << endl << "Enter the number of priority levels : ";
cin >> numPtyLevels;
cout << "Enter the length of time to run the simulator : ";
cin >> simLength;
for (minute = 0; minute < simLength; minute++)
{
// Dequeue the first task in the queue (if any).
// Your code here
// Determine the number of new tasks and add them to
// the queue.
// Your code here
}
return 0;
}
Explanation / Answer
for ( minute = 0 ; minute < simLength ; minute++ )
{
// Dequeue the first task in the queue (if any).
// Your code here
if(!taskPQ.isEmpty())
{
task = taskPQ.dequeue();
cout << "At " << minute << " dequeued : ";
cout << task.priority << " " << task.arrived << " " <<
(minute - task.arrived) << endl;
}
// Determine the number of new tasks and add them to
// the queue.
// Your code here
switch(rand() % 4)
{
case 1:
if(!taskPQ.isFull())
{
task.priority = rand() % numPtyLevels;
task.arrived = minute;
taskPQ.enqueue(task);
}
break;
case 2:
for(j = 0; j < 2 && !taskPQ.isFull(); j++)
{
task.priority = rand() % numPtyLevels;
task.arrived = minute;
taskPQ.enqueue(task);
}
break;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.