动态进程优先高度算法(C++代码)--操作系统

#include"stdio.h"
#include"stdlib.h"
#include"string.h"
#include"iostream.h"
typedef struct node
{
 char name[10];
 int prio;
 int round;
 int cputime;
 int needtime;
 int count;
 char state;
 struct node *next;
}PCB;
PCB *finish,*ready,*tail,*run;
int N;

void firstin()
{run=ready;
run->state='R';
ready=ready->next;
}

void prt1(char a)
{if(toupper(a)=='p')
cout<<" "<<endl;
cout<<"进程名 占用CPU时间 到完成还要的时间 优先级 状态"<<endl;
}

void prt2(char a,PCB *q)
{if(toupper(a)=='P')
cout<<q->name<<"             "<<q->cputime<<"           "<<q->needtime<<"        "<<q->prio<<"    "<<q->state<<endl;
}

void prt(char algo)
{
 PCB *p;
  prt1(algo);
  if(run!=NULL)
  prt2(algo,run);
  p=ready;
 while(p!=NULL)
{prt2
(algo,p);
p=p->next;
}
p=finish;
while(p!=NULL)
{prt2(algo,p);
p=p->next;
}
getchar();
}
void insert(PCB *q)
{PCB *p1,*s,*r;
int b;
s=q;
p1=ready;
r=p1;
b=1;
while((p1!=NULL)&&b)
if(p1->prio>=s->prio)
{r=p1;
p1=p1->next;
}
else
b=0;
if(r!=p1)
{r->next=s;
s->next=p1;
}
else
{s->next=p1;
ready=s;
}
}
void create(char alg)
{
 PCB *p;
 int i,time;
 char na[10];
 ready=NULL;
 finish=NULL;
 run=NULL;
 cout<<"输入进程名及其需要运行的时间:"<<endl;
 for(i=1;i<=N;i++)
 {p=new PCB;
 cin>>na;
 cin>>time;
 strcpy(p->name,na);
 p->cputime=0;
 p->needtime=time;
 p->state='w';
 p->prio=100-time;
 if(ready!=NULL)
  insert(p);
 else
 {p->next=ready;
 ready=p;
 }
 cout<<"输入进程名及其需要运行的时间:"<<endl;
 }
 prt(alg);
 run=ready;
 ready=ready->next;
 run->state='R';
}
void priority(char alg)
{
 while(run!=NULL)
 {run->cputime=run->cputime+10;
 run->needtime=run->needtime-10;
 run->prio=run->prio-10;
 if(run->needtime==0)
 {run->next=finish;
 finish=run;
 run->state='F';
 run=NULL;
 if(ready!=NULL)
  firstin();
 }
 else
  if((ready!=NULL)&&(run->prio<ready->prio))
  {
   run->state='W';
   insert(run);
   firstin();
  }
  prt(alg);
 }
}
void main()
{
 char algo='p';
 cout<<"输入进程的个数:";
 cin>>N;
 create(algo);
 priority(algo);
}
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的C++代码示例,实现了Round Robin调度算法: ```c++ #include <iostream> #include <queue> #include <utility> using namespace std; // 定义进程结构体 struct Process { int id; // 进程ID int arrival; // 到达时间 int burst; // 执行时间 int remaining; // 剩余执行时间 }; int main() { int n; // 进程数量 int quantum; // 时间片大小 int total_time = 0; // 总执行时间 queue<Process> ready_queue; // 就绪队列 // 输入进程信息 cout << "Enter the number of processes: "; cin >> n; cout << "Enter the quantum size: "; cin >> quantum; cout << "Enter the arrival time and burst time for each process:\n"; for (int i = 0; i < n; i++) { int arrival, burst; cout << "Process " << i + 1 << ": "; cin >> arrival >> burst; Process p = {i + 1, arrival, burst, burst}; ready_queue.push(p); total_time += burst; } // 执行调度 int current_time = 0; while (current_time < total_time) { Process p = ready_queue.front(); ready_queue.pop(); if (p.remaining > quantum) { // 如果剩余执行时间大于时间片大小,执行一次时间片 current_time += quantum; p.remaining -= quantum; // 将进程重新加入就绪队列 ready_queue.push(p); } else { // 如果剩余执行时间小于等于时间片大小,执行完当前进程 current_time += p.remaining; p.remaining = 0; cout << "Process " << p.id << " finished at time " << current_time << endl; } } return 0; } ``` 这段代码实现了一个简单的Round Robin调度算法,包括输入进程信息、执行调度等步骤。在执行调度时,先从就绪队列中取出一个进程,如果它的剩余执行时间大于时间片大小,则执行一次时间片并将其重新加入就绪队列,否则执行完当前进程。最终输出每个进程执行结束的时间。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值