哲学家就餐问题实验报告.docx

上传人:b****7 文档编号:15309255 上传时间:2023-07-03 格式:DOCX 页数:13 大小:148.40KB
下载 相关 举报
哲学家就餐问题实验报告.docx_第1页
第1页 / 共13页
哲学家就餐问题实验报告.docx_第2页
第2页 / 共13页
哲学家就餐问题实验报告.docx_第3页
第3页 / 共13页
哲学家就餐问题实验报告.docx_第4页
第4页 / 共13页
哲学家就餐问题实验报告.docx_第5页
第5页 / 共13页
哲学家就餐问题实验报告.docx_第6页
第6页 / 共13页
哲学家就餐问题实验报告.docx_第7页
第7页 / 共13页
哲学家就餐问题实验报告.docx_第8页
第8页 / 共13页
哲学家就餐问题实验报告.docx_第9页
第9页 / 共13页
哲学家就餐问题实验报告.docx_第10页
第10页 / 共13页
哲学家就餐问题实验报告.docx_第11页
第11页 / 共13页
哲学家就餐问题实验报告.docx_第12页
第12页 / 共13页
哲学家就餐问题实验报告.docx_第13页
第13页 / 共13页
亲,该文档总共13页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

哲学家就餐问题实验报告.docx

《哲学家就餐问题实验报告.docx》由会员分享,可在线阅读,更多相关《哲学家就餐问题实验报告.docx(13页珍藏版)》请在冰点文库上搜索。

哲学家就餐问题实验报告.docx

哲学家就餐问题实验报告

 

哲学家就餐问题实验报告

姓名:

何绍金

班级:

自动化1202

学号:

201203870408

指导教师:

张健

 

2014年11月22日

一.实验题目

经典的同步问题——哲学家就餐问题设计。

二.实验内容

1.实验要求

有五个哲学家围坐在一圆桌旁,桌中央有一盘通心粉,每人面前有一只空盘子,每两人之间放一只筷子每个哲学家的行为是思考,感到饥饿,然后吃通心粉.为了吃通心粉,每个哲学家必须拿到两只筷子,并且每个人只能直接从自己的左边或右边去取筷子。

2.程序流程图

图1.主程序模块流程图

图2.状态改变模块流程图

 

图3.返回哲学家状态流程图

图4返回餐具状态模块流程图

 

3.测试结果

图5测试结果1

图6测试结果2

图7测试结果3

4.结果分析和小结

(1)程序分为四大模块,一步步解决了哲学家状态及状态改变的问题,筷子的“闲”、“用”问题;实现了哲学家等待、吃饭、思考三个过程的转换循环,并且避免了死锁问题;让临界资源得到了充分的利用。

(2)这次实验让我学会分模块解决问题,怎样运用互斥锁对临界资源进行管理;此外自己在编程上及一些函数的认识仍存在较大的问题,以后应该多多实践,提高自己的反应速度,加强逻辑思维能力。

三.程序源代码

#include

#include

#include

#include

#include

usingnamespacestd;

booltools[5];//全局变量,用餐工具

CRITICAL_SECTIONcs;//信号量,在线程中使用,临界区

classPhilosopher

{

private:

intnumber;

intstatus;/*标记当前哲学家的状态,0表示正在等待

(即处于饥饿状态),1表示得到两支筷子正在吃饭,2表示正在思考*/

public:

Philosopher(intnum=0):

status

(2),number(num){}

constintfind()

{

returnnumber;

}

constintgetinfo()

{returnstatus;}

voidChange();//状态改变函数

voiddead_lock();

};

/////////

voidPhilosopher:

:

dead_lock()

{

EnterCriticalSection(&cs);//进入临界区

strings;

if(status==1)

{

tools[number%5]=true;

//tools[(number-1)%5]=true;

status=2;

}

elseif(status==2)

{

status=0;

//tools[(number-1)%5]=false;

//tools[(number-1)%5]=true;

}

elseif(status==0)

{

tools[number%5]=false;

tools[(number-1)%5]=false;

status=1;

}

LeaveCriticalSection(&cs);

//cout<<"*********";

}

/////////

voidPhilosopher:

:

Change()

{

EnterCriticalSection(&cs);//进入临界区

if(status==1)//正在进餐

{

tools[number%5]=true;//放下左手工具

tools[(number-1)%5]=true;//放下右手工具

status=2;//改变状态为思考

}

elseif(status==2)//思考中

{

status=0;//改变状态为等待

}

elseif(status==0)//等待中

{

if(tools[number%5]&&tools[(number-1)%5])//左右手两边工具均为空闲状态

{

tools[number%5]=false;//拿起左手工具

tools[(number-1)%5]=false;//拿起右手工具

status=1;

}

}

LeaveCriticalSection(&cs);

}

stringprint(Philosopher*pA)

{

//pA->Change();

inti=pA->getinfo();

stringstr;

if(i==0)

str="等待";

elseif(i==1)

str="就餐";

elsestr="思考";

returnstr;

}

stringtoolstatus(boola)

{

stringstate;

if(a==true)

state="闲";

if(a==false)

state="用";

returnstate;

}

intmain()

{

charcon='y';//判断是否继续

//con='n';

for(inti=0;i<5;i++)

tools[i]=true;//筷子都未使用,初始化

PhilosopherP1

(1),P2

(2),P3(3),P4(4),P5(5);

InitializeCriticalSection(&cs);//初始化初始化临界区

cout<<"-----------------------状态说明示意图:

-----------------------"<

cout<<""<<"哲学家1号的状态"<<""<

cout<<"筷子0的状态"<<""<<"筷子1的状态"<

cout<<"哲学家5号的状态"<<""<<"哲学家2号的状态"<

cout<<"筷子4的状态"<<""<<"筷子2的状态"<

cout<<"哲学家4号的状态"<<""<<"哲学家3号的状态"<

cout<<""<<"筷子3的状态"<

//cout<<""<<"哲学家3号的状态"<<""<

cout<<"筷子的状态,用表示使用中,闲表示空闲中。

"<

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

//cout<<"哲学家们开始生活:

"<

//cout<<"当前状态:

";

cout<

//cin>>con;

while(con=='y')

{

P1.Change();P2.Change();P3.Change();P4.Change();P5.Change();

cout<<"当前状态为:

"<

cout<<""<

cout<<""<

cout<<""<

cout<<""<

cout<<""<

cout<<""<

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

cout<<"若要继续下一状态,输入y;输入n进入死锁;输入其他,结束程序:

";

cin>>con;

Sleep(20);

}

while(con=='n')

{

P1.dead_lock();

P2.dead_lock();P3.dead_lock();P4.dead_lock();P5.dead_lock();

cout<<"死锁情况"<

cout<<""<

cout<<""<

cout<<""<

cout<<""<

cout<<""<

cout<<""<

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

cout<<"输入n继续;输入其他,结束程序:

";

cin>>con;

Sleep(20);

}

DeleteCriticalSection(&cs);//退出资源区

return0;

}

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

当前位置:首页 > 医药卫生 > 基础医学

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2