C # implementation of the philosopher dining problem

zhaozj2021-02-16  65

Written: Zhou Xiang

This is a program written by the semester of the operating system class and organizes an article. When I was published by my friend, I was published in the CSTC forum: http://cstc.net.cn/bbs/viewtopic.php? T = 457, here, I put it here, I hope to help everyone .

The process of learning the operating system should involve three classic issues: producers - consumer issues, readers - writers and philosophers. Let's introduce a philosopher's dining question: A group of philosophers sit around a round table, a group of philosophers sit around, and there is only one chopsticks on the left side of the philosopher (of course, there is also a chopsticks on the right, but this is His left chopsticks on the left side of the philosopher), they think about it, thinking about it, hungry, hungry, I want to eat, however, in order to eat, they must get the left and right chopsticks. When each philosopher only took one chopstick, it will take another chopsticks, and when each philosopher only took a chopstick, it will kill. The traditional way to solve the problem of deadlock is to reference the concept of the management, but in the C #, you can make the Mutex in system.thread for each philosopher Rightchopstick and LeftChopstick, in the main program 5 Mutex assignments to it, with WaitHandle to achieve exclusive access to chopsticks. This example is implemented with a Windows graphical interface, with an event to notify the status of the philosopher.

The following is the code (running in vs.net): //diningphilosophers.cs ----------code:seafrog ------------------- --------------------------------------------------------------------------------------------------------------------------------------- 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] .MessagerriVal = new philosopher.MessagerriveDhandler ShowMessage);}} protected {ix (disposing) {if (disposing) {ix ! Onents = 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; break; case Philosopher ( "Philosopher (" e.philosopherData.PhilosopherId ") ready."). Eating: listbox1.items.add ("Philosopher (" E.PHILOPHERDATA.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.PHILOPHERDATA.PHILOSOPHERID ") Finished. "); Break;}}}} //basteread.cs ----------code:Seafrog ------------ -------------------------------------------- using System; using System. Threading; Namespace SeaFrog.Threading {// Working thread abstraction class, as a package of thread operation.

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 ------------ SEAFROG ----------------------------------------------- --------- using System; using System.Threading; using seafrog.Threading; namespace seafrog.Philosopher {// data structure encapsulated philosophers 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) {// If the philosopher on both sides is holding chopsticks, it will wait. Waithandle.waitall (Chopsticks); / / Otherwise, eat.

Messagerrival (this, New MessagerriveDeventArgs (Eating, PD)); // Eat a part. Pd.totalfood- = pd.amounttoeat; thread.sleep (R.Next (1000, 5000)); Messagerrival (this, new messagearrivedeventargs (thinking, pd)); // put down the left and right chopsticks. Pd.rightchopstick.releasemutex (); pd.leftchopstick.releasemutex (); thread.sleep (R.Next (1000, 5000));} // Eat. Messagerrival (finished, pd)); if ( finished == 4) System.windows.Forms.MessageBox.show ("all finished!");}} // Event: used to notify the main window Reflected in the state of philosophers. public class MessageArrivedEventArgs: EventArgs {public int type; public PhilosopherData philosopherData; public MessageArrivedEventArgs (int t, PhilosopherData pd) {type = t; philosopherData = pd;}}} (End)

转载请注明原文地址:https://www.9cbs.com/read-18582.html

New Post(0)