实验2进程调度.docx
《实验2进程调度.docx》由会员分享,可在线阅读,更多相关《实验2进程调度.docx(21页珍藏版)》请在冰豆网上搜索。
实验2进程调度
实验2:
进程调度
要求:
1、设计进程控制块PCB的结构,分别适用于优先权调度算法和时间片轮转调度算法。
2、建立进程就绪队列。
3、编制两种进程调度算法:
优先权调度(实现动态优先级)和时间片轮转调度。
提示:
1、假设利用两种算法对五个进程进行调度,每个进程有运行、就绪、阻塞三种状态,初始状态为就绪态。
2、为了便于处理,程序中的某进程运行时间以时间片为单位计算。
各进程的优先数或轮转时间数以及进程需运行的时间片数的初值由用户给定。
3、在优先权调度算法中,优先数可以先取50,进程每执行一次,优先数减3,CPU时间片数加1,进程还需要的时间片数减1。
在时间片轮转调度算法中,采用固定时间片,即每执行一次进程,该进程的执行时间片数为已执行了2个单位,这时CPU时间片数加2,进程还需要的时间片数减2,并将该进程排列到就绪队列的队尾。
4、对于优先数一致的情况,采用FIFO策略解决。
源程序代码:
#include"stdio.h"
#include"stdlib.h"
#include"iostream.h"
#defineNULL0
#definefalse0
#definetrue1
bool_state=0;
structPCB
{
intID;
intpriority;
intCPUtime;
intALLtime;
intState;
PCB*next;
};
voidinit();/*产生idle进程,输入用户进程数目,调用insert()*/
voidprint(PCB*pcb);/*输出进程属性信息*/
voidprint_init(PCB*pcb);/*输出所有PCB的初始值*/
voidinsert();/*生成进程属性信息,插入进程就绪队列*/
voidRun_priority(PCB*pcb);/*运行进程,随机阻塞进程、产生新进程,插入就绪队列,唤醒阻塞进程*/
voidblock(PCB*pcb);/*调用destroy()将进程插入阻塞队列*/
voidwakeup();/*唤醒进程,插入就绪队列*/
voidproc_priority();/*优先权调度算法模拟*/
voidproc_loop();/*轮转法调度算法模拟*/
voidupdate(PCB*pcb);/*更新进程信息*/
voidpushback_queue(PCB*queue,PCB*item);/*将item插入到队列的尾部*/
voidinsert_queue(PCB*queue,PCB*item);/*将item插入到队列中,使得插入后,队列中按照优先级从高到低有序*/
voidsort_queue(PCB*&queue);/*对queue中的结点进行排序,按照优先级从大到小*/
PCB*ready_queue,*block_queue,*idleprocess;/*就绪队列,阻塞队列及闲逛进程指针变量*/
intmain(intargc,char*argv[])
{
inti=0;
while
(1)
{
cout<<"\\************PROCESS*************/";
cout<<("\nPleaseselectanumin(1,2,0)");
cout<<("\n1--priority");
cout<<("\n2--loop");
cout<<("\n0--exit\n");
cout<<"----------------------------------";
cout<cout<<"Pleaseselectanum:
";
cin>>i;
while(i)
{
if(i==1)
{
cout<<("\nThisisaexampleforpriorityprocessing!
\n");
init();
proc_priority();
}
elseif(i==2)
{
cout<<("\nThisisaexampleforroundrobinprocessing!
\n");
init();
proc_loop();
}
else
{
cout<<"Pleaseselectanumin(1,2,0)!
!
!
\n";
}
cout<<"Pleaseselectanum:
";
cin>>i;
}
return0;
}
}
//输出所有PCB的初始值
voidprint_init(PCB*pcb)
{
PCB*temp=pcb->next;
cout<<("\nIDpriorityCPUtimeALLtimeState");
while(temp!
=NULL)
{
cout<<"\n"<ID<<""<priority<<""<CPUtime<<""<ALLtime;
if(temp->State==0)
cout<<("ready");
elseif(temp->State==1)
cout<<("running");
else
cout<<("blocked");
temp=temp->next;
}
}
//输出进程属性信息
voidprint(PCB*pcb)
{
PCB*temp;
temp=pcb;
if(pcb->ID==0)
cout<<("\nTheidlepeocessidrunning!
");
else
{
cout<<"\n"<ID<<""<priority<<""<CPUtime<<""<ALLtime;
if(temp->State==0)
cout<<("ready");
elseif(temp->State==1)
cout<<("running");
else
cout<<("blocked");
}
}
//将item插入到队列中,使得插入后,队列中按照优先级从高到低有序
voidinsert_queue(PCB*queue,PCB*item)
{
PCB*p,*q;
q=queue;p=q->next;
while(p!
=0&&p->priority>=item->priority)
{
q=p;
p=p->next;
}
if(p==0)
{
item->next=0;
q->next=item;
}
else
{
item->next=p;
q->next=item;
}
}
//将item插入到阻塞队列的尾部
voidpushback_queue(PCB*queue,PCB*item)
{
PCB*p,*q;
q=queue,p=q->next;
while(p!
=0)
{
q=p;
p=p->next;
}
item->next=q->next;
q->next=item;
}
//对queue中的结点进行排序,按照优先级从大到小
voidsort_queue(PCB*&queue)
{
PCB*temp=newPCB;
temp->next=0;
while(queue->next)
{
PCB*p;
p=queue->next;
queue->next=p->next;
insert_queue(temp,p);
}
queue->next=temp->next;
deletetemp;
}
//生成进程属性信息,插入进程就绪队列,显示进程信息
voidinsert()
{
PCB*newp=0;
staticlongid=0;
newp=newPCB;
id++;
newp->ID=id;
newp->State=0;
newp->CPUtime=0;
newp->priority=rand()%3+1;
newp->ALLtime=rand()%3+1;
newp->next=NULL;
pushback_queue(ready_queue,newp);
//print(newp);
//cout<<("建立:
Creating->ready\n");
}
//生成n个进程属性信息,插入进程就绪队列,显示进程信息
voidinsert(intn)
{
for(inti=0;iinsert();
}
//产生idle进程,输入用户进程数目,调用insert()
voidinit()
{
block_queue=newPCB;
block_queue->next=0;
ready_queue=newPCB;
ready_queue->next=0;
inti=0,pcb_number=-1;//闲逛进程放入就绪队列
idleprocess=NULL;
idleprocess=(PCB*)malloc(sizeof(PCB));
idleprocess->ID=0;
idleprocess->State=0;
idleprocess->CPUtime=0;
idleprocess->priority=0;
idleprocess->ALLtime=0;
idleprocess->next=NULL;
idleprocess->next=ready_queue->next;//闲逛进程放入就绪队列
ready_queue->next=idleprocess;
//也可以假定初始时系统中只有一个idle进程
//输入初始进程的个数
while(pcb_number<0)
{
cout<<("\nInputthenumberofPCBtobestart:
");
cin>>pcb_number;
}
cout<<("\nIDpriorityCPUtimeALLtimeState\n");
for(;iinsert();
cout<<"就绪队列初始化成功!
"<:
:
print_init(ready_queue);
cout<}
//调用destroy()将进程插入阻塞队列
voidblock(PCB*pcb)
{
pcb->State=2;
pcb->CPUtime-=2;
if(pcb->CPUtime<=0)
{
pcb->CPUtime+=2;
}
cout<<"\nTheprocessno"<ID<<"isblocked!
";
print(pcb);
cout<<("变迁2:
running->blocked\n");
pcb->next=block_queue->next;
block_queue->next=pcb;
}
//更新进程信息,就绪队列中进程的优先级均增加1
voidupdate(PCB*pcb)
{
PCB*temp=pcb->next;
while(temp&&temp->next)
{
temp->priority++;
temp=temp->next;
}
}
//唤醒进程,插入就绪队列
voidwakeup()
{
if(block_queue->next==0)/*此时没有阻塞的进程,无所谓的唤醒*/
return;
PCB*q,*p;
while(true)
{
q=block_queue;
p=q->next;
while(p&&rand()%20!
=1)
{
q=p;
p=p->next;
}
if(p!
=0)
{
q->next=p->next;
break;
}
}
p->State=0;
cout<print(p);
cout<<"变迁3:
blocked->ready"<insert_queue(ready_queue,p);
}
//运行进程,随机阻塞进程、产生新进程,插入就绪队列,唤醒阻塞进程
voidRun_priority(PCB*pcb)
{
if(pcb->ID==0)
{
insert_queue(ready_queue,pcb);
print(pcb);
cout<<"变迁1:
ready->running\n";
}
else
{
pcb->State=1;
pcb->CPUtime+=4;
pcb->priority=pcb->priority-3;/*每运行一个时间片,其优先数减3*/
if(pcb->priority<1)
pcb->priority=1;
print(pcb);
printf("变迁1:
ready->running\n");
if(rand()%3==1)/*PCB不是闲逛进程,满足条件侧阻塞此进程*/
{
if(pcb->CPUtime-2ALLtime)
block(pcb);
else/*已执行完毕,应该销毁进程*/
{
cout<<'\n';
cout<<"Theprocessno"<ID<<"iscompleted!
销毁:
running->Destroy"<deletepcb;
}
}
else/*否则看该进程是否执行完毕,如果执行完,则释放,否则再次放入就绪队列*/
{
if(pcb->CPUtime>=pcb->ALLtime)/*释放*/
{
cout<<'\n';
cout<<"Theprocessno"<ID<<"ifcompleted!
销毁:
running->Destrory"<deletepcb;
}
else
{
:
:
insert_queue(:
:
ready_queue,pcb);
}
}
}
update(ready_queue);/*更新就绪队列的优先级数*/
if(rand()%5==1)
{
insert(3);
:
:
sort_queue(ready_queue);
}
if(rand()%7==1)
wakeup();
}
//运行进程,随机阻塞进程、产生新进程,插入就绪队列,唤醒阻塞进程
voidRun_loop(PCB*pcb)
{
if(pcb->ID==0)
{
insert_queue(ready_queue,pcb);
print(pcb);
cout<<"变迁1:
ready->running\n";
}
else
{
pcb->State=1;
pcb->CPUtime=pcb->ALLtime;
print(pcb);
printf("变迁1:
ready->running\n");
if(rand()%3==1)/*PCB不是闲逛进程,满足条件侧阻塞此进程*/
{
_state=1;
block(pcb);
}
else
{
cout<<'\n';
cout<<"Theprocessno"<ID<<"ifcompleted!
销毁:
running->Destrory"<deletepcb;
}
}
if(rand()%5==1)
{
insert(3);
}
if(rand()%7==1)
wakeup();
}
//优先权调度算法模拟
voidproc_priority()
{
sort_queue(ready_queue);
PCB*temp=0,*running=0;
inttimes=0;
cout<<"\n调度前:
";
:
:
print_init(ready_queue);
for(;times<5;times++)
{
running=ready_queue->next;
ready_queue->next=running->next;
cout<cout<<"\n调度开始:
"<Run_priority(running);
}
cout<<"\n本次调度结束。
"<}
//轮转调度算法模拟
voidproc_loop()
{
PCB*temp=0,*running=0;
inttimes=10;
cout<<"\n调度前:
";
:
:
print_init(ready_queue);
while
(1)
{
running=ready_queue->next;
ready_queue->next=running->next;
cout<if(times>0)
{
times=times-running->ALLtime;//每次运行一个进程减去ALLtime;
if(times>=0)
{
Run_loop(running);
}
elseif(_state)//如果运行时被阻塞,则运行2个时间单位
{
times=times+2;
_state=0;
cout<<"5487584574389574fgfgfgfgfgfgfg38954378954375894378954375";
}
else
pushback_queue(block_queue,running);//时间不过,则阻塞该进程,放到阻塞队列
}
elseif(times<=0)
{
cout<<"\n本次调度时间片到!
";
break;
}
}
cout<<"\n本次调度结束。
"<}
优先权调度
(1)输入1选择优先权调度算法模拟。
(2)输入开始进程个数n,创建n个PCB并加入就绪队列ready_queue中。
(3)就绪队列ready_queue不为空,调度就绪队列中第一个进程运行,否则,从闲逛队列idleprocess中调度闲逛进程运行。
(4)在运行过程中,当遇到阻塞,则该进程插入到阻塞队列block_queue中,且将该进程从ready_queue中删除。
(5)如果运行时间CPUtime大于等于Alltime,该进程运行完毕,释放该进程;否则插入到就绪队列中。
(6)更新就绪队列中的优先级数。
(7)随机对阻塞队列block_queue中的进程PCB询问是否要唤醒,唤醒,即从唤醒队列中选择第一个进程,且插入就绪队列中;阻塞队列中没有阻塞进程返回。
(8)重复上述步骤,直到本次调度结束。
时间片轮转调度
(1)输入2选择优先权调度算法模拟。
(2)输入开始进程个数n,创建n个PCB并加入就绪队列ready_queue中。
(3)就绪队列ready_queue不为空,调度就绪队列中第一个进程运行,否则,从闲逛队列idleprocess中调度闲逛进程运行。
(4)在运行过程中,当遇到阻塞,则该进程插入到阻塞队列block_queue中,且将该进程从ready_queue中删除。
(5)如果运行时间CPUtime大于等于Alltime,该进程运行完毕,释放该进程;否则插入到就绪队列中。
(6)随机对阻塞队列block_queue中的进程PCB询问是否要唤醒,唤醒,即从唤醒队列中选择第一个进程,且插入就绪队列中;阻塞队列中没有阻塞进程返回。
(7)如果时间到,本次调度结束,否则重复上述步骤,直到本次调度结束。