一、实验内容完成情况(请写明自己已完成的项目功能):
三、模块说明及相应代码:
1、 初始化操作系统(请文字写明思路并给出核心代码):
1) 创建PCB的结构体:
思路:根据实验要求完成结构体的创建
代码:
typedef struct node
{
char name[20]; //进程名
int id = 0; //进程ID
int priority = 0; //优先级
int ArriTime = 0; //进程到达时间
int serviceTime=0; //进程总需要时间
int needTime = 0; //还需要运行时间
int CPUTime = 0; //已用CPU时间
char state[20]; //进程状态 1.Ready 2.Run 3.Wait 4.Finish
int finishTime = 0; //完成时间
char blockCause[30]; //阻塞原因
int aroundTime=0; //周转时间
} PCB;
2) 创建就绪队列、阻塞队列、运行队列:采用队列的方式/其他方式也可以
思路:用队列的方式
代码:
queue processQueue, readyQueue, blockQueue, runQueue, finishQueue,hungQueue;//临时队列、就绪队列、阻塞队列、运行队列、完成队列、挂起队列
3)编写创建进程函数:
思路:分为手动输入还是自动输入,手动输入为用户自己创建,自动输入为调入我自己初始化的进程。
代码:
//初始化进程
void Init()
{
PCB p1, p2, p3,p4,p5;
strcpy(p1.name, “a”);
p1.id = 1;
p1.priority = 8;
p1.ArriTime = 2;
p1.needTime = 4;
p1.serviceTime=4;
processQueue.push(p1);
strcpy(p2.name, “b”);
p2.id = 2;
p2.priority = 4;
p2.ArriTime = 6;
p2.needTime = 4;
p2.serviceTime=4;
processQueue.push(p2);
strcpy(p3.name, “c”);
p3.id = 3;
p3.priority = 7;
p3.ArriTime = 3;
p3.needTime = 9;
p3.serviceTime=9;
processQueue.push(p3);
strcpy(p4.name, “d”);
p4.id = 4;
p4.priority = 1;
p4.ArriTime = 0;
p4.needTime = 1;
p4.serviceTime=1;
processQueue.push(p4);
strcpy(p5.name, “e”);
p5.id = 5;
p5.priority = 2;
p5.ArriTime = 2;
p5.needTime = 2;
p5.serviceTime=2;
processQueue.push(p5);
}
//添加进程
void addProcess()
{
int n;
cout << “请输入要添加的进程数:” << endl;
cin >> n;
for (int i = 0; i < n; i++)
{
PCB p;
cout << “请输入第”< cin >> p.name>>p.priority>> p.ArriTime>> p.serviceTime;
p.id=i+1;
p.needTime=p.serviceTime;
processQueue.push§;
}
}
2、 进程在其生命周期的都处于不断变换的过程,处理至少四种事件:(请文字写明思路并给出核心代码)
}
//显示运行队列
void displayRun()
{
cout << “----------------运行队列----------------” << endl;
queue temp = runQueue;
while (!temp.empty())
{
if(algorithm == 1)
{
cout << “进程名” <<" 进程ID"<< " 到达时间"<< " 服务时间"<< " 需要时间" << " 已用CPU时间"<<" 周转时间"<< " 状态"<< endl;
while (!temp.empty())
{
cout<
cout << endl;
}
}
if(algorithm == 2)
{
cout << “进程名” <<” 进程ID"<< " 优先级"<< " 服务时间"<< " 需要时间" << " 已用CPU时间"<< " 周转时间"<< " 状态"<< endl;
while (!temp.empty())
{
cout<
cout << endl;
}
}
if(algorithm == 3)
{
cout << “进程名” <<” 进程ID"<< " 服务时间"<< " 需要时间" << " 已用CPU时间"<< " 周转时间"<< " 状态"<< endl;
while (!temp.empty())
{
cout<
cout << endl;
}
}
}
}
//显示阻塞队列
void displayBlock()
{
cout << “----------------阻塞队列----------------” << endl;
queue temp = blockQueue;
while (!temp.empty())
{
if(algorithm == 1)
{
cout << “进程名” <<” 进程ID"<< " 到达时间"<< " 服务时间"<< " 需要时间" << " 已用CPU时间"<<" 周转时间"<< " 状态"<< " 阻塞原因"<< endl;
while (!temp.empty())
{
cout<
cout << endl;
}
}
if(algorithm == 2)
{
cout << “进程名” <<” 进程ID"<< " 优先级"<< " 服务时间"<< " 需要时间" << " 已用CPU时间"<<" 周转时间"<< " 状态"<< " 阻塞原因"<< endl;
while (!temp.empty())
{
cout<
cout << endl;
}
}
if(algorithm == 3)
{
cout << “进程名” <<” 进程ID"<< " 服务时间"<< " 需要时间" << " 已用CPU时间"<<" 周转时间"<< " 状态"<< " 阻塞原因"<< endl;
while (!temp.empty())
{
cout<
cout << endl;
}
}
}
}
//显示完成队列
void displayFinish()
{
cout << “----------------完成队列----------------” << endl;
queue temp = finishQueue;
while (!temp.empty())
{
if(algorithm == 1)
{
cout << “进程名” <<” 进程ID"<< " 到达时间"<< " 服务时间"<< " 需要时间" << " 已用CPU时间"<< " 完成时间"<< " 周转时间"<< " 状态"<< endl;
while (!temp.empty())
{
cout<
cout << endl;
}
}
if(algorithm == 2)
{
cout << “进程名” <<” 进程ID"<< " 优先级"<< " 服务时间"<< " 需要时间" << " 已用CPU时间"<< " 周转时间"<< " 完成时间"<< " 状态"<< endl;
while (!temp.empty())
{
cout<
cout << endl;
}
}
if(algorithm == 3)
{
cout << “进程名” <<” 进程ID"<< " 服务时间"<< " 需要时间" << " 已用CPU时间"<< " 周转时间"<< " 完成时间"<< " 状态"<< endl;
while (!temp.empty())
{
cout<
cout << endl;
}
}
}
}
//显示挂起队列
void displayHung()
{
cout << “----------------挂起队列----------------” << endl;
queue temp = hungQueue;
while (!temp.empty())
{
if(algorithm == 1)
{
cout << “进程名” <<" 进程ID"<< " 到达时间"<< " 服务时间"<< " 需要时间" << " 已用CPU时间"<<" 周转时间"<< " 状态"<< " 挂起原因"<< endl;
while (!temp.empty())
{
cout<
cout << endl;
}
}
if(algorithm == 2)
{
cout << “进程名” <<” 进程ID"<< " 优先级"<< " 服务时间"<< " 需要时间" << " 已用CPU时间"<<" 周转时间"<< " 状态"<< " 挂起原因"<< endl;
while (!temp.empty())
{
cout<
cout << endl;
}
}
if(algorithm == 3)
{
cout << “进程名” <<” 进程ID"<< " 服务时间"<< " 需要时间" << " 已用CPU时间"<<" 周转时间"<< " 状态"<< " 挂起原因"<< endl;
while (!temp.empty())
{
cout<
cout << endl;
}
}
}
}
2) 周转时间的计算。
思路:周转事件=完成时间-到达时间。
代码:
temp.front().finishTime-temp.front().ArriTime
3、 需要实现的调度算法:(请文字写明思路并给出核心代码)
先来先服务调度算法
思路:详细过程在见开始调度algorithm == 1。
代码:
void FCFS()
{
algorithm = 1;
PCB temp[processQueue.size()];
int n = 0;
while (!processQueue.empty())
{
temp[n] = processQueue.front();
processQueue.pop();
n++;
}
sort(temp, temp + n, cmp1());
for (int i = 0; i < n; i++)
{
strcpy(temp[i].state,“Ready”);
readyQueue.push(temp[i]);
}
}
时间片轮转调度算法
思路:详细过程在见开始调度algorithm == 3。
代码:
//时间片轮转
void RR()
{
cout << “请输入时间片大小” << endl;
cin >> timeslice;
algorithm = 3;
readyQueue = processQueue;
for (int i = 0; i < readyQueue.size(); i++)
{
strcpy(readyQueue.front().state,“Ready”);
readyQueue.push(readyQueue.front());
readyQueue.pop();
}
}
优先级调度算法
思路:详细过程在见开始调度algorithm == 2。
代码:
//优先级调度算法
void SJF()
{
algorithm = 2;
PCB temp[processQueue.size()];
int n = 0;
while (!processQueue.empty())
{
temp[n] = processQueue.front();
processQueue.pop();
n++;
}
sort(temp, temp + n, cmp2());
for (int i = 0; i < n; i++)
{
strcpy(temp[i].state,“Ready”);
readyQueue.push(temp[i]);
}
}
#include
#include
#include
#include
#include
#include
using namespace std;
const int MAXN = 1000; //假设能够容纳的进程最多的个数
typedef struct node
{
char name[20]; //进程名
int id = 0; //进程ID
int priority = 0; //优先级
int ArriTime = 0; //进程到达时间
int serviceTime=0; //进程总需要时间
int needTime = 0; //还需要运行时间
int CPUTime = 0; //已用CPU时间
char state[20]; //进程状态 1.Ready 2.Run 3.Wait 4.Finish
int finishTime = 0; //完成时间
char blockCause[30]; //阻塞原因
int aroundTime=0; //周转时间
} PCB;
queue<PCB> processQueue, readyQueue, blockQueue, runQueue, finishQueue,hungQueue;
int time = 0; //系统时间
int algorithm = 0; //算法选择标志
int timeslice = 0; //时间片大小
bool finish; //是否完成
struct cmp1
{
bool operator()(const PCB &p1, const PCB &p2)
{
return p1.ArriTime < p2.ArriTime; //到达时间从小到大排序
}
};
struct cmp2
{
bool operator()(const PCB &p1, const PCB &p2)
{
return p1.priority > p2.priority; //优先级从大到小排序
}
};
//初始化进程
void Init()
{
PCB p1, p2, p3,p4,p5;
strcpy(p1.name, "a");
p1.id = 1;
p1.priority = 8;
p1.ArriTime = 2;
p1.needTime = 4;
p1.serviceTime=4;
processQueue.push(p1);
strcpy(p2.name, "b");
p2.id = 2;
p2.priority = 4;
p2.ArriTime = 6;
p2.needTime = 4;
p2.serviceTime=4;
processQueue.push(p2);
strcpy(p3.name, "c");
p3.id = 3;
p3.priority = 7;
p3.ArriTime = 3;
p3.needTime = 9;
p3.serviceTime=9;
processQueue.push(p3);
strcpy(p4.name, "d");
p4.id = 4;
p4.priority = 1;
p4.ArriTime = 0;
p4.needTime = 1;
p4.serviceTime=1;
processQueue.push(p4);
strcpy(p5.name, "e");
p5.id = 5;
p5.priority = 2;
p5.ArriTime = 2;
p5.needTime = 2;
p5.serviceTime=2;
processQueue.push(p5);
}
//添加进程
void addProcess()
{
int n;
cout << "请输入要添加的进程数:" << endl;
cin >> n;
for (int i = 0; i < n; i++)
{
PCB p;
cout << "请输入第"<<i+1<<"个进程名、进程优先级、进程到达时间、进程需要运行时间:" << endl;
cin >> p.name>>p.priority>> p.ArriTime>> p.serviceTime;
p.id=i+1;
p.needTime=p.serviceTime;
processQueue.push(p);
}
}
//系统开始调度
void beginDispatch()
{
if (runQueue.empty())
{
if (!readyQueue.empty())
{
strcpy(readyQueue.front().state,"Run");
runQueue.push(readyQueue.front());
readyQueue.pop();
runQueue.front().CPUTime++;
runQueue.front().needTime--;
time++;
}
else if(!blockQueue.empty())
{
char chance='p';
cout<<endl;
cout<<"********就绪队列不为空*********************"<<endl;
cout<<"********若结束程序请按y,若不结束请按n******"<<endl;
cin>>chance;
if(chance=='y')
{
finish = true;
return;
}
}
else if(!hungQueue.empty())
{
char chance='p';
cout<<endl;
cout<<"********挂起队列不为空*********************"<<endl;
cout<<"********若结束程序请按y,若不结束请按n******"<<endl;
cin>>chance;
if(chance=='y')
{
finish = true;
return;
}
}
else
{
finish = true;
return;
}
}
else if (!runQueue.empty())
{
if (runQueue.front().needTime == 0)
{
runQueue.front().finishTime = time;
strcpy(runQueue.front().state,"Finish");
finishQueue.push(runQueue.front());
runQueue.pop();
if (!readyQueue.empty())
{
strcpy(readyQueue.front().state,"Run");
runQueue.push(readyQueue.front());
readyQueue.pop();
}
}
//先来先服务
if (algorithm == 1)
{
time++;
runQueue.front().CPUTime++;
runQueue.front().needTime--;
}
//优先级
if (algorithm == 2)
{
time++;
runQueue.front().CPUTime++;
runQueue.front().needTime--;
}
//时间片轮转算法
if(algorithm == 3)
{
time++;
runQueue.front().CPUTime++;
runQueue.front().needTime--;
if (runQueue.front().needTime != 0)
{
if ((runQueue.front().CPUTime >= timeslice) && (runQueue.front().CPUTime % timeslice == 0))
{
strcpy(runQueue.front().state,"Ready");
readyQueue.push(runQueue.front());
runQueue.pop();
if (!readyQueue.empty())
{
strcpy(readyQueue.front().state,"Run");
runQueue.push(readyQueue.front());
readyQueue.pop();
}
}
}
}
}
}
//当前运行进程请求 I/O 事件
void IORequest()
{
if (!runQueue.empty())
{
if (runQueue.front().needTime != 0)
{
strcpy(runQueue.front().blockCause, "请求 I/O 事件");
strcpy(runQueue.front().state,"Wait");
blockQueue.push(runQueue.front());
runQueue.pop();
}
}
else
{
cout << "当前无运行进程" << endl;
}
}
//某进程I/O完成
void IOFinish()
{
//空
if (blockQueue.empty())
{
cout << "目前无阻塞进程" << endl;
}
//不空
else
{
int id;
cout << "请输入I/O完成的进程ID" << endl;
cin >> id;
for (int i = 0; i < blockQueue.size(); i++)
{
if (blockQueue.front().id == id)
{
strcpy(blockQueue.front().blockCause, " ");
strcpy(blockQueue.front().state,"Ready");
readyQueue.push(blockQueue.front());
blockQueue.pop();
if (algorithm == 1)//先来先服务
{
//排序
PCB temp[readyQueue.size()];
int n = 0;
while (!readyQueue.empty())
{
temp[n] = readyQueue.front();
readyQueue.pop();
n++;
}
sort(temp, temp + n, cmp1());
for (int i = 0; i < n; i++)
{
readyQueue.push(temp[i]);
}
}
if (algorithm == 2)//优先级
{
if (algorithm == 1)//先来先服务
{
//排序
PCB temp[readyQueue.size()];
int n = 0;
while (!readyQueue.empty())
{
temp[n] = readyQueue.front();
readyQueue.pop();
n++;
}
sort(temp, temp + n, cmp1());
for (int i = 0; i < n; i++)
{
readyQueue.push(temp[i]);
}
//排序
PCB temp[readyQueue.size()];
int n = 0;
while (!readyQueue.empty())
{
temp[n] = readyQueue.front();
readyQueue.pop();
n++;
}
sort(temp, temp + n, cmp2());
for (int i = 0; i < n; i++)
{
readyQueue.push(temp[i]);
}
}
if (algorithm == 3)//时间片轮转
{
//排序
PCB temp[readyQueue.size()];
int n = 0;
while (!readyQueue.empty())
{
temp[n] = readyQueue.front();
readyQueue.pop();
n++;
}
for (int i = 0; i < n; i++)
{
readyQueue.push(temp[i]);
}
}
}
else
{
blockQueue.push(blockQueue.front());
blockQueue.pop();
}
}
}
}
//时间片到期
void timesliceDaoqi()
{
if (!runQueue.empty())
{
if (runQueue.front().needTime != 0)
{
strcpy(runQueue.front().state,"Ready");
readyQueue.push(runQueue.front());
runQueue.pop();
}
}
else
{
cout << "当前无运行进程" << endl;
}
}
//其他阻塞事件
void ElseZC()
{
if (!runQueue.empty())
{
if (runQueue.front().needTime != 0)
{
strcpy(runQueue.front().blockCause, "其他阻塞事件");
strcpy(runQueue.front().state,"Wait");
blockQueue.push(runQueue.front());
runQueue.pop();
}
}
else
{
cout << "当前无运行进程" << endl;
}
}
void ElseZCFinish()
{
//空
if (blockQueue.empty())
{
cout << "目前无阻塞进程" << endl;
}
//不空
else
{
int id;
cout << "请输入其他阻塞事件完成的进程ID" << endl;
cin >> id;
for (int i = 0; i < blockQueue.size(); i++)
{
if (blockQueue.front().id == id)
{
strcpy(blockQueue.front().blockCause, " ");
strcpy(blockQueue.front().state,"Ready");
readyQueue.push(blockQueue.front());
blockQueue.pop();
if (algorithm == 1)//先来先服务
{
//排序
PCB temp[readyQueue.size()];
int n = 0;
while (!readyQueue.empty())
{
temp[n] = readyQueue.front();
readyQueue.pop();
n++;
}
sort(temp, temp + n, cmp1());
for (int i = 0; i < n; i++)
{
readyQueue.push(temp[i]);
}
}
if (algorithm == 2)//优先级
{
if (algorithm == 1)//先来先服务
{
//排序
PCB temp[readyQueue.size()];
int n = 0;
while (!readyQueue.empty())
{
temp[n] = readyQueue.front();
readyQueue.pop();
n++;
}
sort(temp, temp + n, cmp1());
for (int i = 0; i < n; i++)
{
readyQueue.push(temp[i]);
}
//排序
PCB temp[readyQueue.size()];
int n = 0;
while (!readyQueue.empty())
{
temp[n] = readyQueue.front();
readyQueue.pop();
n++;
}
sort(temp, temp + n, cmp2());
for (int i = 0; i < n; i++)
{
readyQueue.push(temp[i]);
}
}
if (algorithm == 3)//时间片轮转
{
//排序
PCB temp[readyQueue.size()];
int n = 0;
while (!readyQueue.empty())
{
temp[n] = readyQueue.front();
readyQueue.pop();
n++;
}
for (int i = 0; i < n; i++)
{
readyQueue.push(temp[i]);
}
}
}
else
{
blockQueue.push(blockQueue.front());
blockQueue.pop();
}
}
}
}
//挂起
void Hung()
{
if (!runQueue.empty())
{
if (runQueue.front().needTime != 0)
{
strcpy(runQueue.front().blockCause, "请求挂起");
strcpy(runQueue.front().state,"Hung");
hungQueue.push(runQueue.front());
runQueue.pop();
}
}
else
{
cout << "当前无运行进程" << endl;
}
}
//某进程挂起完成
void HungFinish()
{
//空
if (hungQueue.empty())
{
cout << "目前无挂起进程" << endl;
}
//不空
else
{
int id;
cout << "请输入挂起完成的进程ID" << endl;
cin >> id;
for (int i = 0; i < hungQueue.size(); i++)
{
if (hungQueue.front().id == id)
{
strcpy(hungQueue.front().blockCause, " ");
strcpy(hungQueue.front().state,"Ready");
readyQueue.push(hungQueue.front());
hungQueue.pop();
if (algorithm == 1)//先来先服务
{
//排序
PCB temp[readyQueue.size()];
int n = 0;
while (!readyQueue.empty())
{
temp[n] = readyQueue.front();
readyQueue.pop();
n++;
}
sort(temp, temp + n, cmp1());
for (int i = 0; i < n; i++)
{
readyQueue.push(temp[i]);
}
}
if (algorithm == 2)//优先级
{
if (algorithm == 1)//先来先服务
{
//排序
PCB temp[readyQueue.size()];
int n = 0;
while (!readyQueue.empty())
{
temp[n] = readyQueue.front();
readyQueue.pop();
n++;
}
sort(temp, temp + n, cmp1());
for (int i = 0; i < n; i++)
{
readyQueue.push(temp[i]);
}
//排序
PCB temp[readyQueue.size()];
int n = 0;
while (!readyQueue.empty())
{
temp[n] = readyQueue.front();
readyQueue.pop();
n++;
}
sort(temp, temp + n, cmp2());
for (int i = 0; i < n; i++)
{
readyQueue.push(temp[i]);
}
}
if (algorithm == 3)//时间片轮转
{
//排序
PCB temp[readyQueue.size()];
int n = 0;
while (!readyQueue.empty())
{
temp[n] = readyQueue.front();
readyQueue.pop();
n++;
}
for (int i = 0; i < n; i++)
{
readyQueue.push(temp[i]);
}
}
}
else
{
hungQueue.push(hungQueue.front());
hungQueue.pop();
}
}
}
}
//显示就绪队列
void displayReady()
{
cout << "----------------就绪队列----------------" << endl;
queue<PCB> temp = readyQueue;
while (!temp.empty())
{
if(algorithm == 1)
{
cout << "进程名" <<" 进程ID"<< " 到达时间"<< " 服务时间"<< " 需要时间" << " 已用CPU时间"<< " 周转时间"<< " 状态"<< endl;
while (!temp.empty())
{
cout<<temp.front().name<<" "<<temp.front().id<<" "<< temp.front().ArriTime<<" "<< temp.front().serviceTime<<" "<<temp.front().needTime<<" "<<temp.front().CPUTime<<" "<<temp.front().CPUTime-temp.front().ArriTime<<" "<<temp.front().state<<endl;
temp.pop();
cout << endl;
}
}
if(algorithm == 2)
{
cout << "进程名" <<" 进程ID"<< " 优先级"<< " 服务时间"<< " 需要时间" << " 已用CPU时间"<< " 周转时间"<< " 状态"<< endl;
while (!temp.empty())
{
cout<<temp.front().name<<" "<<temp.front().id<<" "<< temp.front().priority<<" "<< temp.front().serviceTime<<" "<<temp.front().needTime<<" "<< temp.front().CPUTime<<" "<<temp.front().finishTime-temp.front().ArriTime<<" "<< temp.front().state<<endl;
temp.pop();
cout << endl;
}
}
if(algorithm == 3)
{
cout << "进程名" <<" 进程ID"<< " 服务时间"<< " 需要时间" << " 已用CPU时间"<< " 周转时间"<< " 状态"<< endl;
while (!temp.empty())
{
cout<<temp.front().name<<" "<<temp.front().id<<" "<< temp.front().serviceTime<<" "<<temp.front().needTime<<" "<<temp.front().CPUTime<<" "<<temp.front().finishTime-temp.front().ArriTime<<" "<<temp.front().state<<endl;
temp.pop();
cout << endl;
}
}
}
}
//显示运行队列
void displayRun()
{
cout << "----------------运行队列----------------" << endl;
queue<PCB> temp = runQueue;
while (!temp.empty())
{
if(algorithm == 1)
{
cout << "进程名" <<" 进程ID"<< " 到达时间"<< " 服务时间"<< " 需要时间" << " 已用CPU时间"<<" 周转时间"<< " 状态"<< endl;
while (!temp.empty())
{
cout<<temp.front().name<<" "<<temp.front().id<<" "<< temp.front().ArriTime<<" "<< temp.front().serviceTime<<" "<<temp.front().needTime<<" "<<temp.front().CPUTime<<" "<<temp.front().CPUTime-temp.front().ArriTime<<" "<<temp.front().state<<endl;
temp.pop();
cout << endl;
}
}
if(algorithm == 2)
{
cout << "进程名" <<" 进程ID"<< " 优先级"<< " 服务时间"<< " 需要时间" << " 已用CPU时间"<< " 周转时间"<< " 状态"<< endl;
while (!temp.empty())
{
cout<<temp.front().name<<" "<<temp.front().id<<" "<< temp.front().priority<<" "<< temp.front().serviceTime<<" "<<temp.front().needTime<<" "<< temp.front().CPUTime<<" "<< temp.front().finishTime-temp.front().ArriTime<<" "<< temp.front().state<<endl;
temp.pop();
cout << endl;
}
}
if(algorithm == 3)
{
cout << "进程名" <<" 进程ID"<< " 服务时间"<< " 需要时间" << " 已用CPU时间"<< " 周转时间"<< " 状态"<< endl;
while (!temp.empty())
{
cout<<temp.front().name<<" "<<temp.front().id<<" "<< temp.front().serviceTime<<" "<<temp.front().needTime<<" "<<temp.front().CPUTime<<" "<<temp.front().finishTime-temp.front().ArriTime<<" "<<temp.front().state<<endl;
temp.pop();
cout << endl;
}
}
}
}
//显示阻塞队列
void displayBlock()
{
cout << "----------------阻塞队列----------------" << endl;
queue<PCB> temp = blockQueue;
while (!temp.empty())
{
if(algorithm == 1)
{
cout << "进程名" <<" 进程ID"<< " 到达时间"<< " 服务时间"<< " 需要时间" << " 已用CPU时间"<<" 周转时间"<< " 状态"<< " 阻塞原因"<< endl;
while (!temp.empty())
{
cout<<temp.front().name<<" "<<temp.front().id<<" "<< temp.front().ArriTime<<" "<< temp.front().serviceTime<<" "<<temp.front().needTime<<" "<<temp.front().CPUTime<<" "<<temp.front().CPUTime-temp.front().ArriTime<<" "<<temp.front().state<<" "<<temp.front().blockCause<<endl;
temp.pop();
cout << endl;
}
}
if(algorithm == 2)
{
cout << "进程名" <<" 进程ID"<< " 优先级"<< " 服务时间"<< " 需要时间" << " 已用CPU时间"<<" 周转时间"<< " 状态"<< " 阻塞原因"<< endl;
while (!temp.empty())
{
cout<<temp.front().name<<" "<<temp.front().id<<" "<< temp.front().priority<<" "<< temp.front().serviceTime<<" "<<temp.front().needTime<<" "<< temp.front().CPUTime<<" "<< temp.front().finishTime-temp.front().ArriTime<<" "<< temp.front().state<<" "<<temp.front().blockCause<<endl;
temp.pop();
cout << endl;
}
}
if(algorithm == 3)
{
cout << "进程名" <<" 进程ID"<< " 服务时间"<< " 需要时间" << " 已用CPU时间"<<" 周转时间"<< " 状态"<< " 阻塞原因"<< endl;
while (!temp.empty())
{
cout<<temp.front().name<<" "<<temp.front().id<<" "<< temp.front().serviceTime<<" "<<temp.front().needTime<<" "<<temp.front().CPUTime<<" "<<temp.front().finishTime-temp.front().ArriTime<<" "<<temp.front().state<<" "<<temp.front().blockCause<<endl;
temp.pop();
cout << endl;
}
}
}
}
//显示完成队列
void displayFinish()
{
cout << "----------------完成队列----------------" << endl;
queue<PCB> temp = finishQueue;
while (!temp.empty())
{
if(algorithm == 1)
{
cout << "进程名" <<" 进程ID"<< " 到达时间"<< " 服务时间"<< " 需要时间" << " 已用CPU时间"<< " 完成时间"<< " 周转时间"<< " 状态"<< endl;
while (!temp.empty())
{
cout<<temp.front().name<<" "<<temp.front().id<<" "<< temp.front().ArriTime<<" "<< temp.front().serviceTime<<" "<<temp.front().needTime<<" "<<temp.front().CPUTime<<" "<< temp.front().finishTime<<" "<< temp.front().finishTime-temp.front().ArriTime<<" "<<temp.front().state<<endl;
temp.pop();
cout << endl;
}
}
if(algorithm == 2)
{
cout << "进程名" <<" 进程ID"<< " 优先级"<< " 服务时间"<< " 需要时间" << " 已用CPU时间"<< " 周转时间"<< " 完成时间"<< " 状态"<< endl;
while (!temp.empty())
{
cout<<temp.front().name<<" "<<temp.front().id<<" "<< temp.front().priority<<" "<< temp.front().serviceTime<<" "<<temp.front().needTime<<" "<< temp.front().CPUTime<<" "<< temp.front().finishTime-temp.front().ArriTime<<" "<< temp.front().finishTime<<" "<< temp.front().state<<endl;
temp.pop();
cout << endl;
}
}
if(algorithm == 3)
{
cout << "进程名" <<" 进程ID"<< " 服务时间"<< " 需要时间" << " 已用CPU时间"<< " 周转时间"<< " 完成时间"<< " 状态"<< endl;
while (!temp.empty())
{
cout<<temp.front().name<<" "<<temp.front().id<<" "<< temp.front().serviceTime<<" "<<temp.front().needTime<<" "<<temp.front().CPUTime<<" "<<temp.front().finishTime-temp.front().ArriTime<<" "<< temp.front().finishTime<<" "<<temp.front().state<<endl;
temp.pop();
cout << endl;
}
}
}
}
//显示挂起队列
void displayHung()
{
cout << "----------------挂起队列----------------" << endl;
queue<PCB> temp = hungQueue;
while (!temp.empty())
{
if(algorithm == 1)
{
cout << "进程名" <<" 进程ID"<< " 到达时间"<< " 服务时间"<< " 需要时间" << " 已用CPU时间"<<" 周转时间"<< " 状态"<< " 挂起原因"<< endl;
while (!temp.empty())
{
cout<<temp.front().name<<" "<<temp.front().id<<" "<< temp.front().ArriTime<<" "<< temp.front().serviceTime<<" "<<temp.front().needTime<<" "<<temp.front().CPUTime<<" "<<temp.front().CPUTime-temp.front().ArriTime<<" "<<temp.front().state<<" "<<temp.front().blockCause<<endl;
temp.pop();
cout << endl;
}
}
if(algorithm == 2)
{
cout << "进程名" <<" 进程ID"<< " 优先级"<< " 服务时间"<< " 需要时间" << " 已用CPU时间"<<" 周转时间"<< " 状态"<< " 挂起原因"<< endl;
while (!temp.empty())
{
cout<<temp.front().name<<" "<<temp.front().id<<" "<< temp.front().priority<<" "<< temp.front().serviceTime<<" "<<temp.front().needTime<<" "<< temp.front().CPUTime<<" "<< temp.front().finishTime-temp.front().ArriTime<<" "<< temp.front().state<<" "<<temp.front().blockCause<<endl;
temp.pop();
cout << endl;
}
}
if(algorithm == 3)
{
cout << "进程名" <<" 进程ID"<< " 服务时间"<< " 需要时间" << " 已用CPU时间"<<" 周转时间"<< " 状态"<< " 挂起原因"<< endl;
while (!temp.empty())
{
cout<<temp.front().name<<" "<<temp.front().id<<" "<< temp.front().serviceTime<<" "<<temp.front().needTime<<" "<<temp.front().CPUTime<<" "<<temp.front().finishTime-temp.front().ArriTime<<" "<<temp.front().state<<" "<<temp.front().blockCause<<endl;
temp.pop();
cout << endl;
}
}
}
}
//先来先服务
void FCFS()
{
algorithm = 1;
PCB temp[processQueue.size()];
int n = 0;
while (!processQueue.empty())
{
temp[n] = processQueue.front();
processQueue.pop();
n++;
}
sort(temp, temp + n, cmp1());
for (int i = 0; i < n; i++)
{
strcpy(temp[i].state,"Ready");
readyQueue.push(temp[i]);
}
}
//优先级调度算法
void SJF()
{
algorithm = 2;
PCB temp[processQueue.size()];
int n = 0;
while (!processQueue.empty())
{
temp[n] = processQueue.front();
processQueue.pop();
n++;
}
sort(temp, temp + n, cmp2());
for (int i = 0; i < n; i++)
{
strcpy(temp[i].state,"Ready");
readyQueue.push(temp[i]);
}
}
//时间片轮转
void RR()
{
cout << "请输入时间片大小" << endl;
cin >> timeslice;
algorithm = 3;
readyQueue = processQueue;
for (int i = 0; i < readyQueue.size(); i++)
{
strcpy(readyQueue.front().state,"Ready");
readyQueue.push(readyQueue.front());
readyQueue.pop();
}
}
int main()
{
cout<<"********进程状态转换系统********"<<endl;
int chance;
cout << "1:自动添加进程 2:手动添加进程" << endl;
cin>>chance;
switch(chance)
{
case 1:
Init();
break;
case 2:
addProcess();
break;
}
int e;
cout << "请输入算法" << endl;
cout << "1.先来先服务 2.优先级抢占 3.时间片轮转" << endl;
cin >> e;
switch (e)
{
case 1:
FCFS();
break;
case 2:
SJF();
break;
case 3:
RR();
break;
}
while (true)
{
int e;
cout << "请输入操作" << endl;
cout << "1.开始调度 2.IO请求 3.IO完成 4.时间片到期 5.其他阻塞事件 6.其他阻塞事件完成 7.挂起 8.挂起完成 0.退出" << endl;
cin >> e;
switch (e)
{
case 1:
beginDispatch();
break;
case 2:
IORequest();
break;
case 3:
IOFinish();
break;
case 4:
timesliceDaoqi();
break;
case 5:
ElseZC();
break;
case 6:
ElseZCFinish();
break;
case 7:
Hung();
break;
case 8:
HungFinish();
break;
case 0:
exit(0);
}
displayReady();
displayRun();
displayBlock();
displayFinish();
displayHung();
cout << "当前系统CPU时间:" << time << endl;
if (finish)
{
cout << "当前进程全部完成" << endl;
cout << "当前时间:" << time << endl;
break;
}
system("pause");
}
system("pause");
return 0;
}