1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
|
<?xml version="1.0" encoding="iso-8859-1"?>
<reference id="ref.shmop">
<title>Shared Memory Functions</title>
<titleabbrev>shmop</titleabbrev>
<partintro>
<para>
Shmop is an easy to use set of functions that allows php to read,
write, create and delete UNIX shared memory segments. The functions
will not work on windows, as it does not support shared memory. To
use shmop you will need to compile php with the --enable-shmop parameter
in your configure line.
</para>
<note>
<simpara>
The functions explained in the chapter begin all with
<function>shm_</function> in PHP 4.0.3, but in PHP 4.0.4 and later
versions these names are changed to begin with
<function>shmop_</function>.
</simpara>
</note>
<para>
<example>
<title>Shared Memory Operations Overview</title>
<programlisting role="php">
<?php
// Create 100 byte shared memory block with system id if 0xff3
$shm_id = shmop_open(0xff3, "c", 0644, 100);
if(!$shm_id) {
echo "Couldn't create shared memory segment\n";
}
// Get shared memory block's size
$shm_size = shmop_size($shm_id);
echo "SHM Block Size: ".$shm_size. " has been created.\n";
// Lets write a test string into shared memory
$shm_bytes_written = shmop_write($shm_id, "my shared memory block", 0);
if($shm_bytes_written != strlen("my shared memory block")) {
echo "Couldn't write the entire length of data\n";
}
// Now lets read the string back
$my_string = shmop_read($shm_id, 0, $shm_size);
if(!$my_string) {
echo "Couldn't read from shared memory block\n";
}
echo "The data inside shared memory was: ".$my_string."\n";
//Now lets delete the block and close the shared memory segment
if(!shmop_delete($shm_id)) {
echo "Couldn't mark shared memory block for deletion.
}
shmop_close($shm_id);
?>
</programlisting>
</example>
</para>
</partintro>
<refentry id="function.shmop_open">
<refnamediv>
<refname>shmop_open</refname>
<refpurpose>Create or open shared memory block</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>int</type><methodname>shmop_open</methodname>
<methodparam><type>int</type><parameter>key</parameter></methodparam>
<methodparam><type>string</type><parameter>flags</parameter></methodparam>
<methodparam><type>int</type><parameter>mode</parameter></methodparam>
<methodparam><type>int</type><parameter>size</parameter></methodparam>
</methodsynopsis>
<para>
<function>shmop_open</function> can create or open a shared memory block.
</para>
<para>
<function>shmop_open</function> takes 4 parameters: key, which is the
system's id for the shared memory block, this parameter can be passed
as a decimal or hex. The second parameter are the flags that you can use:
<itemizedlist>
<listitem>
<simpara>
"a" for access (sets IPC_EXCL)
use this flag when you need to open an existing shared memory segment
</simpara>
</listitem>
<listitem>
<simpara>
"c" for create (sets IPC_CREATE)
use this flag when you need to create a new shared memory segment.
</simpara>
</listitem>
</itemizedlist>
The third parameter is the mode, which are the permissions that you
wish to assign to your memory segment, those are the same as permission
for a file. Permissions need to be passed in octal form ex. 0644.
The last parameter is size of the shared memory block you wish to create
in bytes.
<note><simpara>
Note: the 3rd and 4th should be entered as 0 if you are opening an
existing memory segment. On success <function>shmop_open</function> will
return an id that you can use to access the shared memory segment
you've created.
</simpara></note>
</para>
<para>
<example>
<title>Create a new shared memory block</title>
<programlisting role="php">
<?php
$shm_id = shmop_open(0x0fff, "c", 0644, 100);
?>
</programlisting>
</example>
</para>
<para>
This example opened a shared memory block with a system id of 0x0fff.
</para>
</refsect1>
</refentry>
<refentry id="function.shmop_read">
<refnamediv>
<refname>shmop_read</refname>
<refpurpose>Read data from shared memory block</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>string</type><methodname>shmop_read</methodname>
<methodparam><type>int</type><parameter>shmid</parameter></methodparam>
<methodparam><type>int</type><parameter>start</parameter></methodparam>
<methodparam><type>int</type><parameter>count</parameter></methodparam>
</methodsynopsis>
<para>
<function>shmop_read</function> will read a string from shared memory block.
</para>
<para>
<function>shmop_read</function> takes 3 parameters: shmid, which is the shared
memory block identifier created by <function>shmop_open</function>, offset from
which to start reading and count on the number of bytes to read.
</para>
<para>
<example>
<title>Reading shared memory block</title>
<programlisting role="php">
<?php
$shm_data = shmop_read($shm_id, 0, 50);
?>
</programlisting>
</example>
</para>
<para>
This example will read 50 bytes from shared memory block and place the data
inside <literal>$shm_data</literal>.
</para>
</refsect1>
</refentry>
<refentry id="function.shmop_write">
<refnamediv>
<refname>shmop_write</refname>
<refpurpose>Write data into shared memory block</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>int</type><methodname>shmop_write</methodname>
<methodparam><type>int</type><parameter>shmid</parameter></methodparam>
<methodparam><type>string</type><parameter>data</parameter></methodparam>
<methodparam><type>int</type><parameter>offset</parameter></methodparam>
</methodsynopsis>
<para>
<function>shmop_write</function> will write a string into shared memory block.
</para>
<para>
<function>shmop_write</function> takes 3 parameters: shmid, which is the
shared memory block identifier created by <function>shmop_open</function>,
data, a string that you want to write into shared memory block and offset,
which specifies where to start writing data inside the shared memory segment.
</para>
<para>
<example>
<title>Writing to shared memory block</title>
<programlisting role="php">
<?php
$shm_bytes_written = shmop_write($shm_id, $my_string, 0);
?>
</programlisting>
</example>
</para>
<para>
This example will write data inside <literal>$my_string</literal> into
shared memory block, <literal>$shm_bytes_written</literal> will contain
the number of bytes written.
</para>
</refsect1>
</refentry>
<refentry id="function.size">
<refnamediv>
<refname>shmop_size</refname>
<refpurpose>Get size of shared memory block</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>int</type><methodname>shmop_size</methodname>
<methodparam><type>int</type><parameter>shmid</parameter></methodparam>
</methodsynopsis>
<para>
<function>shmop_size</function> is used to get the size, in bytes of the
shared memory block.
</para>
<para>
<function>shmop_size</function> takes the shmid, which is the shared memory
block identifier created by <function>shmop_open</function>, the function
will return and int, which represents the number of bytes the shared memory
block occupies.
</para>
<para>
<example>
<title>Getting the size of the shared memory block</title>
<programlisting role="php">
<?php
$shm_size = shmop_size($shm_id);
?>
</programlisting>
</example>
</para>
<para>
This example will put the size of shared memory block identified by
<literal>$shm_id</literal> into <literal>$shm_size</literal>.
</para>
</refsect1>
</refentry>
<refentry id="function.shmop_delete">
<refnamediv>
<refname>shmop_delete</refname>
<refpurpose>Delete shared memory block</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>int</type><methodname>shmop_delete</methodname>
<methodparam><type>int</type><parameter>shmid</parameter></methodparam>
</methodsynopsis>
<para>
<function>shmop_delete</function> is used to delete a shared memory block.
</para>
<para>
<function>shmop_delete</function> takes the shmid, which is the shared memory
block identifier created by <function>shmop_open</function>. On success 1 is
returned, on failure 0 is returned.
</para>
<para>
<example>
<title>Deleting shared memory block</title>
<programlisting role="php">
<?php
shmop_delete($shm_id);
?>
</programlisting>
</example>
</para>
<para>
This example will delete shared memory block identified by
<literal>$shm_id</literal>.
</para>
</refsect1>
</refentry>
<refentry id="function.shmop_close">
<refnamediv>
<refname>shmop_close</refname>
<refpurpose>Close shared memory block</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>int</type><methodname>shmop_close</methodname>
<methodparam><type>int</type><parameter>shmid</parameter></methodparam>
</methodsynopsis>
<para>
<function>shmop_close</function> is used to close a shared memory block.
</para>
<para>
<function>shmop_close</function> takes the shmid, which is the shared memory
block identifier created by <function>shmop_open</function>.
</para>
<para>
<example>
<title>Closing shared memory block</title>
<programlisting role="php">
<?php
shmop_close($shm_id);
?>
</programlisting>
</example>
</para>
<para>
This example will close shared memory block identified by <literal>$shm_id</literal>.
</para>
</refsect1>
</refentry>
</reference>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"../../manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
-->
|