📄 pcbtd.cpp
字号:
// PCBtd.cpp : Defines the entry point for the console application.
//PCB的调度,利用FCFS和最高优先级的方式
#include "stdafx.h"
#include"PCB.h"
#include"stdio.h"
int radom(int m); //
void nextpcb(PCB &pcb); //
void FCFS(int time); //
void MAXP(int time); //
unsigned int seed=300;
int main(int argc, char* argv[])
{
//FCFS(10);
MAXP(10);
return 0;
}
//函数说明
int radom(int m){
seed=seed*123+71;
return (seed%m+1);
}
void nextpcb(PCB &pcb){
pcb.name++;
pcb.priority=radom(3);
pcb.arrivetime=pcb.arrivetime+radom(2);
pcb.needtime=radom(4);
pcb.gettime=0;
}
void FCFS(int time){
int curtime=-1,i=radom(4);
gequeue fcfsq;
PCB newpcb(1,0,0,i);
PCB finpcb(1,0,0,i);
nextpcb(newpcb);
cout<<"利用FCFS算法"<<endl;
cout<<"设已有进程1正在开始运行"<<endl;
while(1){
curtime++;
cout<<"时刻"<<curtime<<endl;
//新的进程产生
if(curtime>=newpcb.arrivetime && curtime<=time){
fcfsq.enqueue(newpcb);
cout<<"\t产生一个新进程,进程"<<newpcb.name<<endl;
nextpcb(newpcb);
}
//就绪队列
if(fcfsq.length()){
cout<<"\t如下进程处于就绪状态"<<endl;
fcfsq.print();
}
else
cout<<"\t就绪队列中已无进程"<<endl;
//正在运行的队列
finpcb.gettime++;
if(finpcb.gettime==finpcb.needtime){
cout<<"\t进程"<<finpcb.name<<"运行完成"<<endl;
cout<<"\tarrivetime\tneedtime"<<endl;
cout<<"\t"<<finpcb.arrivetime
<<"\t\t"<<finpcb.needtime<<endl;
if(fcfsq.length())
fcfsq.dequeue(finpcb);
else if(curtime>=time){
cout<<"\t处理完毕。"<<endl;
break;
}
}
else if(finpcb.gettime<finpcb.needtime){
cout<<"\t进程"<<finpcb.name<<"正在运行"<<endl;
cout<<"\tarrivetime\tneedtime\tgettime"<<endl;
cout<<"\t"<<finpcb.arrivetime
<<"\t\t"<<finpcb.needtime
<<"\t\t"<<finpcb.gettime<<endl;
}
else
cout<<"\t现无进程需要处理。"<<endl;
}
}
/*
void MAXP(int time){
int curtime=-1;
sequeue maxpq;
PCB newpcb(1,radom(3),0,radom(4));
PCB finpcb;
cout<<"利用最高优先级算法"<<endl;
while(1){
curtime++;
cout<<"时刻"<<curtime<<endl;
//新进程的产生
if(curtime>=newpcb.arrivetime && curtime<=time){
maxpq.enqueue(newpcb);
cout<<"\t产生一个新进程,进程"<<newpcb.name<<endl;
cout<<"\tname\tpriority\tarrivetime\tneedtime\tgettime"<<endl;
cout<<"\t"<<newpcb.name<<"\t"<<newpcb.priority<<"\t\t"
<<newpcb.arrivetime<<"\t\t"<<newpcb.needtime<<"\t\t"
<<newpcb.gettime<<endl;
nextpcb(newpcb);
}
if(maxpq.length()){
maxpq.dequeue(finpcb);
//就绪队列
if(maxpq.length()){
cout<<"\t如下进程处于就绪状态"<<endl;
maxpq.print();
}
else
cout<<"\t就绪队列中已无进程"<<endl;
//正在运行队列
finpcb.gettime++;
if(finpcb.gettime==finpcb.needtime){
cout<<"\t进程"<<finpcb.name<<"运行完成\n"<<endl;
}
else{
cout<<"\t进程"<<finpcb.name<<"正在运行\n"<<endl;
if(finpcb.priority!=0) finpcb.priority--;
maxpq.enqueue(finpcb);
}
}
else if(curtime>=time){
cout<<"\t处理完毕。"<<endl;
break;
}
else
cout<<"\t现无进程需要处理"<<endl;
}
}*/
PCB::PCB(int nm,int p,int a,int n,int g){
name=nm;
priority=p;
arrivetime=a;
needtime=n;
gettime=g;
}
void PCB::getvalue(const PCB &p){
name=p.name;
priority=p.priority;
arrivetime=p.arrivetime;
needtime=p.needtime;
gettime=p.gettime;
}
node::node(const PCB& p){
pcb.getvalue(p);
next=NULL;
}
queue::~queue(){
node *temp;
while(num){
temp=front;
front=front->next;
delete temp;
num--;
}
}
bool queue::dequeue(PCB &nd){
if(num==0) return false;
node *temp=front;
nd.getvalue(front->pcb);
front=front->next;
num--;
delete temp;
return true;
}
bool sequeue::enqueue(const PCB &nd){
node *temp=new node(nd),*p=front,*q=front;
if(num==0)
front=temp;
else if(nd.priority > front->pcb.priority){
temp->next=front;
front=temp;
}
else{
while(p!=NULL && p->pcb.priority >= nd.priority){
q=p;
p=p->next;
}
q->next=temp;
temp->next=p;
}
num++;
return true;
}
void sequeue::print(){
node *temp=front;
cout<<"\tname"
<<"\tpriority"
<<"\tarrivetime"
<<"\tneedtime"
<<"\tgettime"<<endl;
for(int i=0;i<num;i++){
cout<<"\t"<<temp->pcb.name
<<"\t"<<temp->pcb.priority
<<"\t\t"<<temp->pcb.arrivetime
<<"\t\t"<<temp->pcb.needtime
<<"\t\t"<<temp->pcb.gettime<<endl;
temp=temp->next;
}
}
bool gequeue::enqueue(const PCB &nd){
node *temp=new node(nd);
if(num==0)
rear=front=temp;
else{
rear->next=temp;
rear=temp;
}
num++;
return true;
}
void gequeue::print(){
node *temp=front;
cout<<"\tname"
<<"\tarrivetime"
<<"\tneedtime"<<endl;
for(int i=0;i<num;i++){
cout<<"\t"<<temp->pcb.name
<<"\t"<<temp->pcb.arrivetime
<<"\t\t"<<temp->pcb.needtime<<endl;
temp=temp->next;
}
}
/*//轮转法
void FCFS(int time){
int curtime=-1,i=radom(4);
gequeue fcfsq;
PCB newpcb(1,0,0,i);
PCB finpcb;
cout<<"利用FCFS算法"<<endl;
while(1){
curtime++;
cout<<"时刻"<<curtime<<endl;
//新的进程产生
if(curtime>=newpcb.arrivetime && curtime<=time){
fcfsq.enqueue(newpcb);
cout<<"\t产生一个新进程,进程"<<newpcb.name<<endl;
nextpcb(newpcb);
}
//就绪队列
if(fcfsq.length()){
cout<<"\t如下进程处于就绪状态"<<endl;
fcfsq.print();
fcfsq.dequeue(finpcb);
}
else if(curtime<time)
cout<<"\t就绪队列中已无进程"<<endl;
else{
cout<<"\t处理完毕。"<<endl;
break;
}
//正在运行的队列
finpcb.gettime++;
if(finpcb.gettime==finpcb.needtime){
cout<<"\t进程"<<finpcb.name<<"运行完成"<<endl;
cout<<"\tarrivetime\tneedtime"<<endl;
cout<<"\t"<<finpcb.arrivetime
<<"\t\t"<<finpcb.needtime<<endl;
}
else if(finpcb.gettime<finpcb.needtime){
cout<<"\t进程"<<finpcb.name<<"正在运行"<<endl;
cout<<"\tarrivetime\tneedtime\tgettime"<<endl;
cout<<"\t"<<finpcb.arrivetime
<<"\t\t"<<finpcb.needtime
<<"\t\t"<<finpcb.gettime<<endl;
fcfsq.enqueue(finpcb);
}
else
cout<<"\t现无进程需要处理。"<<endl;
}
}*/
///*//从就绪队列中选择
void MAXP(int time){
int curtime=-1;
sequeue maxpq;
PCB newpcb(1,radom(3),0,radom(4));
PCB finpcb;
cout<<"利用最高优先级算法"<<endl;
while(1){
curtime++;
cout<<"时刻"<<curtime<<endl;
//新进程的产生
if(curtime>=newpcb.arrivetime && curtime<=time){
maxpq.enqueue(newpcb);
cout<<"\t产生一个新进程,进程"<<newpcb.name<<endl;
nextpcb(newpcb);
}
if(maxpq.length()){
//就绪队列
cout<<"\t如下进程处于就绪状态"<<endl;
maxpq.print();
//正在运行队列
maxpq.dequeue(finpcb);
finpcb.gettime++;
if(finpcb.gettime==finpcb.needtime){
cout<<"\t进程"<<finpcb.name<<"运行完成\n"<<endl;
}
else{
cout<<"\t进程"<<finpcb.name<<"正在运行\n"<<endl;
if(finpcb.priority!=0) finpcb.priority--;
maxpq.enqueue(finpcb);
}
}
else if(curtime>=time){
cout<<"\t处理完毕。"<<endl;
break;
}
else
cout<<"\t就绪队列中已无进程,无进程需要处理"<<endl;
}
}//*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -