#include<stdio.h>
#include<iostream.h>
#include <stdlib.h>
struct Process //PCB块结构体信息
{
char Pname[20]; //进程名
int priority; //进程优先级
int wanted_time; //需要运行的时间
char status; //进程状态,W是就绪,R是运行
int runtime; //进程已经执行的时间.初始值为0
struct Process *next;
}*head=NULL;
void inster(Process *p)
{
Process *f,*l;
int count=0;
if(head==NULL||p->priority>head->priority) //链表为空,或者要插入的进程优先高于表头的优先级
{
p->next=head;
head=p;
}
else
{
f=head;
l=f->next;
while(l!=NULL)
{
if((p->priority>l->priority))
{
p->next=l;
f->next=p;
l=NULL;
count=1;
}
else
{
f=f->next;
l=l->next;
}
}
if(count==0)
f->next=p;
}
}
void Creat(int m) //输入各个进程的信息
{
void desp(Process *p);
Process *r;
for(int i=0;i<m;i++)
{
r=new Process();
cout<<"进程名,进程优先级,需要运行的时间:";
cin>>r->Pname>>r->priority>>r->wanted_time;
r->runtime=0;
r->status='w';
r->next=NULL;
Process *p=r;
inster(p);
}
r=head;
cout<<"就绪队列:"<<endl;
while(r!=NULL)
{
desp(r);
r=r->next;
}
}
void desp(Process *p)
{
cout<<"进程名 "<<"优先级 "<<"状态 "<<"运行时间 "<<"已执行时间"<<endl;
cout<<p->Pname<<"\t"<<p->priority<<"\t"<<p->status<<"\t"<<p->wanted_time<<"\t"<<p->runtime<<endl;
}
void check(Process *p)
{
Process *t=head;
printf("\n *********正运行的进程**********: %s\n",p->Pname);
desp(p); //记住表头进程
printf("\n *********就绪的进程************:\n");
while(t!=NULL) //打印出剩余的所有进程的相关信息
{
desp(t);
t=t->next;
}
}
void destroy(Process *p) //打印进程执行信息提示
{
printf("\n进程 [%s] 完成.\n",p->Pname);
free(p); //释放临时变量p的内存空间
}
void running(Process *p)
{
p->runtime++;
if(p->runtime==p->wanted_time)
destroy(p);
else
{
p->priority=p->priority-3;
p->status='w';
inster(p); //重新插入链表
}
}
void main()
{
cout<<"输入进程的个数:"<<endl;
int m;
int h=0;
char ch;
cin>>m;
Creat(m);
Process *p;
Process *t=head;
while((m!=0)&&(head!=NULL)) //条件判断队列是否为空
{
h++;
printf("\n 执行的次数: %d \n",h); //取出最大优先数的进程 即链表表头进程
p=head;
head=head->next;
p->status='R';
p->next=NULL;
check(p);
running(p);
printf("\n按enter键继续..");
ch=getchar();
}
printf("\n\n所有的进程运行完毕!");
ch=getchar();
}
没有合适的资源?快使用搜索试试~ 我知道了~
Process-priority-scheduling.rar_priority scheduling
共1个文件
cpp:1个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 190 浏览量
2022-09-19
19:13:54
上传
评论
收藏 1KB RAR 举报
温馨提示
优先数调度算法常用于批处理系统中。在进程调度中,每次调度时,系统把处理机分配给就绪队列中优先数最高的进程。
资源推荐
资源详情
资源评论
收起资源包目录
Process-priority-scheduling.rar (1个子文件)
Process priority scheduling.cpp 3KB
共 1 条
- 1
资源评论
小贝德罗
- 粉丝: 69
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功