Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello, I'm supposed to write C++ code that creates a Non-Preemptive Scheduler that implements a priority queue using the heap ADT and Manipulates an extensible

Hello, I'm supposed to write C++ code that creates a Non-Preemptive Scheduler that implements a priority queue using the heap ADT and Manipulates an extensible array.

The application should track the following events which occur during each time slice and print these activities:

1. *** Cycle #: c *** at the beginning of each cycle.

2. If the ready queue is empty, the message The CPU is idle should be displayed.

3. Whenever a process is finished executing, the message Process # pid has just terminated should be displayed.

4. If a process is still executing, the message Process # pid is executing. should be displayed.

5. Whenever a new process is created the message Adding job with pid # x and priority p and length t. should be displayed.

6. If no new process is created during a cycle, the message No new job this cycle. should be displayed.

At the end of the simulation, your program should also display the average throughput, number of processes completed per CPU cycle. It should also display the average wait time per process. The wait time of a process is the number of cycles from its creation to when it begins executing. To run your simulation for 1000 time slices (cycles) where the probability that a new process is created during each cycle is 0.2, your program will be executed with these values as command line tokens. Be sure to seed the random number generator using time of day. Do so at the beginning of the main.

This is the source code given to start with which cannot be changed or edited, (the program must be based off of this code):

Single Core Scheduler.cpp

***A application to simulate a non-preemptive scheduler for a single-core CPU

#include

#include

#include

#include

#include "Heap.h"

#include "Heap.cpp"

#include "PCB.h"

using namespace std;

/**

* Single-core processor with non-preemptive scheduling simulator

* @param argc the number of command line arguments

* @param argv an array of array of chanracters containing command line arguments

* argv[0] - this file name

* argv[1] - number of cyles to run the simulation

* argv[2] - the mode: -r or -R for random mode and -f or -F for file mode

* argv[3] - if the mode is random, this entry contains the probability that

* a process is created per cycle and if the simulator is running in

* file mode, this entry contains the name of the file containing the

* the simulated jobs. In file mode, each line of the input file is

* in this format:

*

* @return exit_status

*/

int main(int argc, char** argv)

{

//implement this function

return 0;

}

Heap.cpp

**** An implementation file for a max-heap

#include "Heap.h"

using namespace std;

template

Heap::Heap()

{

//implement this function

}

template

Heap::Heap(std::function fn)

{

//implement this function

}

template

bool Heap::isEmpty() const

{

// implement this function

}

template

void Heap::insert(E item)

{

//implement this function

}

template

E Heap::remove() throw (HeapException)

{

// implement this function

}

template

const E& Heap::peek() const throw (HeapException)

{

//implement this function

}

template

int Heap::size()const

{

//implement this function

}

template

void Heap::swap(int place, int parent)

{

//implement this function

}

template

void Heap::rebuild(int root, int eSize)

{

//implement this function

}

Heap.h

**** Describes the basic operations of a max-heap

#include

#include

#include

#include

#include

#include

#ifndef HEAP_H

#define HEAP_H

using namespace std;

/**

* This class reports Heap exceptions.

*/

class HeapException

{

public:

HeapException(const string& aMessage)

{

message = aMessage;

}

string what() const

{

return message;

}

private:

string message;

};

template

class Heap

{

private:

/**

* A complete tree stored in a vector representing this

* binary heap

*/

vector tree;

/**

* A less than comparator lambda bi-predicate function; that is,

* it compares two elements of this heap when rebuilding it

*/

std::function cmp = nullptr;

/**

* Swaps a parent and child elements of this heap at the specified indices

* @param place an index of the child element on this heap

* @param parent an index of the parent element on this heap

*/

void swap(int place, int parent);

/**

* Rebuilds the heap to ensure that the heap property of the tree is preserved.

* @param root the root index of the sub-tree to be rebuilt

* @param eSize the size of this tree

*/

void rebuild(int root, int eSize);

public:

/**

Constructs an empty heap;

*/

Heap();

/**

* A parameterized constructor

* @param fn - a greater than comparator function

*/

Heap(std::function fn);

/**

Determine whether the heap is empty.

@return true if the heap is empty;

otherwise, it returns false if the tree contains at least one item.

*/

bool isEmpty() const;

/**

Inserts an item into the heap.

@param item the value to be inserted.

@return none

*/

void insert(E item);

/**

An exception is generated if this method is invoked

by an empty heap. The maximum/minimum value is removed

from the heap if the heap is not empty and its effective

size is reduced by 1.

@return the maximum (in the case of a maxheap) or the

minimum (in the case of a minheap) on the heap.

*/

E remove() throw (HeapException);

/**

An exception is generated if this method is invoked

by an empty heap

@return the maximum (in the case of a maxheap) or the

minimum (in the case of a minheap) on the heap.

*/

const E& peek() const throw (HeapException);

/**

@return the size of the heap; the effective size of the

heap.

*/

int size() const;

};

//HEAP_H

#endif

PCB.h

**** Models a process control block.

#include

#include

using namespace std;

class PCB

