程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 哲學家就餐問題的C#實現

哲學家就餐問題的C#實現

編輯:關於C語言
這是我在上操作系統課的那個學期寫的一段程序,並組織成了一篇文章。當初被我的摯友曾毅發表在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;
}
}
}


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved