实验3 高(动态)优先权优先的进程调度算法模拟
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
cout<<pro[i].id;
}
cout<<endl<<"PRIORITY ";
for(i=0;i<j;i++)
{
cout.width(10);
cout<<pro[i].p;
}
cout<<endl<<"CPUTIME ";
for(i=0;i<j;i++)
{Hale Waihona Puke Baidu
cout.width(10);
cout<<pro[i].cputime;
RUNNING PROG:1
===============================================================
ID 0 1 2 3 4
PRIORITY 10 35 31 30 1
CPUTIME 0 1 0 0 0
ALLTIME 3 2 6 3 4
STARTBLOCK 1 -1 -1 -1 -1
(2)每个用来标识进程的进程控制块PCB用结构来描述,包括以下字段:
进程标识数ID;
进程优先数PRIORITY,并规定优先数越大的进程,其优先权越高;
进程已占用的CPU时间CPUTIME;
进程还需占用的CPU时间NEEDTIME。当进程运行完毕时,NEEDTIME变为0;
进程的阻塞时间STARTBLOCK,表示当进程再运行STARTBLOCK个时间片后,进程将进入阻塞状态;
实验3高(动态)优先权优先的进程调度算法模拟
1.
通过动态优先权算法的模拟加深对进程概念和进程调度过程的理解。
2.
装有操作系统Windows XP和开发工具VC++6.0,内存在256M以上的微机;
或者:装有Linux(Fedora 7)操作系统和gcc编译器,内存在256M以上的微机。
3.
(1)用C语言来实现对N个进程采用动态优先权优先算法的进程调度。
}
cout<<endl<<"ALLTIME ";
for(i=0;i<j;i++)
{
cout.width(10);
cout<<pro[i].alltime;
}
cout<<endl<<"STARTBLOCK";
for(i=0;i<j;i++)
{
cout.width(10);
cout<<pro[i].startblock;
{1,38,0,3,-1,0,0},
{2,30,0,6,-1,0,0},
{3,29,0,3,-1,0,0},
{4,0,0,4,-1,0,0}
};
int changestate0()
{
if(pro[0].startblock==0)
{
pro[0].state=-1;
pro[0].startblock--;
ALLTIME 3 1 6 3 4
STARTBLOCK 0 -1 -1 -1 -1
BLOCKTIME 3 0 0 0 0
STATE 0 0 0 0 0
RUNNING PROG:1
===============================================================
return 1;
}
if(pro[0].blocktime==0)
{
pro[0].state=0;
return 1;
}
if(pro[0].state==0&&pro[0].startblock!=-1)
{
pro[0].startblock--;return 1;}
if(pro[0].state==-1&&pro[0].blocktime!=0)
ID 0 2 3 4
PRIORITY 14 31 30 5
CPUTIME 0 1 1 0
ALLTIME 3 5 2 4
STARTBLOCK -1 -1 -1 -1
BLOCKTIME 1 0 0 0
STATE -1 0 0 0
RUNNING PROG:2
===============================================================
}
cout<<endl;
}
int main()
{
j=5;//刚开始有5个进程
while(j!=0)
{
for(i=0;i<j;i++)
{
if(pro[i].state==1){
for(;i<j;i++)
{
pro[i]=pro[i+1];}
j=j-1;
}
}
display();
getchar();
}
}
运行结果:
ID 0 2 3 4
PRIORITY 16 29 28 7
CPUTIME 0 2 2 0
ALLTIME 3 4 1 4
STARTBLOCK -1 -1 -1 -1
BLOCKTIME 0 0 0 0
STATE 0 0 0 0
RUNNING PROG:2
===============================================================
}
cout<<endl<<"BLOCKTIME ";
for(i=0;i<j;i++)
{
cout.width(10);
cout<<pro[i].blocktime;
}
cout<<endl<<"STATE ";
for(i=0;i<j;i++)
{
cout.width(10);
cout<<pro[i].state;
ID 0 1 2 3 4
PRIORITY 12 29 33 32 3
CPUTIME 0 3 0 0 0
ALLTIME 3 0 6 3 4
STARTBLOCK -1 -1 -1 -1 -1
BLOCKTIME 3 0 0 0 0
STATE -1 1 0 0 0
RUNNING PROG:2
===============================================================
==================================================================
IDPRIORITY CPUTIME NEEDTIME STATE STARTBLOCK BLOCKTIME
0 XX XX XX XX XX XX
1 XX XX XX XX XX XX
struct pcb
{
int id;
int p; //priority
int cputime;
int alltime;
int startblock;
int blocktime;
int state; //0表示ready 1表示end -1表示block
};
struct pcb pro[5]={
{0,9,0,3,2,3,0},
(5)为了清楚地观察进程的调度过程,程序应将每个时间片内的进程的情况显示出来,参照的具体格式如下:
RUNNING PROCESS:$id0
READY QUEUE: $id1->$id2
BLOCK QUEUE: $id3->$id4
FINISH QUEUE: $id0->$id1->$id2->$id3->$id4
ID 0 2 3 4
PRIORITY 15 28 31 6
CPUTIME 0 2 1 0
ALLTIME 3 4 2 4
STARTBLOCK -1 -1 -1 -1
BLOCKTIME 0 0 0 0
STATE -1 0 0 0
RUNNING PROG:3
===============================================================
{
change();
cout<<"RUNNING PROG:"<<m<<endl;
cout<<"===============================================================\n";
cout<<"ID ";
for(i=0;i<j;i++)
{
cout.width(10);
ID 0 2 3 4
PRIORITY 13 30 33 4
CPUTIME 0 1 0 0
ALLTIME 3 5 3 4
STARTBLOCK -1 -1 -1 -1
BLOCKTIME 2 0 0 0
STATE -1 0 0 0
RUNNING PROG:3
===============================================================
pro[i].p++;
}
for(i=0;i<j;i++)
{
if(pro[i].id==m)
x=i;
}
pro[x].cputime++;
pro[x].p=pro[x].p-4;
pro[x].alltime--;
if(pro[x].alltime==0)
{
pro[x].state=1;
}
}
void display()
{
pro[0].blocktime--;return 1;
}
}
int state0()
{
changestate0();
s=pro[0].p;
if(pro[0].state==-1)
s=-100;
return s;
}
int maxp()//求出最大priority
{
state0();
int max=s;
(2)将该算法执行过程与高响应比优先调度算法的执行过程进行比较。
#include <stdio.h>
#include <iostream>
using namespace std;
int i; //循环值
int j; //还在阻塞或就绪队列中的进程数
int s;
int m; //最大priority的id
进程每运行一个时间片,优先数减3。
(4)假设在调度前,系统中有5个进程,它们的初始状态如下:
ID01234
PRIORITY93830290
CPUTIME00000
NEEDTIME33634
STARTBLOCK2-1-1-1-1
BLOCKTIME30000
STATEREADYREADYREADYREADYREADY
ID 0 2 4
PRIORITY 19 24 10
CPUTIME 0 4 0
ALLTIME 3 2 4
STARTBLOCK -1 -1 -1
BLOCKTIME 0 0 0
STATE 0 0 0
RUNNING PROG:2
===============================================================
BLOCKTIME 3 0 0 0 0
STATE 0 0 0 0 0
RUNNING PROG:1
===============================================================
ID 0 1 2 3 4
PRIORITY 11 32 32 31 2
CPUTIME 0 2 0 0 0
ID 0 2 3 4
PRIORITY 18 27 26 9
CPUTIME 0 3 3 0
ALLTIME 3 3 0 4
STARTBLOCK -1 -1 -1 -1
BLOCKTIME 0 0 0 0
STATE 0 0 1 0
RUNNING PROG:2
===============================================================
m=pro[0].id;
for(i=0;i<j;i++)
{
if(pro[i+1].p>pro[i].p)
{
max=pro[i+1].p;
m=pro[i+1].id;
}
}
return m;
}
void change()
{
maxp();
int x;//得到m现在的数组编号
for(i=0;i<j;i++)
{
进程被阻塞的时间BLOCKTIME,表示已阻塞的进程再等待BLOCKTIME个时间片后,进程将转换成就绪状态;
进程状态STATE;(READY, RUNNING, BLOCK, FINISH)
队列指针NEXT,用来将PCB排成队列。
(3)优先数改变的原则:
进程在就绪队列中呆一个时间片,优先数增加1;
2 XX XX XX XX XX XX
3XX XX XX XX XX XX
4XX XX XX XX XX XX
====================================================================
4.
(1)将源程序(priority.c)和程序运行结果写入实验报告。
ID 0 2 3 4
PRIORITY 17 26 29 8
CPUTIME 0 3 2 0
ALLTIME 3 3 1 4
STARTBLOCK -1 -1 -1 -1
BLOCKTIME 0 0 0 0
STATE 0 0 0 0
RUNNING PROG:3
===============================================================
}
cout<<endl<<"PRIORITY ";
for(i=0;i<j;i++)
{
cout.width(10);
cout<<pro[i].p;
}
cout<<endl<<"CPUTIME ";
for(i=0;i<j;i++)
{Hale Waihona Puke Baidu
cout.width(10);
cout<<pro[i].cputime;
RUNNING PROG:1
===============================================================
ID 0 1 2 3 4
PRIORITY 10 35 31 30 1
CPUTIME 0 1 0 0 0
ALLTIME 3 2 6 3 4
STARTBLOCK 1 -1 -1 -1 -1
(2)每个用来标识进程的进程控制块PCB用结构来描述,包括以下字段:
进程标识数ID;
进程优先数PRIORITY,并规定优先数越大的进程,其优先权越高;
进程已占用的CPU时间CPUTIME;
进程还需占用的CPU时间NEEDTIME。当进程运行完毕时,NEEDTIME变为0;
进程的阻塞时间STARTBLOCK,表示当进程再运行STARTBLOCK个时间片后,进程将进入阻塞状态;
实验3高(动态)优先权优先的进程调度算法模拟
1.
通过动态优先权算法的模拟加深对进程概念和进程调度过程的理解。
2.
装有操作系统Windows XP和开发工具VC++6.0,内存在256M以上的微机;
或者:装有Linux(Fedora 7)操作系统和gcc编译器,内存在256M以上的微机。
3.
(1)用C语言来实现对N个进程采用动态优先权优先算法的进程调度。
}
cout<<endl<<"ALLTIME ";
for(i=0;i<j;i++)
{
cout.width(10);
cout<<pro[i].alltime;
}
cout<<endl<<"STARTBLOCK";
for(i=0;i<j;i++)
{
cout.width(10);
cout<<pro[i].startblock;
{1,38,0,3,-1,0,0},
{2,30,0,6,-1,0,0},
{3,29,0,3,-1,0,0},
{4,0,0,4,-1,0,0}
};
int changestate0()
{
if(pro[0].startblock==0)
{
pro[0].state=-1;
pro[0].startblock--;
ALLTIME 3 1 6 3 4
STARTBLOCK 0 -1 -1 -1 -1
BLOCKTIME 3 0 0 0 0
STATE 0 0 0 0 0
RUNNING PROG:1
===============================================================
return 1;
}
if(pro[0].blocktime==0)
{
pro[0].state=0;
return 1;
}
if(pro[0].state==0&&pro[0].startblock!=-1)
{
pro[0].startblock--;return 1;}
if(pro[0].state==-1&&pro[0].blocktime!=0)
ID 0 2 3 4
PRIORITY 14 31 30 5
CPUTIME 0 1 1 0
ALLTIME 3 5 2 4
STARTBLOCK -1 -1 -1 -1
BLOCKTIME 1 0 0 0
STATE -1 0 0 0
RUNNING PROG:2
===============================================================
}
cout<<endl;
}
int main()
{
j=5;//刚开始有5个进程
while(j!=0)
{
for(i=0;i<j;i++)
{
if(pro[i].state==1){
for(;i<j;i++)
{
pro[i]=pro[i+1];}
j=j-1;
}
}
display();
getchar();
}
}
运行结果:
ID 0 2 3 4
PRIORITY 16 29 28 7
CPUTIME 0 2 2 0
ALLTIME 3 4 1 4
STARTBLOCK -1 -1 -1 -1
BLOCKTIME 0 0 0 0
STATE 0 0 0 0
RUNNING PROG:2
===============================================================
}
cout<<endl<<"BLOCKTIME ";
for(i=0;i<j;i++)
{
cout.width(10);
cout<<pro[i].blocktime;
}
cout<<endl<<"STATE ";
for(i=0;i<j;i++)
{
cout.width(10);
cout<<pro[i].state;
ID 0 1 2 3 4
PRIORITY 12 29 33 32 3
CPUTIME 0 3 0 0 0
ALLTIME 3 0 6 3 4
STARTBLOCK -1 -1 -1 -1 -1
BLOCKTIME 3 0 0 0 0
STATE -1 1 0 0 0
RUNNING PROG:2
===============================================================
==================================================================
IDPRIORITY CPUTIME NEEDTIME STATE STARTBLOCK BLOCKTIME
0 XX XX XX XX XX XX
1 XX XX XX XX XX XX
struct pcb
{
int id;
int p; //priority
int cputime;
int alltime;
int startblock;
int blocktime;
int state; //0表示ready 1表示end -1表示block
};
struct pcb pro[5]={
{0,9,0,3,2,3,0},
(5)为了清楚地观察进程的调度过程,程序应将每个时间片内的进程的情况显示出来,参照的具体格式如下:
RUNNING PROCESS:$id0
READY QUEUE: $id1->$id2
BLOCK QUEUE: $id3->$id4
FINISH QUEUE: $id0->$id1->$id2->$id3->$id4
ID 0 2 3 4
PRIORITY 15 28 31 6
CPUTIME 0 2 1 0
ALLTIME 3 4 2 4
STARTBLOCK -1 -1 -1 -1
BLOCKTIME 0 0 0 0
STATE -1 0 0 0
RUNNING PROG:3
===============================================================
{
change();
cout<<"RUNNING PROG:"<<m<<endl;
cout<<"===============================================================\n";
cout<<"ID ";
for(i=0;i<j;i++)
{
cout.width(10);
ID 0 2 3 4
PRIORITY 13 30 33 4
CPUTIME 0 1 0 0
ALLTIME 3 5 3 4
STARTBLOCK -1 -1 -1 -1
BLOCKTIME 2 0 0 0
STATE -1 0 0 0
RUNNING PROG:3
===============================================================
pro[i].p++;
}
for(i=0;i<j;i++)
{
if(pro[i].id==m)
x=i;
}
pro[x].cputime++;
pro[x].p=pro[x].p-4;
pro[x].alltime--;
if(pro[x].alltime==0)
{
pro[x].state=1;
}
}
void display()
{
pro[0].blocktime--;return 1;
}
}
int state0()
{
changestate0();
s=pro[0].p;
if(pro[0].state==-1)
s=-100;
return s;
}
int maxp()//求出最大priority
{
state0();
int max=s;
(2)将该算法执行过程与高响应比优先调度算法的执行过程进行比较。
#include <stdio.h>
#include <iostream>
using namespace std;
int i; //循环值
int j; //还在阻塞或就绪队列中的进程数
int s;
int m; //最大priority的id
进程每运行一个时间片,优先数减3。
(4)假设在调度前,系统中有5个进程,它们的初始状态如下:
ID01234
PRIORITY93830290
CPUTIME00000
NEEDTIME33634
STARTBLOCK2-1-1-1-1
BLOCKTIME30000
STATEREADYREADYREADYREADYREADY
ID 0 2 4
PRIORITY 19 24 10
CPUTIME 0 4 0
ALLTIME 3 2 4
STARTBLOCK -1 -1 -1
BLOCKTIME 0 0 0
STATE 0 0 0
RUNNING PROG:2
===============================================================
BLOCKTIME 3 0 0 0 0
STATE 0 0 0 0 0
RUNNING PROG:1
===============================================================
ID 0 1 2 3 4
PRIORITY 11 32 32 31 2
CPUTIME 0 2 0 0 0
ID 0 2 3 4
PRIORITY 18 27 26 9
CPUTIME 0 3 3 0
ALLTIME 3 3 0 4
STARTBLOCK -1 -1 -1 -1
BLOCKTIME 0 0 0 0
STATE 0 0 1 0
RUNNING PROG:2
===============================================================
m=pro[0].id;
for(i=0;i<j;i++)
{
if(pro[i+1].p>pro[i].p)
{
max=pro[i+1].p;
m=pro[i+1].id;
}
}
return m;
}
void change()
{
maxp();
int x;//得到m现在的数组编号
for(i=0;i<j;i++)
{
进程被阻塞的时间BLOCKTIME,表示已阻塞的进程再等待BLOCKTIME个时间片后,进程将转换成就绪状态;
进程状态STATE;(READY, RUNNING, BLOCK, FINISH)
队列指针NEXT,用来将PCB排成队列。
(3)优先数改变的原则:
进程在就绪队列中呆一个时间片,优先数增加1;
2 XX XX XX XX XX XX
3XX XX XX XX XX XX
4XX XX XX XX XX XX
====================================================================
4.
(1)将源程序(priority.c)和程序运行结果写入实验报告。
ID 0 2 3 4
PRIORITY 17 26 29 8
CPUTIME 0 3 2 0
ALLTIME 3 3 1 4
STARTBLOCK -1 -1 -1 -1
BLOCKTIME 0 0 0 0
STATE 0 0 0 0
RUNNING PROG:3
===============================================================