⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sem3.htm

📁 ST20 Embedded Toolset R2.0.5用于开发基于ST20芯片机顶盒软件的开发平台,2.0.5版本,国内找不到的.在国外论坛上花了N天才找到!
💻 HTM
字号:
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<meta name="GENERATOR" content="Quadralay WebWorks Publisher Professional Edition 6.0.5">
<meta name="TEMPLATEBASE" content="book_html">
<meta name="LASTUPDATED" content="12/01/03 14:14:58">
<title>6.2 Use of semaphores</title>

<STYLE TYPE="text/css">
<!--
	span.Signal { text-transform: uppercase; font-family: Verdana }
-->
</STYLE>

</head>

<body link="#3366CC" vlink="#9999CC" text="#000000" alink="#0000CC" bgcolor="#FFFFFF"
background="images/backgrnd.gif">

<p><img src="images/stlogo.gif" width="106" height="83" align="left"
alt="logo here!"> </p>

<table width="331" border="0" align="right" cellpadding="0" cellspacing="0">
  <tr>
    <td><a href="os20toc.htm"><img src="images/navtoc.gif" width="84" height="23"
    border="0" alt="TOC"> </a></td>
    <td><a href="sem2.htm"><img src="images/navprev.gif" width="80" height="23"
    border="0" alt="PREV"> </a></td>
    <td><a href="sem4.htm"><img src="images/navnext.gif" width="83" height="23"
    border="0" alt="NEXT"> </a></td>
    <td><a href="os20ix.htm"><img src="images/navidx.gif" width="84" height="23"
    border="0" alt="INDEX"> </a></td>
  </tr>
</table>

<p><br clear="all">
</p>

<hr align="left">

<blockquote>
<h2>
  <a name="1204477"> </a><font color="#003366"  face="Verdana, Arial, Helvetica, sans-serif">6.2 	 Use of semaphores</font>
</h2><hr>


<p>
  <a name="1204478"> </a><font size=2  face="Verdana, Arial, Helvetica, sans-serif">Semaphores can be defined to allow a given number of tasks simultaneous access to a shared resource. The maximum number of tasks allowed is determined when the semaphore is initialized. When that number of tasks have acquired the resource, the next task to request access to it waits until one of those holding the semaphore relinquishes it.</font>
</p>


<p>
  <a name="1204479"> </a><font size=2  face="Verdana, Arial, Helvetica, sans-serif">Semaphores can protect a resource only if all tasks that wish to use the resource also use the same semaphore. It cannot protect a resource from a task that does not use the semaphore and accesses the resource directly.</font>
</p>


<p>
  <a name="1204480"> </a><font size=2  face="Verdana, Arial, Helvetica, sans-serif">Typically, semaphores are set up to allow at most one task access to a resource at any given time. This is known as using the semaphore in binary mode, where the count either has the value zero or one. This is useful for mutual exclusion or synchronization of access to shared data. Areas of code protected using semaphores are sometimes called critical regions.</font>
</p>


<p>
  <a name="1204481"> </a><font size=2  face="Verdana, Arial, Helvetica, sans-serif">When used for mutual exclusion the semaphore is initialized to one, indicating that no task is currently in the critical region, and that at most one can be. The critical region is surrounded with calls to <font size=2 face=Courier><strong>semaphore_wait</strong></font> at the start and <font size=2 face=Courier><strong>semaphore_signal</strong></font> at the end. Thus the first task which tries to enter the critical region successfully takes the semaphore, and any others are forced to wait. When the task currently in the critical region leaves, it releases the semaphore, and allows the first of the waiting tasks into the critical region.</font>
</p>


<p>
  <a name="1204482"> </a><font size=2  face="Verdana, Arial, Helvetica, sans-serif">Semaphores are also used for synchronization. Usually this is between a task and an interrupt handler, with the task waiting for the interrupt handler. When used in this way the semaphore is initialized to zero. The task then performs a <font size=2 face=Courier><strong>semaphore_wait</strong></font> on the semaphore, and deschedules. Later the interrupt handler performs a <font size=2 face=Courier><strong>semaphore_signal</strong></font>, which reschedules the task. This process can then be repeated, with the semaphore count never changing from zero.</font>
</p>


<p>
  <a name="1204486"> </a><font size=2  face="Verdana, Arial, Helvetica, sans-serif">All the OS20 semaphores can also be used in a counting mode, where the count can be any positive number. The typical application for this is controlling access to a shared resource, where there are multiple resources available. Such a semaphore allows <em>N</em> tasks simultaneous access to a resource and is initialized with the value <em>N</em>. Each task performs a <font size=2 face=Courier><strong>semaphore_wait</strong></font> when it wants a device. If a device is available the call returns immediately, having decremented the counter. If no devices are available then the task is added to the queue. When a task has finished using a device it calls <font size=2 face=Courier><strong>semaphore_signal</strong></font> to release it.</font>
</p>
</blockquote>

<hr>



<table width="331" border="0" align="left" cellpadding="0" cellspacing="0">
  <tr>
    <td><a href="os20toc.htm"><img src="images/navtoc.gif" width="84" height="23"
    border="0" alt="TOC"> </a></td>
    <td><a href="sem2.htm"><img src="images/navprev.gif" width="80" height="23"
    border="0" alt="PREV"> </a></td>
    <td><a href="sem4.htm"><img src="images/navnext.gif" width="83" height="23"
    border="0" alt="NEXT"> </a></td>
    <td><a href="os20ix.htm"><img src="images/navidx.gif" width="84" height="23"
    border="0" alt="INDEX"> </a></td>
  </tr>
</table>
<font size=1 face="Verdana, Arial, Helvetica, sans-serif">&nbsp; &copy; 2001, 2002, 2003 STMicroelectronics. All Rights Reserved.<br>
&nbsp; ADCS 7473749B</font>
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -