📄 c-smo2.html
字号:
int objType;</a></b><dd> <b><a name="85105"> /* get shared buffer address from name database */ if (smNameFind (BUFF_NAME, (void **) &pSharedBuff, &objType, WAIT_FOREVER) == ERROR) return (ERROR);</a></b><dd> <b><a name="85111"> /* convert global address of buff to its local value */ pSharedBuff = (SHARED_BUFF *) smObjGlobalToLocal (pSharedBuff);</a></b><dd> <b><a name="85115"> /* convert shared semaphore ID to host (local) byte order */ mySemSmId = (SEM_ID) ntohl ((int) pSharedBuff->semSmId);</a></b><dd> <b><a name="85119"> /* take shared semaphore before reading the data buffer */ if (semTake (mySemSmId,WAIT_FOREVER) != OK) return (ERROR);</a></b><dd> <b><a name="85124"> /* read data buffer and print it */ printf ("Receiver reading from shared memory: %s\n", pSharedBuff->buff);</a></b><dd> <b><a name="85128"> /* give back the data buffer semaphore */ if (semGive (mySemSmId) != OK) return (ERROR); return (OK); }</a></b></pre></dl></dl><font face="Helvetica, sans-serif" class="sans"><h4 class="H4"><i><a name="85137">Using User-Created Partitions</a></i></h4></font><dl class="margin"><dl class="margin"><dd><p class="Body"><a name="85139"> </a>Shared-memory partitions have a separate create routine, <b class="routine"><i class="routine">memPartSmCreate</i></b><b>(</b> <b>)</b>, that returns a <b class="symbol_UC">MEM_PART_ID</b>. After a user-defined shared-memory partition is created, routines in <b class="library">memPartLib</b> operate on it transparently. Note that the address of the shared-memory area passed to <b class="routine"><i class="routine">memPartSmCreate</i></b><b>(</b> <b>)</b> (or <b class="routine"><i class="routine">memPartAddToPool</i></b><b>(</b> <b>)</b>) must be the global address.</p></dl></dl><h4 class="EntityTitle"><a name="85142"><font face="Helvetica, sans-serif" size="-1" class="sans">Example 6-4: User-Created Partition </font></a></h4><dl class="margin"><dl class="margin"><dd><p class="Body"><a name="85146"> </a>This example is similar to <a href="c-smo2.html#85014">Example 6-3</a>, which uses the shared-memory system partition. This example creates a user-defined partition and stores the shared data in this new partition. A shared semaphore is used to protect the data.</p></dl><dl class="margin"><dd><hr class="Line"></dl><dl class="margin"><dd><pre class="Code"><b><a name="86317">/* memPartExample.h - shared memory partition example header file */</a></b><dd> <b><a name="86319">#define CHUNK_SIZE (2400) #define MEM_PART_NAME "myMemPart" #define PART_BUFF_NAME "myBuff" #define BUFFER_SIZE (40)</a></b><dd> <b><a name="85155">typedef struct shared_buff { SEM_ID semSmId; char buff [BUFFER_SIZE]; } SHARED_BUFF;</a></b></pre></dl><dl class="margin"><dd><hr class="Line"></dl><dl class="margin"><dd><pre class="Code"><b><a name="85161">/* memPartSend.c - shared memory partition example send side */</a></b><dd> <b><a name="85163">/* This file writes to the user-defined shared memory partition. */</a></b><dd> <b><a name="85165">#include "vxWorks.h" #include "memLib.h" #include "semLib.h" #include "semSmLib.h" #include "smNameLib.h" #include "smObjLib.h" #include "smMemLib.h" #include "stdio.h" #include "memPartExample.h"</a></b><dd> <b><a name="85175">/********************************************************************* * * memPartSend - send shared memory partition buffer */</a></b><dd> <b><a name="85180">STATUS memPartSend (void) { char * pMem; PART_ID smMemPartId; SEM_ID mySemSmId; SHARED_BUFF * pSharedBuff;</a></b><dd> <b><a name="85187"> /* allocate shared system memory to use for partition */ pMem = smMemMalloc (CHUNK_SIZE);</a></b><dd> <b><a name="85191"> /* Create user defined partition using the previously allocated * block of memory. * WARNING: memPartSmCreate uses the global address of a memory * pool as first parameter. */ if ((smMemPartId = memPartSmCreate (smObjLocalToGlobal (pMem), CHUNK_SIZE)) == NULL) return (ERROR);</a></b><dd> <b><a name="86395"> /* allocate memory from partition */ pSharedBuff = (SHARED_BUFF *) memPartAlloc ( smMemPartId, sizeof (SHARED_BUFF)); if (pSharedBuff == 0) return (ERROR);</a></b><dd> <b><a name="85208"> /* initialize structure before adding to database */ if ((mySemSmId = semBSmCreate (SEM_Q_FIFO, SEM_EMPTY)) == NULL) return (ERROR); pSharedBuff->semSmId = (SEM_ID) htonl ((int) mySemSmId);</a></b><dd> <b><a name="85213"> /* enter shared partition ID in name database */ if (smNameAdd (MEM_PART_NAME, (void *) smMemPartId, T_SM_PART_ID) == ERROR) return (ERROR);</a></b><dd> <b><a name="85218"> /* convert shared buffer address to a global address and add to database */ if (smNameAdd (PART_BUFF_NAME, (void *) smObjLocalToGlobal(pSharedBuff), T_SM_BLOCK) == ERROR) return (ERROR);</a></b><dd> <b><a name="85224"> /* send data using shared buffer */ sprintf (pSharedBuff->buff,"Hello from sender\n"); if (semGive (mySemSmId) != OK) return (ERROR); return (OK); }</a></b><dd> <b><a name="87868"></a></b></pre></dl><dl class="margin"><dd><hr class="Line"></dl><dl class="margin"><dd><pre class="Code"><b><a name="85234">/* memPartReceive.c - shared memory partition example receive side */</a></b><dd> <b><a name="85236">/* This file reads from the user-defined shared memory partition. */</a></b><dd> <b><a name="85238">#include "vxWorks.h" #include "memLib.h" #include "stdio.h" #include "semLib.h" #include "semSmLib.h" #include "stdio.h" #include "memPartExample.h"</a></b><dd> <b><a name="85246">/********************************************************************* * * memPartReceive - receive shared memory partition buffer * * execute on CPU 1 - use a shared semaphore to protect shared memory * */</a></b><dd> <b><a name="85254">STATUS memPartReceive (void) { SHARED_BUFF * pBuff; SEM_ID mySemSmId; int objType;</a></b><dd> <b><a name="85260"> /* get shared buffer address from name database */ if (smNameFind (PART_BUFF_NAME, (void **) &pBuff, &objType, WAIT_FOREVER) == ERROR) return (ERROR);</a></b><dd> <b><a name="85266"> /* convert global address of buffer to its local value */ pBuff = (SHARED_BUFF *) smObjGlobalToLocal (pBuff);</a></b><dd> <b><a name="85270"> /* Grab shared semaphore before using the shared memory */ mySemSmId = (SEM_ID) ntohl ((int) pBuff->semSmId); semTake (mySemSmId,WAIT_FOREVER); printf ("Receiver reading from shared memory: %s\n", pBuff->buff); semGive (mySemSmId);</a></b><dd> <b><a name="85276"> return (OK); }</a></b></pre></dl></dl><font face="Helvetica, sans-serif" class="sans"><h4 class="H4"><i><a name="85280">Side Effects of Shared-Memory Partition Options</a></i></h4></font><dl class="margin"><dl class="margin"><dd><p class="Body"><a name="85281"> </a>Like their local counterparts, shared-memory partitions (both system- and user-created) can have different options set for error handling; see the reference entries for <b class="routine"><i class="routine">memPartOptionsSet</i></b><b>(</b> <b>)</b> and <b class="routine"><i class="routine">smMemOptionsSet</i></b><b>(</b> <b>)</b>. </p><dd><p class="Body"><a name="85283"> </a>If the <b class="symbol_UC">MEM_BLOCK_CHECK</b> option is used in the following situation, the system can get into a state where the memory partition is no longer available. If a task attempts to free a bad block and a bus error occurs, the task is suspended. Because shared semaphores are used internally for mutual exclusion, the suspended task still has the semaphore, and no other task has access to the memory partition. By default, shared-memory partitions are created without the <b class="symbol_UC">MEM_BLOCK_CHECK </b>option. </p></dl></dl><a name="foot"><hr></a><p class="FootnoteNumberMarker">1: <span class="Footnote"><a name="84398"> </a>Do not confuse this type of priority with the CPU priorities associated with VMEbus access.</span><p class="navbar" align="right"><a href="index.html"><img border="0" alt="[Contents]" src="icons/contents.gif"></a><a href="GuideIX.html"><img border="0" alt="[Index]" src="icons/index.gif"></a><a href="c-smo.html"><img border="0" alt="[Top]" src="icons/top.gif"></a><a href="c-smo1.html"><img border="0" alt="[Prev]" src="icons/prev.gif"></a><a href="c-smo3.html"><img border="0" alt="[Next]" src="icons/next.gif"></a></p></body></html><!---by WRS Documentation (), Wind River Systems, Inc. conversion tool: Quadralay WebWorks Publisher 4.0.11 template: CSS Template, Jan 1998 - Jefro --->
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -