高优先权优先调度算法
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
PCB* p = pBegin;PCB* q = p->next;PCB* pr= pBegin;
while(q != pEnd) {
if(q->priority < priority) {
p = p->next; swap(p,q);
}
q = q->next;
}
swap(p,pBegin);
return p;
}
void QuickSort(PCB* pBeign, PCB* pEnd){
if(pBeign != pEnd){
PCB* partion = GetPartion(pBeign,pEnd);
QuickSort(pBeign,partion);QuickSort(partion->next,pEnd);
q->priority=rand() % 10;
q->finish_time=0;q->turn_around_time=0;q->weight_turn_around_time=0;
q->next=NULL;head->next=q;head=head->next;pid++;pname++;
}
}
b->turn_around_time=t;
t=a->weight_turn_around_time;a->weight_turn_around_time=b->weight_turn_around_tim;
b->weight_turn_around_time=t;
t=a->priority; a->priority=b->priority; b->priority=t;
head->weight_turn_around_time=head->turn_around_time/head->serve_time;
cout<<"进程号"<<"优先级"<<"运行状态"<<"到达时间"<<"服务时间"<<"开始时间”
<<"完成时间"<<"周转时间"<<"带权周转时间"<<endl;
高优先级优先调度算法:
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<ctime>
#define MAX_POSSIBILITY 100
using namespace std;
typedefstruct PCB//创建进程PCB{
q->weight_turn_around_time=0;q->next=NULL;
}
void CreatePCB(PCB *p)//随机创建进程{
int n,pid=0;char pname='A';
cin>>N;//创建的进程数
PCB *head=p;head->sum=N;n=N;srand( (unsigned)time( NULL ) );//设置随机数种子
Output(head);
FPF(head);
}
while(n--){
PCB *q=(PCB *)malloc(sizeof(PCB));q->PID=pid;q->Pname=pname;
q->state=true;// true代表未被执行
q->arrive_time=rand() % MAX_POSSIBILITY;//初始化到达时间
q->serve_time=rand() % MAX_POSSIBILITY;//随机产生进程服务时间
void Run_FPF(PCB *p){
PCB *head=p;head->state=false;//表示已经执行
head->finish_time=Now_time+head->serve_time;//完成时间=开始时间+服务时间
head->turn_around_time=head->finish_time-head->arrive_time;//周转=完成-到达
t=a->serve_time;a->serve_time=b->serve_time;b->serve_time=t;
t=a->finish_time;a->finish_time=b->finish_time;b->finish_time=t;
t=a->turn_around_time;a->turn_around_time=b->turn_around_time;
head=head->next;
}
cout<<"\n\n";
}
void main(){
PCB *head;
head=(PCB *)malloc(sizeof(PCB));//进程链表头节点
InitHead(head);CreatePCB(head);
Output(head);QuickSort(head, NULL);
Now_time=head->finish_time;
}
void FPF(PCB *p){
QuickSort(p, NULL);int sum=0,n=0;PCB *head = p->next;
while(1){
n=0;
while(head){
if(head->state&&Now_time>=head->arrive_time){
PCB *head=p->next;
while(head){
cout<<"进程号"<<"优先级"<<"运行状态"<<"到达时间"<<"服务时间"<<"开始时间"<<"完成时间"<<"周转时间"<<"带权周转时间"<<endl;
cout<<" "<<head->PID<<"\t "<<head->priority<<"\t "<<head->state<<"\t "<<head->arrive_time<<"\t "<<head->serve_time<<"\t "<<Now_time<<"\t "<<head->finish_time<<"\t "<<head->turn_around_time<<"\t "<<head->weight_turn_around_time<<"\t\n"<<endl;
}
/*需要两个指针p和q,这两个指针均往next方向移动,
移动的过程中保持p之前的key都小于选定的key,
p和q之间的key都大于选定的key,
那么当q走到末尾的时候便完成了一次支点的寻找。*/
PCB* GetPartion(PCB *pBegin, PCB *pEnd){
int priority = pBegin->priority;
int sum;//头结点用于存放进程的总个数
int PID;//进程号
char Pname;//进程名
bool state;//进程运行状态
int arrive_time;//进程到达时间
//int start_time;//进程开始时间
int serve_time;//进场服务时间
int finish_time;//进程完成时间
float turn_around_time;//进程周转时间
float weight_turn_around_time;//进程带权周转时间
int priority;//进程优先级
PCB *next;//指向下一个进程
}PCB;
int Now_time,N;
void swap(PCB *a,PCB *b){
}
}
void InitHead(PCB *p){
PCB *q = p;q->PID=-1;q->Pname=-1;
q->state=false;// true代表未被执行
q->arrive_time=0;q->serve_time=0;q->finish_time=0;q->turn_around_time=0;q->priority=0;
int t;char ch;bool f;
t=a->PID;
a->PID=b->PID;
b->PID=t;
ch=a->Pname;a->Pname=b->Pname;b->Pname=ch;
f=a->state;a->state=b->state;b->state=f;
t=a->arrive_time;a->arrive_time=b->arrive_time;b->arrive_time=t;
//可执行状态,并且当前时间>=到达时间可以执行
Run_FPF(head);head=p->next;
sum++;n++;break;
}
head=head->next;
}
if(n==0)Now_time+Leabharlann Baidu;
if(sum==N)return;
head=p->next;
}
}
void Output(PCB *p){
cout<<" "<<head->PID<<"\t "<<head->priority<<"\t "<<head->state<<"\t "<<head->arrive_time<<"\t "<<head->serve_time<<"\t "<<Now_time<<"\t "<<head->finish_time<<"\t "<<head->turn_around_time<<"\t "<<head->weight_turn_around_time<<"\t\n"<<endl;
while(q != pEnd) {
if(q->priority < priority) {
p = p->next; swap(p,q);
}
q = q->next;
}
swap(p,pBegin);
return p;
}
void QuickSort(PCB* pBeign, PCB* pEnd){
if(pBeign != pEnd){
PCB* partion = GetPartion(pBeign,pEnd);
QuickSort(pBeign,partion);QuickSort(partion->next,pEnd);
q->priority=rand() % 10;
q->finish_time=0;q->turn_around_time=0;q->weight_turn_around_time=0;
q->next=NULL;head->next=q;head=head->next;pid++;pname++;
}
}
b->turn_around_time=t;
t=a->weight_turn_around_time;a->weight_turn_around_time=b->weight_turn_around_tim;
b->weight_turn_around_time=t;
t=a->priority; a->priority=b->priority; b->priority=t;
head->weight_turn_around_time=head->turn_around_time/head->serve_time;
cout<<"进程号"<<"优先级"<<"运行状态"<<"到达时间"<<"服务时间"<<"开始时间”
<<"完成时间"<<"周转时间"<<"带权周转时间"<<endl;
高优先级优先调度算法:
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<ctime>
#define MAX_POSSIBILITY 100
using namespace std;
typedefstruct PCB//创建进程PCB{
q->weight_turn_around_time=0;q->next=NULL;
}
void CreatePCB(PCB *p)//随机创建进程{
int n,pid=0;char pname='A';
cin>>N;//创建的进程数
PCB *head=p;head->sum=N;n=N;srand( (unsigned)time( NULL ) );//设置随机数种子
Output(head);
FPF(head);
}
while(n--){
PCB *q=(PCB *)malloc(sizeof(PCB));q->PID=pid;q->Pname=pname;
q->state=true;// true代表未被执行
q->arrive_time=rand() % MAX_POSSIBILITY;//初始化到达时间
q->serve_time=rand() % MAX_POSSIBILITY;//随机产生进程服务时间
void Run_FPF(PCB *p){
PCB *head=p;head->state=false;//表示已经执行
head->finish_time=Now_time+head->serve_time;//完成时间=开始时间+服务时间
head->turn_around_time=head->finish_time-head->arrive_time;//周转=完成-到达
t=a->serve_time;a->serve_time=b->serve_time;b->serve_time=t;
t=a->finish_time;a->finish_time=b->finish_time;b->finish_time=t;
t=a->turn_around_time;a->turn_around_time=b->turn_around_time;
head=head->next;
}
cout<<"\n\n";
}
void main(){
PCB *head;
head=(PCB *)malloc(sizeof(PCB));//进程链表头节点
InitHead(head);CreatePCB(head);
Output(head);QuickSort(head, NULL);
Now_time=head->finish_time;
}
void FPF(PCB *p){
QuickSort(p, NULL);int sum=0,n=0;PCB *head = p->next;
while(1){
n=0;
while(head){
if(head->state&&Now_time>=head->arrive_time){
PCB *head=p->next;
while(head){
cout<<"进程号"<<"优先级"<<"运行状态"<<"到达时间"<<"服务时间"<<"开始时间"<<"完成时间"<<"周转时间"<<"带权周转时间"<<endl;
cout<<" "<<head->PID<<"\t "<<head->priority<<"\t "<<head->state<<"\t "<<head->arrive_time<<"\t "<<head->serve_time<<"\t "<<Now_time<<"\t "<<head->finish_time<<"\t "<<head->turn_around_time<<"\t "<<head->weight_turn_around_time<<"\t\n"<<endl;
}
/*需要两个指针p和q,这两个指针均往next方向移动,
移动的过程中保持p之前的key都小于选定的key,
p和q之间的key都大于选定的key,
那么当q走到末尾的时候便完成了一次支点的寻找。*/
PCB* GetPartion(PCB *pBegin, PCB *pEnd){
int priority = pBegin->priority;
int sum;//头结点用于存放进程的总个数
int PID;//进程号
char Pname;//进程名
bool state;//进程运行状态
int arrive_time;//进程到达时间
//int start_time;//进程开始时间
int serve_time;//进场服务时间
int finish_time;//进程完成时间
float turn_around_time;//进程周转时间
float weight_turn_around_time;//进程带权周转时间
int priority;//进程优先级
PCB *next;//指向下一个进程
}PCB;
int Now_time,N;
void swap(PCB *a,PCB *b){
}
}
void InitHead(PCB *p){
PCB *q = p;q->PID=-1;q->Pname=-1;
q->state=false;// true代表未被执行
q->arrive_time=0;q->serve_time=0;q->finish_time=0;q->turn_around_time=0;q->priority=0;
int t;char ch;bool f;
t=a->PID;
a->PID=b->PID;
b->PID=t;
ch=a->Pname;a->Pname=b->Pname;b->Pname=ch;
f=a->state;a->state=b->state;b->state=f;
t=a->arrive_time;a->arrive_time=b->arrive_time;b->arrive_time=t;
//可执行状态,并且当前时间>=到达时间可以执行
Run_FPF(head);head=p->next;
sum++;n++;break;
}
head=head->next;
}
if(n==0)Now_time+Leabharlann Baidu;
if(sum==N)return;
head=p->next;
}
}
void Output(PCB *p){
cout<<" "<<head->PID<<"\t "<<head->priority<<"\t "<<head->state<<"\t "<<head->arrive_time<<"\t "<<head->serve_time<<"\t "<<Now_time<<"\t "<<head->finish_time<<"\t "<<head->turn_around_time<<"\t "<<head->weight_turn_around_time<<"\t\n"<<endl;