|
|
XCIV. Shared Memory Funktionen (Gemeinsamer Speicher)Einf�hrung
Shmop ist eine Sammlung einfach anzuwendender PHP-Funktionen,
mit denen man mit PHP UNIX shared-memory-Segmente beschreiben,
auslesen und l�schen kann. Die Funktionen k�nnen mit Windows
nicht eingesetzt werden, da Windows keinen gemeinsamen
Speicher unterst�tzt.
Um shmop zu nutzen, m�ssen Sie PHP mit der Option --enable-shmop kompilieren.
Anmerkung:
Die Funktionen, die in diesem Kapitel erkl�rt werden, beginnen
in PHP 4.0.3 alle mit shm, anstelle von
shmop.
AnforderungenDiese Erweiterung ben�tigt zur Erstellung keine externen Bibliotheken. Installation
Um die shmop-Funktionen benutzen zu k�nnen, m�ssen Sie php mit
der Option --enable-shmop
kompilieren.
Laufzeit KonfigurationDiese Erweiterung definiert keine Konfigurationseinstellungen in der php.ini. Vordefinierte KonstantenDiese Erweiterung definiert keine Konstanten. Beispiele
Beispiel 1. Shared Memory Operationen - �bersicht <?php
// Erstelle einen 100 Byte grossen gemeinsam genutzten Speicherblock
// mit mit der System_ID if 0xff3
$shm_id = shmop_open(0xff3, "c", 0644, 100);
if(!$shm_id) {
echo "Konnte kein gemeinsames Speichersegment erstellen\n";
}
// Hole die Gr�sse des gemeinsamen Speicherblocks
$shm_size = shmop_size($shm_id);
echo "SHM Block mit: ".$shm_size. " Bytes wurde erstellt.\n";
// Teststring in den gemeinsamen Speicher schreiben
$shm_bytes_written = shmop_write($shm_id, "mein gemeinsamer Speicherblock", 0);
if($shm_bytes_written != strlen("mein gemeinsamer Speicherblock")) {
echo "Konnte nicht den gesamten String schreiben\n";
}
// Den Teststring wieder auslesen
$my_string = shmop_read($shm_id, 0, $shm_size);
if(!$my_string) {
echo "Konnte nicht aus dem gemeinsamen Speicher lesen\n";
}
echo "Die Daten im gemeinsamen Speicher waren: ".$my_string."\n";
// Den Speicherblock l�schen und den gemeinsamen Speicher schliessen
if(!shmop_delete($shm_id)) {
echo "Konnte den gemeinsamen Speicherblock nicht zum L�schen markieren.";
}
shmop_close($shm_id);
?> |
|
- Inhaltsverzeichnis
- shmop_close -- Schliesst einen gemeinsamen Speicherblock
- shmop_delete -- Einen gemeinsamen Speicherblock l�schen
- shmop_open -- Erstellt oder �ffnet einen gemeinsamen Speicherblock
- shmop_read -- Daten aus dem gemeinsamen Speicherblock auslesen
- shmop_size -- Gibt die Gr�sse des gemeinsamen Speicherblocks zur�ck
- shmop_write -- Schreibt Daten in einen gemeinsamen Speicherblock
User Contributed Notes Shared Memory Funktionen (Gemeinsamer Speicher) |
|
slavapl at mailandnews dot com
11-Jan-2001 10:02 |
|
What you need to realise is that sysvshm is extremly php oriented in it's
ability, it's quite a kludge interfacing other NON PHP utilities with it.
For example have you tried using sysvshm to read an shm segment NOT
created by php? It's not possible, because sysvshm uses a proprietry
format, in essense it can ONLY be used within PHP unless of course you
take time to figure out this format.
So basically, the purpose of
shmop is to provide a symple interface to shared memory that can be used
with OTHER NON php shm creators.
Hope this clears it up.
|
|
rei at prohost dot org
11-Jan-2001 10:16 |
|
The idea behind SHMOP is an easy to use shared memory interface,
without any additional headers added to the shared memory segment
or requiring any special special controls to access the shared
memory
segment outside of PHP. SHMOP borrows its api from C's api to
shm,
which makes it very easy to use, because it treats shared memory,
like C, as
a file of sorts. This makes it very easy to use even
for novices, due to this
functionality. Most importantly SHMOP uses
shm segments to store raw data,
which means you don't need to worry
about matching headers, etc... when you are
using C, perl or other
programming languages to open/create/read/write shm segments
that were
create or are going to be used by PHP. In this it differs
from
sysvshm, who's shm interface uses a specialized header, which
resides inside
the shared memory segment this adds an unnecessary
level of difficulty when
you want to access php shm from external
programs.
Also, from my personal tests in Linux 2.2/2.4 and FreeBSD
3.3 SHMOP is about
20% faster then sysvshm, mostly due to fact it does
not need to parse the
specialized header and stores the data in raw
form.
|
|
andrus at vnet dot ee
13-Jan-2002 09:10 |
|
shared memory functions allows you only to write data in shared memory
block on creation time, on second opening time you can only read, writing
will cause crash (see bugs).
there is no way to update data in
shared block, only way is to read data, delete shared memory block, create
new one, and write changed data back.
crash of writing in
"a" mode will kill not only script but even apache child will
crash with SIGSEGV :\
|
|
ilia at prohost dot org
17-Jan-2002 12:21 |
|
You can write to an existing shared memory segmenet if you have permissions
to do so. In the old version, 'a' option was inproperly documented
& coded, it reality it opened the segment for read & write. In
the current CVS, I have fixed this problem.
|
|
medvitz at medvitz dot net
30-Mar-2002 04:53 |
|
These functions work on windows. You have to install as an ISAPI filter,
but these functions work great......
|
|
hackie at misato dot prohost dot org
02-May-2002 01:15 |
|
Your segment probobly doesn't exist. You should probobly be using the c
flag...
"a" for access (sets SHM_RDONLY for shmat)
use this flag when you need to open an existing shared memory segment for
read only
"c" for create (sets IPC_CREATE) use this
flag when you need to create a new shared memory segment or if a segment
with the same key exists, try to open it for read and write
|
|
stoimenov at email dot com
24-Jul-2002 12:18 |
|
Windows does support shared memory through memory mapped file. Check the
following functions for details:
* CreateFileMapping *
MapViewOfFile
|
|
joeldg AT listbid.com
02-May-2003 07:48 |
|
Just so you know, the ftok function is probably the best for getting the
key.. just so there are not people confused with how they are coming up
with these hex codes for the id.
$fsize =
filesize("/home/joeldg/testdata"); $fdata =
file_get_contents("/home/joeldg/testdata"); $shm_id =
shmop_open(ftok("/home/joeldg/testdata", 'R'), "c",
0644, $fsize);
|
|
|
| |