广东工业大学操作系统哲学家就餐问题.docx

上传人:b****5 文档编号:4646800 上传时间:2022-12-07 格式:DOCX 页数:25 大小:240.83KB
下载 相关 举报
广东工业大学操作系统哲学家就餐问题.docx_第1页
第1页 / 共25页
广东工业大学操作系统哲学家就餐问题.docx_第2页
第2页 / 共25页
广东工业大学操作系统哲学家就餐问题.docx_第3页
第3页 / 共25页
广东工业大学操作系统哲学家就餐问题.docx_第4页
第4页 / 共25页
广东工业大学操作系统哲学家就餐问题.docx_第5页
第5页 / 共25页
点击查看更多>>
下载资源
资源描述

广东工业大学操作系统哲学家就餐问题.docx

《广东工业大学操作系统哲学家就餐问题.docx》由会员分享,可在线阅读,更多相关《广东工业大学操作系统哲学家就餐问题.docx(25页珍藏版)》请在冰豆网上搜索。

广东工业大学操作系统哲学家就餐问题.docx

广东工业大学操作系统哲学家就餐问题

操作系统课程设计

题目:

哲学家就餐问题

 

学院计算机学院

专业软件工程

班级12级3班

学号3112006229

姓名陈志勇

指导教师申建芳

(2015年6月)

 

操作系统课程设计任务书

学生姓名

专业班级

学号

题目

仿真模拟操作系统中的“哲学家就餐问题”

指导教师

申建芳

题目编号

2015春-16

主要内容

多进程模拟哲学家就餐问题

任务要求

五个哲学家围坐在一张圆桌周围,每个哲学家面前都有一盘通心粉。

由于通心粉很滑,所以需要两只筷子才能夹住。

相邻两个盘子之间放有一只筷子。

哲学家的生活中有两种交替的活动:

吃饭和思考(其他活动对哲学家来说都无关紧要)。

当一个哲学家感到饿了时,他就试图分两次去取左边和右边的筷子,每次拿一把,但不分次序。

如果成功得到了两把筷子,就开始吃饭,吃完后放下筷子继续思考。

请为每一个哲学家写一段描述其行为的程序,而且不会死锁。

参考文献

[1]计算机操作系统,汤小丹等,西安电子科技大学出版社

[2]操作系统实验指导书,傅秀芬,广东工业大学(自编)

[3]计算机操作系统教程(第二版),张尧学、史美林,清华大学出版社

[4]现代操作系统,A.S.Tanenbaum著,陈向群等译机械工业出版社

审查意见

指导教师签字:

系主任签字:

年月日

说明:

本表由指导教师填写,由系主任审核后下达给选题学生,装订在设计(论文)首页

设计思想说明

操作系统平台:

Windows2007

开发平台:

eclipse

程序语言:

Java

设计思想:

定义五个类,分别为哲学家A、哲学家B、哲学家C、哲学家D、哲学家E。

五个类均有thinking(思考)和Eating(吃饭)两个方法。

当选定哪几位哲学家就餐后,使用单线程处理选定的哲学家就餐顺序,如果A要就餐,询问C是否要就餐,否则询问D是否要就餐,否则A自己就餐;然后B进行就餐,如果B就餐,询问D是否要就餐,否则询问E是否要就餐,否则B就餐;然后C进行就餐,如果C就餐,询问E是否就餐,否则C就餐;最后D就餐,再E就餐。

数据结构的说明

采用链式timer(计时器),每当一个timer结束时,链接到下一个timer中去,实现多步哲学家就餐顺序,当最后一个哲学家就餐完成后弹出信息面板提示就餐完成。

调用关系:

ShowPannel为主类,StateControl为哲学家类,showPannel在timer中调用哲学家类实现就餐、思考的转换和选择。

各模块的算法流程图

StateControl类:

定义哲学家类

Thinking:

setText(空闲);

Eating:

setText(正在使用);

showPannel类:

点击确定后选定哲学家就餐

A组:

AC就餐AD就餐A就餐

B组:

BD就餐BE就餐B就餐

C组:

CE就餐C就餐

D组:

D就餐

E组:

E就餐

Messagebox类:

仅显示提示信息

程序清单

StateControl类:

packagephilosopher;

classphilosopherA{

voidThinking(){//正在思考时不占用筷子资源

ShowPannel.chops1.setText("筷子1当前状态:

空闲");

ShowPannel.chops2.setText("筷子2当前状态:

空闲");

}

voidEating(){//吃时两个筷子状态为1,即正在使用

ShowPannel.chops1.setText("筷子1当前状态:

正在使用");

ShowPannel.chops2.setText("筷子2当前状态:

正在使用");

}

}

classphilosopherB{

voidThinking(){//正在思考时不占用筷子资源

ShowPannel.chops2.setText("筷子2当前状态:

空闲");

ShowPannel.chops3.setText("筷子3当前状态:

空闲");

}

voidEating(){//吃时两个筷子状态为1,即正在使用

ShowPannel.chops2.setText("筷子2当前状态:

正在使用");

ShowPannel.chops3.setText("筷子3当前状态:

正在使用");

}

}

classphilosopherC{

voidThinking(){//正在思考时不占用筷子资源

ShowPannel.chops3.setText("筷子3当前状态:

空闲");

ShowPannel.chops4.setText("筷子4当前状态:

空闲");

}

voidEating(){//吃时两个筷子状态为1,即正在使用

ShowPannel.chops3.setText("筷子3当前状态:

正在使用");

ShowPannel.chops4.setText("筷子4当前状态:

正在使用");

}

}

classphilosopherD{

voidThinking(){//正在思考时不占用筷子资源

ShowPannel.chops4.setText("筷子4当前状态:

空闲");

ShowPannel.chops5.setText("筷子5当前状态:

空闲");

}

voidEating(){//吃时两个筷子状态为1,即正在使用

ShowPannel.chops4.setText("筷子4当前状态:

正在使用");

ShowPannel.chops5.setText("筷子5当前状态:

正在使用");

}

}

classphilosopherE{

voidThinking(){//正在思考时不占用筷子资源

ShowPannel.chops5.setText("筷子5当前状态:

空闲");

ShowPannel.chops1.setText("筷子1当前状态:

空闲");

}

voidEating(){//吃时两个筷子状态为1,即正在使用

ShowPannel.chops5.setText("筷子5当前状态:

正在使用");

ShowPannel.chops1.setText("筷子1当前状态:

正在使用");

}

}

ShowPannel类:

packagephilosopher;

importjava.awt.Checkbox;

importjava.awt.FlowLayout;

importjava.awt.event.ActionEvent;

importjavax.swing.*;

importjavax.swing.event.ChangeEvent;

importjavax.swing.event.ChangeListener;

importjava.awt.event.ActionListener;

 

publicclassShowPannelextendsJFrameimplementsChangeListener,ActionListener{

/**

*

*/

privatestaticfinallongserialVersionUID=1L;

publicJFrameframe=null;

publicstaticShowPannelspPannel;

publicstaticJButtonselectButton;//选择就餐的哲学家

publicstaticJLabelchops1,chops2,chops3,chops4,chops5;

publicstaticJProgressBarprogressBar1,progressBar2,progressBar3,progressBar4,progressBar5;

publicstaticTimertimer1,timer2,timer3,timer4,timer5;

philosopherAphiA;//五位哲学家

philosopherBphiB;

philosopherCphiC;

philosopherDphiD;

philosopherEphiE;

publicstaticboolean[]philosopher={false,false,false,false,false};//五个哲学家状态

publicstaticint[]Mark={0,0,0,0,0,0,0,0,0,0};//标记就餐组合

JLabelLabel1;

JLabelsenseJLabel1,senseJLabel2,senseJLabel3,senseJLabel4;//横杠

JLabelblank1,blank2,blank3,blank4;

JLabelstate;//当前状态

Checkboxphi1,phi2,phi3,phi4,phi5;//五位哲学家选择

publicstaticvoidmain(String[]args){

spPannel=newShowPannel();

}

publicShowPannel(){

Label1=newJLabel("选择就餐的哲学家:

");

state=newJLabel("资源使用状态");

senseJLabel1=newJLabel("—————————————————");

senseJLabel2=newJLabel("—————————————————");

senseJLabel3=newJLabel("—————————————————");

senseJLabel4=newJLabel("————————————————");

blank1=newJLabel("");//废块

blank2=newJLabel("");

blank3=newJLabel("");

blank4=newJLabel("");

chops1=newJLabel("筷子1当前状态:

空闲");

chops2=newJLabel("筷子2当前状态:

空闲");

chops3=newJLabel("筷子3当前状态:

空闲");

chops4=newJLabel("筷子4当前状态:

空闲");

chops5=newJLabel("筷子5当前状态:

空闲");

phiA=newphilosopherA();

phiB=newphilosopherB();

phiC=newphilosopherC();

phiD=newphilosopherD();

phiE=newphilosopherE();

//checkBox哲学家复选框

phi1=newCheckbox("哲学家A",null,false);

phi2=newCheckbox("哲学家B",null,false);

phi3=newCheckbox("哲学家C",null,false);

phi4=newCheckbox("哲学家D",null,false);

phi5=newCheckbox("哲学家E",null,false);

selectButton=newJButton("就餐");

selectButton.addActionListener(this);

//进度条设置

progressBar1=newJProgressBar(0,100);

progressBar2=newJProgressBar(0,100);

progressBar3=newJProgressBar(0,100);

progressBar4=newJProgressBar(0,100);

progressBar5=newJProgressBar(0,100);

progressBar1.setValue(0);

progressBar2.setValue(0);

progressBar3.setValue(0);

progressBar4.setValue(0);

progressBar5.setValue(0);

progressBar1.setStringPainted(true);

progressBar2.setStringPainted(true);

progressBar3.setStringPainted(true);

progressBar4.setStringPainted(true);

progressBar5.setStringPainted(true);

timer1=newTimer(20,this);//10个计时器

timer2=newTimer(20,this);

timer3=newTimer(20,this);

timer4=newTimer(20,this);

timer5=newTimer(20,this);

frame=newJFrame("哲学家就餐问题");

frame.setLayout(newFlowLayout(FlowLayout.LEFT,20,30));

frame.add(Label1);

frame.add(phi1);frame.add(phi2);frame.add(phi3);

frame.add(phi4);frame.add(phi5);

//资源使用状况

frame.add(senseJLabel1);frame.add(selectButton);frame.add(senseJLabel3);

frame.add(senseJLabel2);frame.add(state);frame.add(senseJLabel4);

//筷子块

frame.add(chops1);frame.add(progressBar1);frame.add(blank1);

frame.add(chops2);frame.add(progressBar2);frame.add(blank2);

frame.add(chops3);frame.add(progressBar3);frame.add(blank3);

frame.add(chops4);frame.add(progressBar4);frame.add(blank4);

frame.add(chops5);frame.add(progressBar5);

frame.setLocationRelativeTo(null);

frame.setSize(600,500);

frame.setResizable(false);

frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

frame.setVisible(true);

}

@Override

publicvoidactionPerformed(ActionEvente){

if(e.getSource()==selectButton){

philosopher[0]=phi1.getState();//获取5个哲学家一轮就餐顺序

philosopher[1]=phi2.getState();

philosopher[2]=phi3.getState();

philosopher[3]=phi4.getState();

philosopher[4]=phi5.getState();

selectButton.setText("就餐中...");

if(philosopher[0]==true){

if(philosopher[2]==true){

//哲学家AC一起吃饭

Mark[0]=1;

philosopher[2]=false;

}elseif(philosopher[3]==true){

//哲学家AD一起吃饭

Mark[1]=1;

philosopher[3]=false;

}else{

//哲学家A要吃饭

Mark[2]=1;

philosopher[0]=false;

}

}

if(philosopher[1]==true){

if(philosopher[3]==true){

//哲学家BD一起吃饭

Mark[3]=1;

philosopher[3]=false;

}elseif(philosopher[4]==true){

//哲学家BE一起吃饭

Mark[4]=1;

philosopher[4]=false;

}else{

//哲学家B要吃饭

Mark[5]=1;

philosopher[1]=false;

}

}

if(philosopher[2]==true){

if(philosopher[4]==true){

//哲学家CE一起吃饭

Mark[6]=1;

philosopher[4]=false;

}

else{

//哲学家C吃饭

Mark[7]=1;

philosopher[2]=false;

}

}

if(philosopher[3]==true){

//哲学家D吃饭

Mark[8]=1;

philosopher[3]=false;

}

if(philosopher[4]==true){

//哲学家E吃饭

Mark[9]=1;

philosopher[4]=false;

}

timer1.start();

}

if(e.getSource()==timer1){

if(Mark[0]==1){//判断AC是否就餐,是则执行AC就餐后执行timer2,否则直接执行timer2

intvalue1=progressBar1.getValue();

intvalue2=progressBar2.getValue();

intvalue3=progressBar3.getValue();

intvalue4=progressBar4.getValue();

if(value1<100){

value1++;

value2++;

value3++;

value4++;

if(value1==1){

phiA.Eating();

phiC.Eating();

}

progressBar1.setValue(value1);

progressBar2.setValue(value2);

progressBar3.setValue(value3);

progressBar4.setValue(value4);

if(value1==100){

phiA.Thinking();

phiC.Thinking();

progressBar1.setValue(0);

progressBar2.setValue(0);

progressBar3.setValue(0);

progressBar4.setValue(0);

Mark[0]=0;//重置标记值

timer1.stop();

timer2.start();

}

}

}elseif(Mark[1]==1){//判断是否AD就餐,是则饭后执行timer2,否则直接执行

intvalue1=progressBar1.getValue();

intvalue2=progressBar2.getValue();

intvalue4=progressBar4.getValue();

intvalue5=progressBar5.getValue();

if(value1<100){

value1++;

value2++;

value4++;

value5++;

if(value1==1){

phiA.Eating();

phiD.Eating();

}

progressBar1.setValue(value1);

progressBar2.setValue(value2);

progressBar4.setValue(value4);

progressBar5.setValue(value5);

if(value1==100){

phiA.Thinking();

phiD.Thinking();

progressBar1.setValue(0);

progressBar2.setValue(0);

progressBar4.setValue(0);

progressBar5.setValue(0);

Mark[1]=0;//重置标记值

timer1.stop();

timer2.start();

}

}

}elseif(Mark[2]==1){//判断A是否就餐,是则饭后执行timer2,否则直接执行

intvalue1=progressBar1.getValue();

intvalue2=progressBar2.getValue();

if(value1<100){

value1++;

value2++;

if(va

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

当前位置:首页 > 幼儿教育 > 唐诗宋词

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

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