先来先服务算法和银行家算法.docx

上传人:b****3 文档编号:26626605 上传时间:2023-06-20 格式:DOCX 页数:17 大小:35.89KB
下载 相关 举报
先来先服务算法和银行家算法.docx_第1页
第1页 / 共17页
先来先服务算法和银行家算法.docx_第2页
第2页 / 共17页
先来先服务算法和银行家算法.docx_第3页
第3页 / 共17页
先来先服务算法和银行家算法.docx_第4页
第4页 / 共17页
先来先服务算法和银行家算法.docx_第5页
第5页 / 共17页
点击查看更多>>
下载资源
资源描述

先来先服务算法和银行家算法.docx

《先来先服务算法和银行家算法.docx》由会员分享,可在线阅读,更多相关《先来先服务算法和银行家算法.docx(17页珍藏版)》请在冰豆网上搜索。

先来先服务算法和银行家算法.docx

先来先服务算法和银行家算法

江西师范大学计算机信息工程学院学生实验报告

(一)

专业__计算机科学与技术(师范)姓名_李洋学号_0908061086日期__2011-4-6

课程名称

计算机操作系统

实验室名称

X4313

实验名称

FCFS的调度算法实现

指导教师

朱明华

成绩

1.实验目的:

熟悉进程调度算法,用C语言实现先来先服务调度算法

2.实验原理和内容:

求进程的处理时间和平均处理时间

3.实验步骤

使用C-Free编译器,编制程序,调试程序

4.程序及运行结果

#include

typedefstructlink{

inttime;

intp;

structlink*next;

}node;

node*creat()

{

intm,n;

node*head,*q,*s;

head=q=(node*)malloc(sizeof(node));

printf("pleaseinputtimeandpriority:

");

scanf("%d%d",&m,&n);

while(m&&n)

{

s=(node*)malloc(sizeof(node));

s->p=n;

s->time=m;

q->next=s;

q=s;

scanf("%d%d",&m,&n);

}

q->next=NULL;

returnhead;

}

intmain()

{

intsum=0,k=0,t=0;

node*head,*l;

head=creat();

l=head->next;

while(l)

{

sum+=l->time;

l->time=sum;

l=l->next;

k+=sum;

}

l=head->next;

printf("eachtimeis:

")

 

}

printf("\n");

printf("theaveragetimeis%d",k/t);

return0;

}

while(l)

{

printf("%4d",l->time);

l=l->next;

t++;

}

printf("\n");

printf("theaveragetimeis%d",k/t);

return0;

}

运行结果:

江西师范大学计算机信息工程学院学生实验报告

(二)

专业__计算机科学与技术(师范)姓名_李洋学号0908061086__日期__2011-4-6

课程名称

计算机操作系统

实验室名称

X4313

实验名称

银行家算法

指导教师

朱明华

成绩

1.实验目的:

模拟实现银行家算法,用银行家算法实现资源分配。

2.实验原理和内容:

设计五个进程{P0,P1,P2,P3,P4}共享三类资源{A,B,C}的系统,{A,B,C}的资源数量分别为10,5,7。

进程可动态地申请资源和释放资源,系统按各进程的申请动态地分配资源。

要求程序具有显示和打印各进程的某一时刻的资源分配表和安全序列;显示和打印各进程依次要求申请的资源号以及为某进程分配资源后的有关资源数据。

3.实验步骤

使用C-Free编译器,编制程序,调试程序

4.源程序:

#include"malloc.h"

#include"stdio.h"

#include"stdlib.h"

#definealloclensizeof(structallocation)

#definemaxlensizeof(structmax)

#defineavalensizeof(structavailable)

#defineneedlensizeof(structneed)

#definefinilensizeof(structfinish)

#definepathlensizeof(structpath)

structallocation

{

intvalue;

structallocation*next;

};

structmax

{

intvalue;

structmax*next;

};

structavailable/*可用资源数*/

{

intvalue;

structavailable*next;

};

structneed/*需求资源数*/

{

intvalue;

structneed*next;

};

structpath

{

intvalue;

structpath*next;

};

structfinish

{

intstat;

structfinish*next;

};

intmain()

{

introw,colum,status=0,i,j,t,temp,processtest;

structallocation*allochead,*alloc1,*alloc2,*alloctemp;

structmax*maxhead,*maxium1,*maxium2,*maxtemp;

structavailable*avahead,*available1,*available2,*workhead,*work1,*work2,*worktemp,*worktemp1;

structneed*needhead,*need1,*need2,*needtemp;

structfinish*finihead,*finish1,*finish2,*finishtemp;

structpath*pathhead,*path1,*path2;

printf("\n请输入系统资源的种类数:

");

scanf("%d",&colum);

printf("请输入现时内存中的进程数:

");

scanf("%d",&row);

printf("请输入已分配资源矩阵:

\n");

for(i=0;i

{

for(j=0;j

{

printf("请输入已分配给进程p%d的%c种系统资源:

",i,'A'+j);

if(status==0)

{

allochead=alloc1=alloc2=(structallocation*)malloc(alloclen);

alloc1->next=alloc2->next=NULL;

scanf("%d",&allochead->value);

status++;

}

else

{

alloc2=(structallocation*)malloc(alloclen);

scanf("%d,%d",&alloc2->value);

if(status==1)

{

allochead->next=alloc2;

status++;

}

alloc1->next=alloc2;

alloc1=alloc2;

}

}

}

alloc2->next=NULL;

status=0;

printf("请输入最大需求矩阵:

\n");

for(i=0;i

{

for(j=0;j

{

printf("请输入进程p%d种类%c系统资源最大需求:

",i,'A'+j);

if(status==0)

{

maxhead=maxium1=maxium2=(structmax*)malloc(maxlen);

maxium1->next=maxium2->next=NULL;

scanf("%d",&maxium1->value);

status++;

}

else

{

maxium2=(structmax*)malloc(maxlen);

scanf("%d,%d",&maxium2->value);

if(status==1)

{

maxhead->next=maxium2;

status++;

}

maxium1->next=maxium2;

maxium1=maxium2;

}

}

}

maxium2->next=NULL;

status=0;

printf("请输入现时系统剩余的资源矩阵:

\n");

for(j=0;j

{

printf("种类%c的系统资源剩余:

",'A'+j);

if(status==0)

{

avahead=available1=available2=(structavailable*)malloc(avalen);

workhead=work1=work2=(structavailable*)malloc(avalen);

available1->next=available2->next=NULL;

work1->next=work2->next=NULL;

scanf("%d",&available1->value);

work1->value=available1->value;

status++;

}

else

{

available2=(structavailable*)malloc(avalen);

work2=(structavailable*)malloc(avalen);

scanf("%d,%d",&available2->value);

work2->value=available2->value;

if(status==1)

{

avahead->next=available2;

workhead->next=work2;

status++;

}

available1->next=available2;

available1=available2;

work1->next=work2;

work1=work2;

}

}

available2->next=NULL;

work2->next=NULL;

status=0;

alloctemp=allochead;

maxtemp=maxhead;

for(i=0;i

for(j=0;j

{

if(status==0)

{

needhead=need1=need2=(structneed*)malloc(needlen);

need1->next=need2->next=NULL;

need1->value=maxtemp->value-alloctemp->value;

status++;

}

else

{

need2=(structneed*)malloc(needlen);

need2->value=(maxtemp->value)-(alloctemp->value);

if(status==1)

{

needhead->next=need2;

status++;

}

need1->next=need2;

need1=need2;

}

maxtemp=maxtemp->next;

alloctemp=alloctemp->next;

}

need2->next=NULL;

status=0;

for(i=0;i

{

if(status==0)

{

finihead=finish1=finish2=(structfinish*)malloc(finilen);

finish1->next=finish2->next=NULL;

finish1->stat=0;

status++;

}

else

{

finish2=(structfinish*)malloc(finilen);

finish2->stat=0;

if(status==1)

{

finihead->next=finish2;

status++;

}

finish1->next=finish2;

finish1=finish2;

}

}

finish2->next=NULL;/*Initializationcompleated*/

status=0;

processtest=0;

for(temp=0;temp

{

alloctemp=allochead;

needtemp=needhead;

finishtemp=finihead;

worktemp=workhead;

for(i=0;i

{

worktemp1=worktemp;

if(finishtemp->stat==0)

{

for(j=0;jnext,worktemp=worktemp->next)

if(needtemp->value<=worktemp->value)

processtest++;

if(processtest==colum)

{

for(j=0;j

{

worktemp1->value+=alloctemp->value;

worktemp1=worktemp1->next;

alloctemp=alloctemp->next;

}

if(status==0)

{

pathhead=path1=path2=(structpath*)malloc(pathlen);

path1->next=path2->next=NULL;

path1->value=i;

status++;

}

else

{

path2=(structpath*)malloc(pathlen);

path2->value=i;

if(status==1)

{

pathhead->next=path2;

status++;

}

path1->next=path2;

path1=path2;

}

finishtemp->stat=1;

}

else

{

for(t=0;t

alloctemp=alloctemp->next;

finishtemp->stat=0;

}

}

else

for(t=0;t

{

needtemp=needtemp->next;

alloctemp=alloctemp->next;

}

processtest=0;

worktemp=workhead;

finishtemp=finishtemp->next;

}

}

path2->next=NULL;

finishtemp=finihead;

for(temp=0;temp

{

if(finishtemp->stat==0)

{

printf("\n系统处于非安全状态!

\n");

exit(0);

}

finishtemp=finishtemp->next;

}

printf("\n系统处于安全状态.\n");

printf("\n安全序列为:

\n");

do

{

printf("p%d",pathhead->value);

}

while(pathhead=pathhead->next);

printf("\n");

return0;

}

运行结果:

请输入系统资源的种类数:

3

请输入现时内存中的进程数:

5

请输入已分配资源矩阵:

请输入已分配给进程p0的A种系统资源:

0

请输入已分配给进程p0的B种系统资源:

1

请输入已分配给进程p0的C种系统资源:

0

请输入已分配给进程p1的A种系统资源:

2

请输入已分配给进程p1的B种系统资源:

0

请输入已分配给进程p1的C种系统资源:

0

请输入已分配给进程p2的A种系统资源:

3

请输入已分配给进程p2的B种系统资源:

0

请输入已分配给进程p2的C种系统资源:

2

请输入已分配给进程p3的A种系统资源:

2

请输入已分配给进程p3的B种系统资源:

1

请输入已分配给进程p3的C种系统资源:

1

请输入已分配给进程p4的A种系统资源:

0

请输入已分配给进程p4的B种系统资源:

0

请输入已分配给进程p4的C种系统资源:

2

请输入最大需求矩阵:

请输入进程p0种类A系统资源最大需求:

7

请输入进程p0种类B系统资源最大需求:

5

请输入进程p0种类C系统资源最大需求:

3

请输入进程p1种类A系统资源最大需求:

3

请输入进程p1种类B系统资源最大需求:

2

请输入进程p1种类C系统资源最大需求:

2

请输入进程p2种类A系统资源最大需求:

9

请输入进程p2种类B系统资源最大需求:

0

请输入进程p2种类C系统资源最大需求:

2

请输入进程p3种类A系统资源最大需求:

2

请输入进程p3种类B系统资源最大需求:

2

请输入进程p3种类C系统资源最大需求:

2

请输入进程p4种类A系统资源最大需求:

4

请输入进程p4种类B系统资源最大需求:

3

请输入进程p4种类C系统资源最大需求:

3

请输入现时系统剩余的资源矩阵:

种类A的系统资源剩余:

10

种类B的系统资源剩余:

5

种类C的系统资源剩余:

7

系统处于安全状态.

安全序列为:

p0p1p2p3p4

请按任意键继续...

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

当前位置:首页 > PPT模板 > 商务科技

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

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