调度算法一:采用基于静态优先级的立即抢占式调度算法

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

#include
#include
#include
struct pcb_list
{
int ID;
int Priority;
int In1;
int In2;
int Alltime;
struct pcb_list *next;
}; //PCB结构体
pcb_list *CreatePcb()
{
FILE *fp;
pcb_list *head,*s,*r;
if((fp=fopen( "jc.txt ", "r "))==NULL) //打开文件
{
cout < < "Can 't open this file! " < exit(0);
}
s=(pcb_list*)malloc(sizeof(pcb_list)); //分配节点空间
head=r=NULL;
fscanf(fp, "%d%d%d%d%d ",&s-> ID,&s-> Priority,&s-> In1,&s-> In2,&s-> Alltime); //读文件
while(s-> ID!=3)
{
if(head==NULL)head=s;
else r-> next=s;
r=s;
s=(pcb_list*)malloc(sizeof(pcb_list));
fscanf(fp, "%d%d%d%d%d ",&s-> ID,&s-> Priority,&s-> In1,&s-> In2,&s-> Alltime);
}
r-> next=s;r=s;
if(r!=NULL)
r-> next=NULL;
fclose(fp);
return head; //返回头指针
}


void Scheduling(struct pcb_list *head) //基于静态优先级的立即抢占式调度
{ pcb_list *p,*q,*r;
p=head; q=p-> next;r=q-> next;//头节点

if(p-> Priority Priority) //第一个进程的优先级小于第二个进程的优先级
{ int time1=(q-> In1*60+q-> In2)-(p-> In1*60+p-> In2);
cout < In1 < < ": " < In2 < < "~ " < In1 < < ": " < In2 < < '0 ' < < " " < ID < if(q-> Priority Priority) //第二个进程的优先级小于第三个进程的优先级
{ int time2=(r-> In1*60+r-> In2)-(q-> In1*60+q-> In2);
int t1=(r-> In1*60+r-> In2+r-> Alltime)/60;
int t2=(r-> In1*60+r-> In2+r-> Alltime)%60;
cout < In1 < < ": " < In2 < < '0 ' < < "~ " < In1 < < ": " < In2 < < " " < ID < cout < In1 < < ": " < In2 < < "~ " < Alltime < < " " < ID < int t5=q-> Alltime-time2;
int t3=(t1*60+t2+t5)/60;
int t4=(t1*60+t2+t5)%60;
cout < ID < int t6=(p-> In1*60+p-> In2+p-> Alltime+q-> Alltime+r-> Alltime)/60;
int t7=(p-> In1*60+p-> In2+p-> Alltime+q-> Alltime+r-> Alltime)%60;
cout < Alltime-time1 < < " " < ID < }//if
else
{ //第二个进程的优先级大于第三个进程的优先级
int q1=(q-> In1*60+q-> In2+q-> Alltime)/60;
int q2=(q-> In1*60+q-> In2+q-> Alltime)%60;
//打印第二个进程的完整信息
cout < In1 < < ": " < In2 < < '0 ' < < "~ " <

< ": " < Alltime < < " " < ID < if(p-> Priority> r-> Priority)
{ //第一个进程的优先级大于第三个进程的优先级
int m1=(q1*60+q2+p-> Alltime-time1)/60;
int m2=(q1*60+q2+p-> Alltime-time1)%60;
cout < Alltime-time1 < < " " < ID < int m3=(m1*60+m2+r-> Alltime)/60;
int m4=(m1*60+m2+r-> Alltime)%60;
cout < Alltime < < " " < ID < }
else
{ //第一个进程的优先级小于第三个进程的优先级
int q3=(q1*60+q2+r-> Alltime)/60;
int q4=(q1*60+q2+r-> Alltime)%60;
cout < Alltime < < " " < ID < int t6=(p-> In1*60+p-> In2+p-> Alltime+q-> Alltime+r-> Alltime)/60;
int t7=(p-> In1*60+p-> In2+p-> Alltime+q-> Alltime+r-> Alltime)%60;
cout < Alltime-time1 < < " " < ID <
}
}//else

}//if
else if(p-> Priority Priority) //第一个进程的优先级大于第二个进程的优先级
{ // 且第一个进程的优先级小于第三个进程的优先级
int time3=r-> In1*60+r-> In2-p-> In1*60-p-> In2;//69
cout < In1 < < ": " < In2 < < "~ " < In1 < < ": " < In2 < < " " < ID < int a1=(r-> In1*60+r-> In2+r-> Alltime)/60;
int a2=(r-> In1*60+r-> In2+r-> Alltime)%60;
cout < In1 < < ": " < In2 < < "~ " < Alltime < < " " < ID < int a3=(a1*60+a2+p-> Alltime-time3)/60;
int a4=(a1*60+a2+p-> Alltime-time3)%60;
cout < Alltime-time3 < < " " < ID < int a5=(p-> In1*60+p-> In2+p-> Alltime+q-> Alltime+r-> Alltime)/60;
int a6=(p-> In1*60+p-> In2+p-> Alltime+q-> Alltime+r-> Alltime)%60;
cout < Alltime < < " " < ID < }
else
{ //第一个进程的优先级最大
int p1=(p-> In1*60+p-> In2+p-> Alltime)/60;
int p2=(p-> In1*60+p-> In2+p-> Alltime)%60;
//打印第一个进程的完整信息
cout < In1 < < ": " < In2 < < "~ " < Alltime < < " " < ID < if(q-> Priority> r-> Priority) //第二个进程的优先级大于第三个进程的优先级
{
//打印第二个进程的完整信息
int r1=(p1*60+p2+q-> All

time)/60;
int r2=(p1*60+p2+q-> Alltime)%60;
cout < Alltime < < " " < ID < //打印第三个进程的完整信息
int r3=(p-> In1*60+p-> In2+p-> Alltime+q-> Alltime+r-> Alltime)/60;
int r4=(p-> In1*60+p-> In2+p-> Alltime+q-> Alltime+r-> Alltime)%60;
cout < Alltime < < " " < ID <
}
else
{ //第二个进程的优先级小于第三个进程的优先级
int p3=(p1*60+p2+r-> Alltime)/60;
int p4=(p1*60+p2+r-> Alltime)%60;
//打印第三个进程的完整信息
cout < Alltime < < " " < ID < int p5=(p-> In1*60+p-> In2+p-> Alltime+q-> Alltime+r-> Alltime)/60;
int p6=(p-> In1*60+p-> In2+p-> Alltime+q-> Alltime+r-> Alltime)%60;
//打印第二个进程的完整信息
cout < Alltime < < " " < ID <
}
}


}//Scheduling


void main()
{pcb_list *h;
h=CreatePcb();
Scheduling(h);
}



相关文档
最新文档