ibm-information-center/dist/eclipse/plugins/i5OS.ic.apis_5.4.0.1/unix3a3.htm

191 lines
8.7 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Copyright" content="Copyright (c) 2006 by IBM Corporation">
<title>Shared Memory Functions</title>
<!-- All rights reserved. Licensed Materials Property of IBM -->
<!-- US Government Users Restricted Rights -->
<!-- Use, duplication or disclosure restricted by -->
<!-- GSA ADP Schedule Contract with IBM Corp. -->
<!-- Begin Header Records ========================================= -->
<!-- 050119 JETAYLOR new for V5R4 -->
<!-- End Header Records -->
<link rel="stylesheet" type="text/css" href="../rzahg/ic.css">
</head>
<body>
<a name="Top_Of_Page"></a>
<!-- Java sync-link -->
<script type="text/javascript" language="Javascript" src="../rzahg/synch.js">
</script>
<h2><a name="sharedmemory">Shared Memory Functions</a></h2>
<p>Processes and threads can communicate directly with one another by sharing
parts of their memory space and then reading and writing the data stored in the
<strong>shared memory</strong>. Synchronization of shared memory is the
responsibility of the application program. Semaphores and mutexes provide ways
to synchronize shared memory use across processes and threads.</p>
<p>A thread gets a shared memory identifier by calling the <strong>
shmget()</strong> function. Depending on the <em>key</em> and <em>shmflg</em>
parameters passed in, either a new shared memory segment is created or an
existing shared memory segment is accessed. The size of the shared memory
segment is specified by the <em>size</em> parameter. When a new shared memory
segment is created, a data structure is also created to contain information
about the shared memory segment. This structure is defined in the <strong>
&lt;sys/shm.h&gt;</strong> header file as follows:</p>
<pre>
typedef struct shmid_ds {
struct ipc_perm shm_perm; /* Operation permission struct*/
int shm_segsz; /* Segment size */
pid_t shm_lpid; /* Process id of last shmop */
pid_t shm_cpid; /* Process id of creator */
int shm_nattch; /* Current # attached */
time_t shm_atime; /* Last shmat time */
time_t shm_dtime; /* Last shmdt time */
time_t shm_ctime; /* Last change time */
} shmtablentry_t;
</pre>
<p>A process gets addressability to the shared memory segment by attaching to
it using the <strong>shmat()</strong> function. The following parameters are
passed in:</p>
<ul>
<li>Shared memory ID</li>
<li>Pointer to an address</li>
<li>Flag specifying how the shared memory segment is to be attached</li>
</ul>
<p>A process detaches a shared memory segment by calling the <strong>
shmdt()</strong> function. The only parameter passed in is the shared memory
segment address. The process implicitly detaches from the shared memory when
the process ends.</p>
<p>A thread removes a shared memory ID by calling the <strong>shmctl()</strong>
function. The thread also can use the <strong>shmctl()</strong> function to
change the data structure values associated with the shared memory ID or to
retrieve the data structure values associated with the shared memory ID. The
following parameters are passed in:</p>
<ul>
<li>Shared memory ID</li>
<li>Command the thread wants to perform (remove ID, set data structure values,
receive data structure values)</li>
<li>Pointer to a buffer from which to set data structure values, or in which to
receive data structure values.</li>
</ul>
<br>
<h4>Shared Memory Differences and Restrictions</h4>
<p>Shared memory segments are created as teraspace-shared memory
segments or as nonteraspace-shared memory segments. A teraspace shared memory
segment is accessed by adding the shared memory segment to a process's
teraspace. A teraspace is a space that has a much larger capacity than other
i5/OS spaces and is addressable from only one process. A nonteraspace shared
memory segment creates shared memory using i5/OS space objects.</p>
<p>A teraspace shared memory segment is created if SHM_TS_NP is specified on
the <em>shmflag</em> parameter of <strong>shmget()</strong> or if a shared
memory segment is created from a program that was compiled using the
TERASPACE(*YES *TSIFC) option of CRTBNDC or CRTCMOD. The following capabilities
and restrictions apply for teraspace shared memory segments.</p>
<ul>
<li>Teraspace shared memory objects may be attached in read-only mode.<br><br></li>
<li>The address specified by <em>shmaddr</em> is only used when <strong>
shmat()</strong> is called from a program that uses data model LLP64 and
attaches to a teraspace shared memory segment. Otherwise it is not possible to
specify the address in teraspace at which the shared memory is to be mapped.
The <em>shmaddr</em> parameter on the <strong>shmat()</strong> function is
ignored.<br><br></li>
<li>After a teraspace shared memory segment is detached, it cannot be addressed
through a pointer saved by the process.<br><br></li>
<li>The maximum size of a teraspace shared memory segment is
4&nbsp;294&nbsp;967&nbsp;295 bytes (4 GB minus 1).<br><br></li>
<li>The maximum number of shared memory segments that can be created
(system-wide) is 2&nbsp;147&nbsp;483&nbsp;646.<br><br></li>
<li>A teraspace shared memory segment may be created such that its size can be
changed after it is created. The maximum size of this type of shared memory
segment is 268&nbsp;435&nbsp;456 bytes (256 MB).</li>
</ul>
<p>The i5/OS nonteraspace shared memory differs from the shared memory definition
in the Single UNIX Specification in the following
ways:</p>
<ul>
<li>The nonteraspace shared memory segments are i5/OS space objects and can be
attached only in read/write mode, not in the read-only mode that the
Single UNIX Specification
allows. If the SHM_RDONLY flag is specified in the <em>
shmflg</em> parameter on a <strong>shmget()</strong> call, the call fails and
the <em>errno</em> variable is set to [EOPNOTSUPP].<br><br></li>
<li>A nonteraspace shared memory segment can be attached only at the actual
address of the i5/OS space object, not at an address specified by the thread.
The <em>shmaddr</em> parameter on the <strong>shmat()</strong> function is
ignored.<br><br></li>
<li>After a nonteraspace shared memory segment is detached from a process, it
still can be addressed through a pointer saved by the process. For nonteraspace
shared memory segments, i5/OS does not &quot;map&quot; and &quot;unmap&quot; regions of storage to
the address space of a process.<br><br></li>
<li>The maximum size of a nonteraspace shared memory segment is
16&nbsp;776&nbsp;960 bytes. Although the maximum size of a shared memory
segment is 16&nbsp;776&nbsp;960 bytes, shared memory segments larger than
16&nbsp;773&nbsp;120 bytes should be created as teraspace shared memory
segments. When the operating system accesses a nonteraspace shared memory
segment that has a size larger than 16&nbsp;773&nbsp;120 bytes, a performance
degradation may be observed.<br><br></li>
<li>The maximum number of shared memory segments that can be created
(system-wide) is 2&nbsp;147&nbsp;483&nbsp;646.<br><br></li>
<li>The size of a nonteraspace shared memory segment may be changed using the
SHM_RESIZE command of <strong>shmctl()</strong>, up to a maximum size of
16&nbsp;773&nbsp;120 bytes.</li>
</ul>
<p>The shared memory functions are:</p>
<!--***************API BEGIN PASTE***************-->
<ul>
<li><A HREF="ipcshmat.htm">shmat()</A> (Attach Shared Memory Segment to Current Process) returns the address of the shared memory segment associated with the specified shared memory identifier.</li>
<li><A HREF="ipcshmct.htm">shmctl()</A> (Perform Shared Memory Control Operations) provides shared memory control operations as specified by cmd on the shared memory segment specified by shmid.</li>
<li><A HREF="ipcshmdt.htm">shmdt()</A> (Detach Shared Memory Segment from Calling Process) detaches the shared memory segment specified by shmaddr from the calling process.</li>
<li><A HREF="ipcshmgt.htm">shmget()</A> (Get ID of Shared Memory Segment with Key) returns the shared memory ID associated with the specified shared memory key.</li>
</ul>
<!--***************API END PASTE***************-->
<p>See also <a href="unix3a4.htm">IPC Key Generation Functions</a> for additional shared memory functions.</p>
<br>
<hr>
<center>
<table cellpadding="2" cellspacing="2">
<tr align="center">
<td valign="middle" align="center"><a href="#Top_Of_Page">Top</a> | <a href=
"unix.htm">UNIX-Type APIs</a> | <a href="aplist.htm">APIs by category</a></td>
</tr>
</table>
</center>
</body>
</html>