PHP  
downloads | documentation | faq | getting help | | php.net sites | links 
search for in the  
previoussession_write_closeshmop_closenext
Last updated: Tue, 11 Jun 2002
view this page in Printer friendly version | English | Brazilian Portuguese | Czech | Dutch | Finnish | German | Hungarian | Italian | Japanese | Korean | Polish | Romanian | Russian | Spanish | Turkish

XCII. M�moire partag�e

Shmop est un ensemble de fonctions simples pour g�rer la m�moire partag�e avec PHP (lecture, �criture, cr�ation et suppressions de segments de m�moire parag�e UNIX). Ces fonctions ne fonctionnent pas sous Windows, car ce syst�me d'exploitation ne supporte pas la m�moire partag�e. Pour utiliser les fonctions shmop, compilez PHP avec l'option --enable-shmop parameter.

Note�: Toutes les fonctions d�crites ci-dessous commencent par shm_ poru les versions jusqu'� PHP 4.0.3, mais en PHP 4.0.4 et plus r�cent, elles sont pr�fix�es par shmop_.

Exemple 1. Introduction � la m�moire partag�e

<?php
// Cr�e 100 octets de m�moire partag�e avec
// un identifiant syst�me "0xff3"
$shm_id = shmop_open(0xff3, "c", 0644, 100);
if(!$shm_id) {
	echo "Impossible de cr�er la m�moire partag�e\n";
}
// Lire la taille de la m�moire partag�e
$shm_size = shmop_size($shm_id);
echo "Un bloc de SHM de taille ".$shm_size. " a �t� cr��.\n";
// Ecriture d'une cha�ne de test dans ce segment
$shm_bytes_written = shmop_write($shm_id, "mon bloc de m�moire partag�e", 0);
if($shm_bytes_written != strlen("mon bloc de m�moire partag�e")) {
	echo "Impossible d'�crire toutes les donn�es en m�moire\n";
}
// Lecture du segment
$my_string = shmop_read($shm_id, 0, $shm_size);
if(!$my_string) {
	echo "Impossible de lire toutes les donn�es en m�moire\n";
}
echo "Les donn�es mis en m�moire partag�es sont : ".$my_string."\n";
//Maintenant, effacons le bloc, et fermons le segment de m�moire
if(!shmop_delete($shm_id)) {
	echo "Impossible d'effacer le segment de m�moire";
}
shmop_close($shm_id);
?>

Table des mati�res
shmop_close -- Ferme un bloc de m�moire partag�e
shmop_delete --  D�truit un bloc de m�moire partag�e
shmop_open --  Cr�e ou ouvre un bloc de m�moire partag�e
shmop_read -- Lit un bloc
shmop_size --  Lire la taille du bloc de m�moire partag�e
shmop_write --  Ecrire dans un bloc de m�moire partag�e
User Contributed Notes
M�moire partag�e
add a note about notes

09-Jan-2001 05:29

Why would anyone use shmop whem sysvshm does all this and more? Serious
question. This manual should explain, unless it really is a redundant
extension. Maybe block functions should be merged into sysvshm?


11-Jan-2001 09: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.


11-Jan-2001 09: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.


13-Jan-2002 08: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 :\


16-Jan-2002 11: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.


30-Mar-2002 03:53

These functions work on windows.  You have to install as an ISAPI filter,
but these functions work great......


30-Apr-2002 11:36

Hi all !
Got a problem when trying to use shmop_open function with "a"
flag :
Warning: shmop_open: unable to attach or create shm segment 
This happens whenever i try to access to my shm. Creating it doesn't make
problems... Please Help ! =)


02-May-2002 12: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

add a note about notes
previoussession_write_closeshmop_closenext
Last updated: Tue, 11 Jun 2002
show source | credits | stats | mirror sites:  
Copyright © 2001, 2002 The PHP Group
All rights reserved.
This mirror generously provided by:
Last updated: Mon Jul 8 12:18:18 2002 CEST