C++ 使用堆和进程控制块创建调度模拟器
因此,我试图创建一个基于优先级值的调度模拟器。我有一个如下所示的进程控制块头,它有所有用于调度的必要值。我的问题是,当我将PCB存储到主循环的堆中时,我不知道如何按优先级值组织堆,-20是优先级中的第一个,19是最后一个。似乎当我将PCB存储在堆中时,它不会按任何值进行组织,当我选择删除或查看某个值时,它只是有一个随机输出。任何帮助都将不胜感激!(如果您需要我进一步解释,请发表评论!) Heap.h:C++ 使用堆和进程控制块创建调度模拟器,c++,heap,scheduling,simulator,C++,Heap,Scheduling,Simulator,因此,我试图创建一个基于优先级值的调度模拟器。我有一个如下所示的进程控制块头,它有所有用于调度的必要值。我的问题是,当我将PCB存储到主循环的堆中时,我不知道如何按优先级值组织堆,-20是优先级中的第一个,19是最后一个。似乎当我将PCB存储在堆中时,它不会按任何值进行组织,当我选择删除或查看某个值时,它只是有一个随机输出。任何帮助都将不胜感激!(如果您需要我进一步解释,请发表评论!) Heap.h: #include <string> #include <iostre
#include <string>
#include <iostream>
#include <stdexcept>
#include <vector>
#ifndef HEAP_H
#define HEAP_H
using namespace std;
/**
* This class reports Heap exceptions.
*/
class HeapException
{
private:
/**
* Reason for the exception
*/
string message;
public:
/**
* Constructs a HeapException
* @param aMessage why the exception occurred
*/
HeapException(const string& aMessage)
{
message = aMessage;
}
/**
* Gives a reason for this exception
* @return why the this exception occurred
*/
string what() const
{
return message;
}
};
template <typename E>
class Heap
{
private:
/**
* A complete tree stored in a vector representing this
* binary heap
*/
vector<E> tree;
/**
* 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 subtree to be rebuilt
* @param eSize the size of this tree
*/
void reheapify(int root, int eSize);
public:
/**
Constructs an empty heap;
*/
Heap<E>();
/**
destructor - returns the heap memory to the system;
*/
~Heap<E>();
/**
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
#包括
#包括
#包括
#包括
#ifndef堆
#定义堆
使用名称空间std;
/**
*此类报告堆异常。
*/
类HeapException
{
私人:
/**
*例外情况的原因
*/
字符串消息;
公众:
/**
*构造一个HeapException
*@param aMessage发生异常的原因
*/
HeapException(常量字符串和aMessage)
{
消息=消息;
}
/**
*给出此异常的原因
*@return此异常发生的原因
*/
字符串what()常量
{
返回消息;
}
};
模板
类堆
{
私人:
/**
*一个完整的树,存储在一个表示
*二进制堆
*/
向量树;
/**
*在指定的索引处交换此堆的父元素和子元素
*@param在此堆上放置子元素的索引
*@param parent此堆上父元素的索引
*/
无效交换(整数位置,整数父项);
/**
*重建堆以确保保留树的heap属性。
*@param root要重建的子树的根索引
*@param设置此树的大小
*/
void-reheapify(int-root,int-eSize);
公众:
/**
构造一个空堆;
*/
Heap();
/**
析构函数-将堆内存返回给系统;
*/
~Heap();
/**
确定堆是否为空。
@如果堆为空,则返回true;
否则,如果树至少包含一项,则返回false。
*/
bool isEmpty()常量;
/**
将项插入堆中。
@param item要插入的值。
@一无所获
*/
无效插入(E项);
/**
如果调用此方法,将生成异常
通过空堆。最大值/最小值被删除
如果堆不为空且其有效
大小减少1。
@返回最大值(对于maxheap)或
堆上的最小值(对于minheap)。
*/
E移除()抛出(HeapException);
/**
如果调用此方法,将生成异常
空空如也
@返回最大值(对于maxheap)或
堆上的最小值(对于minheap)。
*/
常量E&peek()常量抛出(HeapException);
/**
@返回堆的大小;堆的有效大小
堆
*/
int size()常量;
};
//堆
#恩迪夫
Heap.cpp:
#include "Heap.h"
using namespace std;
template <typename E>
Heap<E>::Heap()
{
// compiler-generated code .. no need to implement this
}
template <typename E>
Heap<E>::~Heap()
{
while(tree.size() > 0)
tree.pop_back();
}
template <typename E>
bool Heap<E>::isEmpty() const
{
if (tree.empty())
return true;
else return false;
}
template<typename E>
void Heap<E>::insert(E item)
{
tree.push_back(item);
int place = tree.size() - 1;
int parent = place / 2;
if (place % 2 == 0)
parent = parent - 1;
while (parent >= 0 && tree[place] > tree[parent]){
swap(place,parent);
place = parent;
if (place % 2 == 0)
int parent = (place / 2) - 1;
else
parent = place / 2;
}
}
template<typename E>
E Heap<E>::remove() throw (HeapException)
{
if (isEmpty() == false){
int child = tree.size() - 1;
swap(child, 0);
E number = tree.back();
reheapify(0, tree.size());
return number;}
else {
throw ("Attempted to remove from empty Heap");
}
}
template<typename E>
const E& Heap<E>::peek() const throw (HeapException)
{
if (isEmpty() == false)
return tree[0];
else throw ("Attempted to peek empty Heap");
}
template<typename E>
int Heap<E>::size()const
{
return tree.size;
}
template<typename E>
void Heap<E>::swap(int place, int parent)
{
E temp;
temp = tree[place];
tree[place] = tree[parent];
tree[parent] = temp;
}
template<typename E>
void Heap<E>::reheapify(int root, int eSize)
{
int child = 2 * root + 1;
int right = 2 * root + 2;
if (child < eSize){
if (right < eSize && tree[right] < tree[child]){
child = child + 1;
}
if (tree[root] < tree[child]){
swap(root, child);
reheapify(child, eSize);
}
}
}
#包括“Heap.h”
使用名称空间std;
模板
Heap::Heap()
{
//编译器生成的代码..无需实现此
}
模板
堆::~Heap()
{
while(tree.size()>0)
tree.pop_back();
}
模板
布尔堆::isEmpty()常量
{
if(tree.empty())
返回true;
否则返回false;
}
模板
空堆::插入(E项)
{
树。推回(项目);
int place=tree.size()-1;
int parent=place/2;
如果(位置%2==0)
父母=父母-1;
而(父级>=0&&tree[place]>tree[parent]){
掉期(地点、母公司);
地点=父母;
如果(位置%2==0)
int parent=(place/2)-1;
其他的
家长=地点/2;
}
}
模板
E Heap::remove()抛出(HeapException)
{
如果(isEmpty()==false){
int child=tree.size()-1;
交换(子,0);
E number=tree.back();
再适应(0,tree.size());
返回编号;}
否则{
抛出(“试图从空堆中移除”);
}
}
模板
常量E&Heap::peek()常量抛出(HeapException)
{
if(isEmpty()==false)
返回树[0];
else抛出(“试图窥视空堆”);
}
模板
int Heap::size()常量
{
返回树的大小;
}
模板
无效堆::交换(整数位置,整数父级)
{
E温度;
温度=树[地点];
树[位置]=树[父];
树[父]=临时;
}
模板
void Heap::reheapify(int root,int eSize)
{
int child=2*root+1;
int right=2*root+2;
if(child
PCB.h:
#include <string>
#include <iostream>
using namespace std;
class PCB
{
private:
/**
* the process ID
*/
int pid;
/**
* the priority value of this process [-20,19]
*/
int priority;
/**
* the running status of this process
* [0 = not running, 1 = running]
*/
int running;
/**
* the cycle during which this process was created
*/
int arrived;
/**
* the quantum (number of cycles required to execute)
* this process
*/
int length;
/**
* the cycle when this process began running
*/
int start;
/**
* the number of cycles from the creation of this process
* to when it began running
*/
int wait;
public:
/**
* Create a a PCB with the lowest priority
* and a quantum of 0 (default constructor)
*/
PCB()
{
priority = 19;
running = 0;
arrived = 0;
length = 0;
}
/**
* Creates a PCB with the specified parameters.
* @param id the process ID
* @param pVal priority value [-20,19]
* @param run executing status [0 = not running, 1 = running]
* @param arr the cycle during which the process arrived
* @param len the quantum - number of cycles required to execute
* the process
*/
PCB(int id, int pVal, int run, int arr, int len)
{
pid = id;
priority = pVal;
running = run;
arrived = arr;
length = len;
}
/**
* Gives the ID for this process.
* @return the pid of this process
*/
int getPid() const
{
return pid;
}
/**
* Gives the priority value for this process.
* @return the priority of this process
*/
int getPriority() const
{
return priority;
}
/**
* Gives the running status of this process.
* @return the running status of this process
*/
bool isExecuting() const
{
return (running == 1);
}
/**
* Sets the running status of this process to 1.
*/
void execute()
{
running = 1;
}
/**
* Gives the arrival time for this process.
* @return cycle during with this process was created
*/
int getArrival() const
{
return arrived;
}
/**
* Gives the quantum of 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 running.
* @return the cycle during which this process began running
*/
int getStart() const
{
return start;
}
/**
* Sets the start cyle for this process.
* @param startCycle the cycle during which this process began
* running.
*/
void setStart(int startCycle)
{
start = startCycle;
}
/**
* Gives the wait time for this process.
* @return the number of cycles this process waited
* before running.
*/
int getWait() const
{
return wait;
}
/**
* Sets the wait time for this process.
*/
void setWait(int waitTime)
{
wait = waitTime;
}
/**
* Overloaded == operator that determines whether
* two process control blocks are the same.
* @param pcb1 a process control block
* @param pcb2 a process control block
* @return true if the PCBs are the same; otherwise, false
*/
friend bool operator==(const PCB& pcb1, const PCB& pcb2);
/**
* Overloaded != operator that determines whether
* two process control blocks are different.
* @param pcb1 a process control block
* @param pcb2 a process control block
* @return true if the PCBs are different; otherwise, false
*/
friend bool operator!=(const PCB& pcb1, const PCB& pcb2);
/**
* Overloaded > operator that determines whether
* the first PCB has a higher priority than the second.
* @param pcb1 a process control block
* @param pcb2 a process control block
* @return true if the first PCB has a higher priority than the second;
* otherwise, false
*/
friend bool operator>(const PCB& pcb1, const PCB& pcb2);
/**
* Overloaded < operator that determines whether
* the first PCB has a lower priority than the second.
* @param pcb1 a process control block
* @param pcb2 a process control block
* @return true if the first PCB has a lower priority than the second;
* otherwise, false
*/
friend bool operator<(const PCB& pcb1, const PCB& pcb2);
/**
* Overloaded >= operator that determines whether
* the first PCB has the same or higher priority than the second.
* @param pcb1 a process control block
* @param pcb2 a process control block
* @return true if the first PCB has the same or higher priority than
* the second; otherwise, false
*/
friend bool operator>=(const PCB& pcb1, const PCB& pcb2);
/**
* Overloaded <= operator that determines whether
* the first PCB has the same or lower priority than the second.
* @param pcb1 a process control block
* @param pcb2 a process control block
* @return true if the first PCB has the same or lower priority than
* the second; otherwise, false
*/
friend bool operator<=(const PCB& pcb1, const PCB& pcb2);
};
bool operator==(const PCB& pcb1, const PCB& pcb2)
{
return (pcb1.running == pcb2.running &&
pcb1.priority == pcb2.priority &&
pcb1.arrived == pcb2.arrived);
}
bool operator!=(const PCB& pcb1, const PCB& pcb2)
{
return (!(pcb1 == pcb2));
}
bool operator>(const PCB& pcb1, const PCB& pcb2)
{
if (pcb1.running > pcb2.running)
return true;
if (pcb1.running < pcb2.running)
return false;
if (pcb1.priority < pcb2.priority)
return true;
if (pcb1.priority > pcb2.priority)
return false;
if (pcb1.arrived < pcb2.arrived)
return true;
else
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);
}
#包括
#包括
使用名称空间std;
类印刷电路板
{
私人:
/**
*进程ID
*/
int-pid;
/**
*此过程的优先级值[-20,19]
*/
int优先级;
/**
*此进程的运行状态
*[0=未运行,1=运行]
*/
int运行;
/**
*创建此进程的周期
*/
int到达;
/**
*量程(执行所需的周期数)
*这个过程
*/
整数长度;
/**
*此进程开始运行的周期
*/
int启动;
/**
*从创建此进程开始的循环数
*直到它开始运行的时候
*/
等待;
公众:
/**
*创建具有最低优先级的PCB
*和0的量(默认构造函数)
*/
印刷电路板()
{
优先级=19;
运行=0;
到达=0;
长度=0;
}
/**
*使用指定的参数创建PCB。
*@param id进程id
*@param pVal优先级值[-
#include <cstdlib>
#include "Heap.cpp"
#include "PCB.h"
using namespace std;
/*
*
*/
int main(int argc, char** argv) {
int j = 1;
Heap<PCB> a;
for (int i = 0; i < atoi(argv[2]); i++){
double q = ((double) rand() / (RAND_MAX));
double r = atof(argv[1]);
cout<<"*** Cycle #: "<<i+1<<endl;
if (q <= r){
PCB b(j,rand() % 39 + (-20), 0, i + 1, rand() % 100 + 1);
a.insert(b);
j++;
cout << "Adding job with pid #" <<b.getPid<<" and priority ";
cout << b.getPriority() << " and length "<< b.getLength();
cout << endl;
j++;
}
else
{ cout << "No new job this cycle" << endl;}
if ()
}
return 0;
}