📄 semblib.html
字号:
<html><head><!-- /vobs/wpwr/docs/vxworks/ref/semBLib.html - generated by refgen from semBLib.c --> <title> semBLib </title></head><body bgcolor="#FFFFFF"> <hr><a name="top"></a><p align=right><a href="libIndex.htm"><i>VxWorks API Reference : OS Libraries</i></a></p></blockquote><h1>semBLib</h1> <blockquote></a></blockquote><h4>NAME</h4><blockquote> <p><strong>semBLib</strong> - binary semaphore library </p></blockquote><h4>ROUTINES</h4><blockquote><p><p><b><a href="./semBLib.html#semBCreate">semBCreate</a>( )</b> - create and initialize a binary semaphore<br><p></blockquote><h4>DESCRIPTION</h4><blockquote><p>This library provides the interface to VxWorks binary semaphores.Binary semaphores are the most versatile, efficient, and conceptuallysimple type of semaphore. They can be used to: (1) control mutuallyexclusive access to shared devices or data structures, or (2) synchronizemultiple tasks, or task-level and interrupt-level processes. Binarysemaphores form the foundation of numerous VxWorks facilities.<p>A binary semaphore can be viewed as a cell in memory whose contents are inone of two states, full or empty. When a task takes a binary semaphore,using <b><a href="./semLib.html#semTake">semTake</a>( )</b>, subsequent action depends on the state of the semaphore:<table></tr><tr valign=top><td>(1)<td>If the semaphore is full, the semaphore is made empty, and the calling taskcontinues executing.</tr><tr valign=top><td>(2)<td>If the semaphore is empty, the task will be blocked, pending theavailability of the semaphore. If a timeout is specified and the timeoutexpires, the pended task will be removed from the queue of pended tasksand enter the ready state with an ERROR status. A pended taskis ineligible for CPU allocation. Any number of tasks may be pendedsimultaneously on the same binary semaphore.</table>When a task gives a binary semaphore, using <b><a href="./semLib.html#semGive">semGive</a>( )</b>, the next availabletask in the pend queue is unblocked. If no task is pending on thissemaphore, the semaphore becomes full. Note that if a semaphore is given,and a task is unblocked that is of higher priority than the task that called<b><a href="./semLib.html#semGive">semGive</a>( )</b>, the unblocked task will preempt the calling task.<p></blockquote><h4>MUTUAL EXCLUSION</h4><blockquote><p>To use a binary semaphore as a means of mutual exclusion, first create itwith an initial state of full. For example:<pre> SEM_ID semMutex; /* create a binary semaphore that is initially full */ semMutex = semBCreate (SEM_Q_PRIORITY, SEM_FULL);</pre>Then guard a critical section or resource by taking the semaphore with<b><a href="./semLib.html#semTake">semTake</a>( )</b>, and exit the section or release the resource by givingthe semaphore with <b><a href="./semLib.html#semGive">semGive</a>( )</b>. For example:<pre> semTake (semMutex, WAIT_FOREVER); ... /* critical region, accessible only by one task at a time */ semGive (semMutex);</pre>While there is no restriction on the same semaphore being given, taken, orflushed by multiple tasks, it is important to ensure the properfunctionality of the mutual-exclusion construct. While there is no dangerin any number of processes taking a semaphore, the giving of a semaphoreshould be more carefully controlled. If a semaphore is given by a task thatdid not take it, mutual exclusion could be lost.<p></blockquote><h4>SYNCHRONIZATION</h4><blockquote><p>To use a binary semaphore as a means of synchronization, create itwith an initial state of empty. A task blocks by taking a semaphoreat a synchronization point, and it remains blocked until the semaphore is givenby another task or interrupt service routine.<p>Synchronization with interrupt service routines is a particularly common need.Binary semaphores can be given, but not taken, from interrupt level. Thus, atask can block at a synchronization point with <b><a href="./semLib.html#semTake">semTake</a>( )</b>, and an interruptservice routine can unblock that task with <b><a href="./semLib.html#semGive">semGive</a>( )</b>.<p>In the following example, when <b>init( )</b> is called, the binary semaphore iscreated, an interrupt service routine is attached to an event, and a taskis spawned to process the event. Task 1 will run until it calls <b><a href="./semLib.html#semTake">semTake</a>( )</b>,at which point it will block until an event causes the interrupt serviceroutine to call <b><a href="./semLib.html#semGive">semGive</a>( )</b>. When the interrupt service routine completes,task 1 can execute to process the event.<pre> SEM_ID semSync; /* ID of sync semaphore */ init () { intConnect (..., eventInterruptSvcRout, ...); semSync = semBCreate (SEM_Q_FIFO, SEM_EMPTY); taskSpawn (..., task1); } task1 () { ... semTake (semSync, WAIT_FOREVER); /* wait for event */ ... /* process event */ } eventInterruptSvcRout () { ... semGive (semSync); /* let task 1 process event */ ... }</pre>A <b><a href="./semLib.html#semFlush">semFlush</a>( )</b> on a binary semaphore will atomically unblock all pendedtasks in the semaphore queue, i.e., all tasks will be unblocked at once,before any actually execute.<p></blockquote><h4>CAVEATS</h4><blockquote><p>There is no mechanism to give back or reclaim semaphores automatically whentasks are suspended or deleted. Such a mechanism, though desirable, is notcurrently feasible. Without explicit knowledge of the state of the guardedresource or region, reckless automatic reclamation of a semaphore couldleave the resource in a partial state. Thus, if a task ceases executionunexpectedly, as with a bus error, currently owned semaphores will not begiven back, effectively leaving a resource permanently unavailable. Themutual-exclusion semaphores provided by <b><a href="./semMLib.html#top">semMLib</a></b> offer protection fromunexpected task deletion.<p></blockquote><h4>INCLUDE FILES</h4><blockquote><p><b>semLib.h</b><p></blockquote><h4>SEE ALSO</h4><blockquote><p><b><a href="./semLib.html#top">semLib</a></b>, <b><a href="./semCLib.html#top">semCLib</a></b>, <b><a href="./semMLib.html#top">semMLib</a></b>,<i>VxWorks Programmer's Guide: Basic OS </i><hr><a name="semBCreate"></a><p align=right><a href="rtnIndex.htm"><i>OS Libraries : Routines</i></a></p></blockquote><h1>semBCreate( )</h1> <blockquote></a></blockquote><h4>NAME</h4><blockquote> <p><strong>semBCreate( )</strong> - create and initialize a binary semaphore</p></blockquote><h4>SYNOPSIS</h4><blockquote><p><pre>SEM_ID semBCreate ( int options, /* semaphore options */ SEM_B_STATE initialState /* initial semaphore state */ )</pre></blockquote><h4>DESCRIPTION</h4><blockquote><p>This routine allocates and initializes a binary semaphore. The semaphoreis initialized to the <i>initialState</i> of either <b>SEM_FULL</b> (1) or <b>SEM_EMPTY</b> (0).<p>The <i>options</i> parameter specifies the queuing style for blocked tasks.Tasks can be queued on a priority basis or a first-in-first-out basis.These options are <b>SEM_Q_PRIORITY</b> (0x1) and <b>SEM_Q_FIFO</b> (0x0), respectively.That parameter also specifies if <b><a href="./semLib.html#semGive">semGive</a>( )</b> should return ERROR whenthe semaphore fails to send events. This option is turned off by default;it is activated by doing a bitwise-OR of <b>SEM_EVENTSEND_ERR_NOTIFY</b> (0x10)with the queuing style of the semaphore.<p></blockquote><h4>RETURNS</h4><blockquote><p>The semaphore ID, or NULL if memory cannot be allocated.</blockquote><h4>SEE ALSO</h4><blockquote><p><b><a href="./semBLib.html#top">semBLib</a></b></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -