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

📄 c-smo2.html

📁 vxworks相关论文
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<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="84534"> </a><font face="Helvetica, sans-serif" size="-1" class="sans">Create Routine</font></b></div></th><th rowspan="1" colspan="1"><div class="CellHeading"><b><a name="84536"> </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="84539"> </a><b class="routine"><i class="routine">semBSmCreate</i></b><b>(&nbsp;)</b> &nbsp;</div></td><td colspan=1 rowspan=1><div class="CellBody"><a name="84541"> </a>Create a shared binary semaphore.&nbsp;</div></td></tr><tr valign="top"><td colspan=1 rowspan=1><div class="CellBody"><a name="84544"> </a><b class="routine"><i class="routine">semCSmCreate</i></b><b>(&nbsp;)</b> &nbsp;</div></td><td colspan=1 rowspan=1><div class="CellBody"><a name="84546"> </a>Create a shared counting semaphore.&nbsp;</div></td></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="84548"> </a>The use of shared semaphores and local semaphores differs in several ways:</p></dl><dl class="margin"><p class="listspace"><ul class="Bullet" type="disc"><li><a name="84549"> </a>The shared semaphore queuing order specified when the semaphore is created must be FIFO. <a href="c-smo2.html#84558">Figure&nbsp;6-1</a> shows two tasks executing on different CPUs, both trying to take the same semaphore. Task 1 executes first, and is put at the front of the queue because the semaphore is unavailable (empty). Task 2 (executing on a different CPU) tries to take the semaphore after task 1's attempt and is put on the queue behind task 1.</li></ul></p><p class="listspace"><ul class="Bullet" type="disc"><li><a name="84553"> </a>Shared semaphores <i class="emphasis">cannot </i>be given from interrupt level. </li></ul></p><p class="listspace"><ul class="Bullet" type="disc"><li><a name="84554"> </a>Shared semaphores cannot be deleted. Attempts to delete a shared semaphore return <b class="symbol_UC">ERROR</b> and set <b class="symbol_lc">errno</b> to <b class="symbol_UC">S_smObjLib_NO_OBJECT_DESTROY</b>. <div class="frame"><h4 class="EntityTitle"><a name="84558"><font face="Helvetica, sans-serif" size="-1" class="sans">Figure 6-1:&nbsp;&nbsp;Shared Semaphore Queues</font></a></h4><dl class="margin"><div class="Anchor"><a name="84606"> </a><img class="figure" border="0" src="images/c-smo0.gif"></div></dl></div></li></ul></p></dl><dl class="margin"><dd><p class="Body"><a name="84608"> </a>Use <b class="routine"><i class="routine">semInfo</i></b><b>(</b>&nbsp;<b>)</b> to get the shared task control block of tasks pended on a shared semaphore. Use <b class="routine"><i class="routine">semShow</i></b><b>(</b>&nbsp;<b>)</b>, if <b class="symbol_UC">INCLUDE_SEM_SHOW</b> is included in the project facility VxWorks view, to display the status of the shared semaphore and a list of pended tasks. The following example displays detailed information on the shared semaphore <b class="symbol_lc">mySmSemaphoreId</b> as indicated by the second argument (0 = summary, 1 = details):</p><dl class="margin"><dd><pre class="Code2"><b><a name="84611"></b><tt class="output">-&gt;</tt><b> semShow mySmSemaphoreId, 1  </b><tt class="output">value = 0 = 0x0</tt><b></a></b></pre></dl><dd><p class="Body"><a name="84612"> </a>The output is sent to the standard output device, and looks like the following:</p><dl class="margin"><dd><pre class="Code2"><b><a name="84613"></b><tt class="output">Semaphore&nbsp;Id&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;0x36431d Semaphore&nbsp;Type&nbsp;&nbsp;:&nbsp;SHARED BINARY Task&nbsp;Queuing&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;FIFO Pended&nbsp;Tasks&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;2 State&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;EMPTY TID&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CPU&nbsp;Number&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Shared TCB -------------&nbsp;-------------&nbsp;-------------- 0xd0618&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0x364204 0x3be924&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0x36421c</tt><b> </a></b></pre></dl></dl></dl><h4 class="EntityTitle"><a name="84623"><font face="Helvetica, sans-serif" size="-1" class="sans">Example 6-1:&nbsp;&nbsp;Shared Semaphores </font></a></h4><dl class="margin"><dl class="margin"><dd><p class="Body"><a name="84624"> </a>The following code example depicts two tasks executing on different CPUs and using shared semaphores. The routine <b class="routine"><i class="routine">semTask1</i></b><b>(</b>&nbsp;<b>) </b>creates the shared semaphore, initializing the state to full. It adds the semaphore to the name database (to enable the task on the other CPU to access it), takes the semaphore, does some processing, and gives the semaphore. The routine <b class="routine"><i class="routine">semTask2</i></b><b>(</b>&nbsp;<b>)</b> gets the semaphore ID from the database, takes the semaphore, does some processing, and gives the semaphore.</p></dl><dl class="margin"><dd><hr class="Line"></dl><dl class="margin"><dd><pre class="Code"><b><a name="84626">/* semExample.h - shared semaphore example header file */  #define SEM_NAME "mySmSemaphore"  /* semTask1.c - shared semaphore example */  /* This code is executed by a task on CPU #1 */  #include "vxWorks.h" #include "semLib.h" #include "semSmLib.h" #include "smNameLib.h" #include "stdio.h" #include "taskLib.h" #include "semExample.h"</a></b><dd> <b><a name="84642">/************************************************************************ * * semTask1 - shared semaphore user  */  STATUS semTask1 (void)     {     SEM_ID semSmId;      /* create shared semaphore */      if ((semSmId = semBSmCreate (SEM_Q_FIFO, SEM_FULL)) == NULL)         return (ERROR);</a></b><dd> <b><a name="86707">    /* add object to name database */      if (smNameAdd (SEM_NAME, semSmId, T_SM_SEM_B) == ERROR)         return (ERROR);      /* grab shared semaphore and hold it for awhile */      semTake (semSmId, WAIT_FOREVER);</a></b><dd> <b><a name="84665">    /* normally do something useful */      printf ("Task1 has the shared semaphore\n");     taskDelay (sysClkRateGet () * 5);     printf ("Task1 is releasing the shared semaphore\n");</a></b><dd> <b><a name="84671">    /* release shared semaphore */      semGive (semSmId);      return (OK);     }</a></b></pre></dl><dl class="margin"><dd><hr class="Line"></dl><dl class="margin"><dd><pre class="Code"><b><a name="84678">/* semTask2.c - shared semaphore example */</a></b><dd> <b><a name="84680">/* This code is executed by a task on CPU #2. */</a></b><dd> <b><a name="84682">#include "vxWorks.h" #include "semLib.h" #include "semSmLib.h"</a></b><dd> <b><a name="84683">#include "smNameLib.h" #include "stdio.h" #include "semExample.h"</a></b><dd> <b><a name="84685">/************************************************************************ * * semTask2 - shared semaphore user  */</a></b><dd> <b><a name="84690">STATUS semTask2 (void)     {     SEM_ID&nbsp;semSmId;     int&nbsp;&nbsp;&nbsp;&nbsp;objType;</a></b><dd> <b><a name="84695">    /* find object in name database */      if (smNameFind (SEM_NAME, (void **) &amp;semSmId, &amp;objType, WAIT_FOREVER)         == ERROR)         return (ERROR);</a></b><dd> <b><a name="84701">    /* take the shared semaphore */      printf ("semTask2 is now going to take the shared semaphore\n");     semTake (semSmId, WAIT_FOREVER);</a></b><dd> <b><a name="84706">    /* normally do something useful */      printf ("Task2 got the shared semaphore!!\n");</a></b><dd> <b><a name="84710">    /* release shared semaphore */      semGive (semSmId);      printf ("Task2 has released the shared semaphore\n");      return (OK);     }</a></b></pre></dl></dl><font face="Helvetica, sans-serif" class="sans"><h4 class="H3"><i><a name="84719">6.2.3  &nbsp;&nbsp;Shared Message Queues</a></i></h4></font><dl class="margin"><dl class="margin"><dd><p class="Body"><a name="84722"> </a><b></b><i class="term">Shared message queues</i><b></b> are FIFO queues used by tasks to send and receive variable-length messages on any of the CPUs that have access to the shared memory. They can be used either to synchronize tasks or to exchange data between tasks running on different CPUs. See <a href="c-basic.html#83550"><i class="title">2.&nbsp;Basic OS</i></a> in this manual and the reference entry for <b class="library">msgQLib</b> for a complete discussion of message queues.</p><dd><p class="Body"><a name="84726"> </a>To use a shared message queue, a task creates the message queue and advertises its ID. A task that wants to send or receive a message with this message queue first gets the message queue's ID. It then uses this ID to access the message queue. </p><dd><p class="Body"><a name="84727"> </a>For example, consider a typical server/client scenario where a server<b class="task"> </b>task <b class="task">t1</b> (on CPU 1) reads requests from one message queue and replies to these requests with a different message queue. Task<b class="task"> t1</b> creates the request queue and advertises its ID by adding it to the name database assigning the name <b class="symbol_lc">requestQue</b>. If task<b class="task"> t2</b> (on CPU 0) wants to send a request to<b class="task"> t1</b>, it first gets the message queue ID by looking up the name <b class="symbol_lc">requestQue</b> in the name database. Before sending its first request, task<b class="task"> t2</b> creates a reply message queue. Instead of adding its ID to the database, it advertises the ID by sending it as part of the request message. When <b class="task">t1 </b>receives the request from the client, it finds in the message the ID of the queue to use when replying to that client. Task<b class="task"> t1 </b>then sends the reply to the client by using this ID.</p><dd><p class="Body"><a name="84728"> </a>To pass messages between tasks on different CPUs, first create the message queue by calling <b class="routine"><i class="routine">msgQSmCreate</i></b><b>(</b>&nbsp;<b>)</b><b class="routine"><i class="routine"></i></b>. This routine returns a <b class="symbol_UC">MSG_Q_ID</b>. This ID is used for sending and receiving messages on the shared message queue.</p><dd><p class="Body"><a name="84732"> </a>Like their local counterparts, shared message queues can send both urgent or normal priority messages. </p><dd><p class="Body"><a name="84733"> </a>The use of shared message queues and local message queues differs in several ways:</p></dl><dl class="margin">

⌨️ 快捷键说明

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