Assume you have a computer system with the following simple characteristics:
* The system is capable of multiprogramming, in that it can execute two jobs (programs) at the same time (memory and other resources needed by the jobs is not considered a factor in the solving of this problem).
* Jobs are submitted to the system in a random timeframe (whenever they are submitted by users), yet due to the system having only one input device, at most one job can be submitted each second.
* When a job is submitted for execution, the user specifies an execution priority (a number from 0 to 3, where 0 is the highest priority, and 3 is the lowest priority). A job should be scheduled for execution based on this priority; that is, jobs already submitted and waiting for execution with a higher priority should execute before jobs with a lower priority.
You are to write a C++ program that will simulate this job submission and execution environment by providing a list of jobs to be executed, and then simulate the execution sequence of these jobs. The information you are to provide for each job is:
submission_time jobname priority execution_time
These fields are defined as:
* submission_time - the time when this job is submitted to the system for execution. It is an integer that represents a time (in seconds) counting from 0 (when the simulation begins).
* jobname - a string that represents the name of the current job.
* priority - an integer in the range of 0 to 3.
* execution_time - the time it will take to execute this job. NOTE that we normally wouldn't know this information, but we are using this as part of the simulation to determine the execution time for the job.
You can assume that there will be at most 20 jobs submitted for the simulation, and they will occur within the first 20 seconds of the simulation.
Your output will consist of a report that is based on each execution second of the system, starting from time 0. As jobs are submitted for execution, they are to initially be placed on a waiting queue (one queue for each execution priority). As execution space becomes available (remember we are limited to only running two jobs at a time), items are to be removed from the waiting queues in priority order; that is, jobs with a higher priority are to be executed first, no matter when they were submitted.
At the beginning of each execution second, your report should include information about the current jobs that are running (jobName and point of execution: how many seconds the job has been executing out of its total execution time), and information about each wait queue (a list of jobNames waiting on each queue, with next-to-be-processed listed first).
Following this initial report for each second, all activity should be listed, such as when jobs are removed from queues, when jobs begin execution, when jobs end execution, and when new jobs are submitted and added to an appropriate queue. The order in which the various activities should be processed for each second is:
1. Display of initial status for the jobs executing, and the jobs on the wait queues.
2. If it is time to process a new input job submitted by a user, add the job to the appropriate queue
3. For jobs that have completed execution, remove them from the execution list.
4. If a new job can be run (one or no jobs are currently executing), select highest priority job in wait queues and begin its execution
You are to continue to execute the simulation until all user-submitted jobs have completed their execution.
Below is a sample simulation based on the data shown below:
0 A 2 6
2 B 3 4
4 SYS 0 3
5 C 3 2
7 D 3 2
8 E 3 2
Second 0:
Running: No Jobs
Queue 0: Empty Queue 1: Empty Queue 2: Empty Queue 3: Empty
Processing next input job: Adding A to Queue 2
Removing A from Queue 2 and beginning execution.
Second 1:
Running: Job A (1/6 sec elapsed)
Queue 0: Empty Queue 1: Empty Queue 2: Empty Queue 3: Empty
Second 2:
Running: Job A (2/6 sec elapsed)
Queue 0: Empty Queue 1: Empty Queue 2: Empty Queue 3: Empty
Processing next input job: Adding B to Queue 3
Removing B from Queue 3 and beginning execution.
Second 3:
Running: Job A (3/6 sec elapsed) Job B (1/4 sec elapsed)
Queue 0: Empty Queue 1: Empty Queue 2: Empty Queue 3: Empty
Second 4:
Running: Job A (4/6 sec elapsed) Job B (2/4 sec elapsed)
Queue 0: Empty Queue 1: Empty Queue 2: Empty Queue 3: Empty
Processing next input job: Adding SYS to Queue 0
Second 5:
Running: Job A (5/6 sec elapsed) Job B (3/4 sec elapsed)
Queue 0: SYS Queue 1: Empty Queue 2: Empty Queue 3: Empty
Processing next input job: Adding C to Queue 3
Second 6:
Running: Job A (6/6 sec elapsed) Job B (4/4 sec elapsed)
Queue 0: SYS Queue 1: Empty Queue 2: Empty Queue 3: C
Job A completed execution.
Job B completed execution.
Removing SYS from Queue 0 and beginning execution.
Removing C from Queue 3 and beginning execution.
Second 7:
Running: Job SYS (1/3 sec elapsed) Job C (1/2 sec elapsed)
Queue 0: Empty Queue 1: Empty Queue 2: Empty Queue 3: Empty
Processing next input job: Adding D to Queue 3
Second 8:
Running: Job SYS (2/3 sec elapsed) Job C (2/2 sec elapsed)
Queue 0: Empty Queue 1: Empty Queue 2: Empty Queue 3: D
Processing next input job: Adding E to Queue 3
Job C completed execution.
Removing D from Queue 3 and beginning execution.
Second 9:
Running: Job SYS (3/3 sec elapsed) Job D (1/2 sec elapsed)
Queue 0: Empty Queue 1: Empty Queue 2: Empty Queue 3: E
Job SYS completed execution.
Removing E from Queue 3 and beginning execution.
Second 10:
Running: Job E (1/2 sec elapsed) Job D (2/2 sec elapsed)
Queue 0: Empty Queue 1: Empty Queue 2: Empty Queue 3: Empty
Job D completed execution.
Second 11:
Running: Job E (2/2 sec elapsed)
Queue 0: Empty Queue 1: Empty Queue 2: Empty Queue 3: Empty
Job E completed execution.
Second 12:
Running: No Jobs
Queue 0: Empty Queue 1: Empty Queue 2: Empty Queue 3: Empty
Nothing executing, nothing queued, all jobs submitted, terminating simulation
WHERE DO I GO FROM HERE?!?!? HELP ME PLEASE?!
cpp
#include <iostream>
#include "queue.h"
using namespace std;
int main() {
Queue queue_0, queue_1, queue_2, queue_3;
string jobName;
int priority;
int execTime;
int SubmittedJobsItemType;
int submittedJobs[20];
int submission_time;
for(int i=0; i<3; i++) {
cout<<"Give submission Time: "<< endl;
cin>>submission_time;
cout<<"Give jobname: "<<endl;
cin>> jobName;
cout<<"Give priority: "<<endl;
cin>>priority;
cout<<"Give execution time:"<<endl;
cin>> execTime;
switch(priority) {
case 0: queue_0.enqueue(submission_time, jobName, execTime, priority);
case 1: queue_1.enqueue(submission_time, jobName, execTime, priority);
case 2: queue_2.enqueue(submission_time, jobName, execTime, priority);
case 3: queue_3.enqueue(submission_time, jobName, execTime, priority);
}//endSWITCH
}//endFOR
return 0;
} // end main
// *************************************************************
// Implementation file queue.cpp - Pointer-based implementation
// *************************************************************
#include "queue.h" // header file
Queue::Queue() {
backPtr = frontPtr = NULL;
} // end default constructor
Queue::~Queue() {
while (!isEmpty())
dequeue();
} // end destructor
bool Queue::isEmpty() {
return backPtr == NULL;
} // end isEmpty
void Queue::enqueue(QueueItemType newItem, string newstr,QueueItemType execT, QueueItemType pri) {
// create a new node
QueueNode *newPtr = new QueueNode;
// set data portion of new node
newPtr->item = newItem; newstr; execT; pri;
newPtr->str =newstr;
newPtr->execTime= execT;
newPtr->priority =pri;
newPtr->next = NULL;
// insert the new node
if (isEmpty()) // insertion into empty queue
frontPtr = newPtr;
else // insertion into nonempty queue
backPtr->next = newPtr;
backPtr = newPtr; // new node is at back
} // end enqueue
bool Queue::dequeue() {
if (isEmpty()) return false;
// queue is not empty; remove front
QueueNode *tempPtr = frontPtr;
if (frontPtr == backPtr) { // special case?
// yes, one node in queue
frontPtr = NULL;
backPtr = NULL;
}
else frontPtr = frontPtr->next;
tempPtr->next = NULL;
delete tempPtr;
return true;
} // end dequeue
bool Queue::dequeue(QueueItemType& queueFront) {
if (isEmpty()) return false;
// queue is not empty; retrieve front
queueFront = frontPtr->item;
dequeue(); // delete front
return true;
} // end dequeue
bool Queue::getFront(QueueItemType& queueFront) {
if (isEmpty()) return false;
// queue is not empty; retrieve front
queueFront = frontPtr->item;
return true;
} // end getFront
// End of queue.cpp
// ********************************************************
// Header file queue.h - Pointer-based implementation.
// ********************************************************
typedef int QueueItemType;
class Queue {
public:
// constructors and destructor:
Queue(); // default constructor
~Queue(); // destructor
// Queue operations:
bool isEmpty();
// Determines whether the queue is empty.
// Precondition: None.
// Postcondition: Returns true if the queue is empty;
// otherwise returns false.
void enqueue(QueueItemType newItem, string newstr,QueueItemType execT, QueueItemType pri);
// Inserts an item at the back of a queue.
// Precondition: newItem is the item to be inserted.
// Postcondition: If the insertion is successful, newItem
// is at the back of the queue.
bool dequeue();
// Dequeues the front of a queue.
// Precondition: None.
// Postcondition: If the queue is not empty, the item
// that was added to the queue earliest is deleted
// and returns true; if queue is empty, returns false
bool dequeue(QueueItemType& queueFront);
// Retrieves and deletes the front of a queue.
// Precondition: None.
// Postcondition: If the queue is not empty, queueFront
// contains the item that was added to the queue
// earliest, and the item is deleted and returns
// true; if queue is empty, returns false
bool getFront(QueueItemType& queueFront);
// Retrieves the item at the front of a queue.
// Precondition: None.
// Postcondition: If the queue is not empty, queueFront
// contains the item that was added to the queue
// earliest and returns true; if queue is empty,
// returns false
private:
// The queue is implemented as a linked list
// with one external pointer to the front of the queue
// and a second external pointer to the back of the
// queue.
struct QueueNode {
QueueItemType item, priority, execTime;
string str;
QueueNode *next;
}; // end struct
QueueNode *backPtr;
QueueNode *frontPtr;
}; // end class
*edit: Please use code tags in the future, thanks!
This post has been edited by Martyr2: 13 Jul, 2008 - 05:39 PM