ReadyGo!技术成就梦想 >> .Net技术 >> XML&WebService >> 哲学家就餐问题的c实现

哲学家就餐问题的c实现

ReadyGo!技术成就梦想 www.efish.cn efish 2007-10-25 10:14:31
撰文:周翔

这是我在上操作系统课的那个学期写的一段程序,并组织成了一篇文章。当初被我的挚友曾毅发表在cstc的论坛上:http://cstc.net.cn/bbs/viewtopic.php?t=457,在此,我把它贴在这儿,希望对大家有所裨益。



学操作系统的进程同步都要涉及到三个经典问题:生产者-消费者问题、读者-写者问题和哲学家就餐问题。下面来介绍一下哲学家就餐问题:
哲学家就餐问题中,一组哲学家围坐在一个圆桌旁,每个哲学家的左边都只有一只筷子(当然他的右边也有一只筷子,但是这是他右边哲学家的左边的筷子),他们吃完了就思考,思考了一会就会饿,饿了就想吃,然而,为了吃饭,他们必须获得左边和右边的筷子。当每个哲学家只拿有一只筷子的时候,会坐者等另一只筷子,在每个哲学家都只拿一个筷子的时候,就会发生死锁。传统的解决死锁问题的方法是引用管程的概念,但是在c#中来实现的话可以使system.threading中的mutex为每个哲学家来声名两个信号量rightchopstick和leftchopstick,在主程序中用5个mutex赋值给它,用waithandle来实现对筷子的独占访问。这个例子是用windows图形界面实现,用事件来通知界面哲学家的状态。
以下是代码(在vs.net 下运行通过):

//diningphilosophers.cs----------code:seafrog-----------------------------------------------------
using system;
using system.threading;
using system.windows.forms;

using seafrog.threading;
using seafrog.philosopher;

namespace diningphilosophers
{
public class form1 : system.windows.forms.form
{
private system.windows.forms.button button1;
private system.componentmodel.container components = null;
private system.windows.forms.listbox listbox1;
private philosopher;
public form1()
{
initializecomponent();

mutex;
for(int i=0;i<5;i++)
{
chopsticks=new mutex(false);
}
for(int i=0;i<5;i++)
{
philosopherdata pd;
pd.philosopherid=i;
pd.rightchopstick=chopsticks;
pd.leftchopstick=chopsticks;
pd.amounttoeat=5;
pd.totalfood=35;
p=new philosopher(pd);
p.messagearrival+=new philosopher.messagearrivedhandler(showmessage);
}
}
protected override void dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.dispose();
}
}
base.dispose( disposing );
}

#region windows form designer generated code

private void initializecomponent()
{
this.button1 = new system.windows.forms.button();
this.listbox1 = new system.windows.forms.listbox();
this.suspendlayout();
//
// button1
//
this.button1.location = new system.drawing.point(8, 224);
this.button1.name = "button1";
this.button1.size = new system.drawing.size(272, 40);
this.button1.tabindex = 1;
this.button1.text = "go to restaurant";
this.button1.click += new system.eventhandler(this.button1_click);
//
// listbox1
//
this.listbox1.itemheight = 12;
this.listbox1.name = "listbox1";
this.listbox1.size = new system.drawing.size(296, 220);
this.listbox1.tabindex = 2;
//
// form1
//
this.autoscalebasesize = new system.drawing.size(6, 14);
this.clientsize = new system.drawing.size(292, 273);
this.controls.addrange(new system.windows.forms.control
static void main()
{
application.run(new form1());
}

private void button1_click(object sender, system.eventargs e)
{
for(int i=0;i<5;i++)
p.start();
}

public void showmessage(object sender,messagearrivedeventargs e)
{
switch(e.type)
{
case philosopher.ready:
listbox1.items.add("philosopher("+e.philosopherdata.philosopherid+") ready.");
break;
case philosopher.eating:
listbox1.items.add("philosopher("+
e.philosopherdata.philosopherid+") eating "+
e.philosopherdata.amounttoeat+" of "+
e.philosopherdata.totalfood+" food.");
break;
case philosopher.thinking:
listbox1.items.add("philosopher("+e.philosopherdata.philosopherid+") thinking.");
break;
case philosopher.finished:
listbox1.items.add("philosopher("+e.philosopherdata.philosopherid+") finished.");
break;
}
}
}
}

//basethread.cs----------code:seafrog--------------------------------------------------------
using system;
using system.threading;
namespace seafrog.threading
{
//工作线程抽象类,作为对线程操作的封装。
public abstract class workerthread
{
private object threaddata;
private thread thisthread
相关文章
哲学家就餐问题的c实现 撰文:周翔 这是我在上操作系统课的那个学期写的一段程序,并组织成了一篇文章。当初被我的挚友曾毅发表在c..
哲学家就餐问题的c实现 撰文:周翔 这是我在上操作系统课的那个学期写的一段程序,并组织成了一篇文章。当初被我的挚友曾毅发表在c..
2秒记住本站域名

玩过泡泡龙吗?Readygo?Go! 再加上.Com.Cn的后缀,那就是大名小顶的ReadyGo.com.cn

分类导航
ReadyGo!技术成就梦想