优先数调度算法
设计一个有N个进程处于就绪队列时,单处理机情况下的调度程序采取“优先数调度”算法。
进程的就绪队列中是按优先数的大小从大到小排列,调度算法总是选取队列中优先数高的队列投入运行,采取动态地改变优先数的办法,进程每运行一次优先数相应的减2,从而避免一个作业长期占据处理机,当调度时机出现时,调度算法适时再调度,首先判断此进程是否运行完,未运行完再判断此进程的优先权是否大于等于队列的首进程的优先数,若成立,就继续执行。这样重复,直到就绪队列为空。
#include
#include
#include
#define N 5
#define NULL 0
#define LEN sizeof(struct PCB1)
struct PCB1
{
struct PCB1 *
};
void print10()
{
printf("*----------------------------------------*\n");
printf("| PRIORTY ALGORITHM |\n");
printf("*----------------------------------------*\n");
printf("\n");
}
void print11()
{
printf("+----------------------------------------+\n");
}
void print12()
{
printf("|----------------------------------------|\n");
}
void print13()
{
printf("| PID |PRIORITY | TIME | STA |\n");
}
void print1(struct PCB1 *head)
{
struct PCB1 *p;
p=
if(head!=NULL)
{
print11();
print13();
print11();
do
{
printf("|�|�|d|�|\n",p-&pid,p-&pri,p-&time,p-&sta);
print12();
p=p-&
}
while(p!=NULL);
print11();
}
}
struct PCB1 *insert(struct PCB1 *head,struct PCB1 *p)
{
struct PCB1 *p0,*p1,*p2;
p1=
p0=p;
if(head==NULL)
{
head=p0;
p0-&next=NULL;
}
else
{
while((p0-&pri&=p1-&pri)&&(p1-&next!=NULL))
{
p2=p1;
p1=p1-&
}
if(p0-&pri&p1-&pri)
{
if (head==p1)
head=p0;
else
p2-&next=p0;
p0-&next=p1;
}
else
{
p1-&next=p0;
p0-&next=NULL;
}
}
return(head);
}
struct PCB1 *creat1()
{
struct PCB1 *head,*p;
head=NULL;
printf("please input PCB\n");
printf("format: pri,runtime\n");
for (i=0;i&N;I++)
{
printf("please input p%d PCB\n",i+1);
p=(struct PCB1 *)malloc(LEN);
scanf("%d
%d",&p-&pri,&p-&time);
p-&pid=i+1;
p-&sta='W';
head=insert(head,p);
}
return (head);
}
struct PCB1 *del(struct PCB1 *head,struct PCB1 *p)
{
p-&sta='f';
print1(p);
printf("processes p%d del \n",p-&pid);
return(head);
}
void prio(struct PCB1 *head)
{
struct PCB1 *p,*
p=
while(p!=NULL)
{
rp=p;
p=p-&
do
{
flag='y';
printf("process p%d running\n",rp-&pid);
rp-&sta='r';
rp-&pri=rp-&pri-2;
print1(rp);
printf("again scheduling\?\n");
(rp-&time)--;
rp-&sta='w';
if (rp-&time==0)
p=del(p,rp);
else
{
if
((p==NULL)||(rp-&pri&=p-&pri))
flag='n';
else
{
p=insert(p,rp);
printf("insert after que\n");
print1(p);
}
}
}
while(flag=='n');
}
}
main()
{
struct PCB1 *
head=NULL;
print10();
printf("input data\n");
head=creat1();
print1(head);
printf("begin running\n");
prio(head);
printf("v");
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。【图文】财产险基础知识_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
评价文档:
财产险基础知识
上传于||暂无简介
大小:1.23MB
登录百度文库,专享文档复制特权,财富值每天免费拿!
你可能喜欢