📄 deal.cpp
字号:
#include <iostream>
#include <stdlib.h>
using namespace std;
const int MaxQueueSize = 3;
#include "SeqPQueue.h"
void main()
{
int currenttime = 1;
Process x[] = {{1,5,1,4,0,'W',0},
{2,3,1,4,0,'W',0},
{3,5,2,5,0,'W',0}};
SeqPQueue myQueue;
for(int n = 0 ; n < MaxQueueSize ; n++)
{
if(currenttime >= x[n].arrivetime)
{
myQueue.Append(x[n]);
}
}
while(myQueue.NotEmpty())
{
if(myQueue.GetFront().processnum == 1) //判断是否为进程1
{
myQueue.Delete(); //出队列,分配到CPU
x[0].state = 'R';
x[0].display();
cout << "进程1正在运行!" <<endl;
if(x[0].needtime != x[0].possessivetime) //时间片不够
{
x[0].possessivetime++;
x[0].priority--;
x[0].state='W'; //将运行状态转变为等候状态
x[0].display();
cout<<"进程1需要继续运行,重新插入就绪队列"<<endl;
cout<<" "<<endl;
myQueue.Append(x[0]);
}
else
{ //占用CPU时间等于运行所需时间
x[0].state='F'; //将运行状态转变为完成状态
x[0].display();
cout<<" "<<endl;
cout<<"进程1已经完成,退出队列!"<<endl;
cout<<" "<<endl;
}
}
else
{
x[0].processwaittime++;
if(x[0].processwaittime == 3)
{
x[0].priority++;
x[0].processwaittime = 0;
}
}
if(myQueue.GetFront().processnum == 2) //判断是否为进程2
{
myQueue.Delete(); //出队列,分配到CPU
x[1].state = 'R';
x[1].display();
cout << "进程2正在运行!" <<endl;
if(x[1].needtime != x[1].possessivetime) //时间片不够
{
x[1].possessivetime++;
x[1].priority--;
x[1].state='W'; //将运行状态转变为等候状态
x[1].display();
cout<<"进程2需要继续运行,重新插入就绪队列"<<endl;
cout<<" "<<endl;
myQueue.Append(x[1]);
}
else
{ //占用CPU时间等于运行所需时间
x[1].state='F'; //将运行状态转变为完成状态
x[1].display();
cout<<" "<<endl;
cout<<"进程2已经完成,退出队列!"<<endl;
cout<<" "<<endl;
}
}
else
{
x[1].processwaittime++;
if(x[1].processwaittime == 3)
{
x[1].priority++;
x[1].processwaittime = 0;
}
}
if(myQueue.GetFront().processnum == 3) //判断是否为进程3
{
myQueue.Delete(); //出队列,分配到CPU
x[2].state = 'R';
x[2].display();
cout << "进程3正在运行!" <<endl;
if(x[2].needtime != x[2].possessivetime) //时间片不够
{
x[2].possessivetime++;
x[2].priority--;
x[2].state='W'; //将运行状态转变为等候状态
x[2].display();
cout<<"进程3需要继续运行,重新插入就绪队列"<<endl;
cout<<" "<<endl;
myQueue.Append(x[2]);
}
else
{ //占用CPU时间等于运行所需时间
x[2].state='F'; //将运行状态转变为完成状态
x[2].display();
cout<<" "<<endl;
cout<<"进程3已经完成,退出队列!"<<endl;
cout<<" "<<endl;
}
}
else
{
x[2].processwaittime++;
if(x[2].processwaittime == 3)
{
x[2].priority++;
x[2].processwaittime = 0;
}
}
currenttime++;
for(int i = 0 ; i <MaxQueueSize ; i++)
{
if(currenttime == x[i].arrivetime)
{
myQueue.Append(x[i]);
}
}
}
cout << "进程都已经完成!" <<endl;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -