<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; 
platform and bridge</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">
  <ol>
    <li>Tourists in Mont-Tremblant can hike to a platform to get a wonderful 
    view of the mountains. The platform is precariously placed and can hold at 
    most 5 people at a time. The path to the platform is through an old bridge 
    which can hold only 3 people at a time. To prevent either the bridge or the 
    platform from collapsing, the tourist processes execute the following code, 
    with the initial values of the semaphores being set to 5 for ``platform'' 
    and 3 for ``bridge''.
    <pre>       P(bridge);          /* Ensure that there is place on the bridge */ 
       ENTER BRIDGE
       TOURIST ON BRIDGE
       P(platform);        /* Ensure space on platform before leaving bridge */
       HOP FROM BRIDGE TO PLATFORM
       V(bridge);          /* Let somebody get on the bridge */ 
       DO SIGHTSEEING
       P(bridge);          /* Coming back; ensure a place on the bridge */
       HOP FROM PLATFORM TO BRIDGE
       V(platform);        /* Let someone get on the platform */
       TOURIST ON BRIDGE
       EXIT BRIDGE
       V(bridge) ;         /* Let someone else get on the bridge */ 
       </pre>
    <p>Unfortunately, the above code results in a deadlock when there are 5 
    tourists on the platform and 3 on the bridge headed towards the platform. 
    Note that the tourists on the bridge are not allowed to go back without 
    seeing the sights. Suggest a simple solution to avoid deadlocks, which 
    blocks a tourist when entering the bridge (going towards the platform) if 
    there is a possibility of a deadlock. Your solution should be in two steps: 
    (i) a description in English text of your strategy, and (ii) a pseudo-code 
    extension using additional semaphore(s) and/or shared variable(s).</li>
  </ol>
  <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>If you limit the sum of people on bridge and platform to below 8, then it is ok. So simple a question, right?</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 problem puzzles me a lot is the sequence of ready queue in Windows, it seems that some thread always gets</b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>a chance to run even I forced them to sleep. Unfortunately in XP, I cannot use some functions like &quot;SwitchToThread&quot;</b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>which is available on win 2k.</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;
#include &lt;stdio.h&gt;
//using namespace std;

void sightSeeing(char* myName);

unsigned long __stdcall run(void* param);

const int VisitorNumber=26;
void* visitors[26];
int times[VisitorNumber]={0};
void* bridge;
void* platform;
void* mutex;
int count=0;
int total=0;
bool finish=false;

int main()
{
	char name[VisitorNumber];
	platform=CreateSemaphore(NULL, 5, 5, NULL);
	bridge=CreateSemaphore(NULL, 3, 3, NULL);
	mutex=CreateMutex(NULL, false, NULL);

	for (int i=0; i&lt;VisitorNumber; i++)
	{
		name[i]='A'+i;
		visitors[i]=CreateThread(NULL, 0, run, (void*)(name+i), 0, NULL);
	}
	while (count&lt;1000)
	{
		SleepEx(0, false);
	}
	finish=true;
	SleepEx(0, false);
	SleepEx(0, false);
	SleepEx(0, false);
	for (i=0; i&lt;VisitorNumber; i++)
	{
		printf(&quot;visitor %c visits %d \n&quot;, 'A'+i, times[i]);
	}	
	return 0;
}


void sightSeeing(char* myName)
{
	printf(&quot;%c oh...\n&quot;, *myName);
}


unsigned long __stdcall run(void* param)
{
	while (!finish)
	{
		WaitForSingleObject(mutex, INFINITE);
		if (total&lt;7&amp;&amp;times[*(char*)(param)-'A']&lt;60)
		{
			total++;
			count++;
			ReleaseMutex(mutex);//let others to go

			//before bridge
			WaitForSingleObject(bridge, INFINITE);
			//before platform
			WaitForSingleObject(platform, INFINITE);
			//hop on platform
			ReleaseSemaphore(bridge, 1, NULL);
			//see ....
			sightSeeing((char*)param);
			times[*(char*)(param)-'A']++;
			SleepEx(0, false);//stay for a while...
			//SwitchToThread();
			//go down to bridge
			WaitForSingleObject(bridge, INFINITE);

			//
			ReleaseSemaphore(platform, 1, NULL);
			ReleaseSemaphore(bridge,1, NULL);
			//WaitForSingleObject(mutex, INFINITE);
			total--;
			SleepEx(0, false);
			//ReleaseMutex(mutex);
		}
		else
		{
			ReleaseMutex(mutex);
			SleepEx(0, false);
		}
	}
	return 0;
}

	</pre>
<pre>　</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>

<pre><span lang="en-ca"><font color="#0000FF"><b>...</b></font></span></pre>

<pre><span lang="en-ca"><font color="#0000FF"><b>...</b></font></span></pre>

<p>Q oh...<br>
V oh...<br>
W oh...<br>
X oh...<br>
R oh...<br>
S oh...<br>
C oh...<br>
D oh...<br>
Y oh...<br>
T oh...<br>
U oh...<br>
J oh...<br>
Q oh...<br>
V oh...<br>
W oh...<br>
X oh...<br>
R oh...<br>
visitor A visits 60<br>
visitor B visits 60<br>
visitor C visits 24<br>
visitor D visits 24<br>
visitor E visits 1<br>
visitor F visits 60<br>
visitor G visits 60<br>
visitor H visits 60<br>
visitor I visits 60<br>
visitor J visits 25<br>
visitor K visits 60<br>
visitor L visits 60<br>
visitor M visits 60<br>
visitor N visits 60<br>
visitor O visits 60<br>
visitor P visits 60<br>
visitor Q visits 24<br>
visitor R visits 24<br>
visitor S visits 23<br>
visitor T visits 23<br>
visitor U visits 23<br>
visitor V visits 23<br>
visitor W visits 23<br>
visitor X visits 23<br>
visitor Y visits 22<br>
visitor Z visits 0<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>