{

private:

/**

* the process ID

*/

int pid;

/**

* the priority value of this process

*/

int priority;

/**

* running status

*/

int running;

/**

* cycle during which this process was created

*/

int arrived;

/**

* length of time this process will take to execute

*/

int length;

/**

* cycle during which this process begins executing

*/

int start;

/**

* how long the process waits before it begins executing

*/

int wait;

public:

/**

* Creates a simulated job with default values for its parameters.

*/

PCB()

{

priority = 20;

running = 0;

arrived = 0;

length = 0;

}

/**

* Creates a simulated job with the specified parameters.

* @param iD the process id

* @param pVal the priority value

* @param run the running status

* @param arr the arrival time

* @param len the number of cycles this process takes to execute

*/

PCB(int iD, int pVal, int run, int arr, int len)

{

pid = iD;

priority = pVal;

running = run;

arrived = arr;

length = len;

}

/**

* Gives the ID of this job.

* @return the process ID

*/

int getPid() const

{

return pid;

}

/**

* Gives the priority value of this process.

* @return the priority value of this process

*/

int getPriority() const

{

return priority;

}

/**

* Indicates whether this process is executing..

* @return the execution status of this process

*/

bool isExecuting() const

{

return running == 1;

}

/**

* Sets the running status of this job.

*/

void execute()

{

running = 1;

}

/**

* Gives the cycle during which this process was creates

* @return the cycle during which this process was created.

*/

int getArrival() const

{

return arrived;

}

/**

* Gives the number of cycles required to execute this process.

* @return the number of cycles required to execute this process

*/

int getLength() const

{

return length;

}

/**

* Gives the cycle during which this process began executing.

* @return the cycle during which this process began executing

*/

int getStart() const

{

return start;

}

/**

* Sets the cycle during which the process begins executing.

* @param startCycle the cycle during which this process begins executing.

*/

void setStart(int startCycle)

{

start = startCycle;

}

/**

* Gives the number of cycles this process waited before executing.

* @return the number of cycles from the process creation to its execution

*/

int getWait() const

{

return wait;

}

/**

* Sets the wait time for this process

* @param waitTime the number of cycles that this process waited

*/

void setWait(int waitTime)

{

wait = waitTime;

}

/**

* Determines whether two process control blocks are equal.

* @param pcb1 a simulated process control block

* @param pcb2 a simulated process control block

* @return true when the specified process control blocks

* are equal; otherwise, false.

*/

friend bool operator==(const PCB& pcb1, const PCB& pcb2);

/**

* Determines whether two process control blocks are not equal.

* @param pcb1 a simulated process control block

* @param pcb2 a simulated process control block

* @return true when the specified process control blocks

* are not equal; otherwise, false.

*/

friend bool operator!=(const PCB& pcb1, const PCB& pcb2);

/**

* Determines whether the first process control block will

* execute after the second.

* @param pcb1 a simulated process control block

* @param pcb2 a simulated process control block

* @return true when the first process control blocks

* will be executed after the second; otherwise, false.

*/

friend bool operator>(const PCB& pcb1, const PCB& pcb2);

/**

* Determines whether the first process control block will

* execute before the second.

* @param pcb1 a simulated process control block

* @param pcb2 a simulated process control block

* @return true when the first process control blocks

* will be executed before the second; otherwise, false.

*/

friend bool operator<(const PCB& pcb1, const PCB& pcb2);

/**

* Determines whether the first process control block will

* execute after the second or is the same as the second.

* @param pcb1 a simulated process control block

* @param pcb2 a simulated process control block

* @return true when the first process control blocks

* will be executed after the second or is identical to

* the second; otherwise, false.

*/

friend bool operator>=(const PCB& pcb1, const PCB& pcb2);

/**

* Determines whether the first process control block will

* execute before the second or is the same as the second.

* @param pcb1 a simulated process control block

* @param pcb2 a simulated process control block

* @return true when the first process control blocks

* will be executed before the second or is identical to

* the second; otherwise, false.

*/

friend bool operator<=(const PCB& pcb1, const PCB& pcb2);

};

bool operator==(const PCB& pcb1, const PCB& pcb2)

{

return pcb1.priority == pcb2.priority;

}

bool operator!=(const PCB& pcb1, const PCB& pcb2)

{

return !(pcb1 == pcb2);

}

bool operator>(const PCB& pcb1, const PCB& pcb2)

{

if (pcb1.priority < pcb2.priority)

return true;

return false;

}

bool operator<(const PCB& pcb1, const PCB& pcb2)

{

return (pcb1 != pcb2) && !(pcb1 > pcb2);

}

bool operator<=(const PCB& pcb1, const PCB& pcb2)

{

return (pcb1 > pcb2) || (pcb1 == pcb2);

}

bool operator>=(const PCB& pcb1, const PCB& pcb2)

{

return (pcb1 > pcb2) || (pcb1 == pcb2);

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Management Databases And Organizations

Authors: Richard T. Watson

3rd Edition

0471418455, 978-0471418450

More Books

Students also viewed these Databases questions