<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Creates or open a shared memory segment</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.sem-remove.html">sem_remove</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.shm-detach.html">shm_detach</a></div> <div class="up"><a href="ref.sem.html">Semaphore Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.shm-attach" class="refentry"> <div class="refnamediv"> <h1 class="refname">shm_attach</h1> <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">shm_attach</span> — <span class="dc-title">Creates or open a shared memory segment</span></p> </div> <div class="refsect1 description" id="refsect1-function.shm-attach-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">resource</span> <span class="methodname"><strong>shm_attach</strong></span> ( <span class="methodparam"><span class="type">int</span> <code class="parameter">$key</code></span> [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$memsize</code></span> [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$perm</code><span class="initializer"> = 0666</span></span> ]] )</div> <p class="para rdfs-comment"> <span class="function"><strong>shm_attach()</strong></span> returns an id that can be used to access the System V shared memory with the given <em><code class="parameter">key</code></em>, the first call creates the shared memory segment with <em><code class="parameter">memsize</code></em> and the optional perm-bits <em><code class="parameter">perm</code></em>. </p> <p class="para"> A second call to <span class="function"><strong>shm_attach()</strong></span> for the same <em><code class="parameter">key</code></em> will return a different shared memory identifier, but both identifiers access the same underlying shared memory. <em><code class="parameter">memsize</code></em> and <em><code class="parameter">perm</code></em> will be ignored. </p> </div> <div class="refsect1 parameters" id="refsect1-function.shm-attach-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">key</code></em></span> <dd> <p class="para"> A numeric shared memory segment ID </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">memsize</code></em></span> <dd> <p class="para"> The memory size. If not provided, default to the <em>sysvshm.init_mem</em> in the <var class="filename">php.ini</var>, otherwise 10000 bytes. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">perm</code></em></span> <dd> <p class="para"> The optional permission bits. Default to 0666. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.shm-attach-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns a shared memory segment identifier. </p> </div> <div class="refsect1 changelog" id="refsect1-function.shm-attach-changelog"> <h3 class="title">Changelog</h3> <p class="para"> <table class="doctable informaltable"> <thead> <tr> <th>Version</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>5.3.0</td> <td> This function now returns a <a href="language.types.resource.html" class="link">resource</a> instead of an <a href="language.types.integer.html" class="link">integer</a>. </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 notes" id="refsect1-function.shm-attach-notes"> <h3 class="title">Notes</h3> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> This function used to return an integer value prior to PHP 5.3.0. To achieve the same value in a portable manner, the return value can be cast to an integer like: </p> <p class="para"> <div class="example" id="example-3721"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// Create a temporary file and return its path<br /></span><span style="color: #0000BB">$tmp </span><span style="color: #007700">= </span><span style="color: #0000BB">tempnam</span><span style="color: #007700">(</span><span style="color: #DD0000">'/tmp'</span><span style="color: #007700">, </span><span style="color: #DD0000">'PHP'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Get the file token key<br /></span><span style="color: #0000BB">$key </span><span style="color: #007700">= </span><span style="color: #0000BB">ftok</span><span style="color: #007700">(</span><span style="color: #0000BB">$tmp</span><span style="color: #007700">, </span><span style="color: #DD0000">'a'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Attach the SHM resource, notice the cast afterwards<br /></span><span style="color: #0000BB">$id </span><span style="color: #007700">= </span><span style="color: #0000BB">shm_attach</span><span style="color: #007700">(</span><span style="color: #0000BB">$key</span><span style="color: #007700">);<br /><br />if (</span><span style="color: #0000BB">$id </span><span style="color: #007700">=== </span><span style="color: #0000BB">false</span><span style="color: #007700">) {<br /> die(</span><span style="color: #DD0000">'Unable to create the shared memory segment'</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #FF8000">// Cast to integer, since prior to PHP 5.3.0 the resource id <br />// is returned which can be exposed when casting a resource<br />// to an integer<br /></span><span style="color: #0000BB">$id </span><span style="color: #007700">= (integer) </span><span style="color: #0000BB">$id</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </p></blockquote> </div> <div class="refsect1 seealso" id="refsect1-function.shm-attach-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.shm-detach.html" class="function" rel="rdfs-seeAlso">shm_detach()</a> - Disconnects from shared memory segment</span></li> <li class="member"> <span class="function"><a href="function.ftok.html" class="function" rel="rdfs-seeAlso">ftok()</a> - Convert a pathname and a project identifier to a System V IPC key</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.sem-remove.html">sem_remove</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.shm-detach.html">shm_detach</a></div> <div class="up"><a href="ref.sem.html">Semaphore Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>