包含先来先服务、抢占式最短作业优先、时间片轮转、优先数调度算法。
下图为时间片轮转效果的示例
#include <iostream>
#include <string>
#include <stdlib.h>
#include <iomanip>
using namespace std;
double Sum_T_time = 0.0;
double Sum_WT_time = 0.0;
int current_time = 0; // 当前时间
class Process
{
public:
string Name;
char state; // 运行状态,W就绪队列中,R运行,F结束,N尚未进入就绪队列
int Enter_time; // 到达时间
int Running_time; // 运行时间
int Priorty; // 优先级
int Start_time; // 开始时间
int Finish_time; // 结束时间
int T_time; // 周转时间
double WT_time; // 带权周转时间
int Remaining_Running_Time; // 剩余运行时间
Process *next;
Process()
{
this->state = 'N'; // 运行状态
this->Name = '0';
this->Enter_time = 0;
this->Running_time = 0;
this->Priorty = 0;
this->next = NULL;
this->Start_time = 0;
this->Finish_time = 0;
this->T_time = 0;
this->WT_time = 0.0;
this->Remaining_Running_Time = 0;
}
Process(string name, int Enter_time, int Running_time)
{
this->state = 'N';
this->Name = name;
this->Enter_time = Enter_time;
this->Running_time = Running_time;
this->Priorty = 0;
this->next = NULL;
this->Start_time = 0;
this->Finish_time = 0;
this->T_time = 0;
this->WT_time = 0.0;
this->Remaining_Running_Time = this->Running_time;
}
void init_RRT()
{
this->Remaining_Running_Time = this->Running_time;
}
};
class Process_Queue
{
public:
Process *Head;
Process *Last;
int size;
Process_Queue()
{
this->size = 0;
Last = new Process();
Head = Last;
}
};
Process_Queue *Ready_queue = new Process_Queue(); // 就绪队列
Process_Queue *Run_queue = new Process_Queue(); // 运行队列
Process_Queue *Finish_queue = new Process_Queue(); // 结束队列
void Print_Queue(Process_Queue *queue)
{
Process *current_pro = queue->Head->next;
cout << "队列长度:" << queue->size << endl;
int count = 0;
for (int i = 0; i < queue->size; i++)
{
cout << "进程名字:" << current_pro->Name << "\t";
cout << "进程到达时间:" << current_pro->Enter_time << "\t";
cout << "进程运行时间:" << current_pro->Running_time << "\t";
cout << "进程剩余运行时间:" << current_pro->Remaining_Running_Time << "\t";
cout << "序号:" << count++;
cout << endl;
current_pro = current_pro->next;
}
}
void Process_Show(Process *current_pro) // 输出
{
cout << setiosflags(ios::fixed) << setprecision(2);
cout << current_pro->Name << "\t" << current_pro->Enter_time << "\t" << current_pro->Running_time << "\t"
<< " " << current_pro->Priorty << "\t"
<< " " << current_pro->Start_time << "\t"
<< " " << current_pro->Finish_time << "\t"
<< " " << current_pro->T_time << "\t"
<< " " << current_pro->WT_time << endl;
}
void Finish_queue_Show(Process_Queue *queue)
{
Process *current_pro = queue->Head->next;
cout << "进程 到达时间 运行时间 优先级 开始时间 结束时间 周转时间 带权周转时间" << endl;
for (int i = 0; i < queue->size; i++)
{
Process_Show(current_pro);
current_pro = current_pro->next;
}
}
int Max(int a, int b)
{
return a > b ? a : b;
}
void init_Remaining_Running_Time(Process *process, int Num)
{
for (int i = 0; i < Num; i++)
{
process[i].init_RRT();
}
}
void SortbyEnter_time(Process *process, int First, int Last)
{
for (int i = First; i < Last - 1; i++)
{
for (int j = First; j < Last - i - 1; j++)
{
if (process[j].Enter_time > process[j + 1].Enter_time)
{
Process pro;
pro = process[j];
process[j] = process[j + 1];
process[j + 1] = pro;
}
}
}
}
void SortbyPriorty(Process *process, int First, int