您好,欢迎来到尚车旅游网。
搜索
您的当前位置:首页优先数调度文库

优先数调度文库

来源:尚车旅游网
设计一个有N个进程处于就绪队列时,单处理机情况下的调度程序采取“优先数调度”算法。

进程的就绪队列中是按优先数的大小从大到小排列,调度算法总是选取队列中优先数高的队列投入运行,采取动态地改变优先数的办法,进程每运行一次优先数相应的减2,从而避免一个作业长期占据处理机,当调度时机出现时,调度算法适时再调度,首先判断此进程是否运行完,未运行完再判断此进程的优先权是否大于等于队列的首进程的优先数,若成立,就继续执行。这样重复,直到就绪队列为空。 

#include #include #include 

#define N 5 #define NULL 0

#define LEN sizeof(struct PCB1) 

struct PCB1 {

 int pid; /*进程编号*/  int pri; /*优先权*/  int time; /**/  char sta; /*状态*/  struct PCB1 *next; }; 

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 *head;*/ {

 struct PCB1 *p;  p=head;

 if(head!=NULL)  {

 print11();  print13();  print11();  do

 {

 printf(\"|%9d|%9d|%10d|%9c|\\n\ print12();  p=p->next;  }

 while(p!=NULL);  print11();  } }



struct PCB1 *insert(struct PCB1 *head,struct PCB1 *p) /* 建立对进程进行优先级排列函数*/

/*struct PCB1 *head,*p;*/ {

 struct PCB1 *p0,*p1,*p2;

 p1=head;  p0=p;

 if(head==NULL) /*优先级最大者,插入队首*/  {

 head=p0;  p0->next=NULL;  }

 else /* 进程比较优先级,插入适当的位置中*/  {

 while((p0->pri<=p1->pri)&&(p1->next!=NULL))  {

 p2=p1;  p1=p1->next;  }

 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;  int i;

 head=NULL;

 printf(\"please input PCB\\n\");  printf(\"format: pri,runtime\\n\");  for (i=0;i {

 printf(\"please input p%d PCB\\n\ p=(struct PCB1 *)malloc(LEN);  scanf(\"%d %d\ p->pid=i+1;  p->sta='W';

 head=insert(head,p);  }

 return (head); } 

struct PCB1 *del(struct PCB1 *head,struct PCB1 *p) /*定义判断进程模块结构体*/

/*struct PCB1 *head,*p;*/ {

 p->sta='f';

 print1(p);

 printf(\"processes p%d del \\n\ return(head); }



void prio(struct PCB1 *head) /*进程判断模块*/ /*struct PCB1 *head;*/

{

 char flag;

 struct PCB1 *p,*rp;  p=head;

 while(p!=NULL)  {

 rp=p;  p=p->next;  do  {

 flag='y';

 printf(\"process p%d running\\n\ 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;  head=NULL;  print10();

 printf(\"input data\\n\");  head=creat1();  print1(head);

 printf(\"begin running\\n\");  prio(head);  printf(\"v\"); }

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- sceh.cn 版权所有 湘ICP备2023017654号-4

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务