先来先服务:按照进程来的时间早晚属性来判断,先来的先执行
最短:按照进程运行需要的弯庆时间长短属性来判断,最短的先执行
时间片轮转:和进程属性无关,每个进埋中握程都分配相同的时间去运行,轮着来
优先权设置:根据进程的优培颤先级属性判断谁先执行,优先级是用户可以设定的
希望能够帮到你
没有完全符合的,但是差不多的,你自己改改吧!#include<iostream.h>
#include<stdlib.h>
#include<string.h>
typedef struct PCBA
{
char name[30]
int round
int prio
int cputime
int needtime
char state
struct PCBA *next
} PCB,*CurrentRunPCB,*LinkQueue
CurrentRunPCB SelMosHeiPrio(LinkQueue *Q)//从Q链表中选择一个优先级最大的进程
void InitQueue(LinkQueue *Q)//初始化链表Q,如果链表没有头指针,可以省略;
void InsertQueue(LinkQueue * Q,PCB* run)
{
}//将Run结点插入链表Q中,表示就绪队列里多了一个进程
void Priosch(LinkQueue Wait_Queue,LinkQueue Finish_Queue)//优先法运行,从就绪队列Wait_Queue中选择进程执行,(选择队列中优先级最高的进程圆首),执行结束的进程放入完成队列中;
{ while (WaitQueue!=NULL)
{
CurrentRunPCB run=SelMosHeiPrio(&WaitQueue) //run为就绪队列中优先级最高的进程
run->cputime+=2//时间片为2
run->needtime-=2
run->prio=50-run->needtime//动态优先级,但是优先级随着运行时间的增加,优先级增加,所以谁优先级高会一直占用CPU
run->state='r'
if(run->needtime>0)
{ run->state='w'
InsertQueue(&WaitQueue,run)
}
else
{cout<<"当前运行进称为:"<<run->name<<"运行结束后就绪队列情况:"<<'\n'
run->state='F'
run->needtime=0
InsertQueue(&FinishQueue,run)
Print(&WaitQueue)
}
//当然可以采用不看进程运行过程,直接将优先级高的运行完,插入完成队列
/*
CurrentRunPCB run=SelMosHeiPrio(&WaitQueue)
cout<<"当前运行进称为:"<<run->name<<"运行结束后就绪队列情况:"<<'\n'
Print(&WaitQueue)
run->cputime=run->needtime
run->needtime=0
run->prio=50//动态优先级,但是优先级随着运行时间的增加,优先级增加,所以谁优先级高会一直占用CPU
run->state='F'
InsertQueue(&FinishQueue,run) */
}
void Print(LinkQueue*Q)//将队列的元素显示输出,输出包含进程名,进程CPUTIME,NEEDTIME,状态,优先级
{LinkQueue p=*Q
cout<<"name cputime needtimestateprio'"<<'\n'
while (p!=NULL)
{ cout<<p->name<<'\t'<<p->cputime<<'\t'<<p->needtime<<'\t'<<p->state<<'\t'<<p->prio<<'\n'
p=p->next
}
}
CurrentRunPCB DeQueue(LinkQueue*Q)//从就绪队列中取出一个进程
{LinkQueue p=*Q
*Q=(*Q)->next
p->next=NULL
return p
}
void Roundsch(LinkQueue WaitQueue,LinkQueue FinishQueue)//轮转法运行,从就绪队列Wait_Queue中选择进程执行一个橘袭数时间片,执行结束的进程放入完成队列中;若一个时间片未能执行完的进程再插入到就禅薯绪队列
{ while (WaitQueue!=NULL)
{
CurrentRunPCB run=DeQueue(&WaitQueue)
cout<<"当前运行进称为:"<<run->name<<"运行结束后就绪队列情况:"<<'\n'
run->cputime+=2//时间片为2
run->needtime-=2
run->prio=50- run->cputime
run->state='r'
if(run->needtime>0)
{InsertQueue(&WaitQueue,run)
run->state='w'
}
else
{run->state='F'
run->needtime=0
InsertQueue(&FinishQueue,run)
}
Print(&WaitQueue)
}
cout<<"完成队列情况:"<<'\n'
Print(&FinishQueue)
}
void SetAllpro(LinkQueue*Q)//设置优先级函数50-p->needtime
{int max=0
LinkQueue p,t
t=NULL
p=*Q
if (p!=NULL)
{
max=p->prio
p=p->next
while(p)
{
if (max<p->prio) max=p->prio
p=p->next
}
p=*Q
t=*Q
if (t==p&&max==t->prio)
{*Q=(*Q)->next
t->next=NULL
return t}
else{
t=t->next
while(t)
{
if (max==t->prio)
{
p->next=t->next
t->next=NULL
return t
}
else{p=t
t=t->next
}
}
}
}
return t
}
void main()
{
PCBA *pcb0,*pcb1,*pcb2,*pcb3,*pcb4 //five processes 五个进程
LinkQueue Wait_Queue,Finish_Queue //两个队列 等待和完成
Wait_Queue=NULL//给队列赋初值,如果带有头指针的链表,可以用函数;
Finish_Queue=NULL
//InitQueue(&Wait_Queue)
//InitQueue(&Finish_Queue)
char ch
//给各个进程设置初值
pcb0= new PCBA()
pcb1= new PCBA()
pcb2= new PCBA()
pcb3= new PCBA()
pcb4= new PCBA()
//example
strcpy(pcb0->name,"process1")
pcb0->round=2
pcb0->prio=0
pcb0->cputime=0
pcb0->needtime=5
pcb0->state='W'
pcb0->next=NULL
strcpy(pcb1->name,"process2")
pcb1->round=2
pcb1->prio=0
pcb1->cputime=0
pcb1->needtime=7
pcb1->state='W'
pcb1->next=NULL
strcpy(pcb2->name,"process3")
pcb2->round=2
pcb2->prio=0
pcb2->cputime=0
pcb2->needtime=3
pcb2->state='W'
pcb2->next=NULL
strcpy(pcb3->name,"process4")
pcb3->round=2
pcb3->prio=0
pcb3->cputime=0
pcb3->needtime=11
pcb3->state='W'
pcb3->next=NULL
strcpy(pcb4->name,"process5")
pcb4->round=2
pcb4->prio=0
pcb4->cputime=0
pcb4->needtime=8
pcb4->state='W'
pcb4->next=NULL
//将各个进程插入就绪队列中
InsertQueue(&Wait_Queue,pcb0)
InsertQueue(&Wait_Queue,pcb1)
InsertQueue(&Wait_Queue,pcb2)
InsertQueue(&Wait_Queue,pcb3)
InsertQueue(&Wait_Queue,pcb4)
//利用此算法实现Wait_Queue中prio=50-needtime
SetAllpro(&Wait_Queue)
cout<<"请输入选择的调度算法(1 or 2 or anykey exit!):"<<endl
cin>>ch
switch(ch)
{
case '1':
Print(&Wait_Queue)
Roundsch(Wait_Queue,Finish_Queue)
break
case '2':
Print(&Wait_Queue)
Priosch(Wait_Queue,Finish_Queue)
break
default :
cout<<"你选择了退出!"<<endl
system("pause")
return
}
return
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)