📄 c-smo2.html
字号:
<dl class="margin"><dd><p class="Body"><a name="84951"> </a>To make use of user-created shared-memory partitions, a task creates a shared-memory partition and adds it to the name database. Before a task can use the shared-memory partition, it must first look in the name database to get the partition ID. When the task has the partition ID, it can access the memory in the shared-memory partition.</p><dd><p class="Body"><a name="84952"> </a>For example, task<b class="task"> t1</b> creates a shared-memory partition and adds it to the name database using the name <b class="symbol_lc">myMemPartition</b>. Task<b class="task"> t2 </b>executing on another CPU wants to allocate memory from the new partition. Task<b class="task"> t2</b> first looks up <b class="symbol_lc">myMemPartition</b> in the name database to get the partition ID. It can then allocate memory from it, using the ID. </p></dl></dl><font face="Helvetica, sans-serif" class="sans"><h4 class="H4"><i><a name="84954">Using the Shared-Memory System Partition</a></i></h4></font><dl class="margin"><dl class="margin"><dd><p class="Body"><a name="84955"> </a>The shared-memory system partition is analogous to the system partition for local memory. <a href="c-smo2.html#84962">Table 6-4</a> lists routines for manipulating the shared-memory system partition.<p class="table"><h4 class="EntityTitle"><a name="84962"><font face="Helvetica, sans-serif" size="-1" class="sans">Table 6-4: Shared-Memory System Partition Routines</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="84966"> </a><font face="Helvetica, sans-serif" size="-1" class="sans">Routine</font></b></div></th><th rowspan="1" colspan="1"><div class="CellHeading"><b><a name="84968"> </a><font face="Helvetica, sans-serif" size="-1" class="sans">Functionality</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="84971"> </a><b class="routine"><i class="routine">smMemMalloc</i></b><b>( )</b> </div></td><td colspan=1 rowspan=1><div class="CellBody"><a name="84973"> </a>Allocate a block of shared system memory. </div></td></tr><tr valign="top"><td colspan=1 rowspan=1><div class="CellBody"><a name="84976"> </a><b class="routine"><i class="routine">smMemCalloc</i></b><b>( )</b> </div></td><td colspan=1 rowspan=1><div class="CellBody"><a name="84978"> </a>Allocate a block of shared system memory for an array. </div></td></tr><tr valign="top"><td colspan=1 rowspan=1><div class="CellBody"><a name="84981"> </a><b class="routine"><i class="routine">smMemRealloc</i></b><b>( )</b> </div></td><td colspan=1 rowspan=1><div class="CellBody"><a name="84983"> </a>Resize a block of shared system memory. </div></td></tr><tr valign="top"><td colspan=1 rowspan=1><div class="CellBody"><a name="84986"> </a><b class="routine"><i class="routine">smMemFree</i></b><b>( )</b> </div></td><td colspan=1 rowspan=1><div class="CellBody"><a name="84988"> </a>Free a block of shared system memory. </div></td></tr><tr valign="top"><td colspan=1 rowspan=1><div class="CellBody"><a name="84991"> </a><b class="routine"><i class="routine">smMemShow</i></b><b>( )</b> </div></td><td colspan=1 rowspan=1><div class="CellBody"><a name="84993"> </a>Display usage statistics of the shared-memory system partition on the standard output device; this routine is automatically included if <b class="symbol_UC">INCLUDE_SM_OBJ</b> is selected for inclusion in the project facility VxWorks view. </div></td></tr><tr valign="top"><td colspan=1 rowspan=1><div class="CellBody"><a name="84996"> </a><b class="routine"><i class="routine">smMemOptionsSet</i></b><b>( )</b> </div></td><td colspan=1 rowspan=1><div class="CellBody"><a name="84998"> </a>Set the debugging options for the shared-memory system partition. </div></td></tr><tr valign="top"><td colspan=1 rowspan=1><div class="CellBody"><a name="85001"> </a><b class="routine"><i class="routine">smMemAddToPool</i></b><b>( )</b> </div></td><td colspan=1 rowspan=1><div class="CellBody"><a name="85003"> </a>Add memory to the shared-memory system pool. </div></td></tr><tr valign="top"><td colspan=1 rowspan=1><div class="CellBody"><a name="85006"> </a><b class="routine"><i class="routine">smMemFindMax</i></b><b>( )</b> </div></td><td colspan=1 rowspan=1><div class="CellBody"><a name="85008"> </a>Find the size of the largest free block in the shared-memory system partition. </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="85009"> </a>Routines that return a pointer to allocated memory return a local address (that is, an address suitable for use from the local CPU). To share this memory across processors, this address must be converted to a global address before it is advertised to tasks on other CPUs. Before a task on another CPU uses the memory, it must convert the global address to a local address. Macros and routines are provided to convert between local addresses and global addresses; see the header file <b class="file">smObjLib.h</b> and the reference entry for <b class="library">smObjLib</b>.</p></dl></dl><h4 class="EntityTitle"><a name="85014"><font face="Helvetica, sans-serif" size="-1" class="sans">Example 6-3: Shared-Memory System Partition </font></a></h4><dl class="margin"><dl class="margin"><dd><p class="Body"><a name="85015"> </a>The following code example uses memory from the shared-memory system partition to share data between tasks on different CPUs. The first member of the data structure is a shared semaphore that is used for mutual exclusion. The send task creates and initializes the structure, then the receive task accesses the data and displays it. </p></dl><dl class="margin"><dd><hr class="Line"></dl><dl class="margin"><dd><pre class="Code"><b><a name="85017">/* buffProtocol.h - simple buffer exchange protocol header file */</a></b><dd> <b><a name="85019">#define BUFFER_SIZE 200 /* shared data buffer size */ #define BUFF_NAME "myMemory" /* name of data buffer in database */</a></b><dd> <b><a name="85022">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="85028">/* buffSend.c - simple buffer exchange protocol send side */</a></b><dd> <b><a name="85030">/* This file writes to the shared memory. */</a></b><dd> <b><a name="85032">#include "vxWorks.h" #include "semLib.h" #include "semSmLib.h" #include "smNameLib.h" #include "smObjLib.h" #include "stdio.h" #include "buffProtocol.h"</a></b><dd> <b><a name="85040">/************************************************************************ * * buffSend - write to shared semaphore protected buffer * */</a></b><dd> <b><a name="85046">STATUS buffSend (void) { SHARED_BUFF * pSharedBuff; SEM_ID mySemSmId;</a></b><dd> <b><a name="85049"> /* grab shared system memory */ pSharedBuff = (SHARED_BUFF *) smMemMalloc (sizeof (SHARED_BUFF));</a></b><dd> <b><a name="86212"> /* * Initialize shared buffer structure before adding to database. The * protection semaphore is initially unavailable and the receiver blocks. */</a></b><dd> <b><a name="86214"> if ((mySemSmId = semBSmCreate (SEM_Q_FIFO, SEM_EMPTY)) == NULL) return (ERROR); pSharedBuff->semSmId = (SEM_ID) htonl ((int) mySemSmId);</a></b><dd> <b><a name="85061"> /* * Convert address of shared buffer to a global address and add to * database. */ if (smNameAdd (BUFF_NAME, (void *) smObjLocalToGlobal (pSharedBuff), T_SM_BLOCK) == ERROR) return (ERROR);</a></b><dd> <b><a name="86238"> /* put data into shared buffer */ sprintf (pSharedBuff->buff,"Hello from sender\n");</a></b><dd> <b><a name="85074"> /* allow receiver to read data by giving protection semaphore */ if (semGive (mySemSmId) != OK) return (ERROR); return (OK); }</a></b></pre></dl><dl class="margin"><dd><hr class="Line"></dl><dl class="margin"><dd><pre class="Code"><b><a name="85082">/* buffReceive.c - simple buffer exchange protocol receive side */ </a></b><dd> <b><a name="85084">/* This file reads the shared memory. */</a></b><dd> <b><a name="85086">#include "vxWorks.h" #include "semLib.h" #include "semSmLib.h" #include "smNameLib.h" #include "smObjLib.h" #include "stdio.h" #include "buffProtocol.h"</a></b><dd> <b><a name="85094">/************************************************************************ * * buffReceive - receive shared semaphore protected buffer */</a></b><dd> <b><a name="85099">STATUS buffReceive (void) { SHARED_BUFF * pSharedBuff; SEM_ID mySemSmId;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -