撰文:周翔
这是我在上操作系统课的那个学期写的一段程序,并组织成了一篇文章。当初被我的挚友曾毅发表在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[] p=new Philosopher[5];
public Form1()
{
InitializeComponent();
Mutex[] chopSticks=new Mutex[5];
for(int i=0;i<5;i++)
{
chopSticks[i]=new Mutex(false);
}
for(int i=0;i<5;i++)
{
PhilosopherData pd;
pd.PhilosopherId=i;
pd.RightChopStick=chopSticks[(i+1)%5];
pd.LeftChopStick=chopSticks[(i+4)%5];
pd.AmountToEat=5;
pd.TotalFood=35;
p[i]=new Philosopher(pd);
p[i].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[] {
this.listBox1,
this.button1});
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
for(int i=0;i<5;i++)
p[i].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;
public object Data
{
get{return ThreadData;}
set{ThreadData=value;}
}
public object IsAlive
{
get{return thisThread==null?false:thisThread.IsAlive;}
}
public WorkerThread(object data)
{
this.ThreadData=data;
}
public WorkerThread()
{
ThreadData=null;
}
public void Start()
{
thisThread=new Thread(new ThreadStart(this.Run));
thisThread.Start();
}
public void Stop()
{
thisThread.Abort();
while(thisThread.IsAlive);
thisThread=null;
}
protected abstract void Run();
}
}
//Philosophers.cs----------code:seafrog--------------------------------------------------------
using System;
using System.Threading;
using seafrog.Threading;
namespace seafrog.Philosopher
{
//封装哲学家数据的结构
public struct PhilosopherData
{
public int PhilosopherId;
public Mutex RightChopStick;
public Mutex LeftChopStick;
public int AmountToEat;
public int TotalFood;
}
public class Philosopher : seafrog.Threading.WorkerThread
{
public const int READY=0;
public const int EATING=1;
public const int THINKING=2;
public const int FINISHED=3;
public Philosopher(object data):base(data){}
public delegate void MessageArrivedHandler(Object sender,MessageArrivedEventArgs args);
public event MessageArrivedHandler MessageArrival;
public static int finished=0;
protected override void Run()
{
PhilosopherData pd=(PhilosopherData)Data;
Random r=new Random(pd.PhilosopherId);
MessageArrival(this,new MessageArrivedEventArgs(READY,pd));
WaitHandle[] chopSticks=new WaitHandle[]{pd.LeftChopStick,pd.RightChopStick};
while(pd.TotalFood>0)
{
//如果两边的哲学家拿着筷子,则等待。
WaitHandle.WaitAll(chopSticks);
//否则,吃饭。
MessageArrival(this,new MessageArrivedEventArgs(EATING,pd));
//把饭吃掉一部分。
pd.TotalFood-=pd.AmountToEat;
Thread.Sleep(r.Next(1000,5000));
MessageArrival(this,new MessageArrivedEventArgs(THINKING,pd));
//放下左边和右边的筷子。
pd.RightChopStick.ReleaseMutex();
pd.LeftChopStick.ReleaseMutex();
Thread.Sleep(r.Next(1000,5000));
}
//饭都吃完了。
MessageArrival(this,new MessageArrivedEventArgs(FINISHED,pd));
if(++finished==4)
System.Windows.Forms.MessageBox.Show("All Finished!");
}
}
//事件:用来通知主窗体现在哲学家的状态。
public class MessageArrivedEventArgs : EventArgs
{
public int type;
public PhilosopherData philosopherData;
public MessageArrivedEventArgs(int t,PhilosopherData pd)
{
type=t;
philosopherData=pd;
}
}
}
( 完)
分享到:
相关推荐
使用了C#对哲学家就餐问题进行了仿真,采用进程交互法,并使用批均值法、重复删除法对仿真数据进行了分析
哲学家用餐问题C#实现,源码可以运行,多线程互斥操作
哲学家就餐问题的C#实现
哲学家就餐问题可以这样表述,假设有六位哲学家围坐在一张圆形餐桌旁,做以下两件事情之一:吃饭,或者思考。吃东西的时候,他们就停止思考,思考的时候也停止吃东西。餐桌中间有一大碗意大利面,每两个哲学家之间有...
用C#代码实现操作系统中的哲学家就餐问题。程序所涉及包括面向对象程序编程,多线程,metux等。
1. 使用信号量的方式模拟哲学家就餐问题。 2. 用一个输入变量控制是否有左撇子哲学家。如果有,其数量由随机数生成。 3. 模拟程序分为两种情况, (1) 可能发生死锁的情况,输出发生死锁时的资源分配状态和历史资源...
C# 多线程模拟哲学家就餐问题+实验报告
计算机操作系统中的哲学家就餐问题,用C#把整个过程可视化出来。
操作系统课程作业,有可视化动态,VS2017可运行,C#。
用C#实现操作系统中银行家算法、读者写者、哲学家进餐、消费者生产者的窗体
.NET哲学家进餐算法,vs2010可打开,面向对象设计
C#语言编写的哲学家算法演示。自动演示,随机选择几人,能同时拿到筷子的进餐,其它思考。手动演示,鼠标点击选择某人。
银行家算法避免死锁 VM软件 Linux系统 C语言 成功编译 成功运行 内附完整课设报告,代码,运行cpp ...如哲学家就餐、生产者-消费者或者读者-写者问题等。 (5)要求在linux ubuntu环境下使用c/c++编写