<html><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title></title><style type="text/css">
<!--
.style1 {font-size: 18px;font-weight: bold;font-family: Georgia, "Times New Roman", Times, serif;}
a{color:#0000cc;text-decoration:none}
a:visited{color:#0000cc;text-decoration:none}
.style2 {color: #660000}
-->
</style></head>
<body style="margin:0px 0px 0px 0px;">
<table width="115%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td bgcolor="#cccccc"><span class="style1" style="margin-left:5px">SHMGET</span></td>
</tr>
<tr>
<td><p style="margin-left:22px;margin-top:0px"><br />
Return the identifier of the shared memory segment associated with the value of the argument <em>ecx</em>.<br />
<br />
If a new shared memory segment is created, then its associated data structure <em>shmid_ds</em> (see <a href="ipc_shmctl.html">SHMCTL</a>) is initialized as follows: <br />
<br />
<em>shm_perm.cuid</em> and <em>shm_perm.uid</em> are set to the effective user ID of the calling process. <br />
<em>shm_perm.cgid</em> and <em>shm_perm.gid</em> are set to the effective group ID of the calling process. <br />
The least significant 9 bits of <em>shm_perm.mode</em> are set to the least significant 9 bit of <em>shmflg</em>. <br />
<em>shm_segsz</em> is set to the value of <em>size</em>. <br />
<em>shm_lpid</em>, <em>shm_nattch</em>, <em>shm_atime</em> and <em>shm_dtime</em> are set to 0. <br />
<em>shm_ctime</em> is set to the current time.<br />
<br />
If the shared memory segment already exists, the permissions are verified, and a check is made to see if it is marked for destruction. </p>
<p style="margin-left:10px"><strong>Arguments</strong></p>
<table width="100%" border="0" style="margin-left:20px; margin-right:20px">
<tr>
<td width="6%" valign="top"><em>ecx</em></td>
<td width="94%">Key value. If this value is IPC_PRIVATE then a new shared memory segment, with size equal to the value of <em>edx</em> rounded up to a multiple of PAGE_SIZE, is created and the system call ignores everything but the least significant 9 bits of <em>esi.</em></td>
</tr>
<tr>
<td valign="top"><em>edx</em></td>
<td>Shared memory segment size (used when creating new segment). </td>
</tr>
<tr>
<td valign="top"><em>esi</em></td>
<td>Flags. It is should be compsed of least significant 9 bits specifying the permissions granted to the owner, group, and world. (These bits have the same format, and the same meaning, as the <em>mode</em> argument of <a href="005.html">sys_open</a>. Presently, the execute permissions are not used by the system.) and the following flags: <br />
<br />
<table width="100%" border="0" style="border:dashed;border-bottom-width:1px;border-left-width:1px;border-right-width:1px;border-top-width:1px;border-color:#333333">
<tr>
<td><table width="100%" border="0">
<tr>
<td width="11%" valign="top"><code>SHM_HUGETLB </code></td>
<td width="89%"><p><code>Allocate HUGETLB pages for shared memory. IPC_CREAT to create a new segment. If this flag is not used, then SHMGET will find the segment associated with <em>ecx</em> and check to see if the user has permission to access the segment.<br>
</code></p> </td>
</tr>
<tr>
<td valign="top"><code>SHM_NORESERVE</code></td>
<td><code>(since Linux 2.6.15)<br>
This flag serves the same purpose as the sys_mmap MAP_NORESERVE flag. Do not reserve swap space for this segment. When swap space is reserved, one has the guarantee that it is possible to modify the segment. When swap space is not reserved one might get SIGSEGV upon a write if no physical memory is available.</code></td>
</tr>
<tr>
<td valign="top"><code>SHM_DEST</code></td>
<td><code>Destroy segment on last detach. </code></td>
</tr>
<tr>
<td valign="top"><code>SHM_LOCKED</code></td>
<td><code>Segment wont be swapped. </code></td>
</tr>
<tr>
<td valign="top"><code>IPC_CREAT</code></td>
<td><code>Create a new segment. If this flag is not used, then SHMGET will find the segment associated with <em>ecx</em>, check to see if the user has permission to receive the <em>shmid</em> associated with the segment, and ensure the segment is not marked for destruction. </code></td>
</tr>
<tr>
<td valign="top"><code>IPC_EXCL</code></td>
<td><code>Used with IPC_CREAT to ensure failure if the segment exists. </code></td>
</tr>
</table></td>
</tr>
</table>
<br />
If <em>esi</em> specifies both IPC_CREAT and IPC_EXCL and a shared memory segment already exists for <em>ecx</em>, then SHMGET fails with <em>-</em>EEXIST (This is analogous to the effect of the combination "O_CREAT or O_EXCL" for <a href="005.html">sys_open</a>). </td>
</tr>
</table>
<p style="margin-left:10px" ><strong>Return values</strong></p>
<p style="margin-left:22px;margin-right:10px" >If the function succeeds the return value is a valid segment identifier.<br />
If the function fails the return value is one of the following <em>errno</em> values:</p>
<table width="100%" border="0" style="border:dashed;border-bottom-width:1px;border-left-width:1px;border-right-width:1px;border-top-width:1px;border-color:#333333;margin-left:22px;margin-right:22px">
<tr>
<td><table width="100%" border="0">
<tr>
<td width="10%" valign="top"><code>-EACCES</code></td>
<td width="90%"><code>The user does not have permission to access the shared memory segment, and does not have the CAP_IPC_OWNER capability. </code></td>
</tr>
<tr>
<td valign="top"><code>-EEXIST </code></td>
<td><code>"IPC_CREAT or IPC_EXCL" was specified and the segment exists. </code></td>
</tr>
<tr>
<td valign="top"><code>-EINVAL</code></td>
<td><code>A new segment was to be created and <em>size</em> < SHMMIN or <em>size</em> > SHMMAX, or no new segment was to be created, a segment with given key existed, but <em>edx</em> is greater than the size of that segment. </code></td>
</tr>
<tr>
<td valign="top"><code>-ENFILE </code></td>
<td><code>The system limit on the total number of open files has been reached. </code></td>
</tr>
<tr>
<td valign="top"><code>-ENOENT </code></td>
<td><code>No segment exists for the given <em>ecx</em>, and IPC_CREAT was not specified. </code></td>
</tr>
<tr>
<td valign="top"><code>-ENOMEM </code></td>
<td><code>No memory could be allocated for segment overhead. </code></td>
</tr>
<tr>
<td valign="top"><code>-ENOSPC </code></td>
<td><code>All possible shared memory IDs have been taken (SHMMNI), or allocating a segment of the requested <em>size</em> would cause the system to exceed the system-wide limit on shared memory (SHMALL). </code></td>
</tr>
<tr>
<td valign="top"><code>-EPERM </code></td>
<td><code>The SHM_HUGETLB flag was specified, but the caller was not privileged (did not have the CAP_IPC_LOCK capability).</code></td>
</tr>
</table></td>
</tr>
</table>
<p style="margin-left:10px"><strong>Remarks</strong></p>
<p style="margin-left:22px">The following limits on shared memory segment resources affect the SHMGET call: </p>
<table width="100%" border="0" style="border:dashed;border-bottom-width:1px;border-left-width:1px;border-right-width:1px;border-top-width:1px;border-color:#333333;margin-left:22px;margin-right:22px">
<tr>
<td><table width="100%" border="0">
<tr>
<td width="7%" valign="top"><code>SHMALL</code></td>
<td width="93%"><code>System wide maximum of shared memory pages (this limit can be read and modified via <em>/proc/sys/kernel/shmall</em>). </code></td>
</tr>
<tr>
<td valign="top"><code>SHMMAX</code></td>
<td><code>Maximum size in bytes for a shared memory segment: policy dependent (this limit can be read and modified via <em>/proc/sys/kernel/shmmax</em>). </code></td>
</tr>
<tr>
<td valign="top"><code>SHMMIN</code></td>
<td><code>Minimum size in bytes for a shared memory segment: implementation dependent (currently 1 byte, though PAGE_SIZE is the effective minimum size).</code></td>
</tr>
<tr>
<td valign="top"><code>SHMMNI</code></td>
<td><code>System wide maximum number of shared memory segments: implementation dependent (currently 4096, this limit can be read and modified via <em>/proc/sys/kernel/shmmni</em>).</code></td>
</tr>
</table></td>
</tr>
</table>
<p style="margin-left:22px">After a <a href="002.html">sys_fork</a> the child inherits the attached shared memory segments.<br />
After a <a href="011.html">sys_exec</a> all attached shared memory segments are detached (not destroyed). <br />
Upon <a href="001.html">sys_exit</a> all attached shared memory segments are detached (not destroyed).</p>
<p style="margin-left:22px">See samples/ipc/shmem.asm for an example. </p></td>
</tr>
</table>
</body></html>