操作系统实验包括处理机调度页面置换驱动调度银行家.docx

上传人:b****3 文档编号:26566607 上传时间:2023-06-20 格式:DOCX 页数:38 大小:23.28KB
下载 相关 举报
操作系统实验包括处理机调度页面置换驱动调度银行家.docx_第1页
第1页 / 共38页
操作系统实验包括处理机调度页面置换驱动调度银行家.docx_第2页
第2页 / 共38页
操作系统实验包括处理机调度页面置换驱动调度银行家.docx_第3页
第3页 / 共38页
操作系统实验包括处理机调度页面置换驱动调度银行家.docx_第4页
第4页 / 共38页
操作系统实验包括处理机调度页面置换驱动调度银行家.docx_第5页
第5页 / 共38页
点击查看更多>>
下载资源
资源描述

操作系统实验包括处理机调度页面置换驱动调度银行家.docx

《操作系统实验包括处理机调度页面置换驱动调度银行家.docx》由会员分享,可在线阅读,更多相关《操作系统实验包括处理机调度页面置换驱动调度银行家.docx(38页珍藏版)》请在冰豆网上搜索。

操作系统实验包括处理机调度页面置换驱动调度银行家.docx

操作系统实验包括处理机调度页面置换驱动调度银行家

#include

#include

#include

#include

#include

#include

#defineFalse0

#defineTrue1

#defineL20//页面走向长度最大为20

#defineMAX100

usingnamespacestd;

intMax[100][100]={0};//各进程所需各类资源的最大需求

intAvaliable[100]={0};//系统可用资源

charname[100]={0};//资源的名称

intAllocation[100][100]={0};//系统已分配资源

intNeed[100][100]={0};//还需要资源

intRequest[100]={0};//请求资源向量

inttemp[100]={0};//存放安全序列

intWork[100]={0};//存放系统可提供资源

intM=100;//进程的最大数为

intN=100;//资源的最大数为

intMemory;//内存块

structPro//定义一个结构体(页面置换)

{

intnum,time;

};

//处理器调度部分代码

structpcb

{

charname[20];

pcb*next;

intarrivetime,runtime;

charstate;//状态

intpriority;//优先级

pcb(){next=NULL;}

};

enumKind

{_EMPTY,_FCFS,_ROUNDROBIN,_PRIORITY};

classprocess

{

pcb*head;

intnumber;

Kindkind;

public:

process();

~process();

voidClear();//清空进程序列

voidInputProcess();//输入进程序列

voidRunProcess();//运行进程序列

voidFCFS();//先来先服务算法

voidRoundRobin(inttime=1);//按时间片轮转法

voidPriority(inttime=1);//优先级调度算法

};

process:

:

process()

{

head=NULL;

number=0;

kind=_EMPTY;

}

process:

:

~process()

{

Clear();

}

voidprocess:

:

Clear()

{

pcb*p1,*p2;

for(p1=head;p1;p1=p2)

{

p2=p1->next;

deletep1;

}

head=NULL;

number=0;

kind=_EMPTY;

}

voidprocess:

:

InputProcess()

{

if(head!

=NULL)

Clear();

intk;

cout<<"-----------------------------------------------"<

cout<<"1:

先来先服务"<

cout<<"2:

时间片轮转"<

cout<<"3:

优先级调度"<

cout<<"------------------------------------------------"<

cout<<"请选择:

";

cin>>k;

switch(k)

{

case1:

kind=_FCFS;break;

case2:

kind=_ROUNDROBIN;break;

case3:

kind=_PRIORITY;break;

}

cout<<"输入进程数:

";

cin>>number;

pcb*rear,*p;

for(inti=0;i

{

p=newpcb;

if(kind==_PRIORITY)

{

cout<<"输入第"<

";

cin>>p->name>>p->arrivetime>>p->runtime>>p->priority;

}

else

{

cout<<"输入第"<

";

cin>>p->name>>p->arrivetime>>p->runtime;

}

if(head==NULL)

{

head=rear=p;

}

else

{

rear->next=p;rear=p;

}

}

}

voidprocess:

:

RunProcess()

{

switch(kind)

{

case_FCFS:

FCFS();break;

case_ROUNDROBIN:

RoundRobin();break;

case_PRIORITY:

Priority();break;

default:

cout<<"进程序列未创建!

"<

}

}

voidprocess:

:

FCFS()

{

cout<<"先来先服务算法:

"<

intsystime=0;

for(pcb*current=head;current;)

{

if(systimearrivetime)

systime=current->arrivetime;

cout<<"timeis"<

<name<<"start,"<

systime+=current->runtime;

cout<<"timeis"<

<name<<"end."<

current=current->next;

}

}

structnode1

{

pcb*index;

intlast;

node1*next;

node1(){next=NULL;}

};

voidprocess:

:

RoundRobin(inttime/*=1*/)

{

cout<<"轮转法调度:

"<

intsystime=0;

node1*h=NULL,*rear=NULL,*p;

for(pcb*current=head;current||h;)

{

if(h==NULL&&systimearrivetime)

systime=current->arrivetime;

while(current&&systime>=current->arrivetime)

{

p=newnode1;

p->index=current;

p->last=current->runtime;

if(h==NULL)

{

h=rear=p;

p->next=h;

}

else

{

p->next=rear->next;

h=rear->next=p;

}

current=current->next;

}

if(h->last==h->index->runtime)

{

cout<<"timeis"<

<index->name<<"start,"<

}

h->last-=time;systime+=time;//

if(h->last<=0)

{

cout<<"timeis"<

<index->name<<"end."<

if(h!

=rear)

{

rear->next=h->next;

deleteh;

h=rear->next;

}

else

{

deleteh;

h=rear=NULL;

}

}

else

{

rear=h;

h=h->next;

}

}

}

voidprocess:

:

Priority(inttime/*=1*/)

{

cout<<"优先级调度算法:

"<

intsystime=0;

node1*h=NULL,*p;

for(pcb*current=head;current||h;)

{

if(h==NULL&&systimearrivetime)

systime=current->arrivetime;

while(current&&systime>=current->arrivetime)

{

p=newnode1;

p->index=current;

p->last=current->runtime;

if(h==NULL||h->index->priorityindex->priority)

{

p->next=h;h=p;

}

else

{

node1*p1=h,*p2=h->next;

while(p2&&p2->index->priority>=p->index->priority)

p1=p2,p2=p2->next;

p1->next=p,p->next=p2;

}

current=current->next;

}

if(h->last==h->index->runtime)

{

cout<<"timeis"<

<index->name<<"start,"<

}

h->last-=time;systime+=time;//

h->index->priority--;

if(h->last<=0)

{

cout<<"timeis"<

<index->name<<"end."<

p=h;h=h->next;

deletep;

}

else

{

p=h;h=h->next;

if(h==NULL||h->index->priorityindex->priority)

{

p->next=h;h=p;

}

else

{

node1*p1=h,*p2=h->next;

while(p2&&p2->index->priority>=p->index->priority)

p1=p2,p2=p2->next;

p1->next=p,p->next=p2;

}

}

}

}

//移臂调度所用代码

voidFCFS_equipment()

{

inti,m,a[100],n,sum=0,t;

printf("请输入正在访问的页面号:

");

scanf("%d",&m);

t=m;

printf("请输入访问的页面总数:

");

scanf("%d",&n);

printf("请依次输入请求访问的页面号:

\n");

for(i=1;i<=n;i++)

{

scanf("%d",&a[i]);

sum+=(int)(fabs(t-a[i]));

t=a[i];

}

printf("页面的访问顺序为:

\n");

for(i=1;i<=n;i++)

printf("%d",a[i]);

printf("\n移动的距离为:

%d\n",sum);

}

voidsorting(intlocation,intn,inta[])

{//排序

inti,j,temp;

a[0]=location;

for(i=0;i

{

for(j=i+1;j<=n;j++)

{

if(a[i]>a[j])

{temp=a[i];a[i]=a[j];a[j]=temp;}

}

}

cout<<"磁道序列由小到大排列:

";

for(i=0;i<=n;i++)

cout<

cout<

}

voidscan(intlocation,intn,inta[])

{//电梯调度算法

booldirection;

inti=0,j,m;

cout<<"请输入磁头移动方向(向外--0,向里--1):

";

cin>>direction;

for(i=0;i<=n;i++)

{

if(a[i]==location)j=i;

}

cout<<"电梯调度算法实际服务次序为:

";

cout<<"("<

if(direction==0)

{

for(i=j-1;i>=0;i--)

{

cout<

}

for(i=j+1;i<=n;i++)

{

cout<

}

m=(a[j]-a[0])+(a[n]-a[0]);

}

else

{

intx=1,j1=j;

while(a[j]==a[j-1])

{

cout<

j--;x++;

}

for(i=j1+1;i<=n;i++)

{

cout<

}

for(i=j1-x;i>=0;i--)

{

cout<

}

m=(a[n]-a[j1])+(a[n]-a[0]);

}

cout<

cout<<"存取臂移动的磁道总数为:

"<

}

voidsstf(intlocation,intn,inta[])

{//最短寻找时间优先调度算法

inti,j,m=0;

for(i=0;i<=n;i++)

{

if(a[i]==location)

{

j=i;

}

}

cout<<"最短寻找时间优先调度算法实际服务次序为:

";

cout<<"("<

while(n>0)

{

if(j>0&&(location-a[j-1])<=(a[j+1]-location)||j==n)

{

cout<

m=m+location-a[j-1];

location=a[j-1];

for(i=j;i

a[i]=a[i+1];

n=n-1;

j=j-1;

}

else

{

cout<

m=m+a[j+1]-location;

for(i=j;i

a[i]=a[i+1];

n=n-1;

location=a[j];

}

}

cout<

cout<<"存取臂移动的磁道总数为:

"<

}

//页面置换所用代码

Input(intm,Prop[L])//打印页面流向状态

{

inti,j;

cout<<"请输入实际页面走向长度L(15<=L<=20):

";

do

{

cin>>m;

if(m>20||m<15)cout<<"实际页面长度须在15~20之间;请重新输入L:

";

elsebreak;

}while

(1);

j=time(NULL);//取时钟时间

srand(j);//以时钟时间x为种子,初始化随机数发生器

cout<<"输出随机数:

";

for(i=0;i

{

p[i].num=rand()%10+1;//产生1到10之间的随即数放到数组p中

p[i].time=0;

cout<

}

cout<

returnm;

}

voidprint(Pro*page1)//打印当前的页面

{

Pro*page=newPro[Memory];

page=page1;

for(inti=0;i

if(page[i].num==-1)

cout<<"";

else

cout<

cout<<"√"<

}

intSearch(inte,Pro*page1)//寻找内存块中与e相同的块号

{

Pro*page=newPro[Memory];

page=page1;

for(inti=0;i

if(e==page[i].num)returni;//返回i值

return-1;

}

intLRU(Pro*page1)//寻找最近最长未使用的页面

{

Pro*page=newPro[Memory];

page=page1;

inte=page[0].time,i=0;

while(i

{

if(e

e=page[i].time;

i++;

}

for(i=0;i

if(e==page[i].time)returni;//找到离现在时间最长的页面返回其块号

return-1;

}

intToNextLength(Pro*page1,inti,intt,Prop[L])//记录当前内存块中页面离下次使用间隔长度

{

Pro*page=newPro[Memory];

page=page1;

intcount=0;

for(intj=i;j

{

if(page[t].num==p[j].num)break;//当前页面再次被访问时循环结束

elsecount++;//否则count+1

}

returncount;//返回count的值

}

//银行家所用代码

voidshowdata()//显示资源矩阵

{

inti,j;

cout<<"系统目前可用的资源[Avaliable]:

"<

for(i=0;i

cout<

cout<

for(j=0;j

cout<

cout<

cout<<"MaxAllocationNeed"<

cout<<"进程名";

for(j=0;j<3;j++)

{

for(i=0;i

cout<

cout<<"";

}

cout<

for(i=0;i

{

cout<<""<

for(j=0;j

cout<

cout<<"";

for(j=0;j

cout<

cout<<"";

for(j=0;j

cout<

cout<

}

}

intchangdata(inti)//进行资源分配

{

intj;

for(j=0;j

{

Avaliable[j]=Avaliable[j]-Request[j];

Allocation[i][j]=Allocation[i][j]+Request[j];

Need[i][j]=Need[i][j]-Request[j];

}

return1;

}

intsafe()//安全性算法

{

inti,k=0,m,apply,Finish[100]={0};

intj;

intflag=0;

Work[0]=Avaliable[0];

Work[1]=Avalia

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 职业教育 > 职高对口

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1