将最高优先数优先的调度算法改为时间片轮转调度算法
发布网友
发布时间:2022-05-06 18:39
我来回答
共1个回答
热心网友
时间:2023-10-15 11:12
#include<iostream>
using namespace std;
struct PCB_Type{
char name;
int cpu_time;
};
struct QueueNode{
struct PCB_Type PCB;
struct QueueNode *next;
};
int main(){
int m,n,t;
int usecpu=0,unusecpu=0;
cout<<"输入就绪队列中的进程数目m:";
cin>>m;
cout<<"输入阻塞队列中的进程的数目n:";
cin>>n;
cout<<"输入唤醒系统资源的相隔时间片个数t:";
cin>>t;
struct QueueNode *readyhead=new QueueNode ,*readytail=new QueueNode,
*blockedhead=new QueueNode,*blockedtail=new QueueNode;
// readyhead=NULL;readytail=NULL;blockedhead=NULL;blockedtail=NULL;
readyhead=readytail;
blockedhead=blockedtail;
for(int i=1;i<=m;i++){
struct QueueNode *t1=new QueueNode;
cout<<"输入就绪队列中进程的name和cpu_time:";
cin>>t1->PCB.name>>t1->PCB.cpu_time;
readytail->next=t1;
readytail=t1;
}
for(int j=1;j<=n;j++){
struct QueueNode *t2=new QueueNode;
cout<<"输入阻塞队列中进程的name和cpu_time:";
cin>>t2->PCB.name>>t2->PCB.cpu_time;
blockedtail->next=t2;
blockedtail=t2;
}
cout<<"输出就绪队列的进程信息:";
for(struct QueueNode *t3=readyhead->next;t3!=readytail->next;t3=t3->next){
cout<<t3->PCB.name<<"、"<<t3->PCB.cpu_time<<"---> ";
}
cout<<"无进程";
cout<<endl;
cout<<"输出阻塞队列的进程信息:";
struct QueueNode *t4;
t4=blockedhead->next;
while(t4!=blockedtail->next){
cout<<t4->PCB.name<<"、"<<t4->PCB.cpu_time<<"---> ";
t4=t4->next;
}
cout<<"无进程";
cout<<endl<<"进程的运行顺序:";
int x=0;
while(readyhead!=readytail||blockedhead!=blockedtail){
if(readyhead!=readytail){
struct QueueNode *p=readyhead->next;
cout<<p->PCB.name<<",";
p->PCB.cpu_time--;
usecpu++;
if(readyhead->next!=readytail){
if(p->PCB.cpu_time>0){
readyhead->next=p->next;//出队列
readytail->next=p;
readytail=p;
}
else{
readyhead->next=p->next;
delete p;
}
}
else//队列中只有两个节点 头结点和尾结点
{
if(p->PCB.cpu_time<=0){readytail=readyhead;//只有进程为执行完,就继续执行,完成之后,把队列清空,释放指针p;就绪队列无进程
delete p;}
}
}
else
{
unusecpu++;
cout<<"_,";
}
x++;
if(x==t){
if(blockedhead!=blockedtail){
struct QueueNode *q=blockedhead->next;
if(blockedhead->next!=blockedtail)
{
blockedhead->next=q->next;
}
else
{
blockedhead=blockedtail;
}
readytail->next=q;
readytail=q;
x=0;
}
}
}
cout<<endl;
cout<<"cpu的利用率="<<usecpu<<"/"<<usecpu+unusecpu<<endl;
return 0;
}