PHP  
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
search for in the  
<session_write_closeshmop_close>
view the version of this page
Last updated: Sun, 02 May 2004

XCVII. Funzioni relative alla memoria condivisa

Introduzione

Shmop č un set di funzioni di semplice utilizzo che permettono al PHP di leggere, scrivere, creare e cancellare i segmenti di memoria condivisa di Unix. Occorre rilevare che nelle versioni di Windows precedenti a Windows 2000 non supportano la memoria condivisa. In Windows, le funzioni Shmop sono eseguilbili solo se il PHP sta girando in modalitą ISAPI, tipo con Apache o ISS (nelle modalitą CLI o CGI non č utilizzabile).

Nota: Nella versione 4.0.3 di PHP queste funzioni hanno il prefisso shm anzichč shmop.

Requisiti

Non sono necessarie librerie esterne per utilizzare questo modulo.

Installazione

Per utilizzare shmop occorre compilare il PHP con il parametro --enable-shmop nella linea di configurazione.

Configurazione di Runtime

Questa estensione non definisce alcuna direttiva di configurazione in php.ini

Tipi di risorse

Costanti predefinite

Questa estensione non definisce alcuna costante.

Esempi

Esempio 1. Descrizione delle operazioni con la memoria condivisa

<?php
  
// Crea un blocco di memoria condivisa di 100 byte con id 0xff3
$shm_id = shmop_open(0xff3, "c", 0644, 100);
if (!
$shm_id) {
   echo
"Non si riesce a creare il segmento di memoria condivisa\n";
}

// Ottiene la dimensione del blocco di memoria
$shm_size = shmop_size($shm_id);
echo
"Dimesione blocco creato: " . $shm_size . ".\n";

// Scrittura di una stringa di test nella memoria condivisa
$shm_bytes_written = shmop_write($shm_id, "my shared memory block", 0);
if (
$shm_bytes_written != strlen("my shared memory block")) {
   echo
"Non si riesce a scrivere tutti i dati\n";
}

// Ora si rilegge la stringa
$my_string = shmop_read($shm_id, 0, $shm_size);
if (!
$my_string) {
   echo
"Non si riesce a leggere dalla memoria condivisa\n";
}
echo
"I dati presenti nella memoria condivisa sono: ".$my_string."\n";

// Ora si cancella il blocco e si chiude il segmento di memoria condivisa
if (!shmop_delete($shm_id)) {
   echo
"Non si riesce a marcare il blocco di memoria condivisa per la cancellazione.";
}
shmop_close($shm_id);
  
?>

Sommario
shmop_close -- Chiusura di un blocco di memoria condivisa
shmop_delete -- Cancella un blocco di memoria condivisa
shmop_open -- Crea oppure apre un segmento di memoria condivisa
shmop_read -- Legge i dati da un segmento di memoria condivisa
shmop_size -- Restituisce la dimensione di un blocco di memoria condivisa
shmop_write -- Scrittura di dati nel blocco di memoria condivisa


add a note add a note User Contributed Notes
Funzioni relative alla memoria condivisa
joeldg AT listbid.com
02-May-2003 06: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);
stoimenov at email dot com
23-Jul-2002 11:18
Windows does support shared memory through memory mapped file. Check the following functions for details:

 * CreateFileMapping
 * MapViewOfFile
hackie at misato dot prohost dot org
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
medvitz at medvitz dot net
30-Mar-2002 03:53
These functions work on windows.  You have to install as an ISAPI filter, but these functions work great......
rei at prohost dot org
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.
slavapl at mailandnews dot com
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.

<session_write_closeshmop_close>
 Last updated: Sun, 02 May 2004
show source | credits | sitemap | contact | advertising | mirror sites 
Copyright © 2001-2004 The PHP Group
All rights reserved.
This mirror generously provided by: Italia OnLine S.p.a.
Last updated: Fri May 21 04:11:23 2004 CEST