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

📄 c-basic4.html

📁 vxworks相关论文
💻 HTML
📖 第 1 页 / 共 5 页
字号:
</dl><dl class="margin"><dd><div class="Item"><a name="85700"> </a><b class="symbol_UC">O_CREAT</b> </div><dl class="margin"><dl class="margin"><dl class="margin"><dd><div class="Indent3"><a name="87761"> </a>Create the semaphore if it does not already exist (if it exists, either fail or open the semaphore, depending on whether <b class="symbol_UC">O_EXCL</b> is specified).</><br></dl></dl></dl><dd><div class="Item"><a name="85702"> </a><b class="symbol_UC">O_EXCL</b> </div><dl class="margin"><dl class="margin"><dl class="margin"><dd><div class="Indent3"><a name="87733"> </a>Open the semaphore only if newly created; fail if the semaphore exists.</><br></dl></dl></dl></dl><dl class="margin"><dd><p class="Body"><a name="85703"> </a>The possible effects of a call to <b class="routine"><i class="routine">sem_open</i></b><b>(&nbsp;)</b>, depending on which flags are set and on whether the semaphore accessed already exists, are shown in <a href="c-basic4.html#85710">Table&nbsp;2-16</a>. There is no entry for <b class="symbol_UC">O_EXCL</b> alone, because using that flag alone is not meaningful.<p class="table"><h4 class="EntityTitle"><a name="85710"><font face="Helvetica, sans-serif" size="-1" class="sans">Table 2-16:&nbsp;&nbsp;Possible Outcomes of Calling <b class="routine"><i class="routine">sem_open</i></b><b>(&nbsp;)</b></font></a></h4><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="88177"> </a><font face="Helvetica, sans-serif" size="-1" class="sans">Flag Settings</font></b></div></th><th rowspan="1" colspan="1"><div class="CellHeading"><b><a name="88179"> </a><font face="Helvetica, sans-serif" size="-1" class="sans">Semaphore Exists</font></b></div></th><th rowspan="1" colspan="1"><div class="CellHeading"><b><a name="88181"> </a><font face="Helvetica, sans-serif" size="-1" class="sans">Semaphore Does Not Exist</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="85728"> </a>None&nbsp;</div></td><td colspan=1 rowspan=1><div class="CellBody"><a name="85730"> </a>Semaphore is opened&nbsp;</div></td><td colspan=1 rowspan=1><div class="CellBody"><a name="85732"> </a>Routine fails&nbsp;</div></td></tr><tr valign="top"><td colspan=1 rowspan=1><div class="CellBody"><a name="85734"> </a><b class="symbol_UC">O_CREAT</b> &nbsp;</div></td><td colspan=1 rowspan=1><div class="CellBody"><a name="85736"> </a>Semaphore is opened&nbsp;</div></td><td colspan=1 rowspan=1><div class="CellBody"><a name="85738"> </a>Semaphore is created&nbsp;</div></td></tr><tr valign="top"><td colspan=1 rowspan=2><div class="CellBody"><a name="85740"> </a><b class="symbol_UC">O_CREAT </b>and<b class="symbol_UC"> O_EXCL</b> &nbsp;</div></td><td colspan=1 rowspan=2><div class="CellBody"><a name="85742"> </a>Routine fails&nbsp;</div></td><td colspan=1 rowspan=2><div class="CellBody"><a name="85744"> </a>Semaphore is created&nbsp;</div></td></tr><tr valign="top"></tr><tr><td colspan="20"><hr class="tablerule"></td></tr><tr valign="middle"><td colspan="20"></td></tr></table></p></p><dd><p class="Body"><a name="85757"> </a>A POSIX named semaphore, once initialized, remains usable until explicitly destroyed. Tasks can explicitly mark a semaphore for destruction at any time, but the semaphore remains in the system until no task has the semaphore open.</p><dd><p class="Body"><a name="85758"> </a>If <b class="symbol_UC">INCLUDE_POSIX_SEM_SHOW</b> is selected for inclusion in the project facility VxWorks view (for details, see <i class="title">Tornado User's Guide: Projects</i>), you can use <b class="routine"><i class="routine">show</i></b><b>(&nbsp;)</b> from the Tornado shell to display information about a POSIX semaphore:<sup><a href="#foot"><b class="FootnoteMarker">2</b></a></sup> </p><dl class="margin"><dd><pre class="Code2"><b><a name="85766"></b><tt class="output">-&gt; </tt><b>show semId </b><tt class="output">value = 0 = 0x0</tt><b></a></b></pre></dl><dd><p class="Body"><a name="87707"> </a>The output is sent to the standard output device, and provides information about the POSIX semaphore <b class="symbol_lc">mySem</b> with two tasks blocked waiting for it:</p><dl class="margin"><dd><pre class="Code2"><b><a name="87708"></b><tt class="output">Semaphore name        :mySem sem_open() count       :3 Semaphore value       :0 No. of blocked tasks     :2</tt><b></a></b></pre></dl><dd><p class="Body"><a name="87709"> </a>For a group of collaborating tasks to use a named semaphore, one of the tasks first creates and initializes the semaphore (by calling <b class="routine"><i class="routine">sem_open</i></b><b>(&nbsp;)</b> with the <b class="symbol_UC">O_CREAT</b> flag). Any task that needs to use the semaphore thereafter opens it by calling <b class="routine"><i class="routine">sem_open</i></b><b>(&nbsp;)</b> with the same name (but without setting <b class="symbol_UC">O_CREAT</b>). Any task that has opened the semaphore can use it by locking it with <b class="routine"><i class="routine">sem_wait</i></b><b>(&nbsp;)</b> (blocking) or <b class="routine"><i class="routine">sem_trywait</i></b><b>(&nbsp;)</b> (non-blocking) and unlocking it with <b class="routine"><i class="routine">sem_post</i></b><b>(&nbsp;)</b>. </p><dd><p class="Body"><a name="85774"> </a>To remove a semaphore, all tasks using it must first close it with <b class="routine"><i class="routine">sem_close</i></b><b>(&nbsp;)</b>, and one of the tasks must also unlink it. Unlinking a semaphore with <b class="routine"><i class="routine">sem_unlink</i></b><b>(&nbsp;)</b>removes the semaphore name from the name table. After the name is removed from the name table, tasks that currently have the semaphore open can still use it, but no new tasks can open this semaphore. The next time a task tries to open the semaphore without the <b class="symbol_UC">O_CREAT</b> flag, the operation fails. The semaphore vanishes when the last task closes it.</p></dl></dl><h4 class="EntityTitle"><a name="85777"><font face="Helvetica, sans-serif" size="-1" class="sans">Example 2-7:&nbsp;&nbsp;POSIX Named Semaphores </font></a></h4><dl class="margin"><dl class="margin"><dd><pre class="Code"><b><a name="85778">/* In this example, nameSem() creates a task for synchronization. The &nbsp;* new task,<b class="task"> tSyncSemTask</b>, blocks on the semaphore created in nameSem().  &nbsp;* Once the synchronization takes place, both tasks close the semaphore, &nbsp;* and nameSem() unlinks it. To run this task from the shell, spawn &nbsp;* nameSem as a task:  * &nbsp;&nbsp;-&gt; sp nameSem, "myTest"  */</a></b><dd> <b><a name="85780">/* includes */ #include "vxWorks.h" #include "semaphore.h" #include "fcntl.h"</a></b><dd> <b><a name="85782">/* forward declaration */ int syncSemTask (char * name);</a></b><dd> <b><a name="85784">int nameSem      (     char * name     )     {     sem_t * semId;</a></b><dd> <b><a name="85786">    /* create a named semaphore, initialize to 0*/     printf ("nameSem: creating semaphore\n");     if ((semId = sem_open (name, O_CREAT, 0, 0)) == (sem_t *) -1)         {         printf ("nameSem: sem_open failed\n");          return;         }</a></b><dd> <b><a name="92879">    printf ("nameSem: spawning sync task\n");</a></b><dd> <b><a name="92880">    taskSpawn ("tSyncSemTask", 90, 0, 2000, syncSemTask, name);</a></b><dd> <b><a name="92905">    /* do something useful to synchronize with syncSemTask */</a></b><dd> <b><a name="85794">    /* give semaphore */     printf ("nameSem: posting semaphore - synchronizing action\n");     if (sem_post (semId) == -1)         {         printf ("nameSem: sem_post failed\n");         return;         }</a></b><dd> <b><a name="85796">    /* all done */     if (sem_close (semId) == -1)         {         printf ("nameSem: sem_close failed\n");         return;         }</a></b><dd> <b><a name="85798">    if (sem_unlink (name) == -1)         {         printf ("nameSem: sem_unlink failed\n");         return;         }</a></b><dd> <b><a name="85800">    printf ("nameSem: closed and unlinked semaphore\n");     }</a></b><dd> <b><a name="85802">int syncSemTask     (     char * name     )</a></b><dd> <b><a name="85803">    {     sem_t * semId;</a></b><dd> <b><a name="85805">    /* open semaphore */     printf ("syncSemTask: opening semaphore\n");     if ((semId = sem_open (name, 0)) == (sem_t *) -1)         {         printf ("syncSemTask: sem_open failed\n");         return;         }</a></b><dd> <b><a name="85807">    /* block waiting for synchronization from nameSem */     printf ("syncSemTask: attempting to take semaphore...\n");     if (sem_wait (semId) == -1)         {         printf ("syncSemTask: taking sem failed\n");         return;         }</a></b><dd> <b><a name="85809">    printf ("syncSemTask: has semaphore, doing sync'ed action ...\n");</a></b><dd> <b><a name="85811">    /* do something useful here */</a></b><dd> <b><a name="85813">    if (sem_close (semId) == -1)         {         printf ("syncSemTask: sem_close failed\n");         return;         }     }</a></b></pre></dl></dl><font face="Helvetica, sans-serif" class="sans"><h4 class="H3"><i><a name="85816">2.4.4  &nbsp;&nbsp;Message Queues</a></i></h4></font><dl class="margin"><dl class="margin"><dd><p class="Body"><a name="85817"> </a>Modern real-time applications are constructed as a set of independent but cooperating tasks. While semaphores provide a high-speed mechanism for the synchronization and interlocking of tasks, often a higher-level mechanism is necessary to allow cooperating tasks to communicate with each other. In VxWorks, the primary intertask communication mechanism within a single CPU is <i class="term">message queues. </i>The optional product, VxMP, provides global message queues that can be used across processors; for more information, see <a href="c-smo.html#84368"><i class="title">6.&nbsp;Shared-Memory Objects</i></a>.</p><dd><p class="Body"><a name="85821"> </a>Message queues allow a variable number of messages, each of variable length, to be queued. Any task or ISR can send messages to a message queue. Any task can receive messages from a message queue. Multiple tasks can send to and receive from the same message queue. Full-duplex communication between two tasks generally requires two message queues, one for each direction; see <a href="c-basic4.html#85857">Figure&nbsp;2-14</a>. <div class="frame"><h4 class="EntityTitle"><a name="85857"><font face="Helvetica, sans-serif" size="-1" class="sans">Figure 2-14:&nbsp;&nbsp;Full Duplex Communication Using Message Queues</font></a></h4><dl class="margin"><div class="Anchor"><a name="85854"> </a><img class="figure" border="0" src="images/c-basic11.gif"></div></dl></div></p><dd><p class="Body"><a name="85858"> </a>There are two message-queue subroutine libraries in VxWorks. The first of these, <b class="library">msgQLib</b>, provides Wind message queues, designed expressly for VxWorks; the second, <b class="library">mqPxLib</b>, is compatible with the POSIX standard (1003.1b) for real-time extensions. See <a href="c-basic4.html#86192"><i class="title">Comparison of POSIX and Wind Message Queues</i></a> for a discussion of the differences between the two message-queue designs.</p></dl></dl><font face="Helvetica, sans-serif

⌨️ 快捷键说明

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