<html>

<head><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- MyFirstUnitAd -->
<ins class="adsbygoogle"
     style="display:inline-block;width:970px;height:250px"
     data-ad-client="ca-pub-5778386704669218"
     data-ad-slot="1503492166"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>CFGReader</title>
</head>

<body>



<p align="left"><span lang="en-ca"><font size="6" color="#FF0000"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
Dining philosopher problem</b></font></span></p>

<div align="left">
  <pre><b><font color="#ff0000" size="5">A. First Edition</font></b></pre>
</div>
<div align="left">
  <pre><b>I am practicing with multi-thread.</b></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">B</span>.<span lang="en-ca"><a name="problem"></a>The problem</span></font></b></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>Five philosophers are sitting around a table, each with one chopstick on table. They keep thinking and eat rice</b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>when they feel hungry. When they want to eat, they have to pick up the chopstick in front of him and another </b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>one from either left or right of him. However, there exists a potential dead-lock problem such that when all of</b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>them are hungry and try to pick up chopstick and wait for the one from left of them. How to solve this classic</b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>synchronization problem?</b></span></pre>
</div>
<div align="left">
  <p ALIGN="LEFT">กก</div>
<div align="left">
  <b><font color="#ff0000" size="5"><span lang="en-ca"><a name="explain"></a>C</span>.<span lang="en-ca">The
  </span></font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>idea of 
  program</b></font></span></div>
<div align="left">
  กก</div>
<div align="left">
  <pre><span lang="en-ca"><b>Each philosopher must have three states to represent him to be &quot;thinking, eating, hungry&quot;. And each of them has</b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>a semaphore. Besides that there is a shared mutex to allow only one of them update the state of him in an atomic</b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>manner. The algorithm is given in Dr. Sinha's slides and I translate them into C++ with little modifications.</b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>1. When picking chopstick, philosopher must enter an atomic block to change his state to &quot;hungry&quot; state which </b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>differs him from &quot;thinking&quot; and &quot;eating&quot; state. Then he must test to see if NOT both left and right of him is </b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>in &quot;eating&quot; states which means that he is guaranteed to pick up one more chopstick from either side. And signal</b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>his semaphore after the test successful.</b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>2. He then must wait for semaphore because it is possible that some other philosopher might already have been</b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>waiting for that chopstick. If this is the case, he must wait for either of left or right side is released.</b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>3. When a philosopher finished eating, it is his duty to notify both left and right hand side of him, if they </b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>are in waiting state----&quot;hungry&quot;.</b></span></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5">D.<span lang="en-ca"><a name="Method"></a>The </span>major functions</font></b></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">E</span>.</font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>Further improvement</b></font></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>One of big problem is synchronization with &quot;std cout&quot;. You see, my thread is messed up when they use &quot;cout&quot; for</b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>display. I guess the reason is that I/O is extremely slow and context switch between threads is not synchronized</b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>with output. So, I have to store all result in an array called &quot;log&quot;. And display it only after threads terminate.</b></span></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">F</span>.</font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>File listing</b></font></span></pre>
</div>
<div align="left">
  <pre><font size="3"><b>1<span lang="en-ca">. main.cpp (main)</span></b></font></pre>
  <pre>กก</pre>
</div>
<div align="left">
  <pre>กก</pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><font size="3" color="#FF0000"><b>file name: </b></font></span><font size="3" color="#FF0000"><b><span lang="en-ca">main</span></b></font><span lang="en-ca"><font size="3" color="#FF0000"><b>.h</b></font></span></pre>
</div>
<pre>#include &lt;iostream&gt;
#include &lt;windows.h&gt;

using namespace std;

const int mainIndex=100;
const int PhiNumber=5;
int counter=50;
int log[500];
int logIndex=0;


#define LEFT(i) ((i-1)%PhiNumber)
#define RIGHT(i) ((i+1)%PhiNumber)

enum State
{THINKING, EATING, HUNGERY};
char* stateStr[3]= {&quot;THINKING&quot;, &quot;EATING&quot;, &quot;HUNGERY&quot;};

HANDLE phis[PhiNumber];
HANDLE sems[PhiNumber];
HANDLE mutex;
HANDLE display;
State states[PhiNumber];
int index[PhiNumber];

void initialize();

DWORD WINAPI run(void* param);

void think(int i);
void eat(int i);
void pickFork(int i);
void putFork(int i);
void test(int i);

int main()
{
	initialize();
	while (counter&gt;0)
	{
		Sleep(0);
		
		WaitForSingleObject(display, INFINITE);
		//log[logIndex++]=mainIndex;
		for (int i=0; i&lt;PhiNumber; i++)
		{
			log[logIndex++]=states[i];
		}
		ReleaseMutex(display);
		
		/*
		WaitForSingleObject(display, INFINITE);
		cout&lt;&lt;&quot;now the philosphors are in \n&quot;;
		for (int i=0; i&lt;PhiNumber; i++)
		{
			cout&lt;&lt;stateStr[states[i]]&lt;&lt;&quot;,&quot;;
		}
		cout&lt;&lt;&quot;\n&quot;;
		ReleaseMutex(display);
		Sleep(0);
		*/
	}
	for (int i=0; i&lt;PhiNumber; i++)
	{
		CloseHandle(sems[i]);
		CloseHandle(phis[i]);
	}
	i=0;
	cout&lt;&lt;&quot;now it is what main observes\n&quot;;
	while (i&lt;logIndex)
	{
		cout&lt;&lt;&quot;mainthread observes that\n&quot;;			
		for (int j=0; j&lt;PhiNumber; j++)
		{
			cout&lt;&lt;&quot;philosophor &quot;&lt;&lt;j&lt;&lt;&quot; is &quot;&lt;&lt;stateStr[log[i++]]&lt;&lt;endl;
		}
		/*
		cout&lt;&lt;&quot;philosophor &quot;&lt;&lt;log[i];
		i++;
		cout&lt;&lt;&quot; says he is &quot;&lt;&lt;stateStr[log[i]]&lt;&lt;endl;
		i++;
		*/
		//cout&lt;&lt;&quot;philosophor &quot;&lt;&lt;log[i++]&lt;&lt;&quot; says he is &quot;&lt;&lt;stateStr[log[i++]]&lt;&lt;endl;
		/*
		if (log[i]!=mainIndex)
		{
			cout&lt;&lt;&quot;philosophor &quot;&lt;&lt;log[i++]&lt;&lt;&quot; says he is &quot;&lt;&lt;stateStr[log[i++]]&lt;&lt;endl;
		}
		else
		{
			cout&lt;&lt;&quot;mainthread observes that\n&quot;;			
			for (int j=0; j&lt;PhiNumber; j++)
			{
				cout&lt;&lt;&quot;philosophor &quot;&lt;&lt;j&lt;&lt;&quot; is &quot;&lt;&lt;stateStr[log[++i]]&lt;&lt;endl;
			}
		}
		*/
	}

	return 0;
}

void initialize()
{
	for (int i=0; i&lt;PhiNumber; i++)
	{
		index[i]=i;
		sems[i]=CreateSemaphore(NULL, 0, PhiNumber-1, NULL);
		phis[i]=CreateThread(NULL, 0, run, index+i, 0, NULL);
		display=CreateMutex(NULL, false, NULL);
		states[i]=THINKING;
	}
	mutex=CreateMutex(NULL, false, NULL);
}

DWORD WINAPI run(void* param)
{
	while (counter&gt;0)
	{
		think(*((int*)(param)));
		pickFork(*((int*)(param)));
		eat(*((int*)(param)));
		putFork(*((int*)(param)));
		counter--;
	}
	return *((int*)(param));
}

void test(int i)
{
	if (states[i]==HUNGERY&amp;&amp;states[LEFT(i)]!=EATING&amp;&amp;states[RIGHT(i)]!=EATING)
	{
		states[i]=EATING;
		ReleaseSemaphore(sems[i], 1, NULL);
	}
}

void pickFork(int i)
{
	WaitForSingleObject(mutex, INFINITE);
	states[i]=HUNGERY;
	test(i);
	ReleaseMutex(mutex);
	WaitForSingleObject(sems[i], INFINITE);
}

void putFork(int i)
{
	WaitForSingleObject(mutex, INFINITE);

	states[i]=THINKING;
	test(LEFT(i));
	test(RIGHT(i));

	ReleaseMutex(mutex);
}

void think(int i)
{
	/*
	WaitForSingleObject(display, INFINITE);
	cout&lt;&lt;&quot;philosopher &quot;&lt;&lt;i&lt;&lt;&quot; is thinking ...\n&quot;;
	ReleaseMutex(display);
	*/
	/*
	WaitForSingleObject(display, INFINITE);
	log[logIndex++]=i;
	log[logIndex++]=states[i];
	ReleaseMutex(display);
	*/
	Sleep(0);
}

void eat(int i)
{
	/*
	WaitForSingleObject(display, INFINITE);
	cout&lt;&lt;&quot;philosopher &quot;&lt;&lt;i&lt;&lt;&quot; is eating ...\n&quot;;
	ReleaseMutex(display);
	*/
	/*
	WaitForSingleObject(display, INFINITE);
	log[logIndex++]=i;
	log[logIndex++]=states[i];
	ReleaseMutex(display);
	*/
	Sleep(0);
}</pre>
<pre>
</pre>
<pre><font color="#0000FF"><b><span lang="en-ca">The input is something like following:</span></b></font></pre>
<pre><font color="#0000FF"><b>Here is the <a name="result"></a>result:</b></font></pre>

<p>now it is what main observes<br>
mainthread observes that<br>
philosophor 0 is THINKING<br>
philosophor 1 is THINKING<br>
philosophor 2 is THINKING<br>
philosophor 3 is THINKING<br>
philosophor 4 is THINKING<br>
mainthread observes that<br>
philosophor 0 is EATING<br>
philosophor 1 is HUNGERY<br>
philosophor 2 is EATING<br>
philosophor 3 is HUNGERY<br>
philosophor 4 is HUNGERY<br>
mainthread observes that<br>
philosophor 0 is THINKING<br>
philosophor 1 is EATING<br>
philosophor 2 is THINKING<br>
philosophor 3 is EATING<br>
philosophor 4 is HUNGERY<br>
mainthread observes that<br>
philosophor 0 is EATING<br>
philosophor 1 is THINKING<br>
philosophor 2 is EATING<br>
philosophor 3 is THINKING<br>
philosophor 4 is HUNGERY<br>
mainthread observes that<br>
philosophor 0 is THINKING<br>
philosophor 1 is EATING<br>
philosophor 2 is THINKING<br>
philosophor 3 is EATING<br>
philosophor 4 is HUNGERY<br>
mainthread observes that<br>
philosophor 0 is EATING<br>
philosophor 1 is THINKING<br>
philosophor 2 is EATING<br>
philosophor 3 is THINKING<br>
philosophor 4 is HUNGERY<br>
mainthread observes that<br>
philosophor 0 is THINKING<br>
philosophor 1 is EATING<br>
philosophor 2 is THINKING<br>
philosophor 3 is EATING<br>
philosophor 4 is HUNGERY<br>
mainthread observes that<br>
philosophor 0 is EATING<br>
philosophor 1 is THINKING<br>
philosophor 2 is EATING<br>
philosophor 3 is THINKING<br>
philosophor 4 is HUNGERY<br>
mainthread observes that<br>
philosophor 0 is THINKING<br>
philosophor 1 is EATING<br>
philosophor 2 is THINKING<br>
philosophor 3 is EATING<br>
philosophor 4 is HUNGERY<br>
mainthread observes that<br>
philosophor 0 is EATING<br>
philosophor 1 is THINKING<br>
philosophor 2 is EATING<br>
philosophor 3 is THINKING<br>
philosophor 4 is HUNGERY<br>
mainthread observes that<br>
philosophor 0 is THINKING<br>
philosophor 1 is EATING<br>
philosophor 2 is THINKING<br>
philosophor 3 is EATING<br>
philosophor 4 is HUNGERY<br>
mainthread observes that<br>
philosophor 0 is EATING<br>
philosophor 1 is THINKING<br>
philosophor 2 is EATING<br>
philosophor 3 is THINKING<br>
philosophor 4 is HUNGERY<br>
mainthread observes that<br>
philosophor 0 is THINKING<br>
philosophor 1 is EATING<br>
philosophor 2 is THINKING<br>
philosophor 3 is EATING<br>
philosophor 4 is HUNGERY<br>
mainthread observes that<br>
philosophor 0 is EATING<br>
philosophor 1 is THINKING<br>
philosophor 2 is EATING<br>
philosophor 3 is THINKING<br>
philosophor 4 is HUNGERY<br>
mainthread observes that<br>
philosophor 0 is THINKING<br>
philosophor 1 is EATING<br>
philosophor 2 is THINKING<br>
philosophor 3 is EATING<br>
philosophor 4 is HUNGERY<br>
mainthread observes that<br>
philosophor 0 is EATING<br>
philosophor 1 is THINKING<br>
philosophor 2 is EATING<br>
philosophor 3 is THINKING<br>
philosophor 4 is HUNGERY<br>
mainthread observes that<br>
philosophor 0 is THINKING<br>
philosophor 1 is EATING<br>
philosophor 2 is THINKING<br>
philosophor 3 is EATING<br>
philosophor 4 is HUNGERY<br>
mainthread observes that<br>
philosophor 0 is EATING<br>
philosophor 1 is THINKING<br>
philosophor 2 is EATING<br>
philosophor 3 is THINKING<br>
philosophor 4 is HUNGERY<br>
mainthread observes that<br>
philosophor 0 is THINKING<br>
philosophor 1 is EATING<br>
philosophor 2 is THINKING<br>
philosophor 3 is EATING<br>
philosophor 4 is HUNGERY<br>
mainthread observes that<br>
philosophor 0 is EATING<br>
philosophor 1 is THINKING<br>
philosophor 2 is EATING<br>
philosophor 3 is THINKING<br>
philosophor 4 is HUNGERY<br>
mainthread observes that<br>
philosophor 0 is THINKING<br>
philosophor 1 is EATING<br>
philosophor 2 is THINKING<br>
philosophor 3 is EATING<br>
philosophor 4 is HUNGERY<br>
mainthread observes that<br>
philosophor 0 is EATING<br>
philosophor 1 is THINKING<br>
philosophor 2 is EATING<br>
philosophor 3 is THINKING<br>
philosophor 4 is HUNGERY<br>
mainthread observes that<br>
philosophor 0 is THINKING<br>
philosophor 1 is EATING<br>
philosophor 2 is THINKING<br>
philosophor 3 is EATING<br>
philosophor 4 is HUNGERY<br>
mainthread observes that<br>
philosophor 0 is EATING<br>
philosophor 1 is THINKING<br>
philosophor 2 is EATING<br>
philosophor 3 is THINKING<br>
philosophor 4 is HUNGERY<br>
mainthread observes that<br>
philosophor 0 is THINKING<br>
philosophor 1 is EATING<br>
philosophor 2 is THINKING<br>
philosophor 3 is EATING<br>
philosophor 4 is HUNGERY<br>
mainthread observes that<br>
philosophor 0 is EATING<br>
philosophor 1 is THINKING<br>
philosophor 2 is EATING<br>
philosophor 3 is THINKING<br>
philosophor 4 is HUNGERY<br>
mainthread observes that<br>
philosophor 0 is THINKING<br>
philosophor 1 is EATING<br>
philosophor 2 is THINKING<br>
philosophor 3 is EATING<br>
philosophor 4 is HUNGERY<br>
Press any key to continue</p>

<pre>
</pre>

<pre></pre>

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                                   
&nbsp;&nbsp;&nbsp; <a href="WhoAmI.htm">                  







                       <img src="picture/back.gif" style="border: medium none" alt="back.gif (341 bytes)" width="32" height="35"></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
<a href="index.htm">
<img src="picture/up.gif" style="border: medium none" alt="up.gif (335 bytes)" width="35" height="32"></a>       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                         
<img src="picture/next.gif" style="border: medium none" alt="next.gif (337 bytes)" width="32" height="35">          


</p>

</body>

</html>