📄 c-basic4.html
字号:
<table border="0" cellpadding="0" cellspacing="0"><tr><td colspan="20"><hr class="tablerule"></td></tr><tr valign="middle"><th rowspan="1" colspan="1"><div class="CellHeading"><b><a name="91287"> </a><font face="Helvetica, sans-serif" size="-1" class="sans">Call</font></b></div></th><th rowspan="1" colspan="1"><div class="CellHeading"><b><a name="91289"> </a><font face="Helvetica, sans-serif" size="-1" class="sans">Description</font></b></div></th></tr><tr><td colspan="20"><hr class="tablerule2"></td></tr><tr valign="top"><td colspan=1 rowspan=1><div class="CellBody"><a name="91292"> </a><b class="routine"><i class="routine">semPxLibInit</i></b><b>( )</b> </div></td><td colspan=1 rowspan=1><div class="CellBody"><a name="91294"> </a>Initialize the POSIX semaphore library (non-POSIX). </div></td></tr><tr valign="top"><td colspan=1 rowspan=1><div class="CellBody"><a name="91297"> </a><b class="routine"><i class="routine">sem_init</i></b><b>( )</b><b class="routine"><i class="routine"> </i></b> </div></td><td colspan=1 rowspan=1><div class="CellBody"><a name="91299"> </a>Initialize an unnamed semaphore. </div></td></tr><tr valign="top"><td colspan=1 rowspan=1><div class="CellBody"><a name="91302"> </a><b class="routine"><i class="routine">sem_destroy</i></b><b>( )</b><b class="routine"><i class="routine"> </i></b> </div></td><td colspan=1 rowspan=1><div class="CellBody"><a name="91304"> </a>Destroy an unnamed semaphore. </div></td></tr><tr valign="top"><td colspan=1 rowspan=1><div class="CellBody"><a name="91307"> </a><b class="routine"><i class="routine">sem_open</i></b><b>( )</b><b class="routine"><i class="routine"> </i></b> </div></td><td colspan=1 rowspan=1><div class="CellBody"><a name="91309"> </a>Initialize/open a named semaphore. </div></td></tr><tr valign="top"><td colspan=1 rowspan=1><div class="CellBody"><a name="91312"> </a><b class="routine"><i class="routine">sem_close</i></b><b>( )</b><b class="routine"><i class="routine"> </i></b> </div></td><td colspan=1 rowspan=1><div class="CellBody"><a name="91314"> </a>Close a named semaphore. </div></td></tr><tr valign="top"><td colspan=1 rowspan=1><div class="CellBody"><a name="91317"> </a><b class="routine"><i class="routine">sem_unlink</i></b><b>( )</b><b class="routine"><i class="routine"> </i></b> </div></td><td colspan=1 rowspan=1><div class="CellBody"><a name="91319"> </a>Remove a named semaphore. </div></td></tr><tr valign="top"><td colspan=1 rowspan=1><div class="CellBody"><a name="91322"> </a><b class="routine"><i class="routine">sem_wait</i></b><b>( )</b><b class="routine"><i class="routine"> </i></b> </div></td><td colspan=1 rowspan=1><div class="CellBody"><a name="91324"> </a>Lock a semaphore. </div></td></tr><tr valign="top"><td colspan=1 rowspan=1><div class="CellBody"><a name="91327"> </a><b class="routine"><i class="routine">sem_trywait</i></b><b>( )</b> </div></td><td colspan=1 rowspan=1><div class="CellBody"><a name="91329"> </a>Lock a semaphore only if it is not already locked. </div></td></tr><tr valign="top"><td colspan=1 rowspan=1><div class="CellBody"><a name="91332"> </a><b class="routine"><i class="routine">sem_post</i></b><b>( )</b> </div></td><td colspan=1 rowspan=1><div class="CellBody"><a name="91334"> </a>Unlock a semaphore. </div></td></tr><tr valign="top"><td colspan=1 rowspan=1><div class="CellBody"><a name="91337"> </a><b class="routine"><i class="routine">sem_getvalue</i></b><b>( )</b> </div></td><td colspan=1 rowspan=1><div class="CellBody"><a name="91339"> </a>Get the value of a semaphore. </div></td></tr><tr><td colspan="20"><hr class="tablerule"></td></tr><tr valign="middle"><td colspan="20"></td></tr></table></p></p></dl></dl><dl class="margin"><dd><p class="table" callout><table border="0" cellpadding="0" cellspacing="0"><tr valign="top"><td valign="top" width="40"><br><img border="0" alt="*" src="icons/warning.gif"></td><td><hr><div class="CalloutCell"><a name="92825"><b class="symbol_UC"><font face="Helvetica, sans-serif" size="-1" class="sans">WARNING: </font></b></a>The <b class="routine"><i class="routine">sem_destroy</i></b><b>( )</b> call terminates an unnamed semaphore and deallocates any associated memory; the combination of <b class="routine"><i class="routine">sem_close</i></b><b>( )</b> and <b class="routine"><i class="routine">sem_unlink</i></b><b>( )</b> has the same effect for named semaphores. Take care when deleting semaphores, particularly mutual exclusion semaphores, to avoid deleting a semaphore still required by another task. Do not delete a semaphore unless the deleting task first succeeds in locking that semaphore. (Likewise, for named semaphores, close semaphores only from the same task that opens them.)</div></td></tr><tr valign="top"><td></td><td><hr></td></tr><tr valign="middle"><td colspan="20"></td></tr></table></p callout></dl><dl class="margin"><dd><font face="Helvetica, sans-serif" size="-1" class="sans"><h5 class="HU"><i><a name="85654">Comparison of POSIX and Wind Semaphores</a></i></h5></font><dl class="margin"><dd><p class="Body"><a name="85656"> </a>POSIX semaphores are <i class="term">counting</i> semaphores; that is, they keep track of the number of times they are given.</p><dd><p class="Body"><a name="85657"> </a>The Wind semaphore mechanism is similar to that specified by POSIX, except that Wind semaphores offer additional features: priority inheritance, task-deletion safety, the ability for a single task to take a semaphore multiple times, ownership of mutual-exclusion semaphores, semaphore timeouts, and the choice of queuing mechanism. When these features are important, Wind semaphores are preferable.</p></dl><dd><font face="Helvetica, sans-serif" size="-1" class="sans"><h5 class="HU"><i><a name="85659">Using Unnamed Semaphores </a></i></h5></font><dl class="margin"><dd><p class="Body"><a name="85660"> </a>In using unnamed semaphores, normally one task allocates memory for the semaphore and initializes it. A semaphore is represented with the data structure <b class="symbol_lc">sem_t</b>, defined in <b class="file">semaphore.h</b>. The semaphore initialization routine, <b class="routine"><i class="routine">sem_init</i></b><b>( )</b>, allows you to specify the initial value. </p><dd><p class="Body"><a name="85662"> </a>Once the semaphore is initialized, any task can use the semaphore by locking it with <b class="routine"><i class="routine">sem_wait</i></b><b>( )</b> (blocking) or <b class="routine"><i class="routine">sem_trywait</i></b><b>( )</b> (non-blocking), and unlocking it with <b class="routine"><i class="routine">sem_post</i></b><b>( )</b>.</p><dd><p class="Body"><a name="85666"> </a>As noted earlier, semaphores can be used for both synchronization and mutual exclusion. When a semaphore is used for synchronization, it is typically initialized to zero (locked). The task waiting to be synchronized blocks on a <b class="routine"><i class="routine">sem_wait</i></b><b>( )</b>. The task doing the synchronizing unlocks the semaphore using <b class="routine"><i class="routine">sem_post</i></b><b>( )</b>. If the task blocked on the semaphore is the only one waiting for that semaphore, the task unblocks and becomes ready to run. If other tasks are blocked on the semaphore, the task with the highest priority is unblocked.</p><dd><p class="Body"><a name="85667"> </a>When a semaphore is used for mutual exclusion, it is typically initialized to a value greater than zero (meaning that the resource is available). Therefore, the first task to lock the semaphore does so without blocking; subsequent tasks block (if the semaphore value was initialized to 1).</p></dl></dl><h4 class="EntityTitle"><a name="85669"><font face="Helvetica, sans-serif" size="-1" class="sans">Example 2-6: POSIX Unnamed Semaphores </font></a></h4><dl class="margin"><dl class="margin"><dd><pre class="Code"><b><a name="85670">/* This example uses unnamed semaphores to synchronize an action between * the calling task and a task that it spawns (tSyncTask). To run from * the shell, spawn as a task: * -> sp unnameSem */</a></b><dd> <b><a name="85672">/* includes */ #include "vxWorks.h" #include "semaphore.h"</a></b><dd> <b><a name="85674">/* forward declarations */ void syncTask (sem_t * pSem);</a></b><dd> <b><a name="85676">void unnameSem (void) { sem_t * pSem;</a></b><dd> <b><a name="85678"> /* reserve memory for semaphore */ pSem = (sem_t *) malloc (sizeof (sem_t));</a></b><dd> <b><a name="85680"> /* initialize semaphore to unavailable */ if (sem_init (pSem, 0, 0) == -1) { printf ("unnameSem: sem_init failed\n"); return; }</a></b><dd> <b><a name="85682"> /* create sync task */ printf ("unnameSem: spawning task...\n"); taskSpawn ("tSyncTask", 90, 0, 2000, syncTask, pSem);</a></b><dd> <b><a name="85684"> /* do something useful to synchronize with syncTask */</a></b><dd> <b><a name="85686"> /* unlock sem */ printf ("unnameSem: posting semaphore - synchronizing action\n"); if (sem_post (pSem) == -1) { printf ("unnameSem: posting semaphore failed\n"); return; }</a></b><dd> <b><a name="85688"> /* all done - destroy semaphore */ if (sem_destroy (pSem) == -1) { printf ("unnameSem: sem_destroy failed\n"); return; } }</a></b><dd> <b><a name="85690">void syncTask ( sem_t * pSem ) { /* wait for synchronization from unnameSem */ if (sem_wait (pSem) == -1) { printf ("syncTask: sem_wait failed \n"); return; } else printf ("syncTask:sem locked; doing sync'ed action...\n");</a></b><dd> <b><a name="85693"> /* do something useful here */ }</a></b></pre></dl></dl><dl class="margin"><dd><font face="Helvetica, sans-serif" size="-1" class="sans"><h5 class="HU"><i><a name="85696">Using Named Semaphores</a></i></h5></font><dl class="margin"><dd><p class="Body"><a name="85698"> </a>The<b class="routine"><i class="routine"> sem_open</i></b><b>( )</b> routine either opens a named semaphore that already exists, or, as an option, creates a new semaphore. You can specify which of these possibilities you want by combining the following flag values:</p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -